/* Send interrupt notification to the phone or * read data from the accelerometer and send it to the phone */ static void AccelerometerSendDataHandler(void) { tMessage Msg; SetupMessageWithBuffer(&Msg, AccelIndMsg, MSG_OPT_NONE); if (Msg.pBuffer != NULL) { if (Control & WUF_ENABLE) { AccelerometerRead(KIONIX_XOUT_HPF_L, Data, XYZ_DATA_LENGTH); AccelerometerRead(KIONIX_INT_REL, Data, ONE_BYTE); //clear int } else AccelerometerRead(KIONIX_XOUT_L, Data, XYZ_DATA_LENGTH); Msg.pBuffer[0] = CONVERT_TO_8_BIT(0); Msg.pBuffer[1] = CONVERT_TO_8_BIT(2); Msg.pBuffer[2] = CONVERT_TO_8_BIT(4); Msg.Length = 3; RouteMsg(&Msg); PrintH(Msg.pBuffer[0]); PrintC(SPACE); PrintH(Msg.pBuffer[1]); PrintC(SPACE); PrintH(Msg.pBuffer[2]); PrintR(); } }
/* Send interrupt notification to the phone or * read data from the accelerometer and send it to the phone */ static void AccelerometerSendDataHandler(void) { tMessage Msg = {3, AccelIndMsg, MSG_OPT_NONE, NULL}; if (CreateMessage(&Msg)) { if (Control & DRDYE_DATA_AVAILABLE || (Control & WUF_ENABLE)) { AccelerometerRead(Control & DRDYE_DATA_AVAILABLE ? KIONIX_XOUT_L : KIONIX_XOUT_HPF_L, Data, XYZ_DATA_LENGTH); Msg.pBuffer[0] = Data[1]; Msg.pBuffer[1] = Data[3]; Msg.pBuffer[2] = Data[5]; RouteMsg(&Msg); // PrintQ(Msg.pBuffer, 3); } else if (Control & TILT_ENABLE_TPE) { AccelerometerRead(KIONIX_TILT_POS_CUR, Msg.pBuffer, ONE_BYTE); Msg.Length = 1; RouteMsg(&Msg); PrintH(Msg.pBuffer[0]); PrintR(); } AccelerometerRead(KIONIX_INT_REL, Data, ONE_BYTE); //clear int } }
static void InitAccelerometer(void) { InitAccelerometerPeripheral(); /* make sure accelerometer has had 20 ms to power up */ vTaskDelay(ACCELEROMETER_POWER_UP_TIME_MS); Control = ACCEL_CONTROL_DEFAULT0; // 0:wakeup; 1:streaming AccelerometerWrite(KIONIX_CTRL_REG1, &Control, ONE_BYTE); /* change to output data rate to 25 Hz */ *Data = WUF_ODR_25HZ; AccelerometerWrite(KIONIX_CTRL_REG3, Data, ONE_BYTE); /* enable interrupt and make it active high */ *Data = IEN | IEA; AccelerometerWrite(KIONIX_INT_CTRL_REG1, Data, ONE_BYTE); /* enable motion detection interrupt for all three axis */ *Data = ZBW; AccelerometerWrite(KIONIX_INT_CTRL_REG2, Data, ONE_BYTE); /* WUF delay = COUNT * (1/WUF_ODR_25HZ) */ *Data = WUF_DEFAULT_DELAY_COUNT; AccelerometerWrite(KIONIX_WUF_TIMER, Data, ONE_BYTE); /* 0.5g = 0x08 */ *Data = WUF_DEFAULT_THRESHOLD; AccelerometerWrite(KIONIX_WUF_THRESH, Data, ONE_BYTE); /* Make sure HW is functioning */ AccelerometerRead(KIONIX_DCST_RESP, Data, ONE_BYTE); PrintF("%s Accel Initd", *Data == PROOF_READ_CODE ? OK : NOK); }