Petr Manas / Mbed OS pdiot-f-mbed

Dependencies:   MPU9250

Committer:
fasand
Date:
Wed Oct 23 12:57:13 2019 +0000
Revision:
3:7ad91f59dcfa
Parent:
2:6d171062f1e2
Child:
4:357e8a209777
back to default battery example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fasand 3:7ad91f59dcfa 1 /* mbed Microcontroller Library
fasand 3:7ad91f59dcfa 2 * Copyright (c) 2006-2014 ARM Limited
fasand 3:7ad91f59dcfa 3 *
fasand 3:7ad91f59dcfa 4 * Licensed under the Apache License, Version 2.0 (the "License");
fasand 3:7ad91f59dcfa 5 * you may not use this file except in compliance with the License.
fasand 3:7ad91f59dcfa 6 * You may obtain a copy of the License at
fasand 3:7ad91f59dcfa 7 *
fasand 3:7ad91f59dcfa 8 * http://www.apache.org/licenses/LICENSE-2.0
fasand 3:7ad91f59dcfa 9 *
fasand 3:7ad91f59dcfa 10 * Unless required by applicable law or agreed to in writing, software
fasand 3:7ad91f59dcfa 11 * distributed under the License is distributed on an "AS IS" BASIS,
fasand 3:7ad91f59dcfa 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fasand 3:7ad91f59dcfa 13 * See the License for the specific language governing permissions and
fasand 3:7ad91f59dcfa 14 * limitations under the License.
fasand 3:7ad91f59dcfa 15 */
fasand 3:7ad91f59dcfa 16
fasand 3:7ad91f59dcfa 17 #include <events/mbed_events.h>
fasand 3:7ad91f59dcfa 18 #include <mbed.h>
fasand 1:eb38d5821d79 19 #include "ble/BLE.h"
fasand 3:7ad91f59dcfa 20 #include "ble/Gap.h"
fasand 3:7ad91f59dcfa 21 #include "ble/services/BatteryService.h"
fasand 3:7ad91f59dcfa 22 #include "pretty_printer.h"
fasand 3:7ad91f59dcfa 23
fasand 3:7ad91f59dcfa 24 static DigitalOut led1(LED1, 1);
fasand 3:7ad91f59dcfa 25
fasand 3:7ad91f59dcfa 26 const static char DEVICE_NAME[] = "BATTERY GROUP F";
fasand 3:7ad91f59dcfa 27
fasand 3:7ad91f59dcfa 28 static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE);
fasand 1:eb38d5821d79 29
fasand 3:7ad91f59dcfa 30 class BatteryDemo : ble::Gap::EventHandler {
fasand 3:7ad91f59dcfa 31 public:
fasand 3:7ad91f59dcfa 32 BatteryDemo(BLE &ble, events::EventQueue &event_queue) :
fasand 3:7ad91f59dcfa 33 _ble(ble),
fasand 3:7ad91f59dcfa 34 _event_queue(event_queue),
fasand 3:7ad91f59dcfa 35 _battery_uuid(GattService::UUID_BATTERY_SERVICE),
fasand 3:7ad91f59dcfa 36 _battery_level(50),
fasand 3:7ad91f59dcfa 37 _battery_service(ble, _battery_level),
fasand 3:7ad91f59dcfa 38 _adv_data_builder(_adv_buffer) { }
fasand 3:7ad91f59dcfa 39
fasand 3:7ad91f59dcfa 40 void start() {
fasand 3:7ad91f59dcfa 41 _ble.gap().setEventHandler(this);
fasand 3:7ad91f59dcfa 42
fasand 3:7ad91f59dcfa 43 _ble.init(this, &BatteryDemo::on_init_complete);
fasand 3:7ad91f59dcfa 44
fasand 3:7ad91f59dcfa 45 _event_queue.call_every(500, this, &BatteryDemo::blink);
fasand 3:7ad91f59dcfa 46 _event_queue.call_every(1000, this, &BatteryDemo::update_sensor_value);
fasand 3:7ad91f59dcfa 47
fasand 3:7ad91f59dcfa 48 _event_queue.dispatch_forever();
fasand 3:7ad91f59dcfa 49 }
fasand 1:eb38d5821d79 50
fasand 3:7ad91f59dcfa 51 private:
fasand 3:7ad91f59dcfa 52 /** Callback triggered when the ble initialization process has finished */
fasand 3:7ad91f59dcfa 53 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
fasand 3:7ad91f59dcfa 54 if (params->error != BLE_ERROR_NONE) {
fasand 3:7ad91f59dcfa 55 print_error(params->error, "Ble initialization failed.");
fasand 3:7ad91f59dcfa 56 return;
fasand 3:7ad91f59dcfa 57 }
fasand 3:7ad91f59dcfa 58
fasand 3:7ad91f59dcfa 59 print_mac_address();
fasand 3:7ad91f59dcfa 60
fasand 3:7ad91f59dcfa 61 start_advertising();
fasand 3:7ad91f59dcfa 62 }
fasand 3:7ad91f59dcfa 63
fasand 3:7ad91f59dcfa 64 void start_advertising() {
fasand 3:7ad91f59dcfa 65 /* Create advertising parameters and payload */
fasand 3:7ad91f59dcfa 66
fasand 3:7ad91f59dcfa 67 ble::AdvertisingParameters adv_parameters(
fasand 3:7ad91f59dcfa 68 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
fasand 3:7ad91f59dcfa 69 ble::adv_interval_t(ble::millisecond_t(1000))
fasand 3:7ad91f59dcfa 70 );
fasand 1:eb38d5821d79 71
fasand 3:7ad91f59dcfa 72 _adv_data_builder.setFlags();
fasand 3:7ad91f59dcfa 73 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_battery_uuid, 1));
fasand 3:7ad91f59dcfa 74 _adv_data_builder.setName(DEVICE_NAME);
fasand 3:7ad91f59dcfa 75
fasand 3:7ad91f59dcfa 76 /* Setup advertising */
fasand 3:7ad91f59dcfa 77
fasand 3:7ad91f59dcfa 78 ble_error_t error = _ble.gap().setAdvertisingParameters(
fasand 3:7ad91f59dcfa 79 ble::LEGACY_ADVERTISING_HANDLE,
fasand 3:7ad91f59dcfa 80 adv_parameters
fasand 3:7ad91f59dcfa 81 );
fasand 3:7ad91f59dcfa 82
fasand 3:7ad91f59dcfa 83 if (error) {
fasand 3:7ad91f59dcfa 84 print_error(error, "_ble.gap().setAdvertisingParameters() failed");
fasand 3:7ad91f59dcfa 85 return;
fasand 3:7ad91f59dcfa 86 }
fasand 0:68da41d2fe5c 87
fasand 3:7ad91f59dcfa 88 error = _ble.gap().setAdvertisingPayload(
fasand 3:7ad91f59dcfa 89 ble::LEGACY_ADVERTISING_HANDLE,
fasand 3:7ad91f59dcfa 90 _adv_data_builder.getAdvertisingData()
fasand 3:7ad91f59dcfa 91 );
fasand 3:7ad91f59dcfa 92
fasand 3:7ad91f59dcfa 93 if (error) {
fasand 3:7ad91f59dcfa 94 print_error(error, "_ble.gap().setAdvertisingPayload() failed");
fasand 3:7ad91f59dcfa 95 return;
fasand 3:7ad91f59dcfa 96 }
fasand 3:7ad91f59dcfa 97
fasand 3:7ad91f59dcfa 98 /* Start advertising */
fasand 3:7ad91f59dcfa 99
fasand 3:7ad91f59dcfa 100 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
fasand 3:7ad91f59dcfa 101
fasand 3:7ad91f59dcfa 102 if (error) {
fasand 3:7ad91f59dcfa 103 print_error(error, "_ble.gap().startAdvertising() failed");
fasand 3:7ad91f59dcfa 104 return;
fasand 3:7ad91f59dcfa 105 }
fasand 3:7ad91f59dcfa 106 }
fasand 1:eb38d5821d79 107
fasand 3:7ad91f59dcfa 108 void update_sensor_value() {
fasand 3:7ad91f59dcfa 109 if (_ble.gap().getState().connected) {
fasand 3:7ad91f59dcfa 110 _battery_level++;
fasand 3:7ad91f59dcfa 111 if (_battery_level > 100) {
fasand 3:7ad91f59dcfa 112 _battery_level = 20;
fasand 3:7ad91f59dcfa 113 }
fasand 1:eb38d5821d79 114
fasand 3:7ad91f59dcfa 115 _battery_service.updateBatteryLevel(_battery_level);
fasand 3:7ad91f59dcfa 116 }
fasand 3:7ad91f59dcfa 117 }
fasand 3:7ad91f59dcfa 118
fasand 3:7ad91f59dcfa 119 void blink(void) {
fasand 3:7ad91f59dcfa 120 led1 = !led1;
fasand 3:7ad91f59dcfa 121 }
fasand 3:7ad91f59dcfa 122
fasand 3:7ad91f59dcfa 123 private:
fasand 3:7ad91f59dcfa 124 /* Event handler */
fasand 0:68da41d2fe5c 125
fasand 3:7ad91f59dcfa 126 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
fasand 3:7ad91f59dcfa 127 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
fasand 3:7ad91f59dcfa 128 }
fasand 3:7ad91f59dcfa 129
fasand 3:7ad91f59dcfa 130 private:
fasand 3:7ad91f59dcfa 131 BLE &_ble;
fasand 3:7ad91f59dcfa 132 events::EventQueue &_event_queue;
fasand 3:7ad91f59dcfa 133
fasand 3:7ad91f59dcfa 134 UUID _battery_uuid;
fasand 3:7ad91f59dcfa 135
fasand 3:7ad91f59dcfa 136 uint8_t _battery_level;
fasand 3:7ad91f59dcfa 137 BatteryService _battery_service;
fasand 3:7ad91f59dcfa 138
fasand 3:7ad91f59dcfa 139 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
fasand 3:7ad91f59dcfa 140 ble::AdvertisingDataBuilder _adv_data_builder;
fasand 3:7ad91f59dcfa 141 };
fasand 3:7ad91f59dcfa 142
fasand 3:7ad91f59dcfa 143 /** Schedule processing of events from the BLE middleware in the event queue. */
fasand 3:7ad91f59dcfa 144 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
fasand 3:7ad91f59dcfa 145 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
fasand 1:eb38d5821d79 146 }
fasand 0:68da41d2fe5c 147
fasand 3:7ad91f59dcfa 148 int main()
fasand 1:eb38d5821d79 149 {
fasand 3:7ad91f59dcfa 150 BLE &ble = BLE::Instance();
fasand 3:7ad91f59dcfa 151 ble.onEventsToProcess(schedule_ble_events);
fasand 0:68da41d2fe5c 152
fasand 3:7ad91f59dcfa 153 BatteryDemo demo(ble, event_queue);
fasand 3:7ad91f59dcfa 154 demo.start();
fasand 0:68da41d2fe5c 155
fasand 3:7ad91f59dcfa 156 return 0;
fasand 1:eb38d5821d79 157 }