ble button A on microbit
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_HeartRate by
main.cpp@81:64378603fb0d, 2018-01-26 (annotated)
- Committer:
- yramesh
- Date:
- Fri Jan 26 16:23:35 2018 +0000
- Revision:
- 81:64378603fb0d
- Parent:
- 80:62f38a170d5e
bb works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:87a7fc231fae | 1 | /* mbed Microcontroller Library |
rgrover1 | 67:b2d2dee347c0 | 2 | * Copyright (c) 2006-2015 ARM Limited |
ktownsend | 0:87a7fc231fae | 3 | * |
ktownsend | 0:87a7fc231fae | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ktownsend | 0:87a7fc231fae | 5 | * you may not use this file except in compliance with the License. |
ktownsend | 0:87a7fc231fae | 6 | * You may obtain a copy of the License at |
ktownsend | 0:87a7fc231fae | 7 | * |
ktownsend | 0:87a7fc231fae | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ktownsend | 0:87a7fc231fae | 9 | * |
ktownsend | 0:87a7fc231fae | 10 | * Unless required by applicable law or agreed to in writing, software |
ktownsend | 0:87a7fc231fae | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ktownsend | 0:87a7fc231fae | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ktownsend | 0:87a7fc231fae | 13 | * See the License for the specific language governing permissions and |
ktownsend | 0:87a7fc231fae | 14 | * limitations under the License. |
ktownsend | 0:87a7fc231fae | 15 | */ |
yramesh | 81:64378603fb0d | 16 | //#defines for each edge connector pin |
yramesh | 81:64378603fb0d | 17 | #define MICROBIT_PIN_P0 P0_3 //P0 is the left most pad (ANALOG/DIGITAL) used to be P0_3 on green board |
yramesh | 81:64378603fb0d | 18 | #define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL) |
yramesh | 81:64378603fb0d | 19 | #define MICROBIT_PIN_P2 P0_1 //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board |
yramesh | 81:64378603fb0d | 20 | #define MICROBIT_PIN_P3 P0_4 //COL1 (ANALOG/DIGITAL) |
yramesh | 81:64378603fb0d | 21 | #define MICROBIT_PIN_P4 P0_5 //COL2 (ANALOG/DIGITAL) |
yramesh | 81:64378603fb0d | 22 | #define MICROBIT_PIN_P5 P0_17 //BTN_A |
yramesh | 81:64378603fb0d | 23 | #define MICROBIT_PIN_P6 P0_12 //COL9 |
yramesh | 81:64378603fb0d | 24 | #define MICROBIT_PIN_P7 P0_11 //COL8 |
yramesh | 81:64378603fb0d | 25 | #define MICROBIT_PIN_P8 P0_18 //PIN 18 |
yramesh | 81:64378603fb0d | 26 | #define MICROBIT_PIN_P9 P0_10 //COL7 |
yramesh | 81:64378603fb0d | 27 | #define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL) |
yramesh | 81:64378603fb0d | 28 | #define MICROBIT_PIN_P11 P0_26 //BTN_B |
yramesh | 81:64378603fb0d | 29 | #define MICROBIT_PIN_P12 P0_20 //PIN 20 |
yramesh | 81:64378603fb0d | 30 | #define MICROBIT_PIN_P13 P0_23 //SCK |
yramesh | 81:64378603fb0d | 31 | #define MICROBIT_PIN_P14 P0_22 //MISO |
yramesh | 81:64378603fb0d | 32 | #define MICROBIT_PIN_P15 P0_21 //MOSI |
yramesh | 81:64378603fb0d | 33 | #define MICROBIT_PIN_P16 P0_16 //PIN 16 |
yramesh | 81:64378603fb0d | 34 | #define MICROBIT_PIN_P19 P0_0 //SCL |
yramesh | 81:64378603fb0d | 35 | #define MICROBIT_PIN_P20 P0_30 //SDA |
ktownsend | 0:87a7fc231fae | 36 | #include "mbed.h" |
rgrover1 | 67:b2d2dee347c0 | 37 | #include "ble/BLE.h" |
yramesh | 80:62f38a170d5e | 38 | #include "ButtonService.h" |
ktownsend | 0:87a7fc231fae | 39 | |
yramesh | 80:62f38a170d5e | 40 | const static char DEVICE_NAME[] = "uBit"; |
yramesh | 80:62f38a170d5e | 41 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
rgrover1 | 39:6390604f904c | 42 | static volatile bool triggerSensorPolling = false; |
Rohit Grover |
36:ea2a1b4f51c1 | 43 | |
yramesh | 80:62f38a170d5e | 44 | DigitalOut col9(P0_12, 0); |
yramesh | 80:62f38a170d5e | 45 | |
yramesh | 80:62f38a170d5e | 46 | DigitalOut heartbeat(P0_13); |
rgrover1 | 72:99c283dfe28d | 47 | |
yramesh | 81:64378603fb0d | 48 | DigitalIn touch(P0_3); |
yramesh | 80:62f38a170d5e | 49 | bool touchState = false; |
yramesh | 80:62f38a170d5e | 50 | |
yramesh | 80:62f38a170d5e | 51 | |
yramesh | 80:62f38a170d5e | 52 | InterruptIn btnA(BUTTON_A); |
yramesh | 80:62f38a170d5e | 53 | bool btnState = false; |
yramesh | 80:62f38a170d5e | 54 | |
yramesh | 80:62f38a170d5e | 55 | ButtonService *buttonServicePtr; |
rgrover1 | 72:99c283dfe28d | 56 | |
rgrover1 | 71:469dbde1a238 | 57 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
ktownsend | 0:87a7fc231fae | 58 | { |
rgrover1 | 73:49b6090478e2 | 59 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
rgrover1 | 7:daab8ba5139e | 60 | } |
Rohit Grover |
3:24e2b056d229 | 61 | |
Rohit Grover |
11:1d9aafee4984 | 62 | void periodicCallback(void) |
Rohit Grover |
11:1d9aafee4984 | 63 | { |
yramesh | 80:62f38a170d5e | 64 | heartbeat = !heartbeat; /* do heartbeat periodically */ |
rgrover1 | 47:430545f41113 | 65 | |
rgrover1 | 39:6390604f904c | 66 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
rgrover1 | 39:6390604f904c | 67 | * heavy-weight sensor polling from the main thread. */ |
rgrover1 | 39:6390604f904c | 68 | triggerSensorPolling = true; |
yramesh | 80:62f38a170d5e | 69 | if(touch.read() == 0) { |
yramesh | 80:62f38a170d5e | 70 | touchState = false; |
yramesh | 80:62f38a170d5e | 71 | }else { |
yramesh | 80:62f38a170d5e | 72 | touchState = true; |
yramesh | 80:62f38a170d5e | 73 | } |
Rohit Grover |
11:1d9aafee4984 | 74 | } |
Rohit Grover |
11:1d9aafee4984 | 75 | |
rgrover1 | 74:c9d58e7847c4 | 76 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
ktownsend | 0:87a7fc231fae | 77 | { |
rgrover1 | 74:c9d58e7847c4 | 78 | BLE &ble = params->ble; |
rgrover1 | 74:c9d58e7847c4 | 79 | ble_error_t error = params->error; |
rgrover1 | 74:c9d58e7847c4 | 80 | |
rgrover1 | 72:99c283dfe28d | 81 | if (error != BLE_ERROR_NONE) { |
rgrover1 | 72:99c283dfe28d | 82 | return; |
rgrover1 | 72:99c283dfe28d | 83 | } |
ktownsend | 0:87a7fc231fae | 84 | |
rgrover1 | 65:cb76569f74f6 | 85 | ble.gap().onDisconnection(disconnectionCallback); |
ktownsend | 0:87a7fc231fae | 86 | |
rgrover1 | 45:98c5a34b07a4 | 87 | /* Setup primary service. */ |
yramesh | 80:62f38a170d5e | 88 | buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */); |
rgrover1 | 45:98c5a34b07a4 | 89 | |
rgrover1 | 45:98c5a34b07a4 | 90 | |
rgrover1 | 45:98c5a34b07a4 | 91 | /* Setup advertising. */ |
rgrover1 | 65:cb76569f74f6 | 92 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 65:cb76569f74f6 | 93 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 65:cb76569f74f6 | 94 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
rgrover1 | 65:cb76569f74f6 | 95 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 65:cb76569f74f6 | 96 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 67:b2d2dee347c0 | 97 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
rgrover1 | 65:cb76569f74f6 | 98 | ble.gap().startAdvertising(); |
rgrover1 | 72:99c283dfe28d | 99 | } |
rgrover1 | 72:99c283dfe28d | 100 | |
yramesh | 80:62f38a170d5e | 101 | void btn_pressed() |
yramesh | 80:62f38a170d5e | 102 | { |
yramesh | 80:62f38a170d5e | 103 | btnState = true; |
yramesh | 80:62f38a170d5e | 104 | } |
yramesh | 80:62f38a170d5e | 105 | |
yramesh | 80:62f38a170d5e | 106 | void btn_released() |
yramesh | 80:62f38a170d5e | 107 | { |
yramesh | 80:62f38a170d5e | 108 | btnState = false; |
yramesh | 80:62f38a170d5e | 109 | } |
rgrover1 | 72:99c283dfe28d | 110 | int main(void) |
rgrover1 | 72:99c283dfe28d | 111 | { |
yramesh | 80:62f38a170d5e | 112 | heartbeat = 1; |
rgrover1 | 72:99c283dfe28d | 113 | Ticker ticker; |
yramesh | 80:62f38a170d5e | 114 | ticker.attach(periodicCallback, 1); // blink LED every second and also send button state update |
rgrover1 | 72:99c283dfe28d | 115 | |
rgrover1 | 73:49b6090478e2 | 116 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
rgrover1 | 72:99c283dfe28d | 117 | ble.init(bleInitComplete); |
Rohit Grover |
3:24e2b056d229 | 118 | |
rgrover1 | 73:49b6090478e2 | 119 | /* SpinWait for initialization to complete. This is necessary because the |
rgrover1 | 73:49b6090478e2 | 120 | * BLE object is used in the main loop below. */ |
vcoubard | 75:8128a06c0a21 | 121 | while (ble.hasInitialized() == false) { /* spin loop */ } |
yramesh | 80:62f38a170d5e | 122 | btnA.fall(btn_pressed); /* register callback for button pressed interrupt */ |
yramesh | 80:62f38a170d5e | 123 | btnA.rise(btn_released); /* register callback for button released interrupt */ |
mbedAustin | 55:3a7d497a3e03 | 124 | // infinite loop |
mbedAustin | 55:3a7d497a3e03 | 125 | while (1) { |
mbedAustin | 55:3a7d497a3e03 | 126 | // check for trigger from periodicCallback() |
rgrover1 | 50:477004d54431 | 127 | if (triggerSensorPolling && ble.getGapState().connected) { |
Rohit Grover |
36:ea2a1b4f51c1 | 128 | triggerSensorPolling = false; |
yramesh | 80:62f38a170d5e | 129 | buttonServicePtr->updateButtonState(btnState); |
Rohit Grover |
36:ea2a1b4f51c1 | 130 | } else { |
mbedAustin | 55:3a7d497a3e03 | 131 | ble.waitForEvent(); // low power wait for event |
Rohit Grover |
36:ea2a1b4f51c1 | 132 | } |
ktownsend | 0:87a7fc231fae | 133 | } |
ktownsend | 0:87a7fc231fae | 134 | } |