
LED and Button for BLE workshop (TinyBLE)
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LED by
main.cpp@12:7eebfdfdd892, 2016-05-02 (annotated)
- Committer:
- janjongboom
- Date:
- Mon May 02 09:09:59 2016 +0000
- Revision:
- 12:7eebfdfdd892
- Parent:
- 11:5e43f35f64a8
Change interval to 50 ms. due to bug in evothings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:ed5c53845de7 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:ed5c53845de7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:ed5c53845de7 | 3 | * |
rgrover1 | 0:ed5c53845de7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:ed5c53845de7 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:ed5c53845de7 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:ed5c53845de7 | 7 | * |
rgrover1 | 0:ed5c53845de7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:ed5c53845de7 | 9 | * |
rgrover1 | 0:ed5c53845de7 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:ed5c53845de7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:ed5c53845de7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:ed5c53845de7 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:ed5c53845de7 | 14 | * limitations under the License. |
rgrover1 | 0:ed5c53845de7 | 15 | */ |
rgrover1 | 0:ed5c53845de7 | 16 | |
rgrover1 | 0:ed5c53845de7 | 17 | #include "mbed.h" |
andresag | 10:af76616e4d75 | 18 | #include "ble/BLE.h" |
rgrover1 | 0:ed5c53845de7 | 19 | #include "LEDService.h" |
janjongboom | 11:5e43f35f64a8 | 20 | #include "ButtonService.h" |
rgrover1 | 0:ed5c53845de7 | 21 | |
janjongboom | 11:5e43f35f64a8 | 22 | #define LED_ON 0 |
janjongboom | 11:5e43f35f64a8 | 23 | #define LED_OFF 1 |
rgrover1 | 0:ed5c53845de7 | 24 | |
janjongboom | 11:5e43f35f64a8 | 25 | DigitalOut alivenessLED(LED1, LED_OFF); // green |
janjongboom | 11:5e43f35f64a8 | 26 | DigitalOut actuatedLED(LED2, LED_OFF); // red |
janjongboom | 11:5e43f35f64a8 | 27 | InterruptIn button(BUTTON1); |
janjongboom | 11:5e43f35f64a8 | 28 | |
janjongboom | 11:5e43f35f64a8 | 29 | const static char DEVICE_NAME[] = "MY_BLE_DEVICE"; |
janjongboom | 11:5e43f35f64a8 | 30 | static const uint16_t uuid16_list[] = { |
janjongboom | 11:5e43f35f64a8 | 31 | LEDService::LED_SERVICE_UUID, |
janjongboom | 11:5e43f35f64a8 | 32 | ButtonService::BUTTON_SERVICE_UUID |
janjongboom | 11:5e43f35f64a8 | 33 | }; |
rgrover1 | 0:ed5c53845de7 | 34 | |
rgrover1 | 0:ed5c53845de7 | 35 | LEDService *ledServicePtr; |
janjongboom | 11:5e43f35f64a8 | 36 | ButtonService *buttonServicePtr; |
rgrover1 | 0:ed5c53845de7 | 37 | |
andresag | 10:af76616e4d75 | 38 | Ticker ticker; |
andresag | 10:af76616e4d75 | 39 | |
janjongboom | 11:5e43f35f64a8 | 40 | void periodicCallback(void) |
janjongboom | 11:5e43f35f64a8 | 41 | { |
janjongboom | 11:5e43f35f64a8 | 42 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
janjongboom | 11:5e43f35f64a8 | 43 | } |
janjongboom | 11:5e43f35f64a8 | 44 | |
rgrover1 | 9:35a5a5796286 | 45 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
rgrover1 | 0:ed5c53845de7 | 46 | { |
andresag | 10:af76616e4d75 | 47 | BLE::Instance().gap().startAdvertising(); |
janjongboom | 11:5e43f35f64a8 | 48 | |
janjongboom | 11:5e43f35f64a8 | 49 | ticker.attach(periodicCallback, 1); |
rgrover1 | 0:ed5c53845de7 | 50 | } |
rgrover1 | 0:ed5c53845de7 | 51 | |
janjongboom | 11:5e43f35f64a8 | 52 | void connectionCallback(const Gap::ConnectionCallbackParams_t * params) |
rgrover1 | 0:ed5c53845de7 | 53 | { |
janjongboom | 11:5e43f35f64a8 | 54 | // stop blinking when we connect |
janjongboom | 11:5e43f35f64a8 | 55 | ticker.detach(); |
janjongboom | 11:5e43f35f64a8 | 56 | // also put the led off |
janjongboom | 11:5e43f35f64a8 | 57 | alivenessLED = LED_OFF; |
rgrover1 | 0:ed5c53845de7 | 58 | } |
rgrover1 | 0:ed5c53845de7 | 59 | |
rgrover1 | 0:ed5c53845de7 | 60 | /** |
rgrover1 | 0:ed5c53845de7 | 61 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
rgrover1 | 0:ed5c53845de7 | 62 | * |
rgrover1 | 0:ed5c53845de7 | 63 | * @param[in] params |
rgrover1 | 0:ed5c53845de7 | 64 | * Information about the characterisitc being updated. |
rgrover1 | 0:ed5c53845de7 | 65 | */ |
rgrover1 | 7:c9271599ec5f | 66 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
janjongboom | 11:5e43f35f64a8 | 67 | // handle corresponds to the characteristic being written |
janjongboom | 11:5e43f35f64a8 | 68 | // then we can read data to get a buffer of the actual data |
rgrover1 | 7:c9271599ec5f | 69 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { |
janjongboom | 11:5e43f35f64a8 | 70 | // When writing 1 -> turn LED on, 0 -> turn LED off |
janjongboom | 11:5e43f35f64a8 | 71 | char val = params->data[0]; |
janjongboom | 11:5e43f35f64a8 | 72 | actuatedLED = val == 1 ? LED_ON : LED_OFF; |
rgrover1 | 0:ed5c53845de7 | 73 | } |
rgrover1 | 0:ed5c53845de7 | 74 | } |
rgrover1 | 0:ed5c53845de7 | 75 | |
andresag | 10:af76616e4d75 | 76 | /** |
andresag | 10:af76616e4d75 | 77 | * This function is called when the ble initialization process has failed |
andresag | 10:af76616e4d75 | 78 | */ |
andresag | 10:af76616e4d75 | 79 | void onBleInitError(BLE &ble, ble_error_t error) |
rgrover1 | 0:ed5c53845de7 | 80 | { |
janjongboom | 11:5e43f35f64a8 | 81 | // blink fast when we encountered an error |
janjongboom | 11:5e43f35f64a8 | 82 | ticker.attach(periodicCallback, 0.2); |
andresag | 10:af76616e4d75 | 83 | } |
rgrover1 | 0:ed5c53845de7 | 84 | |
andresag | 10:af76616e4d75 | 85 | /** |
andresag | 10:af76616e4d75 | 86 | * Callback triggered when the ble initialization process has finished |
andresag | 10:af76616e4d75 | 87 | */ |
andresag | 10:af76616e4d75 | 88 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 10:af76616e4d75 | 89 | { |
andresag | 10:af76616e4d75 | 90 | BLE& ble = params->ble; |
andresag | 10:af76616e4d75 | 91 | ble_error_t error = params->error; |
rgrover1 | 0:ed5c53845de7 | 92 | |
andresag | 10:af76616e4d75 | 93 | if (error != BLE_ERROR_NONE) { |
andresag | 10:af76616e4d75 | 94 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 10:af76616e4d75 | 95 | onBleInitError(ble, error); |
andresag | 10:af76616e4d75 | 96 | return; |
andresag | 10:af76616e4d75 | 97 | } |
andresag | 10:af76616e4d75 | 98 | |
andresag | 10:af76616e4d75 | 99 | /* Ensure that it is the default instance of BLE */ |
andresag | 10:af76616e4d75 | 100 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 10:af76616e4d75 | 101 | return; |
andresag | 10:af76616e4d75 | 102 | } |
andresag | 10:af76616e4d75 | 103 | |
rgrover1 | 7:c9271599ec5f | 104 | ble.gap().onDisconnection(disconnectionCallback); |
janjongboom | 11:5e43f35f64a8 | 105 | ble.gap().onConnection(connectionCallback); |
rgrover1 | 7:c9271599ec5f | 106 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
rgrover1 | 0:ed5c53845de7 | 107 | |
janjongboom | 11:5e43f35f64a8 | 108 | // Begin - If you add a new service, add it here! |
janjongboom | 11:5e43f35f64a8 | 109 | ledServicePtr = new LEDService(ble, false /* inital value */); |
janjongboom | 11:5e43f35f64a8 | 110 | buttonServicePtr = new ButtonService(ble, false /* initial value */); |
janjongboom | 11:5e43f35f64a8 | 111 | // End - If you add a new service, add it here! |
rgrover1 | 0:ed5c53845de7 | 112 | |
rgrover1 | 7:c9271599ec5f | 113 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 7:c9271599ec5f | 114 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 7:c9271599ec5f | 115 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:c9271599ec5f | 116 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
janjongboom | 12:7eebfdfdd892 | 117 | ble.gap().setAdvertisingInterval(50); /* 50ms. Hack for EvoThings. */ |
rgrover1 | 7:c9271599ec5f | 118 | ble.gap().startAdvertising(); |
andresag | 10:af76616e4d75 | 119 | } |
andresag | 10:af76616e4d75 | 120 | |
janjongboom | 11:5e43f35f64a8 | 121 | void button_down() { |
janjongboom | 11:5e43f35f64a8 | 122 | if (!buttonServicePtr) return; |
janjongboom | 11:5e43f35f64a8 | 123 | buttonServicePtr->updateButtonState(true); |
janjongboom | 11:5e43f35f64a8 | 124 | } |
janjongboom | 11:5e43f35f64a8 | 125 | void button_up() { |
janjongboom | 11:5e43f35f64a8 | 126 | if (!buttonServicePtr) return; |
janjongboom | 11:5e43f35f64a8 | 127 | buttonServicePtr->updateButtonState(false); |
janjongboom | 11:5e43f35f64a8 | 128 | } |
janjongboom | 11:5e43f35f64a8 | 129 | |
andresag | 10:af76616e4d75 | 130 | int main(void) |
andresag | 10:af76616e4d75 | 131 | { |
janjongboom | 11:5e43f35f64a8 | 132 | // Blink the green LED! |
janjongboom | 11:5e43f35f64a8 | 133 | ticker.attach(periodicCallback, 1); |
janjongboom | 11:5e43f35f64a8 | 134 | |
janjongboom | 11:5e43f35f64a8 | 135 | button.fall(&button_down); |
janjongboom | 11:5e43f35f64a8 | 136 | button.rise(&button_up); |
janjongboom | 11:5e43f35f64a8 | 137 | |
andresag | 10:af76616e4d75 | 138 | BLE &ble = BLE::Instance(); |
andresag | 10:af76616e4d75 | 139 | ble.init(bleInitComplete); |
andresag | 10:af76616e4d75 | 140 | |
andresag | 10:af76616e4d75 | 141 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 10:af76616e4d75 | 142 | * BLE object is used in the main loop below. */ |
andresag | 10:af76616e4d75 | 143 | while (ble.hasInitialized() == false) { /* spin loop */ } |
rgrover1 | 0:ed5c53845de7 | 144 | |
rgrover1 | 0:ed5c53845de7 | 145 | while (true) { |
rgrover1 | 0:ed5c53845de7 | 146 | ble.waitForEvent(); |
rgrover1 | 0:ed5c53845de7 | 147 | } |
rgrover1 | 0:ed5c53845de7 | 148 | } |