hw

Committer:
b05901043
Date:
Thu Apr 23 07:55:49 2020 +0000
Revision:
0:38e06c586f64
Child:
1:b07c6505284b
.

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"
b05901043 0:38e06c586f64 22 #include "pretty_printer.h"
b05901043 0:38e06c586f64 23
b05901043 0:38e06c586f64 24 const static char DEVICE_NAME[] = "Heartrate";
b05901043 0:38e06c586f64 25
b05901043 0:38e06c586f64 26 static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE);
b05901043 0:38e06c586f64 27
b05901043 0:38e06c586f64 28 class HeartrateDemo : ble::Gap::EventHandler {
b05901043 0:38e06c586f64 29 public:
b05901043 0:38e06c586f64 30 HeartrateDemo(BLE &ble, events::EventQueue &event_queue) :
b05901043 0:38e06c586f64 31 _ble(ble),
b05901043 0:38e06c586f64 32 _event_queue(event_queue),
b05901043 0:38e06c586f64 33 _led1(LED1, 1),
b05901043 0:38e06c586f64 34 _connected(false),
b05901043 0:38e06c586f64 35 _hr_uuid(GattService::UUID_HEART_RATE_SERVICE),
b05901043 0:38e06c586f64 36 _hr_counter(100),
b05901043 0:38e06c586f64 37 _hr_service(ble, _hr_counter, HeartRateService::LOCATION_FINGER),
b05901043 0:38e06c586f64 38 _adv_data_builder(_adv_buffer) { }
b05901043 0:38e06c586f64 39
b05901043 0:38e06c586f64 40 void start() {
b05901043 0:38e06c586f64 41 _ble.gap().setEventHandler(this);
b05901043 0:38e06c586f64 42
b05901043 0:38e06c586f64 43 _ble.init(this, &HeartrateDemo::on_init_complete);
b05901043 0:38e06c586f64 44
b05901043 0:38e06c586f64 45 _event_queue.call_every(500, this, &HeartrateDemo::blink);
b05901043 0:38e06c586f64 46 _event_queue.call_every(1000, this, &HeartrateDemo::update_sensor_value);
b05901043 0:38e06c586f64 47
b05901043 0:38e06c586f64 48 _event_queue.dispatch_forever();
b05901043 0:38e06c586f64 49 }
b05901043 0:38e06c586f64 50
b05901043 0:38e06c586f64 51 private:
b05901043 0:38e06c586f64 52 /** Callback triggered when the ble initialization process has finished */
b05901043 0:38e06c586f64 53 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
b05901043 0:38e06c586f64 54 if (params->error != BLE_ERROR_NONE) {
b05901043 0:38e06c586f64 55 printf("Ble initialization failed.");
b05901043 0:38e06c586f64 56 return;
b05901043 0:38e06c586f64 57 }
b05901043 0:38e06c586f64 58
b05901043 0:38e06c586f64 59 print_mac_address();
b05901043 0:38e06c586f64 60
b05901043 0:38e06c586f64 61 start_advertising();
b05901043 0:38e06c586f64 62 }
b05901043 0:38e06c586f64 63
b05901043 0:38e06c586f64 64 void start_advertising() {
b05901043 0:38e06c586f64 65 /* Create advertising parameters and payload */
b05901043 0:38e06c586f64 66
b05901043 0:38e06c586f64 67 ble::AdvertisingParameters adv_parameters(
b05901043 0:38e06c586f64 68 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
b05901043 0:38e06c586f64 69 ble::adv_interval_t(ble::millisecond_t(1000))
b05901043 0:38e06c586f64 70 );
b05901043 0:38e06c586f64 71
b05901043 0:38e06c586f64 72 _adv_data_builder.setFlags();
b05901043 0:38e06c586f64 73 _adv_data_builder.setAppearance(ble::adv_data_appearance_t::GENERIC_HEART_RATE_SENSOR);
b05901043 0:38e06c586f64 74 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_hr_uuid, 1));
b05901043 0:38e06c586f64 75 _adv_data_builder.setName(DEVICE_NAME);
b05901043 0:38e06c586f64 76
b05901043 0:38e06c586f64 77 /* Setup advertising */
b05901043 0:38e06c586f64 78
b05901043 0:38e06c586f64 79 ble_error_t error = _ble.gap().setAdvertisingParameters(
b05901043 0:38e06c586f64 80 ble::LEGACY_ADVERTISING_HANDLE,
b05901043 0:38e06c586f64 81 adv_parameters
b05901043 0:38e06c586f64 82 );
b05901043 0:38e06c586f64 83
b05901043 0:38e06c586f64 84 if (error) {
b05901043 0:38e06c586f64 85 printf("_ble.gap().setAdvertisingParameters() failed\r\n");
b05901043 0:38e06c586f64 86 return;
b05901043 0:38e06c586f64 87 }
b05901043 0:38e06c586f64 88
b05901043 0:38e06c586f64 89 error = _ble.gap().setAdvertisingPayload(
b05901043 0:38e06c586f64 90 ble::LEGACY_ADVERTISING_HANDLE,
b05901043 0:38e06c586f64 91 _adv_data_builder.getAdvertisingData()
b05901043 0:38e06c586f64 92 );
b05901043 0:38e06c586f64 93
b05901043 0:38e06c586f64 94 if (error) {
b05901043 0:38e06c586f64 95 printf("_ble.gap().setAdvertisingPayload() failed\r\n");
b05901043 0:38e06c586f64 96 return;
b05901043 0:38e06c586f64 97 }
b05901043 0:38e06c586f64 98
b05901043 0:38e06c586f64 99 /* Start advertising */
b05901043 0:38e06c586f64 100
b05901043 0:38e06c586f64 101 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
b05901043 0:38e06c586f64 102
b05901043 0:38e06c586f64 103 if (error) {
b05901043 0:38e06c586f64 104 printf("_ble.gap().startAdvertising() failed\r\n");
b05901043 0:38e06c586f64 105 return;
b05901043 0:38e06c586f64 106 }
b05901043 0:38e06c586f64 107 }
b05901043 0:38e06c586f64 108
b05901043 0:38e06c586f64 109 void update_sensor_value() {
b05901043 0:38e06c586f64 110 if (_connected) {
b05901043 0:38e06c586f64 111 // Do blocking calls or whatever is necessary for sensor polling.
b05901043 0:38e06c586f64 112 // In our case, we simply update the HRM measurement.
b05901043 0:38e06c586f64 113 _hr_counter++;
b05901043 0:38e06c586f64 114
b05901043 0:38e06c586f64 115 // 100 <= HRM bps <=175
b05901043 0:38e06c586f64 116 if (_hr_counter == 175) {
b05901043 0:38e06c586f64 117 _hr_counter = 100;
b05901043 0:38e06c586f64 118 }
b05901043 0:38e06c586f64 119
b05901043 0:38e06c586f64 120 _hr_service.updateHeartRate(_hr_counter);
b05901043 0:38e06c586f64 121 }
b05901043 0:38e06c586f64 122 }
b05901043 0:38e06c586f64 123
b05901043 0:38e06c586f64 124 void blink(void) {
b05901043 0:38e06c586f64 125 _led1 = !_led1;
b05901043 0:38e06c586f64 126 }
b05901043 0:38e06c586f64 127
b05901043 0:38e06c586f64 128 private:
b05901043 0:38e06c586f64 129 /* Event handler */
b05901043 0:38e06c586f64 130
b05901043 0:38e06c586f64 131 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
b05901043 0:38e06c586f64 132 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
b05901043 0:38e06c586f64 133 _connected = false;
b05901043 0:38e06c586f64 134 }
b05901043 0:38e06c586f64 135
b05901043 0:38e06c586f64 136 virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) {
b05901043 0:38e06c586f64 137 if (event.getStatus() == BLE_ERROR_NONE) {
b05901043 0:38e06c586f64 138 _connected = true;
b05901043 0:38e06c586f64 139 }
b05901043 0:38e06c586f64 140 }
b05901043 0:38e06c586f64 141
b05901043 0:38e06c586f64 142 private:
b05901043 0:38e06c586f64 143 BLE &_ble;
b05901043 0:38e06c586f64 144 events::EventQueue &_event_queue;
b05901043 0:38e06c586f64 145 DigitalOut _led1;
b05901043 0:38e06c586f64 146
b05901043 0:38e06c586f64 147 bool _connected;
b05901043 0:38e06c586f64 148
b05901043 0:38e06c586f64 149 UUID _hr_uuid;
b05901043 0:38e06c586f64 150
b05901043 0:38e06c586f64 151 uint8_t _hr_counter;
b05901043 0:38e06c586f64 152 HeartRateService _hr_service;
b05901043 0:38e06c586f64 153
b05901043 0:38e06c586f64 154 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
b05901043 0:38e06c586f64 155 ble::AdvertisingDataBuilder _adv_data_builder;
b05901043 0:38e06c586f64 156 };
b05901043 0:38e06c586f64 157
b05901043 0:38e06c586f64 158 /** Schedule processing of events from the BLE middleware in the event queue. */
b05901043 0:38e06c586f64 159 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
b05901043 0:38e06c586f64 160 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
b05901043 0:38e06c586f64 161 }
b05901043 0:38e06c586f64 162
b05901043 0:38e06c586f64 163 int main()
b05901043 0:38e06c586f64 164 {
b05901043 0:38e06c586f64 165 BLE &ble = BLE::Instance();
b05901043 0:38e06c586f64 166 ble.onEventsToProcess(schedule_ble_events);
b05901043 0:38e06c586f64 167
b05901043 0:38e06c586f64 168 HeartrateDemo demo(ble, event_queue);
b05901043 0:38e06c586f64 169 demo.start();
b05901043 0:38e06c586f64 170
b05901043 0:38e06c586f64 171 return 0;
b05901043 0:38e06c586f64 172 }
b05901043 0:38e06c586f64 173