StepOne / Mbed 2 deprecated StepOne (store accelerometer value)

Dependencies:   I2C_FUNCTION LSM6DS0 MAX30100 mbed BLE_API

Committer:
ajeje41
Date:
Sun Apr 17 17:56:39 2016 +0000
Revision:
3:34f974c907d3
Parent:
2:4cac3109599d
StepOne ble

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ajeje41 0:285a4bb14ae3 1 #include "mbed.h"
ajeje41 2:4cac3109599d 2 #include "ble/BLE.h"
ajeje41 0:285a4bb14ae3 3 #include "functions.h"
ajeje41 0:285a4bb14ae3 4 #include "lsm6ds0.h"
ajeje41 2:4cac3109599d 5 #include "ButtonService.h"
ajeje41 0:285a4bb14ae3 6
ajeje41 0:285a4bb14ae3 7 static Serial pc(SERIAL_TX, SERIAL_RX);
ajeje41 0:285a4bb14ae3 8
ajeje41 2:4cac3109599d 9
ajeje41 0:285a4bb14ae3 10 InterruptIn LSM6DS0_int(D4);
ajeje41 0:285a4bb14ae3 11 DigitalOut led(LED1);
ajeje41 0:285a4bb14ae3 12 LSM6DS0 *gyro_accel;
ajeje41 0:285a4bb14ae3 13 xl_output data_FIFO[FIFO_length_by_five];
ajeje41 0:285a4bb14ae3 14 int offset;
ajeje41 0:285a4bb14ae3 15
ajeje41 2:4cac3109599d 16 BLE ble;
ajeje41 2:4cac3109599d 17 const static char DEVICE_NAME[] = "StepCounter";
ajeje41 2:4cac3109599d 18 static const uint16_t uuid16_list[] = {StepCounterService::STEPCOUNTER_SERVICE_UUID};
ajeje41 2:4cac3109599d 19
ajeje41 2:4cac3109599d 20
ajeje41 2:4cac3109599d 21 StepCounterService *StepCounterServicePtr;
ajeje41 2:4cac3109599d 22
ajeje41 2:4cac3109599d 23
ajeje41 2:4cac3109599d 24
ajeje41 2:4cac3109599d 25
ajeje41 2:4cac3109599d 26
ajeje41 2:4cac3109599d 27
ajeje41 2:4cac3109599d 28
ajeje41 2:4cac3109599d 29
ajeje41 2:4cac3109599d 30
ajeje41 2:4cac3109599d 31 static volatile bool triggerSensorPolling = false;
ajeje41 2:4cac3109599d 32 static uint8_t StepCounter = 0;
ajeje41 2:4cac3109599d 33 // Restart advertising when phone app disconnects
ajeje41 2:4cac3109599d 34 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
ajeje41 2:4cac3109599d 35 {
ajeje41 2:4cac3109599d 36 ble.gap().startAdvertising();
ajeje41 2:4cac3109599d 37
ajeje41 2:4cac3109599d 38 }
ajeje41 0:285a4bb14ae3 39
ajeje41 0:285a4bb14ae3 40 void get_data() {
ajeje41 0:285a4bb14ae3 41 LSM6DS0_int.disable_irq(); //Disable IRQ interrupt
ajeje41 0:285a4bb14ae3 42 led = !led; //Blink led
ajeje41 2:4cac3109599d 43 gyro_accel->storeFIFO(&offset, data_FIFO); //Store the data from FIFO to data_FIFO variable
ajeje41 2:4cac3109599d 44 LSM6DS0_int.enable_irq(); //Re-enable IRQ interrupt
ajeje41 2:4cac3109599d 45 }
ajeje41 2:4cac3109599d 46
ajeje41 2:4cac3109599d 47
ajeje41 2:4cac3109599d 48 void periodicCallback(void)
ajeje41 2:4cac3109599d 49 {
ajeje41 2:4cac3109599d 50
ajeje41 2:4cac3109599d 51 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
ajeje41 2:4cac3109599d 52 * heavy-weight sensor polling from the main thread. */
ajeje41 2:4cac3109599d 53 triggerSensorPolling = true;
ajeje41 2:4cac3109599d 54 }
ajeje41 2:4cac3109599d 55
ajeje41 2:4cac3109599d 56 // Initialization callback
ajeje41 2:4cac3109599d 57 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ajeje41 2:4cac3109599d 58 {
ajeje41 2:4cac3109599d 59 BLE &ble = params->ble;
ajeje41 2:4cac3109599d 60 ble_error_t error = params->error;
ajeje41 2:4cac3109599d 61
ajeje41 2:4cac3109599d 62 if (error != BLE_ERROR_NONE) {
ajeje41 2:4cac3109599d 63 return;
ajeje41 2:4cac3109599d 64 }
ajeje41 2:4cac3109599d 65 ble.gap().onDisconnection(disconnectionCallback);
ajeje41 2:4cac3109599d 66
ajeje41 2:4cac3109599d 67 StepCounterService StepCounterService(ble, false); /* initial value for button pressed */
ajeje41 2:4cac3109599d 68 StepCounterServicePtr = &StepCounterService;
ajeje41 2:4cac3109599d 69
ajeje41 2:4cac3109599d 70 /* Setup advertising */
ajeje41 2:4cac3109599d 71 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
ajeje41 2:4cac3109599d 72 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
ajeje41 2:4cac3109599d 73 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
ajeje41 2:4cac3109599d 74 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
ajeje41 2:4cac3109599d 75 ble.gap().setAdvertisingInterval(100); // 100ms.
ajeje41 2:4cac3109599d 76
ajeje41 2:4cac3109599d 77 /* Start advertising */
ajeje41 2:4cac3109599d 78 ble.gap().startAdvertising();
ajeje41 0:285a4bb14ae3 79 }
ajeje41 0:285a4bb14ae3 80
ajeje41 0:285a4bb14ae3 81 int main()
ajeje41 0:285a4bb14ae3 82 {
ajeje41 0:285a4bb14ae3 83 LSM6DS0_ACC_ODR_t odr;
ajeje41 0:285a4bb14ae3 84 LSM6DS0_ACC_HR_t high_res;
ajeje41 0:285a4bb14ae3 85 LSM6DS0_FIFO_mode_t work_mode;
ajeje41 0:285a4bb14ae3 86
ajeje41 2:4cac3109599d 87 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)
ajeje41 0:285a4bb14ae3 88 odr = LSM6DS0_ACC_ODR_50Hz ;
ajeje41 0:285a4bb14ae3 89 high_res = LSM6DS0_ACC_HR_Disabled;
ajeje41 0:285a4bb14ae3 90 work_mode = LSM6DS0_FIFO_mode_CONTINUOUS;
ajeje41 0:285a4bb14ae3 91
ajeje41 2:4cac3109599d 92 LSM6DS0_int.rise(&get_data); //LSM6DS0's interrupt initializzation
ajeje41 0:285a4bb14ae3 93 gyro_accel->LSM6DS0_reset(); //LSM6DS0 reset
ajeje41 0:285a4bb14ae3 94 gyro_accel->Init(odr, high_res, work_mode); //LSM6DS0 initializzation
ajeje41 0:285a4bb14ae3 95
ajeje41 2:4cac3109599d 96 Ticker ticker;
ajeje41 2:4cac3109599d 97 ticker.attach(periodicCallback, 1);
ajeje41 2:4cac3109599d 98
ajeje41 2:4cac3109599d 99 ble.init(bleInitComplete);
ajeje41 2:4cac3109599d 100
ajeje41 2:4cac3109599d 101 /* SpinWait for initialization to complete. This is necessary because the
ajeje41 2:4cac3109599d 102 * BLE object is used in the main loop below. */
ajeje41 2:4cac3109599d 103 while (ble.hasInitialized() == false) { /* spin loop */ }
ajeje41 2:4cac3109599d 104
ajeje41 2:4cac3109599d 105 /* Infinite loop waiting for BLE interrupt events */
ajeje41 2:4cac3109599d 106 while (1)
ajeje41 0:285a4bb14ae3 107 {
ajeje41 2:4cac3109599d 108 // check for trigger from periodicCallback()
ajeje41 2:4cac3109599d 109 if (triggerSensorPolling && ble.getGapState().connected)
ajeje41 2:4cac3109599d 110 {
ajeje41 2:4cac3109599d 111 triggerSensorPolling = false;
ajeje41 2:4cac3109599d 112
ajeje41 2:4cac3109599d 113 // Do blocking calls or whatever is necessary for sensor polling.
ajeje41 2:4cac3109599d 114 // In our case, we simply update the HRM measurement.
ajeje41 2:4cac3109599d 115 StepCounter++;
ajeje41 2:4cac3109599d 116 StepCounterServicePtr->updateStepCounterState(StepCounter);
ajeje41 2:4cac3109599d 117 } else {
ajeje41 2:4cac3109599d 118 ble.waitForEvent(); // low power wait for event
ajeje41 2:4cac3109599d 119 }
ajeje41 0:285a4bb14ae3 120 }
ajeje41 2:4cac3109599d 121
ajeje41 0:285a4bb14ae3 122 }