ble button A on microbit
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_HeartRate by
main.cpp
- Committer:
- yramesh
- Date:
- 2018-01-23
- Revision:
- 80:62f38a170d5e
- Parent:
- 75:8128a06c0a21
- Child:
- 81:64378603fb0d
File content as of revision 80:62f38a170d5e:
/* mbed Microcontroller Library * Copyright (c) 2006-2015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" #include "ble/BLE.h" #include "ButtonService.h" const static char DEVICE_NAME[] = "uBit"; static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; static volatile bool triggerSensorPolling = false; DigitalOut col9(P0_12, 0); DigitalOut heartbeat(P0_13); DigitalIn touch(P0_0); bool touchState = false; InterruptIn btnA(BUTTON_A); bool btnState = false; ButtonService *buttonServicePtr; void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising } void periodicCallback(void) { 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) { BLE &ble = params->ble; ble_error_t error = params->error; if (error != BLE_ERROR_NONE) { return; } ble.gap().onDisconnection(disconnectionCallback); /* Setup primary service. */ buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); /* Setup advertising. */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(1000); /* 1000ms */ ble.gap().startAdvertising(); } void btn_pressed() { btnState = true; } void btn_released() { btnState = false; } int main(void) { heartbeat = 1; Ticker ticker; ticker.attach(periodicCallback, 1); // blink LED every second and also send button state update BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); ble.init(bleInitComplete); /* 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; buttonServicePtr->updateButtonState(btnState); } else { ble.waitForEvent(); // low power wait for event } } }