add codes for data reading and PCA

Dependencies:   mpu9250_i2c Eigen

Committer:
nikoletakuneva
Date:
Fri Nov 15 15:31:52 2019 +0000
Revision:
0:dbcc46819a5e
Child:
1:28c107f8cf24
Step counter characteristic

Who changed what in which revision?

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