StepOne (store accelerometer value)

Dependencies:   I2C_FUNCTION LSM6DS0 MAX30100 mbed BLE_API

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 #include "functions.h"
00004 #include "lsm6ds0.h"
00005 #include "ButtonService.h"
00006 
00007 static Serial pc(SERIAL_TX, SERIAL_RX);
00008 
00009 
00010 InterruptIn LSM6DS0_int(D4);
00011 DigitalOut led(LED1);
00012 LSM6DS0 *gyro_accel;
00013 xl_output data_FIFO[FIFO_length_by_five];
00014 int offset;
00015 
00016 BLE         ble;
00017 const static char     DEVICE_NAME[] = "StepCounter";
00018 static const uint16_t uuid16_list[] = {StepCounterService::STEPCOUNTER_SERVICE_UUID};
00019 
00020 
00021 StepCounterService *StepCounterServicePtr;
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 static volatile bool  triggerSensorPolling = false;
00032 static uint8_t StepCounter = 0;
00033 //   Restart advertising when phone app disconnects
00034 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00035 {
00036     ble.gap().startAdvertising();
00037     
00038 }
00039 
00040 void get_data() {
00041     LSM6DS0_int.disable_irq(); //Disable IRQ interrupt
00042     led = !led; //Blink led
00043     gyro_accel->storeFIFO(&offset, data_FIFO); //Store the data from FIFO to data_FIFO variable
00044     LSM6DS0_int.enable_irq(); //Re-enable IRQ interrupt
00045 }
00046 
00047 
00048 void periodicCallback(void)
00049 {
00050     
00051     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00052      * heavy-weight sensor polling from the main thread. */
00053     triggerSensorPolling = true;
00054 }
00055 
00056 //  Initialization callback
00057 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00058 {
00059     BLE &ble          = params->ble;
00060     ble_error_t error = params->error;
00061     
00062     if (error != BLE_ERROR_NONE) {
00063         return;
00064     }
00065     ble.gap().onDisconnection(disconnectionCallback);
00066     
00067     StepCounterService StepCounterService(ble, false);    /* initial value for button pressed */
00068     StepCounterServicePtr = &StepCounterService;
00069     
00070     /* Setup advertising */
00071     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
00072     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
00073     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
00074     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
00075     ble.gap().setAdvertisingInterval(100); // 100ms.
00076     
00077     /* Start advertising */
00078     ble.gap().startAdvertising();
00079 }
00080 
00081 int main()
00082 {
00083     LSM6DS0_ACC_ODR_t odr;
00084     LSM6DS0_ACC_HR_t high_res;
00085     LSM6DS0_FIFO_mode_t work_mode;
00086     
00087     offset = 0; //Offset in the data_FIFO vector. It is useful to store the FIFO value in the data_FIFO vector without overwritting (after 5 FIFO value the data_FIFO vector has been overwritten)
00088     odr = LSM6DS0_ACC_ODR_50Hz ;
00089     high_res = LSM6DS0_ACC_HR_Disabled;
00090     work_mode = LSM6DS0_FIFO_mode_CONTINUOUS;
00091     
00092     LSM6DS0_int.rise(&get_data); //LSM6DS0's interrupt initializzation
00093     gyro_accel->LSM6DS0_reset(); //LSM6DS0 reset
00094     gyro_accel->Init(odr, high_res, work_mode); //LSM6DS0 initializzation
00095     
00096     Ticker ticker;
00097     ticker.attach(periodicCallback, 1);
00098     
00099     ble.init(bleInitComplete);
00100     
00101     /* SpinWait for initialization to complete. This is necessary because the
00102      * BLE object is used in the main loop below. */
00103     while (ble.hasInitialized()  == false) { /* spin loop */ }
00104     
00105     /* Infinite loop waiting for BLE interrupt events */
00106     while (1)
00107     {
00108         // check for trigger from periodicCallback()
00109         if (triggerSensorPolling && ble.getGapState().connected)
00110         {
00111             triggerSensorPolling = false;
00112             
00113             // Do blocking calls or whatever is necessary for sensor polling.
00114             // In our case, we simply update the HRM measurement.
00115             StepCounter++;
00116             StepCounterServicePtr->updateStepCounterState(StepCounter);
00117         } else {
00118             ble.waitForEvent(); // low power wait for event
00119         }
00120     }
00121     
00122 }