Karl Chuang
/
bluetooth-hw
hw
source/main.cpp@1:b07c6505284b, 2020-04-23 (annotated)
- Committer:
- jim_lsj
- Date:
- Thu Apr 23 08:43:44 2020 +0000
- Revision:
- 1:b07c6505284b
- Parent:
- 0:38e06c586f64
- Child:
- 2:69687bd56a07
add services
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
b05901043 | 0:38e06c586f64 | 1 | /* mbed Microcontroller Library |
b05901043 | 0:38e06c586f64 | 2 | * Copyright (c) 2006-2015 ARM Limited |
b05901043 | 0:38e06c586f64 | 3 | * |
b05901043 | 0:38e06c586f64 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
b05901043 | 0:38e06c586f64 | 5 | * you may not use this file except in compliance with the License. |
b05901043 | 0:38e06c586f64 | 6 | * You may obtain a copy of the License at |
b05901043 | 0:38e06c586f64 | 7 | * |
b05901043 | 0:38e06c586f64 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
b05901043 | 0:38e06c586f64 | 9 | * |
b05901043 | 0:38e06c586f64 | 10 | * Unless required by applicable law or agreed to in writing, software |
b05901043 | 0:38e06c586f64 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
b05901043 | 0:38e06c586f64 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
b05901043 | 0:38e06c586f64 | 13 | * See the License for the specific language governing permissions and |
b05901043 | 0:38e06c586f64 | 14 | * limitations under the License. |
b05901043 | 0:38e06c586f64 | 15 | */ |
b05901043 | 0:38e06c586f64 | 16 | |
b05901043 | 0:38e06c586f64 | 17 | #include <events/mbed_events.h> |
b05901043 | 0:38e06c586f64 | 18 | #include <mbed.h> |
b05901043 | 0:38e06c586f64 | 19 | #include "ble/BLE.h" |
b05901043 | 0:38e06c586f64 | 20 | #include "ble/gap/Gap.h" |
b05901043 | 0:38e06c586f64 | 21 | #include "ble/services/HeartRateService.h" |
jim_lsj | 1:b07c6505284b | 22 | #include "ble/services/BatteryService.h" |
jim_lsj | 1:b07c6505284b | 23 | #include "ble/services/DeviceInformationService.h" |
b05901043 | 0:38e06c586f64 | 24 | #include "pretty_printer.h" |
b05901043 | 0:38e06c586f64 | 25 | |
b05901043 | 0:38e06c586f64 | 26 | const static char DEVICE_NAME[] = "Heartrate"; |
b05901043 | 0:38e06c586f64 | 27 | |
b05901043 | 0:38e06c586f64 | 28 | static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
b05901043 | 0:38e06c586f64 | 29 | |
b05901043 | 0:38e06c586f64 | 30 | class HeartrateDemo : ble::Gap::EventHandler { |
b05901043 | 0:38e06c586f64 | 31 | public: |
b05901043 | 0:38e06c586f64 | 32 | HeartrateDemo(BLE &ble, events::EventQueue &event_queue) : |
b05901043 | 0:38e06c586f64 | 33 | _ble(ble), |
b05901043 | 0:38e06c586f64 | 34 | _event_queue(event_queue), |
b05901043 | 0:38e06c586f64 | 35 | _led1(LED1, 1), |
b05901043 | 0:38e06c586f64 | 36 | _connected(false), |
b05901043 | 0:38e06c586f64 | 37 | _hr_counter(100), |
jim_lsj | 1:b07c6505284b | 38 | _bt_service(ble, 25), |
b05901043 | 0:38e06c586f64 | 39 | _hr_service(ble, _hr_counter, HeartRateService::LOCATION_FINGER), |
jim_lsj | 1:b07c6505284b | 40 | _deviceInfo(ble, "ST", "Nucleo", "SN1" ), |
jim_lsj | 1:b07c6505284b | 41 | _adv_data_builder(_adv_buffer) { |
jim_lsj | 1:b07c6505284b | 42 | _uuid_list = new UUID(3); |
jim_lsj | 1:b07c6505284b | 43 | _uuid_list[0] = GattService::UUID_HEART_RATE_SERVICE; |
jim_lsj | 1:b07c6505284b | 44 | _uuid_list[1] = GattService::UUID_BATTERY_SERVICE; |
jim_lsj | 1:b07c6505284b | 45 | _uuid_list[2] = GattService::UUID_DEVICE_INFORMATION_SERVICE; |
jim_lsj | 1:b07c6505284b | 46 | } |
jim_lsj | 1:b07c6505284b | 47 | ~HeartrateDemo(){ |
jim_lsj | 1:b07c6505284b | 48 | delete [] _uuid_list; |
jim_lsj | 1:b07c6505284b | 49 | } |
b05901043 | 0:38e06c586f64 | 50 | |
b05901043 | 0:38e06c586f64 | 51 | void start() { |
b05901043 | 0:38e06c586f64 | 52 | _ble.gap().setEventHandler(this); |
b05901043 | 0:38e06c586f64 | 53 | |
b05901043 | 0:38e06c586f64 | 54 | _ble.init(this, &HeartrateDemo::on_init_complete); |
b05901043 | 0:38e06c586f64 | 55 | |
b05901043 | 0:38e06c586f64 | 56 | _event_queue.call_every(500, this, &HeartrateDemo::blink); |
b05901043 | 0:38e06c586f64 | 57 | _event_queue.call_every(1000, this, &HeartrateDemo::update_sensor_value); |
b05901043 | 0:38e06c586f64 | 58 | |
b05901043 | 0:38e06c586f64 | 59 | _event_queue.dispatch_forever(); |
b05901043 | 0:38e06c586f64 | 60 | } |
b05901043 | 0:38e06c586f64 | 61 | |
b05901043 | 0:38e06c586f64 | 62 | private: |
b05901043 | 0:38e06c586f64 | 63 | /** Callback triggered when the ble initialization process has finished */ |
b05901043 | 0:38e06c586f64 | 64 | void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { |
b05901043 | 0:38e06c586f64 | 65 | if (params->error != BLE_ERROR_NONE) { |
b05901043 | 0:38e06c586f64 | 66 | printf("Ble initialization failed."); |
b05901043 | 0:38e06c586f64 | 67 | return; |
b05901043 | 0:38e06c586f64 | 68 | } |
b05901043 | 0:38e06c586f64 | 69 | |
b05901043 | 0:38e06c586f64 | 70 | print_mac_address(); |
b05901043 | 0:38e06c586f64 | 71 | |
b05901043 | 0:38e06c586f64 | 72 | start_advertising(); |
b05901043 | 0:38e06c586f64 | 73 | } |
b05901043 | 0:38e06c586f64 | 74 | |
b05901043 | 0:38e06c586f64 | 75 | void start_advertising() { |
b05901043 | 0:38e06c586f64 | 76 | /* Create advertising parameters and payload */ |
b05901043 | 0:38e06c586f64 | 77 | |
b05901043 | 0:38e06c586f64 | 78 | ble::AdvertisingParameters adv_parameters( |
b05901043 | 0:38e06c586f64 | 79 | ble::advertising_type_t::CONNECTABLE_UNDIRECTED, |
b05901043 | 0:38e06c586f64 | 80 | ble::adv_interval_t(ble::millisecond_t(1000)) |
b05901043 | 0:38e06c586f64 | 81 | ); |
b05901043 | 0:38e06c586f64 | 82 | |
b05901043 | 0:38e06c586f64 | 83 | _adv_data_builder.setFlags(); |
b05901043 | 0:38e06c586f64 | 84 | _adv_data_builder.setAppearance(ble::adv_data_appearance_t::GENERIC_HEART_RATE_SENSOR); |
jim_lsj | 1:b07c6505284b | 85 | _adv_data_builder.setLocalServiceList(mbed::make_Span(_uuid_list, 3)); |
b05901043 | 0:38e06c586f64 | 86 | _adv_data_builder.setName(DEVICE_NAME); |
b05901043 | 0:38e06c586f64 | 87 | |
b05901043 | 0:38e06c586f64 | 88 | /* Setup advertising */ |
b05901043 | 0:38e06c586f64 | 89 | |
b05901043 | 0:38e06c586f64 | 90 | ble_error_t error = _ble.gap().setAdvertisingParameters( |
b05901043 | 0:38e06c586f64 | 91 | ble::LEGACY_ADVERTISING_HANDLE, |
b05901043 | 0:38e06c586f64 | 92 | adv_parameters |
b05901043 | 0:38e06c586f64 | 93 | ); |
b05901043 | 0:38e06c586f64 | 94 | |
b05901043 | 0:38e06c586f64 | 95 | if (error) { |
b05901043 | 0:38e06c586f64 | 96 | printf("_ble.gap().setAdvertisingParameters() failed\r\n"); |
b05901043 | 0:38e06c586f64 | 97 | return; |
b05901043 | 0:38e06c586f64 | 98 | } |
b05901043 | 0:38e06c586f64 | 99 | |
b05901043 | 0:38e06c586f64 | 100 | error = _ble.gap().setAdvertisingPayload( |
b05901043 | 0:38e06c586f64 | 101 | ble::LEGACY_ADVERTISING_HANDLE, |
b05901043 | 0:38e06c586f64 | 102 | _adv_data_builder.getAdvertisingData() |
b05901043 | 0:38e06c586f64 | 103 | ); |
b05901043 | 0:38e06c586f64 | 104 | |
b05901043 | 0:38e06c586f64 | 105 | if (error) { |
b05901043 | 0:38e06c586f64 | 106 | printf("_ble.gap().setAdvertisingPayload() failed\r\n"); |
b05901043 | 0:38e06c586f64 | 107 | return; |
b05901043 | 0:38e06c586f64 | 108 | } |
b05901043 | 0:38e06c586f64 | 109 | |
b05901043 | 0:38e06c586f64 | 110 | /* Start advertising */ |
b05901043 | 0:38e06c586f64 | 111 | |
b05901043 | 0:38e06c586f64 | 112 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
b05901043 | 0:38e06c586f64 | 113 | |
b05901043 | 0:38e06c586f64 | 114 | if (error) { |
b05901043 | 0:38e06c586f64 | 115 | printf("_ble.gap().startAdvertising() failed\r\n"); |
b05901043 | 0:38e06c586f64 | 116 | return; |
b05901043 | 0:38e06c586f64 | 117 | } |
b05901043 | 0:38e06c586f64 | 118 | } |
b05901043 | 0:38e06c586f64 | 119 | |
b05901043 | 0:38e06c586f64 | 120 | void update_sensor_value() { |
b05901043 | 0:38e06c586f64 | 121 | if (_connected) { |
b05901043 | 0:38e06c586f64 | 122 | // Do blocking calls or whatever is necessary for sensor polling. |
b05901043 | 0:38e06c586f64 | 123 | // In our case, we simply update the HRM measurement. |
b05901043 | 0:38e06c586f64 | 124 | _hr_counter++; |
b05901043 | 0:38e06c586f64 | 125 | |
b05901043 | 0:38e06c586f64 | 126 | // 100 <= HRM bps <=175 |
jim_lsj | 1:b07c6505284b | 127 | if (_hr_counter == 150) { |
jim_lsj | 1:b07c6505284b | 128 | _hr_counter = 80; |
b05901043 | 0:38e06c586f64 | 129 | } |
b05901043 | 0:38e06c586f64 | 130 | |
b05901043 | 0:38e06c586f64 | 131 | _hr_service.updateHeartRate(_hr_counter); |
b05901043 | 0:38e06c586f64 | 132 | } |
b05901043 | 0:38e06c586f64 | 133 | } |
b05901043 | 0:38e06c586f64 | 134 | |
b05901043 | 0:38e06c586f64 | 135 | void blink(void) { |
b05901043 | 0:38e06c586f64 | 136 | _led1 = !_led1; |
b05901043 | 0:38e06c586f64 | 137 | } |
b05901043 | 0:38e06c586f64 | 138 | |
b05901043 | 0:38e06c586f64 | 139 | private: |
b05901043 | 0:38e06c586f64 | 140 | /* Event handler */ |
b05901043 | 0:38e06c586f64 | 141 | |
b05901043 | 0:38e06c586f64 | 142 | void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { |
b05901043 | 0:38e06c586f64 | 143 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
b05901043 | 0:38e06c586f64 | 144 | _connected = false; |
b05901043 | 0:38e06c586f64 | 145 | } |
b05901043 | 0:38e06c586f64 | 146 | |
b05901043 | 0:38e06c586f64 | 147 | virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) { |
b05901043 | 0:38e06c586f64 | 148 | if (event.getStatus() == BLE_ERROR_NONE) { |
b05901043 | 0:38e06c586f64 | 149 | _connected = true; |
b05901043 | 0:38e06c586f64 | 150 | } |
b05901043 | 0:38e06c586f64 | 151 | } |
b05901043 | 0:38e06c586f64 | 152 | |
b05901043 | 0:38e06c586f64 | 153 | private: |
b05901043 | 0:38e06c586f64 | 154 | BLE &_ble; |
b05901043 | 0:38e06c586f64 | 155 | events::EventQueue &_event_queue; |
b05901043 | 0:38e06c586f64 | 156 | DigitalOut _led1; |
b05901043 | 0:38e06c586f64 | 157 | |
b05901043 | 0:38e06c586f64 | 158 | bool _connected; |
b05901043 | 0:38e06c586f64 | 159 | |
jim_lsj | 1:b07c6505284b | 160 | UUID * _uuid_list; |
b05901043 | 0:38e06c586f64 | 161 | |
b05901043 | 0:38e06c586f64 | 162 | uint8_t _hr_counter; |
b05901043 | 0:38e06c586f64 | 163 | HeartRateService _hr_service; |
jim_lsj | 1:b07c6505284b | 164 | |
jim_lsj | 1:b07c6505284b | 165 | uint8_t _battery_level; |
jim_lsj | 1:b07c6505284b | 166 | BatteryService _bt_service; |
jim_lsj | 1:b07c6505284b | 167 | |
jim_lsj | 1:b07c6505284b | 168 | DeviceInformationService _deviceInfo; |
b05901043 | 0:38e06c586f64 | 169 | |
b05901043 | 0:38e06c586f64 | 170 | uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; |
b05901043 | 0:38e06c586f64 | 171 | ble::AdvertisingDataBuilder _adv_data_builder; |
b05901043 | 0:38e06c586f64 | 172 | }; |
b05901043 | 0:38e06c586f64 | 173 | |
b05901043 | 0:38e06c586f64 | 174 | /** Schedule processing of events from the BLE middleware in the event queue. */ |
b05901043 | 0:38e06c586f64 | 175 | void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { |
b05901043 | 0:38e06c586f64 | 176 | event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); |
b05901043 | 0:38e06c586f64 | 177 | } |
b05901043 | 0:38e06c586f64 | 178 | |
b05901043 | 0:38e06c586f64 | 179 | int main() |
b05901043 | 0:38e06c586f64 | 180 | { |
b05901043 | 0:38e06c586f64 | 181 | BLE &ble = BLE::Instance(); |
b05901043 | 0:38e06c586f64 | 182 | ble.onEventsToProcess(schedule_ble_events); |
b05901043 | 0:38e06c586f64 | 183 | |
b05901043 | 0:38e06c586f64 | 184 | HeartrateDemo demo(ble, event_queue); |
b05901043 | 0:38e06c586f64 | 185 | demo.start(); |
b05901043 | 0:38e06c586f64 | 186 | |
b05901043 | 0:38e06c586f64 | 187 | return 0; |
b05901043 | 0:38e06c586f64 | 188 | } |
b05901043 | 0:38e06c586f64 | 189 |