rajarajan elango
/
mbed-os-example-ble-LED
Testing of Onboard Led using nRF control panel app in Android device.
source/main.cpp@79:4c62b7c6081a, 2019-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Aug 15 17:01:56 2019 +0100
- Revision:
- 79:4c62b7c6081a
- Parent:
- 74:51fde11a771f
Merge pull request #252 from donatieng/mbed_os_update
Update Master branch to use Mbed OS 5.13.1
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 2:864ddfb70a9c | 1 | /* mbed Microcontroller Library |
mbed_official | 2:864ddfb70a9c | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 2:864ddfb70a9c | 3 | * |
mbed_official | 2:864ddfb70a9c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 2:864ddfb70a9c | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 2:864ddfb70a9c | 6 | * You may obtain a copy of the License at |
mbed_official | 2:864ddfb70a9c | 7 | * |
mbed_official | 2:864ddfb70a9c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 2:864ddfb70a9c | 9 | * |
mbed_official | 2:864ddfb70a9c | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 2:864ddfb70a9c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 2:864ddfb70a9c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 2:864ddfb70a9c | 13 | * See the License for the specific language governing permissions and |
mbed_official | 2:864ddfb70a9c | 14 | * limitations under the License. |
mbed_official | 2:864ddfb70a9c | 15 | */ |
mbed_official | 2:864ddfb70a9c | 16 | |
mbed_official | 11:7404978b24e7 | 17 | #include <events/mbed_events.h> |
mbed_official | 2:864ddfb70a9c | 18 | #include <mbed.h> |
mbed_official | 2:864ddfb70a9c | 19 | #include "ble/BLE.h" |
mbed_official | 2:864ddfb70a9c | 20 | #include "LEDService.h" |
mbed_official | 74:51fde11a771f | 21 | #include "pretty_printer.h" |
mbed_official | 2:864ddfb70a9c | 22 | |
mbed_official | 74:51fde11a771f | 23 | const static char DEVICE_NAME[] = "LED"; |
mbed_official | 2:864ddfb70a9c | 24 | |
mbed_official | 74:51fde11a771f | 25 | static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); |
mbed_official | 2:864ddfb70a9c | 26 | |
mbed_official | 74:51fde11a771f | 27 | class LEDDemo : ble::Gap::EventHandler { |
mbed_official | 74:51fde11a771f | 28 | public: |
mbed_official | 74:51fde11a771f | 29 | LEDDemo(BLE &ble, events::EventQueue &event_queue) : |
mbed_official | 74:51fde11a771f | 30 | _ble(ble), |
mbed_official | 74:51fde11a771f | 31 | _event_queue(event_queue), |
mbed_official | 74:51fde11a771f | 32 | _alive_led(LED1, 1), |
mbed_official | 74:51fde11a771f | 33 | _actuated_led(LED2, 0), |
mbed_official | 74:51fde11a771f | 34 | _led_uuid(LEDService::LED_SERVICE_UUID), |
mbed_official | 74:51fde11a771f | 35 | _led_service(NULL), |
mbed_official | 74:51fde11a771f | 36 | _adv_data_builder(_adv_buffer) { } |
mbed_official | 2:864ddfb70a9c | 37 | |
mbed_official | 74:51fde11a771f | 38 | ~LEDDemo() { |
mbed_official | 74:51fde11a771f | 39 | delete _led_service; |
mbed_official | 2:864ddfb70a9c | 40 | } |
mbed_official | 2:864ddfb70a9c | 41 | |
mbed_official | 74:51fde11a771f | 42 | void start() { |
mbed_official | 74:51fde11a771f | 43 | _ble.gap().setEventHandler(this); |
mbed_official | 74:51fde11a771f | 44 | |
mbed_official | 74:51fde11a771f | 45 | _ble.init(this, &LEDDemo::on_init_complete); |
mbed_official | 74:51fde11a771f | 46 | |
mbed_official | 74:51fde11a771f | 47 | _event_queue.call_every(500, this, &LEDDemo::blink); |
mbed_official | 74:51fde11a771f | 48 | |
mbed_official | 74:51fde11a771f | 49 | _event_queue.dispatch_forever(); |
mbed_official | 74:51fde11a771f | 50 | } |
mbed_official | 2:864ddfb70a9c | 51 | |
mbed_official | 74:51fde11a771f | 52 | private: |
mbed_official | 74:51fde11a771f | 53 | /** Callback triggered when the ble initialization process has finished */ |
mbed_official | 74:51fde11a771f | 54 | void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { |
mbed_official | 74:51fde11a771f | 55 | if (params->error != BLE_ERROR_NONE) { |
mbed_official | 74:51fde11a771f | 56 | printf("Ble initialization failed."); |
mbed_official | 74:51fde11a771f | 57 | return; |
mbed_official | 74:51fde11a771f | 58 | } |
mbed_official | 44:df8adb3bc797 | 59 | |
mbed_official | 74:51fde11a771f | 60 | _led_service = new LEDService(_ble, false); |
mbed_official | 74:51fde11a771f | 61 | |
mbed_official | 74:51fde11a771f | 62 | _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written); |
mbed_official | 2:864ddfb70a9c | 63 | |
mbed_official | 74:51fde11a771f | 64 | print_mac_address(); |
mbed_official | 74:51fde11a771f | 65 | |
mbed_official | 74:51fde11a771f | 66 | start_advertising(); |
mbed_official | 2:864ddfb70a9c | 67 | } |
mbed_official | 2:864ddfb70a9c | 68 | |
mbed_official | 74:51fde11a771f | 69 | void start_advertising() { |
mbed_official | 74:51fde11a771f | 70 | /* Create advertising parameters and payload */ |
mbed_official | 74:51fde11a771f | 71 | |
mbed_official | 74:51fde11a771f | 72 | ble::AdvertisingParameters adv_parameters( |
mbed_official | 74:51fde11a771f | 73 | ble::advertising_type_t::CONNECTABLE_UNDIRECTED, |
mbed_official | 74:51fde11a771f | 74 | ble::adv_interval_t(ble::millisecond_t(1000)) |
mbed_official | 74:51fde11a771f | 75 | ); |
mbed_official | 74:51fde11a771f | 76 | |
mbed_official | 74:51fde11a771f | 77 | _adv_data_builder.setFlags(); |
mbed_official | 74:51fde11a771f | 78 | _adv_data_builder.setLocalServiceList(mbed::make_Span(&_led_uuid, 1)); |
mbed_official | 74:51fde11a771f | 79 | _adv_data_builder.setName(DEVICE_NAME); |
mbed_official | 74:51fde11a771f | 80 | |
mbed_official | 74:51fde11a771f | 81 | /* Setup advertising */ |
mbed_official | 74:51fde11a771f | 82 | |
mbed_official | 74:51fde11a771f | 83 | ble_error_t error = _ble.gap().setAdvertisingParameters( |
mbed_official | 74:51fde11a771f | 84 | ble::LEGACY_ADVERTISING_HANDLE, |
mbed_official | 74:51fde11a771f | 85 | adv_parameters |
mbed_official | 74:51fde11a771f | 86 | ); |
mbed_official | 74:51fde11a771f | 87 | |
mbed_official | 74:51fde11a771f | 88 | if (error) { |
mbed_official | 74:51fde11a771f | 89 | printf("_ble.gap().setAdvertisingParameters() failed\r\n"); |
mbed_official | 74:51fde11a771f | 90 | return; |
mbed_official | 74:51fde11a771f | 91 | } |
mbed_official | 74:51fde11a771f | 92 | |
mbed_official | 74:51fde11a771f | 93 | error = _ble.gap().setAdvertisingPayload( |
mbed_official | 74:51fde11a771f | 94 | ble::LEGACY_ADVERTISING_HANDLE, |
mbed_official | 74:51fde11a771f | 95 | _adv_data_builder.getAdvertisingData() |
mbed_official | 74:51fde11a771f | 96 | ); |
mbed_official | 74:51fde11a771f | 97 | |
mbed_official | 74:51fde11a771f | 98 | if (error) { |
mbed_official | 74:51fde11a771f | 99 | printf("_ble.gap().setAdvertisingPayload() failed\r\n"); |
mbed_official | 74:51fde11a771f | 100 | return; |
mbed_official | 74:51fde11a771f | 101 | } |
mbed_official | 74:51fde11a771f | 102 | |
mbed_official | 74:51fde11a771f | 103 | /* Start advertising */ |
mbed_official | 74:51fde11a771f | 104 | |
mbed_official | 74:51fde11a771f | 105 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
mbed_official | 74:51fde11a771f | 106 | |
mbed_official | 74:51fde11a771f | 107 | if (error) { |
mbed_official | 74:51fde11a771f | 108 | printf("_ble.gap().startAdvertising() failed\r\n"); |
mbed_official | 74:51fde11a771f | 109 | return; |
mbed_official | 74:51fde11a771f | 110 | } |
mbed_official | 2:864ddfb70a9c | 111 | } |
mbed_official | 2:864ddfb70a9c | 112 | |
mbed_official | 74:51fde11a771f | 113 | /** |
mbed_official | 74:51fde11a771f | 114 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
mbed_official | 74:51fde11a771f | 115 | * |
mbed_official | 74:51fde11a771f | 116 | * @param[in] params Information about the characterisitc being updated. |
mbed_official | 74:51fde11a771f | 117 | */ |
mbed_official | 74:51fde11a771f | 118 | void on_data_written(const GattWriteCallbackParams *params) { |
mbed_official | 74:51fde11a771f | 119 | if ((params->handle == _led_service->getValueHandle()) && (params->len == 1)) { |
mbed_official | 74:51fde11a771f | 120 | _actuated_led = *(params->data); |
mbed_official | 74:51fde11a771f | 121 | } |
mbed_official | 74:51fde11a771f | 122 | } |
mbed_official | 2:864ddfb70a9c | 123 | |
mbed_official | 74:51fde11a771f | 124 | void blink() { |
mbed_official | 74:51fde11a771f | 125 | _alive_led = !_alive_led; |
mbed_official | 74:51fde11a771f | 126 | } |
mbed_official | 74:51fde11a771f | 127 | |
mbed_official | 74:51fde11a771f | 128 | private: |
mbed_official | 74:51fde11a771f | 129 | /* Event handler */ |
mbed_official | 2:864ddfb70a9c | 130 | |
mbed_official | 74:51fde11a771f | 131 | void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { |
mbed_official | 74:51fde11a771f | 132 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
mbed_official | 74:51fde11a771f | 133 | } |
mbed_official | 74:51fde11a771f | 134 | |
mbed_official | 74:51fde11a771f | 135 | private: |
mbed_official | 74:51fde11a771f | 136 | BLE &_ble; |
mbed_official | 74:51fde11a771f | 137 | events::EventQueue &_event_queue; |
mbed_official | 74:51fde11a771f | 138 | DigitalOut _alive_led; |
mbed_official | 74:51fde11a771f | 139 | DigitalOut _actuated_led; |
mbed_official | 44:df8adb3bc797 | 140 | |
mbed_official | 74:51fde11a771f | 141 | UUID _led_uuid; |
mbed_official | 74:51fde11a771f | 142 | LEDService *_led_service; |
mbed_official | 2:864ddfb70a9c | 143 | |
mbed_official | 74:51fde11a771f | 144 | uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; |
mbed_official | 74:51fde11a771f | 145 | ble::AdvertisingDataBuilder _adv_data_builder; |
mbed_official | 74:51fde11a771f | 146 | }; |
mbed_official | 74:51fde11a771f | 147 | |
mbed_official | 74:51fde11a771f | 148 | /** Schedule processing of events from the BLE middleware in the event queue. */ |
mbed_official | 74:51fde11a771f | 149 | void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { |
mbed_official | 74:51fde11a771f | 150 | event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); |
mbed_official | 2:864ddfb70a9c | 151 | } |
mbed_official | 2:864ddfb70a9c | 152 | |
mbed_official | 2:864ddfb70a9c | 153 | int main() |
mbed_official | 2:864ddfb70a9c | 154 | { |
mbed_official | 74:51fde11a771f | 155 | BLE &ble = BLE::Instance(); |
mbed_official | 74:51fde11a771f | 156 | ble.onEventsToProcess(schedule_ble_events); |
mbed_official | 2:864ddfb70a9c | 157 | |
mbed_official | 74:51fde11a771f | 158 | LEDDemo demo(ble, event_queue); |
mbed_official | 74:51fde11a771f | 159 | demo.start(); |
mbed_official | 2:864ddfb70a9c | 160 | |
mbed_official | 2:864ddfb70a9c | 161 | return 0; |
mbed_official | 2:864ddfb70a9c | 162 | } |
mbed_official | 74:51fde11a771f | 163 |