hw

Committer:
b05901043
Date:
Thu Apr 30 15:08:01 2020 +0000
Revision:
2:69687bd56a07
Parent:
1:b07c6505284b
debug devise info

Who changed what in which revision?

UserRevisionLine numberNew 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),
b05901043 2:69687bd56a07 40 _deviceInfo(ble, "ST", "Nucleo", "SN1", "", "", ""),
jim_lsj 1:b07c6505284b 41 _adv_data_builder(_adv_buffer) {
b05901043 2:69687bd56a07 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 2:69687bd56a07 87
b05901043 2:69687bd56a07 88 //_ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
b05901043 0:38e06c586f64 89
b05901043 0:38e06c586f64 90 /* Setup advertising */
b05901043 0:38e06c586f64 91
b05901043 0:38e06c586f64 92 ble_error_t error = _ble.gap().setAdvertisingParameters(
b05901043 0:38e06c586f64 93 ble::LEGACY_ADVERTISING_HANDLE,
b05901043 0:38e06c586f64 94 adv_parameters
b05901043 0:38e06c586f64 95 );
b05901043 0:38e06c586f64 96
b05901043 0:38e06c586f64 97 if (error) {
b05901043 0:38e06c586f64 98 printf("_ble.gap().setAdvertisingParameters() failed\r\n");
b05901043 0:38e06c586f64 99 return;
b05901043 0:38e06c586f64 100 }
b05901043 0:38e06c586f64 101
b05901043 0:38e06c586f64 102 error = _ble.gap().setAdvertisingPayload(
b05901043 0:38e06c586f64 103 ble::LEGACY_ADVERTISING_HANDLE,
b05901043 0:38e06c586f64 104 _adv_data_builder.getAdvertisingData()
b05901043 0:38e06c586f64 105 );
b05901043 0:38e06c586f64 106
b05901043 0:38e06c586f64 107 if (error) {
b05901043 0:38e06c586f64 108 printf("_ble.gap().setAdvertisingPayload() failed\r\n");
b05901043 0:38e06c586f64 109 return;
b05901043 0:38e06c586f64 110 }
b05901043 0:38e06c586f64 111
b05901043 0:38e06c586f64 112 /* Start advertising */
b05901043 0:38e06c586f64 113
b05901043 0:38e06c586f64 114 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
b05901043 0:38e06c586f64 115
b05901043 0:38e06c586f64 116 if (error) {
b05901043 0:38e06c586f64 117 printf("_ble.gap().startAdvertising() failed\r\n");
b05901043 0:38e06c586f64 118 return;
b05901043 0:38e06c586f64 119 }
b05901043 0:38e06c586f64 120 }
b05901043 0:38e06c586f64 121
b05901043 0:38e06c586f64 122 void update_sensor_value() {
b05901043 0:38e06c586f64 123 if (_connected) {
b05901043 0:38e06c586f64 124 // Do blocking calls or whatever is necessary for sensor polling.
b05901043 0:38e06c586f64 125 // In our case, we simply update the HRM measurement.
b05901043 0:38e06c586f64 126 _hr_counter++;
b05901043 0:38e06c586f64 127
b05901043 0:38e06c586f64 128 // 100 <= HRM bps <=175
jim_lsj 1:b07c6505284b 129 if (_hr_counter == 150) {
jim_lsj 1:b07c6505284b 130 _hr_counter = 80;
b05901043 0:38e06c586f64 131 }
b05901043 0:38e06c586f64 132
b05901043 0:38e06c586f64 133 _hr_service.updateHeartRate(_hr_counter);
b05901043 0:38e06c586f64 134 }
b05901043 0:38e06c586f64 135 }
b05901043 0:38e06c586f64 136
b05901043 0:38e06c586f64 137 void blink(void) {
b05901043 0:38e06c586f64 138 _led1 = !_led1;
b05901043 0:38e06c586f64 139 }
b05901043 0:38e06c586f64 140
b05901043 0:38e06c586f64 141 private:
b05901043 0:38e06c586f64 142 /* Event handler */
b05901043 0:38e06c586f64 143
b05901043 0:38e06c586f64 144 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
b05901043 0:38e06c586f64 145 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
b05901043 0:38e06c586f64 146 _connected = false;
b05901043 0:38e06c586f64 147 }
b05901043 0:38e06c586f64 148
b05901043 0:38e06c586f64 149 virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) {
b05901043 0:38e06c586f64 150 if (event.getStatus() == BLE_ERROR_NONE) {
b05901043 0:38e06c586f64 151 _connected = true;
b05901043 0:38e06c586f64 152 }
b05901043 0:38e06c586f64 153 }
b05901043 0:38e06c586f64 154
b05901043 0:38e06c586f64 155 private:
b05901043 0:38e06c586f64 156 BLE &_ble;
b05901043 0:38e06c586f64 157 events::EventQueue &_event_queue;
b05901043 0:38e06c586f64 158 DigitalOut _led1;
b05901043 0:38e06c586f64 159
b05901043 0:38e06c586f64 160 bool _connected;
b05901043 0:38e06c586f64 161
jim_lsj 1:b07c6505284b 162 UUID * _uuid_list;
b05901043 0:38e06c586f64 163
b05901043 0:38e06c586f64 164 uint8_t _hr_counter;
b05901043 0:38e06c586f64 165 HeartRateService _hr_service;
jim_lsj 1:b07c6505284b 166
jim_lsj 1:b07c6505284b 167 uint8_t _battery_level;
jim_lsj 1:b07c6505284b 168 BatteryService _bt_service;
jim_lsj 1:b07c6505284b 169
jim_lsj 1:b07c6505284b 170 DeviceInformationService _deviceInfo;
b05901043 0:38e06c586f64 171
b05901043 0:38e06c586f64 172 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
b05901043 0:38e06c586f64 173 ble::AdvertisingDataBuilder _adv_data_builder;
b05901043 0:38e06c586f64 174 };
b05901043 0:38e06c586f64 175
b05901043 0:38e06c586f64 176 /** Schedule processing of events from the BLE middleware in the event queue. */
b05901043 0:38e06c586f64 177 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
b05901043 0:38e06c586f64 178 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
b05901043 0:38e06c586f64 179 }
b05901043 0:38e06c586f64 180
b05901043 0:38e06c586f64 181 int main()
b05901043 0:38e06c586f64 182 {
b05901043 0:38e06c586f64 183 BLE &ble = BLE::Instance();
b05901043 0:38e06c586f64 184 ble.onEventsToProcess(schedule_ble_events);
b05901043 0:38e06c586f64 185
b05901043 0:38e06c586f64 186 HeartrateDemo demo(ble, event_queue);
b05901043 0:38e06c586f64 187 demo.start();
b05901043 0:38e06c586f64 188
b05901043 0:38e06c586f64 189 return 0;
b05901043 0:38e06c586f64 190 }
b05901043 0:38e06c586f64 191