ble button A on microbit
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_HeartRate by
Diff: main.cpp
- Revision:
- 80:62f38a170d5e
- Parent:
- 75:8128a06c0a21
- Child:
- 81:64378603fb0d
diff -r 8b7c8c240540 -r 62f38a170d5e main.cpp --- a/main.cpp Tue Sep 20 12:36:16 2016 +0100 +++ b/main.cpp Tue Jan 23 18:45:00 2018 +0000 @@ -16,21 +16,24 @@ #include "mbed.h" #include "ble/BLE.h" -#include "ble/services/HeartRateService.h" -#include "ble/services/BatteryService.h" -#include "ble/services/DeviceInformationService.h" +#include "ButtonService.h" -DigitalOut led1(LED1); - -const static char DEVICE_NAME[] = "HRM1"; -static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, - GattService::UUID_DEVICE_INFORMATION_SERVICE}; +const static char DEVICE_NAME[] = "uBit"; +static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; static volatile bool triggerSensorPolling = false; -uint8_t hrmCounter = 100; // init HRM to 100bps +DigitalOut col9(P0_12, 0); + +DigitalOut heartbeat(P0_13); -HeartRateService *hrService; -DeviceInformationService *deviceInfo; +DigitalIn touch(P0_0); +bool touchState = false; + + +InterruptIn btnA(BUTTON_A); +bool btnState = false; + +ButtonService *buttonServicePtr; void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { @@ -39,11 +42,16 @@ void periodicCallback(void) { - led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ + heartbeat = !heartbeat; /* do heartbeat periodically */ /* Note that the periodicCallback() executes in interrupt context, so it is safer to do * heavy-weight sensor polling from the main thread. */ triggerSensorPolling = true; + if(touch.read() == 0) { + touchState = false; + }else { + touchState = true; + } } void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) @@ -58,10 +66,8 @@ ble.gap().onDisconnection(disconnectionCallback); /* Setup primary service. */ - hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); + buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); - /* Setup auxiliary service. */ - deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); /* Setup advertising. */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); @@ -73,11 +79,20 @@ ble.gap().startAdvertising(); } +void btn_pressed() +{ + btnState = true; +} + +void btn_released() +{ + btnState = false; +} int main(void) { - led1 = 1; + heartbeat = 1; Ticker ticker; - ticker.attach(periodicCallback, 1); // blink LED every second + ticker.attach(periodicCallback, 1); // blink LED every second and also send button state update BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); ble.init(bleInitComplete); @@ -85,21 +100,14 @@ /* SpinWait for initialization to complete. This is necessary because the * BLE object is used in the main loop below. */ while (ble.hasInitialized() == false) { /* spin loop */ } - + btnA.fall(btn_pressed); /* register callback for button pressed interrupt */ + btnA.rise(btn_released); /* register callback for button released interrupt */ // infinite loop while (1) { // check for trigger from periodicCallback() if (triggerSensorPolling && ble.getGapState().connected) { triggerSensorPolling = false; - - // Do blocking calls or whatever is necessary for sensor polling. - // In our case, we simply update the HRM measurement. - hrmCounter++; - if (hrmCounter == 175) { // 100 <= HRM bps <=175 - hrmCounter = 100; - } - - hrService->updateHeartRate(hrmCounter); + buttonServicePtr->updateButtonState(btnState); } else { ble.waitForEvent(); // low power wait for event }