Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.
Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.
source/main.cpp@0:6b34c49b5285, 2018-04-17 (annotated)
- Committer:
- Davidroid
- Date:
- Tue Apr 17 15:13:04 2018 +0000
- Revision:
- 0:6b34c49b5285
- Child:
- 1:bb6dbece87c1
Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:6b34c49b5285 | 1 | /** |
Davidroid | 0:6b34c49b5285 | 2 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 3 | * @file main.cpp |
Davidroid | 0:6b34c49b5285 | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:6b34c49b5285 | 5 | * @version V1.0.0 |
Davidroid | 0:6b34c49b5285 | 6 | * @date March 31st, 2018 |
Davidroid | 0:6b34c49b5285 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1 |
Davidroid | 0:6b34c49b5285 | 8 | * Bluetooth Low energy Expansion Board. |
Davidroid | 0:6b34c49b5285 | 9 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 10 | * @attention |
Davidroid | 0:6b34c49b5285 | 11 | * |
Davidroid | 0:6b34c49b5285 | 12 | * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> |
Davidroid | 0:6b34c49b5285 | 13 | * |
Davidroid | 0:6b34c49b5285 | 14 | * Redistribution and use in source and binary forms, with or without |
Davidroid | 0:6b34c49b5285 | 15 | * modification, are permitted provided that the following conditions are met: |
Davidroid | 0:6b34c49b5285 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:6b34c49b5285 | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:6b34c49b5285 | 18 | * 2. Redistributions in binary form must reproduce the above copyright |
Davidroid | 0:6b34c49b5285 | 19 | * notice, this list of conditions and the following disclaimer in the |
Davidroid | 0:6b34c49b5285 | 20 | * documentation and/or other materials provided with the distribution. |
Davidroid | 0:6b34c49b5285 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its |
Davidroid | 0:6b34c49b5285 | 22 | * contributors may be used to endorse or promote products derived from |
Davidroid | 0:6b34c49b5285 | 23 | * this software without specific prior written permission. |
Davidroid | 0:6b34c49b5285 | 24 | * |
Davidroid | 0:6b34c49b5285 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:6b34c49b5285 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:6b34c49b5285 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
Davidroid | 0:6b34c49b5285 | 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
Davidroid | 0:6b34c49b5285 | 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
Davidroid | 0:6b34c49b5285 | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
Davidroid | 0:6b34c49b5285 | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
Davidroid | 0:6b34c49b5285 | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
Davidroid | 0:6b34c49b5285 | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
Davidroid | 0:6b34c49b5285 | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Davidroid | 0:6b34c49b5285 | 35 | * POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:6b34c49b5285 | 36 | * |
Davidroid | 0:6b34c49b5285 | 37 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 38 | */ |
Davidroid | 0:6b34c49b5285 | 39 | |
Davidroid | 0:6b34c49b5285 | 40 | |
Davidroid | 0:6b34c49b5285 | 41 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 42 | |
Davidroid | 0:6b34c49b5285 | 43 | /* mbed specific header files. */ |
Davidroid | 0:6b34c49b5285 | 44 | #include <events/mbed_events.h> |
Davidroid | 0:6b34c49b5285 | 45 | #include <mbed.h> |
Davidroid | 0:6b34c49b5285 | 46 | #include "ble/BLE.h" |
Davidroid | 0:6b34c49b5285 | 47 | #include "CustomService.h" |
Davidroid | 0:6b34c49b5285 | 48 | |
Davidroid | 0:6b34c49b5285 | 49 | /* Helper header files. */ |
Davidroid | 0:6b34c49b5285 | 50 | #include "DevSPI.h" |
Davidroid | 0:6b34c49b5285 | 51 | |
Davidroid | 0:6b34c49b5285 | 52 | /* Component specific header files. */ |
Davidroid | 0:6b34c49b5285 | 53 | #include "L6474.h" |
Davidroid | 0:6b34c49b5285 | 54 | |
Davidroid | 0:6b34c49b5285 | 55 | |
Davidroid | 0:6b34c49b5285 | 56 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 57 | |
Davidroid | 0:6b34c49b5285 | 58 | #define BLE_ADVERTISING_INTERVAL_ms 1000 |
Davidroid | 0:6b34c49b5285 | 59 | #define BLUETOOTH_PERIODIC_CALLBACK_ms 500 |
Davidroid | 0:6b34c49b5285 | 60 | #define DELAY_1 100 |
Davidroid | 0:6b34c49b5285 | 61 | |
Davidroid | 0:6b34c49b5285 | 62 | |
Davidroid | 0:6b34c49b5285 | 63 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 64 | |
Davidroid | 0:6b34c49b5285 | 65 | /* Toggle state. */ |
Davidroid | 0:6b34c49b5285 | 66 | typedef enum |
Davidroid | 0:6b34c49b5285 | 67 | { |
Davidroid | 0:6b34c49b5285 | 68 | TOGGLE_OFF = 0, |
Davidroid | 0:6b34c49b5285 | 69 | TOGGLE_ON |
Davidroid | 0:6b34c49b5285 | 70 | } toggle_t; |
Davidroid | 0:6b34c49b5285 | 71 | |
Davidroid | 0:6b34c49b5285 | 72 | |
Davidroid | 0:6b34c49b5285 | 73 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 74 | |
Davidroid | 0:6b34c49b5285 | 75 | /* Button state. */ |
Davidroid | 0:6b34c49b5285 | 76 | bool toggle_state; |
Davidroid | 0:6b34c49b5285 | 77 | |
Davidroid | 0:6b34c49b5285 | 78 | /* LED to indicate system state. */ |
Davidroid | 0:6b34c49b5285 | 79 | DigitalOut led_state(LED1); //Conflicts with SPI CLK on D13. |
Davidroid | 0:6b34c49b5285 | 80 | |
Davidroid | 0:6b34c49b5285 | 81 | /* Bluetooth. */ |
Davidroid | 0:6b34c49b5285 | 82 | const static char DEVICE_NAME[] = "SWITCH_DEVICE_2"; |
Davidroid | 0:6b34c49b5285 | 83 | const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0x00,0x00,0x00}; |
Davidroid | 0:6b34c49b5285 | 84 | static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); |
Davidroid | 0:6b34c49b5285 | 85 | CustomService *custom_service; |
Davidroid | 0:6b34c49b5285 | 86 | |
Davidroid | 0:6b34c49b5285 | 87 | /* Button event. */ |
Davidroid | 0:6b34c49b5285 | 88 | InterruptIn event(USER_BUTTON); |
Davidroid | 0:6b34c49b5285 | 89 | |
Davidroid | 0:6b34c49b5285 | 90 | |
Davidroid | 0:6b34c49b5285 | 91 | /* Bluetooth related functions -----------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 92 | |
Davidroid | 0:6b34c49b5285 | 93 | void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params) |
Davidroid | 0:6b34c49b5285 | 94 | { |
Davidroid | 0:6b34c49b5285 | 95 | (void) params; |
Davidroid | 0:6b34c49b5285 | 96 | BLE::Instance().gap().startAdvertising(); |
Davidroid | 0:6b34c49b5285 | 97 | } |
Davidroid | 0:6b34c49b5285 | 98 | |
Davidroid | 0:6b34c49b5285 | 99 | /** |
Davidroid | 0:6b34c49b5285 | 100 | * This callback allows the custom service to read updates from the |
Davidroid | 0:6b34c49b5285 | 101 | * characteristic. |
Davidroid | 0:6b34c49b5285 | 102 | * |
Davidroid | 0:6b34c49b5285 | 103 | * @param[in] params |
Davidroid | 0:6b34c49b5285 | 104 | * Information about the characterisitc being updated. |
Davidroid | 0:6b34c49b5285 | 105 | */ |
Davidroid | 0:6b34c49b5285 | 106 | void on_data_read_callback(const GattReadCallbackParams *params) { |
Davidroid | 0:6b34c49b5285 | 107 | static uint16_t time_stamp = 0; |
Davidroid | 0:6b34c49b5285 | 108 | |
Davidroid | 0:6b34c49b5285 | 109 | if (params->handle == custom_service->getValueHandle()) { |
Davidroid | 0:6b34c49b5285 | 110 | // Reading led state and sending it via bluetooth. |
Davidroid | 0:6b34c49b5285 | 111 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 0:6b34c49b5285 | 112 | //event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF); |
Davidroid | 0:6b34c49b5285 | 113 | event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, TOGGLE_OFF); // To clean the BLE buffer. |
Davidroid | 0:6b34c49b5285 | 114 | } |
Davidroid | 0:6b34c49b5285 | 115 | } |
Davidroid | 0:6b34c49b5285 | 116 | } |
Davidroid | 0:6b34c49b5285 | 117 | |
Davidroid | 0:6b34c49b5285 | 118 | /** |
Davidroid | 0:6b34c49b5285 | 119 | * This callback allows the custom service to write updates to the |
Davidroid | 0:6b34c49b5285 | 120 | * characteristic. |
Davidroid | 0:6b34c49b5285 | 121 | * |
Davidroid | 0:6b34c49b5285 | 122 | * @param[in] params |
Davidroid | 0:6b34c49b5285 | 123 | * Information about the characterisitc being updated. |
Davidroid | 0:6b34c49b5285 | 124 | */ |
Davidroid | 0:6b34c49b5285 | 125 | void on_data_written_callback(const GattWriteCallbackParams *params) { |
Davidroid | 0:6b34c49b5285 | 126 | |
Davidroid | 0:6b34c49b5285 | 127 | if (params->handle == custom_service->getValueHandle()) { |
Davidroid | 0:6b34c49b5285 | 128 | /* Receiving command via bluetooth and writing it to the led state. */ |
Davidroid | 0:6b34c49b5285 | 129 | led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[0])); |
Davidroid | 0:6b34c49b5285 | 130 | } |
Davidroid | 0:6b34c49b5285 | 131 | } |
Davidroid | 0:6b34c49b5285 | 132 | |
Davidroid | 0:6b34c49b5285 | 133 | /** |
Davidroid | 0:6b34c49b5285 | 134 | * This function is called periodically. |
Davidroid | 0:6b34c49b5285 | 135 | */ |
Davidroid | 0:6b34c49b5285 | 136 | void periodic_callback(void) |
Davidroid | 0:6b34c49b5285 | 137 | { |
Davidroid | 0:6b34c49b5285 | 138 | static uint16_t time_stamp = 0; |
Davidroid | 0:6b34c49b5285 | 139 | |
Davidroid | 0:6b34c49b5285 | 140 | /* Reading toggle command and sending it via bluetooth. */ |
Davidroid | 0:6b34c49b5285 | 141 | if (toggle_state) { |
Davidroid | 0:6b34c49b5285 | 142 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 0:6b34c49b5285 | 143 | event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, toggle_state ? TOGGLE_ON : TOGGLE_OFF); |
Davidroid | 0:6b34c49b5285 | 144 | toggle_state = false; |
Davidroid | 0:6b34c49b5285 | 145 | } |
Davidroid | 0:6b34c49b5285 | 146 | } |
Davidroid | 0:6b34c49b5285 | 147 | } |
Davidroid | 0:6b34c49b5285 | 148 | |
Davidroid | 0:6b34c49b5285 | 149 | /** |
Davidroid | 0:6b34c49b5285 | 150 | * This function is called when the ble initialization process has failled. |
Davidroid | 0:6b34c49b5285 | 151 | */ |
Davidroid | 0:6b34c49b5285 | 152 | void on_ble_init_error_callback(BLE &ble, ble_error_t error) |
Davidroid | 0:6b34c49b5285 | 153 | { |
Davidroid | 0:6b34c49b5285 | 154 | /* Initialization error handling should go here */ |
Davidroid | 0:6b34c49b5285 | 155 | } |
Davidroid | 0:6b34c49b5285 | 156 | |
Davidroid | 0:6b34c49b5285 | 157 | /** |
Davidroid | 0:6b34c49b5285 | 158 | * Callback triggered when the ble initialization process has finished. |
Davidroid | 0:6b34c49b5285 | 159 | */ |
Davidroid | 0:6b34c49b5285 | 160 | void ble_init_complete(BLE::InitializationCompleteCallbackContext *params) |
Davidroid | 0:6b34c49b5285 | 161 | { |
Davidroid | 0:6b34c49b5285 | 162 | BLE& ble = params->ble; |
Davidroid | 0:6b34c49b5285 | 163 | ble_error_t error = params->error; |
Davidroid | 0:6b34c49b5285 | 164 | |
Davidroid | 0:6b34c49b5285 | 165 | if (error != BLE_ERROR_NONE) { |
Davidroid | 0:6b34c49b5285 | 166 | /* In case of error, forward the error handling to on_ble_init_error_callback */ |
Davidroid | 0:6b34c49b5285 | 167 | on_ble_init_error_callback(ble, error); |
Davidroid | 0:6b34c49b5285 | 168 | return; |
Davidroid | 0:6b34c49b5285 | 169 | } |
Davidroid | 0:6b34c49b5285 | 170 | |
Davidroid | 0:6b34c49b5285 | 171 | /* Ensure that it is the default instance of BLE */ |
Davidroid | 0:6b34c49b5285 | 172 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Davidroid | 0:6b34c49b5285 | 173 | return; |
Davidroid | 0:6b34c49b5285 | 174 | } |
Davidroid | 0:6b34c49b5285 | 175 | |
Davidroid | 0:6b34c49b5285 | 176 | ble.gap().onDisconnection(on_disconnection_callback); |
Davidroid | 0:6b34c49b5285 | 177 | ble.gattServer().onDataWritten(on_data_written_callback); |
Davidroid | 0:6b34c49b5285 | 178 | //ble.gattServer().onDataRead(on_data_read_callback); |
Davidroid | 0:6b34c49b5285 | 179 | |
Davidroid | 0:6b34c49b5285 | 180 | custom_service = new CustomService(ble); |
Davidroid | 0:6b34c49b5285 | 181 | |
Davidroid | 0:6b34c49b5285 | 182 | /* Setup advertising data. */ |
Davidroid | 0:6b34c49b5285 | 183 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Davidroid | 0:6b34c49b5285 | 184 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *) CUSTOM_SWITCH_SERVICE_UUID, sizeof(CUSTOM_SWITCH_SERVICE_UUID)); |
Davidroid | 0:6b34c49b5285 | 185 | ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, MANUFACTURER_SPECIFIC_DATA, sizeof(MANUFACTURER_SPECIFIC_DATA)); |
Davidroid | 0:6b34c49b5285 | 186 | ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
Davidroid | 0:6b34c49b5285 | 187 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Davidroid | 0:6b34c49b5285 | 188 | ble.gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL_ms); |
Davidroid | 0:6b34c49b5285 | 189 | ble.gap().startAdvertising(); |
Davidroid | 0:6b34c49b5285 | 190 | } |
Davidroid | 0:6b34c49b5285 | 191 | |
Davidroid | 0:6b34c49b5285 | 192 | void schedule_ble_events_processing(BLE::OnEventsToProcessCallbackContext* context) { |
Davidroid | 0:6b34c49b5285 | 193 | BLE &ble = BLE::Instance(); |
Davidroid | 0:6b34c49b5285 | 194 | event_queue.call(Callback<void()>(&ble, &BLE::processEvents)); |
Davidroid | 0:6b34c49b5285 | 195 | } |
Davidroid | 0:6b34c49b5285 | 196 | |
Davidroid | 0:6b34c49b5285 | 197 | |
Davidroid | 0:6b34c49b5285 | 198 | /* Custom service related functions ------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 199 | |
Davidroid | 0:6b34c49b5285 | 200 | void button_callback(void) { |
Davidroid | 0:6b34c49b5285 | 201 | toggle_state = true; |
Davidroid | 0:6b34c49b5285 | 202 | } |
Davidroid | 0:6b34c49b5285 | 203 | |
Davidroid | 0:6b34c49b5285 | 204 | |
Davidroid | 0:6b34c49b5285 | 205 | /* Main function -------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 206 | |
Davidroid | 0:6b34c49b5285 | 207 | int main() |
Davidroid | 0:6b34c49b5285 | 208 | { |
Davidroid | 0:6b34c49b5285 | 209 | /*----- Initialization. -----*/ |
Davidroid | 0:6b34c49b5285 | 210 | |
Davidroid | 0:6b34c49b5285 | 211 | /* Printing to the console. */ |
Davidroid | 0:6b34c49b5285 | 212 | printf("Switch Node Application Example\r\n\n"); |
Davidroid | 0:6b34c49b5285 | 213 | |
Davidroid | 0:6b34c49b5285 | 214 | /* Bluetooth. */ |
Davidroid | 0:6b34c49b5285 | 215 | event_queue.call_every(BLUETOOTH_PERIODIC_CALLBACK_ms, periodic_callback); |
Davidroid | 0:6b34c49b5285 | 216 | BLE &ble = BLE::Instance(); |
Davidroid | 0:6b34c49b5285 | 217 | ble.onEventsToProcess(schedule_ble_events_processing); |
Davidroid | 0:6b34c49b5285 | 218 | ble.init(ble_init_complete); |
Davidroid | 0:6b34c49b5285 | 219 | |
Davidroid | 0:6b34c49b5285 | 220 | /* Attaching and enabling interrupt handlers. */ |
Davidroid | 0:6b34c49b5285 | 221 | event.fall(button_callback); |
Davidroid | 0:6b34c49b5285 | 222 | |
Davidroid | 0:6b34c49b5285 | 223 | /* Setting up initial state: led OFF, no toggle state. */ |
Davidroid | 0:6b34c49b5285 | 224 | led_state.write(CustomService::SWITCH_OFF); |
Davidroid | 0:6b34c49b5285 | 225 | toggle_state = false; |
Davidroid | 0:6b34c49b5285 | 226 | |
Davidroid | 0:6b34c49b5285 | 227 | /* Start. */ |
Davidroid | 0:6b34c49b5285 | 228 | event_queue.dispatch_forever(); |
Davidroid | 0:6b34c49b5285 | 229 | |
Davidroid | 0:6b34c49b5285 | 230 | return 0; |
Davidroid | 0:6b34c49b5285 | 231 | } |