GPS to Pulga
source/main.cpp@22:2c6161c96a76, 2019-07-18 (annotated)
- Committer:
- pancotinho
- Date:
- Thu Jul 18 13:42:17 2019 +0000
- Revision:
- 22:2c6161c96a76
- Parent:
- 21:6cf50085f9f3
- Child:
- 23:7f1c9c1a4c57
working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 14:de95c96e3305 | 1 | /* mbed Microcontroller Library |
mbed_official | 14:de95c96e3305 | 2 | * Copyright (c) 2017 ARM Limited |
mbed_official | 14:de95c96e3305 | 3 | * |
mbed_official | 14:de95c96e3305 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 14:de95c96e3305 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 14:de95c96e3305 | 6 | * You may obtain a copy of the License at |
mbed_official | 14:de95c96e3305 | 7 | * |
mbed_official | 14:de95c96e3305 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 14:de95c96e3305 | 9 | * |
mbed_official | 14:de95c96e3305 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 14:de95c96e3305 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 14:de95c96e3305 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 14:de95c96e3305 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 14:de95c96e3305 | 14 | * limitations under the License. |
mbed_official | 14:de95c96e3305 | 15 | */ |
pancotinho | 20:86417f956e22 | 16 | |
pancotinho | 20:86417f956e22 | 17 | #include <events/mbed_events.h> |
mbed_official | 14:de95c96e3305 | 18 | #include <stdio.h> |
pancotinho | 20:86417f956e22 | 19 | #include "mbed.h" |
mbed_official | 14:de95c96e3305 | 20 | |
mbed_official | 14:de95c96e3305 | 21 | #include "platform/Callback.h" |
mbed_official | 14:de95c96e3305 | 22 | #include "events/EventQueue.h" |
mbed_official | 14:de95c96e3305 | 23 | #include "platform/NonCopyable.h" |
mbed_official | 14:de95c96e3305 | 24 | |
mbed_official | 14:de95c96e3305 | 25 | #include "ble/BLE.h" |
mbed_official | 14:de95c96e3305 | 26 | #include "ble/Gap.h" |
mbed_official | 14:de95c96e3305 | 27 | #include "ble/GattClient.h" |
mbed_official | 14:de95c96e3305 | 28 | #include "ble/GapAdvertisingParams.h" |
mbed_official | 14:de95c96e3305 | 29 | #include "ble/GapAdvertisingData.h" |
mbed_official | 14:de95c96e3305 | 30 | #include "ble/GattServer.h" |
mbed_official | 14:de95c96e3305 | 31 | #include "BLEProcess.h" |
pancotinho | 20:86417f956e22 | 32 | #include "Si1133.h" |
pancotinho | 22:2c6161c96a76 | 33 | #include "BME280.h" |
pancotinho | 20:86417f956e22 | 34 | |
pancotinho | 22:2c6161c96a76 | 35 | /* defines the axis for acc */ |
pancotinho | 22:2c6161c96a76 | 36 | #define ACC_NOOF_AXIS 3 |
pancotinho | 22:2c6161c96a76 | 37 | #define GYR_NOOF_AXIS 2 |
pancotinho | 22:2c6161c96a76 | 38 | |
pancotinho | 22:2c6161c96a76 | 39 | /* bmi160 slave address */ |
pancotinho | 22:2c6161c96a76 | 40 | #define BMI160_ADDR ((0x69)<<1) |
pancotinho | 22:2c6161c96a76 | 41 | |
pancotinho | 22:2c6161c96a76 | 42 | #define RAD_DEG 57.29577951 |
pancotinho | 22:2c6161c96a76 | 43 | |
pancotinho | 22:2c6161c96a76 | 44 | using mbed::callback; |
pancotinho | 22:2c6161c96a76 | 45 | |
pancotinho | 22:2c6161c96a76 | 46 | Si1133 sensor_light(P0_13, P0_15); |
pancotinho | 22:2c6161c96a76 | 47 | BME280 sensor_amb(P0_13, P0_15, 0x77 << 1) ; |
pancotinho | 22:2c6161c96a76 | 48 | |
pancotinho | 22:2c6161c96a76 | 49 | I2C i2c(p13, p15); |
mbed_official | 14:de95c96e3305 | 50 | |
pancotinho | 22:2c6161c96a76 | 51 | int msg; |
pancotinho | 22:2c6161c96a76 | 52 | int sens; |
pancotinho | 22:2c6161c96a76 | 53 | uint32_t lux = 0; |
pancotinho | 22:2c6161c96a76 | 54 | uint32_t amb = 0; |
pancotinho | 22:2c6161c96a76 | 55 | uint32_t imux = 0; |
pancotinho | 22:2c6161c96a76 | 56 | uint32_t imuy = 0; |
pancotinho | 22:2c6161c96a76 | 57 | uint32_t imuz = 0; |
pancotinho | 22:2c6161c96a76 | 58 | float sensor_get = 0; |
pancotinho | 22:2c6161c96a76 | 59 | |
pancotinho | 22:2c6161c96a76 | 60 | int16_t acc_sample_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555}; |
pancotinho | 22:2c6161c96a76 | 61 | int16_t gyr_sample_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555}; |
pancotinho | 22:2c6161c96a76 | 62 | |
pancotinho | 22:2c6161c96a76 | 63 | double acc_result_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555}; |
pancotinho | 22:2c6161c96a76 | 64 | double gyr_result_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555}; |
pancotinho | 22:2c6161c96a76 | 65 | |
pancotinho | 22:2c6161c96a76 | 66 | double accel_ang_x, accel_ang_y; |
pancotinho | 22:2c6161c96a76 | 67 | double tiltx, tilty; |
pancotinho | 22:2c6161c96a76 | 68 | double tiltx_prev, tilty_prev; |
pancotinho | 22:2c6161c96a76 | 69 | |
pancotinho | 22:2c6161c96a76 | 70 | char i2c_reg_buffer[2] = {0}; |
pancotinho | 20:86417f956e22 | 71 | |
mbed_official | 14:de95c96e3305 | 72 | |
mbed_official | 14:de95c96e3305 | 73 | /** |
pancotinho | 20:86417f956e22 | 74 | * A My service that demonstrate the GattServer features. |
mbed_official | 14:de95c96e3305 | 75 | * |
pancotinho | 22:2c6161c96a76 | 76 | * The My service host three characteristics that model the current amb, |
pancotinho | 22:2c6161c96a76 | 77 | * led and lux of the My. The value of the lux characteristic is |
mbed_official | 14:de95c96e3305 | 78 | * incremented automatically by the system. |
mbed_official | 14:de95c96e3305 | 79 | * |
pancotinho | 20:86417f956e22 | 80 | * A client can subscribe to updates of the My characteristics and get |
mbed_official | 14:de95c96e3305 | 81 | * notified when one of the value is changed. Clients can also change value of |
pancotinho | 22:2c6161c96a76 | 82 | * the lux, led and amb characteristric. |
mbed_official | 14:de95c96e3305 | 83 | */ |
pancotinho | 20:86417f956e22 | 84 | class MyService { |
pancotinho | 20:86417f956e22 | 85 | typedef MyService Self; |
mbed_official | 14:de95c96e3305 | 86 | |
mbed_official | 14:de95c96e3305 | 87 | public: |
pancotinho | 20:86417f956e22 | 88 | MyService() : |
pancotinho | 22:2c6161c96a76 | 89 | _amb_char("00000000-000a-000a-a000-a0a000aa00aa", 0), |
pancotinho | 22:2c6161c96a76 | 90 | _lux_char("11111111-111b-111b-b111-b1b111bb11bb", 0), |
pancotinho | 22:2c6161c96a76 | 91 | _imux_char("22222222-222c-222c-c222-c2c222cc22cc", 0), |
pancotinho | 22:2c6161c96a76 | 92 | _imuy_char("33333333-333d-333d-d333-d3d333dd33dd", 0), |
pancotinho | 22:2c6161c96a76 | 93 | // _imuz_char("44444444-444e-444e-e444-e4e444ee44ee", 0), |
pancotinho | 22:2c6161c96a76 | 94 | _led_char("55555555-555f-555f-f555-f5f555ff55ff", 0), |
pancotinho | 20:86417f956e22 | 95 | _My_service( |
mbed_official | 14:de95c96e3305 | 96 | /* uuid */ "51311102-030e-485f-b122-f8f381aa84ed", |
pancotinho | 20:86417f956e22 | 97 | /* characteristics */ _My_characteristics, |
pancotinho | 20:86417f956e22 | 98 | /* numCharacteristics */ sizeof(_My_characteristics) / |
pancotinho | 20:86417f956e22 | 99 | sizeof(_My_characteristics[0]) |
mbed_official | 14:de95c96e3305 | 100 | ), |
mbed_official | 14:de95c96e3305 | 101 | _server(NULL), |
pancotinho | 20:86417f956e22 | 102 | _event_queue(NULL), |
pancotinho | 20:86417f956e22 | 103 | _alive_led(P1_13, 1), |
pancotinho | 20:86417f956e22 | 104 | _actuated_led(P1_14,0) |
pancotinho | 20:86417f956e22 | 105 | |
mbed_official | 14:de95c96e3305 | 106 | { |
mbed_official | 14:de95c96e3305 | 107 | // update internal pointers (value, descriptors and characteristics array) |
pancotinho | 22:2c6161c96a76 | 108 | _My_characteristics[4] = &_led_char; |
pancotinho | 22:2c6161c96a76 | 109 | // _My_characteristics[4] = &_imuz_char; |
pancotinho | 22:2c6161c96a76 | 110 | _My_characteristics[3] = &_imuy_char; |
pancotinho | 22:2c6161c96a76 | 111 | _My_characteristics[2] = &_imux_char; |
pancotinho | 22:2c6161c96a76 | 112 | _My_characteristics[1] = &_amb_char; |
pancotinho | 22:2c6161c96a76 | 113 | _My_characteristics[0] = &_lux_char; |
mbed_official | 14:de95c96e3305 | 114 | |
mbed_official | 14:de95c96e3305 | 115 | // setup authorization handlers |
pancotinho | 22:2c6161c96a76 | 116 | _amb_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 22:2c6161c96a76 | 117 | _imux_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 22:2c6161c96a76 | 118 | _imuy_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 22:2c6161c96a76 | 119 | // _imuz_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 22:2c6161c96a76 | 120 | _lux_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 20:86417f956e22 | 121 | _led_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
mbed_official | 14:de95c96e3305 | 122 | } |
mbed_official | 14:de95c96e3305 | 123 | |
mbed_official | 14:de95c96e3305 | 124 | |
mbed_official | 14:de95c96e3305 | 125 | |
mbed_official | 14:de95c96e3305 | 126 | void start(BLE &ble_interface, events::EventQueue &event_queue) |
mbed_official | 14:de95c96e3305 | 127 | { |
mbed_official | 14:de95c96e3305 | 128 | if (_event_queue) { |
mbed_official | 14:de95c96e3305 | 129 | return; |
mbed_official | 14:de95c96e3305 | 130 | } |
mbed_official | 14:de95c96e3305 | 131 | |
mbed_official | 14:de95c96e3305 | 132 | _server = &ble_interface.gattServer(); |
mbed_official | 14:de95c96e3305 | 133 | _event_queue = &event_queue; |
mbed_official | 14:de95c96e3305 | 134 | |
pancotinho | 20:86417f956e22 | 135 | |
mbed_official | 14:de95c96e3305 | 136 | // register the service |
mbed_official | 14:de95c96e3305 | 137 | printf("Adding demo service\r\n"); |
pancotinho | 20:86417f956e22 | 138 | ble_error_t err = _server->addService(_My_service); |
mbed_official | 14:de95c96e3305 | 139 | |
mbed_official | 14:de95c96e3305 | 140 | if (err) { |
mbed_official | 14:de95c96e3305 | 141 | printf("Error %u during demo service registration.\r\n", err); |
mbed_official | 14:de95c96e3305 | 142 | return; |
mbed_official | 14:de95c96e3305 | 143 | } |
mbed_official | 14:de95c96e3305 | 144 | |
mbed_official | 14:de95c96e3305 | 145 | // read write handler |
mbed_official | 14:de95c96e3305 | 146 | _server->onDataSent(as_cb(&Self::when_data_sent)); |
mbed_official | 14:de95c96e3305 | 147 | _server->onDataWritten(as_cb(&Self::when_data_written)); |
mbed_official | 14:de95c96e3305 | 148 | _server->onDataRead(as_cb(&Self::when_data_read)); |
mbed_official | 14:de95c96e3305 | 149 | |
mbed_official | 14:de95c96e3305 | 150 | // updates subscribtion handlers |
mbed_official | 14:de95c96e3305 | 151 | _server->onUpdatesEnabled(as_cb(&Self::when_update_enabled)); |
mbed_official | 14:de95c96e3305 | 152 | _server->onUpdatesDisabled(as_cb(&Self::when_update_disabled)); |
mbed_official | 14:de95c96e3305 | 153 | _server->onConfirmationReceived(as_cb(&Self::when_confirmation_received)); |
mbed_official | 14:de95c96e3305 | 154 | |
mbed_official | 14:de95c96e3305 | 155 | // print the handles |
pancotinho | 20:86417f956e22 | 156 | printf("My service registered\r\n"); |
pancotinho | 20:86417f956e22 | 157 | printf("service handle: %u\r\n", _My_service.getHandle()); |
pancotinho | 22:2c6161c96a76 | 158 | printf("\tamb characteristic value handle %u\r\n", _amb_char.getValueHandle()); |
pancotinho | 22:2c6161c96a76 | 159 | printf("\timu characteristic value handle %u\r\n", _imux_char.getValueHandle()); |
pancotinho | 22:2c6161c96a76 | 160 | printf("\timu characteristic value handle %u\r\n", _imuy_char.getValueHandle()); |
pancotinho | 22:2c6161c96a76 | 161 | // printf("\timu characteristic value handle %u\r\n", _imuz_char.getValueHandle()); |
pancotinho | 22:2c6161c96a76 | 162 | printf("\tlux characteristic value handle %u\r\n", _lux_char.getValueHandle()); |
pancotinho | 22:2c6161c96a76 | 163 | printf("\tled characteristic value handle %u\r\n", _led_char.getValueHandle()); |
mbed_official | 14:de95c96e3305 | 164 | |
pancotinho | 22:2c6161c96a76 | 165 | _event_queue->call_every(1741 /* ms */, callback(this, &Self::update_char)); |
pancotinho | 22:2c6161c96a76 | 166 | _event_queue->call_every(1621 /* ms */, callback(this, &Self::read_sensors)); |
pancotinho | 22:2c6161c96a76 | 167 | _event_queue->call_every(503 /* ms */, callback(this, &Self::blink)); |
pancotinho | 20:86417f956e22 | 168 | |
mbed_official | 14:de95c96e3305 | 169 | } |
mbed_official | 14:de95c96e3305 | 170 | |
mbed_official | 14:de95c96e3305 | 171 | private: |
mbed_official | 14:de95c96e3305 | 172 | |
mbed_official | 14:de95c96e3305 | 173 | /** |
mbed_official | 14:de95c96e3305 | 174 | * Handler called when a notification or an indication has been sent. |
mbed_official | 14:de95c96e3305 | 175 | */ |
mbed_official | 14:de95c96e3305 | 176 | void when_data_sent(unsigned count) |
mbed_official | 14:de95c96e3305 | 177 | { |
mbed_official | 14:de95c96e3305 | 178 | printf("sent %u updates\r\n", count); |
mbed_official | 14:de95c96e3305 | 179 | } |
mbed_official | 14:de95c96e3305 | 180 | |
mbed_official | 14:de95c96e3305 | 181 | /** |
mbed_official | 14:de95c96e3305 | 182 | * Handler called after an attribute has been written. |
mbed_official | 14:de95c96e3305 | 183 | */ |
mbed_official | 14:de95c96e3305 | 184 | void when_data_written(const GattWriteCallbackParams *e) |
mbed_official | 14:de95c96e3305 | 185 | { |
mbed_official | 14:de95c96e3305 | 186 | printf("data written:\r\n"); |
mbed_official | 14:de95c96e3305 | 187 | printf("\tconnection handle: %u\r\n", e->connHandle); |
mbed_official | 14:de95c96e3305 | 188 | printf("\tattribute handle: %u", e->handle); |
pancotinho | 22:2c6161c96a76 | 189 | if (e->handle == _amb_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 190 | printf(" (amb characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 191 | } else if (e->handle == _led_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 192 | printf(" (led characteristic)\r\n"); |
pancotinho | 20:86417f956e22 | 193 | _actuated_led = *(e->data); |
pancotinho | 22:2c6161c96a76 | 194 | } else if (e->handle == _lux_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 195 | printf(" (lux characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 196 | } else if (e->handle == _imux_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 197 | printf(" (imu_x characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 198 | } else if (e->handle == _imuy_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 199 | printf(" (imu_y characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 200 | // } else if (e->handle == _imuz_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 201 | // printf(" (imu_z characteristic)\r\n"); |
mbed_official | 14:de95c96e3305 | 202 | } else { |
mbed_official | 14:de95c96e3305 | 203 | printf("\r\n"); |
mbed_official | 14:de95c96e3305 | 204 | } |
mbed_official | 14:de95c96e3305 | 205 | printf("\twrite operation: %u\r\n", e->writeOp); |
mbed_official | 14:de95c96e3305 | 206 | printf("\toffset: %u\r\n", e->offset); |
mbed_official | 14:de95c96e3305 | 207 | printf("\tlength: %u\r\n", e->len); |
mbed_official | 14:de95c96e3305 | 208 | printf("\t data: "); |
mbed_official | 14:de95c96e3305 | 209 | |
mbed_official | 14:de95c96e3305 | 210 | for (size_t i = 0; i < e->len; ++i) { |
mbed_official | 14:de95c96e3305 | 211 | printf("%02X", e->data[i]); |
mbed_official | 14:de95c96e3305 | 212 | } |
mbed_official | 14:de95c96e3305 | 213 | |
mbed_official | 14:de95c96e3305 | 214 | printf("\r\n"); |
mbed_official | 14:de95c96e3305 | 215 | } |
mbed_official | 14:de95c96e3305 | 216 | |
mbed_official | 14:de95c96e3305 | 217 | /** |
mbed_official | 14:de95c96e3305 | 218 | * Handler called after an attribute has been read. |
mbed_official | 14:de95c96e3305 | 219 | */ |
mbed_official | 14:de95c96e3305 | 220 | void when_data_read(const GattReadCallbackParams *e) |
mbed_official | 14:de95c96e3305 | 221 | { |
mbed_official | 14:de95c96e3305 | 222 | printf("data read:\r\n"); |
mbed_official | 14:de95c96e3305 | 223 | printf("\tconnection handle: %u\r\n", e->connHandle); |
mbed_official | 14:de95c96e3305 | 224 | printf("\tattribute handle: %u", e->handle); |
pancotinho | 22:2c6161c96a76 | 225 | if (e->handle == _amb_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 226 | printf(" (amb characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 227 | } else if (e->handle == _led_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 228 | printf(" (led characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 229 | } else if (e->handle == _lux_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 230 | printf(" (lux characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 231 | } else if (e->handle == _imux_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 232 | printf(" (imu_x characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 233 | } else if (e->handle == _imuy_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 234 | printf(" (imu_y characteristic)\r\n"); |
pancotinho | 22:2c6161c96a76 | 235 | // } else if (e->handle == _imuz_char.getValueHandle()) { |
pancotinho | 22:2c6161c96a76 | 236 | // printf(" (imu_z characteristic)\r\n"); |
mbed_official | 14:de95c96e3305 | 237 | } else { |
mbed_official | 14:de95c96e3305 | 238 | printf("\r\n"); |
mbed_official | 14:de95c96e3305 | 239 | } |
mbed_official | 14:de95c96e3305 | 240 | } |
mbed_official | 14:de95c96e3305 | 241 | |
mbed_official | 14:de95c96e3305 | 242 | /** |
mbed_official | 14:de95c96e3305 | 243 | * Handler called after a client has subscribed to notification or indication. |
mbed_official | 14:de95c96e3305 | 244 | * |
mbed_official | 14:de95c96e3305 | 245 | * @param handle Handle of the characteristic value affected by the change. |
mbed_official | 14:de95c96e3305 | 246 | */ |
mbed_official | 14:de95c96e3305 | 247 | void when_update_enabled(GattAttribute::Handle_t handle) |
mbed_official | 14:de95c96e3305 | 248 | { |
mbed_official | 14:de95c96e3305 | 249 | printf("update enabled on handle %d\r\n", handle); |
mbed_official | 14:de95c96e3305 | 250 | } |
mbed_official | 14:de95c96e3305 | 251 | |
mbed_official | 14:de95c96e3305 | 252 | /** |
mbed_official | 14:de95c96e3305 | 253 | * Handler called after a client has cancelled his subscription from |
mbed_official | 14:de95c96e3305 | 254 | * notification or indication. |
mbed_official | 14:de95c96e3305 | 255 | * |
mbed_official | 14:de95c96e3305 | 256 | * @param handle Handle of the characteristic value affected by the change. |
mbed_official | 14:de95c96e3305 | 257 | */ |
mbed_official | 14:de95c96e3305 | 258 | void when_update_disabled(GattAttribute::Handle_t handle) |
mbed_official | 14:de95c96e3305 | 259 | { |
mbed_official | 14:de95c96e3305 | 260 | printf("update disabled on handle %d\r\n", handle); |
mbed_official | 14:de95c96e3305 | 261 | } |
mbed_official | 14:de95c96e3305 | 262 | |
mbed_official | 14:de95c96e3305 | 263 | /** |
mbed_official | 14:de95c96e3305 | 264 | * Handler called when an indication confirmation has been received. |
mbed_official | 14:de95c96e3305 | 265 | * |
mbed_official | 14:de95c96e3305 | 266 | * @param handle Handle of the characteristic value that has emitted the |
mbed_official | 14:de95c96e3305 | 267 | * indication. |
mbed_official | 14:de95c96e3305 | 268 | */ |
mbed_official | 14:de95c96e3305 | 269 | void when_confirmation_received(GattAttribute::Handle_t handle) |
mbed_official | 14:de95c96e3305 | 270 | { |
mbed_official | 14:de95c96e3305 | 271 | printf("confirmation received on handle %d\r\n", handle); |
mbed_official | 14:de95c96e3305 | 272 | } |
mbed_official | 14:de95c96e3305 | 273 | |
mbed_official | 14:de95c96e3305 | 274 | /** |
mbed_official | 14:de95c96e3305 | 275 | * Handler called when a write request is received. |
mbed_official | 14:de95c96e3305 | 276 | * |
mbed_official | 14:de95c96e3305 | 277 | * This handler verify that the value submitted by the client is valid before |
mbed_official | 14:de95c96e3305 | 278 | * authorizing the operation. |
mbed_official | 14:de95c96e3305 | 279 | */ |
mbed_official | 14:de95c96e3305 | 280 | void authorize_client_write(GattWriteAuthCallbackParams *e) |
mbed_official | 14:de95c96e3305 | 281 | { |
mbed_official | 14:de95c96e3305 | 282 | printf("characteristic %u write authorization\r\n", e->handle); |
mbed_official | 14:de95c96e3305 | 283 | |
mbed_official | 14:de95c96e3305 | 284 | if (e->offset != 0) { |
mbed_official | 14:de95c96e3305 | 285 | printf("Error invalid offset\r\n"); |
mbed_official | 14:de95c96e3305 | 286 | e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbed_official | 14:de95c96e3305 | 287 | return; |
mbed_official | 14:de95c96e3305 | 288 | } |
mbed_official | 14:de95c96e3305 | 289 | |
mbed_official | 14:de95c96e3305 | 290 | if (e->len != 1) { |
mbed_official | 14:de95c96e3305 | 291 | printf("Error invalid len\r\n"); |
mbed_official | 14:de95c96e3305 | 292 | e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; |
mbed_official | 14:de95c96e3305 | 293 | return; |
mbed_official | 14:de95c96e3305 | 294 | } |
mbed_official | 14:de95c96e3305 | 295 | |
mbed_official | 14:de95c96e3305 | 296 | |
mbed_official | 14:de95c96e3305 | 297 | e->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbed_official | 14:de95c96e3305 | 298 | } |
mbed_official | 14:de95c96e3305 | 299 | |
mbed_official | 14:de95c96e3305 | 300 | /** |
pancotinho | 22:2c6161c96a76 | 301 | * Increment the lux luxer. |
mbed_official | 14:de95c96e3305 | 302 | */ |
pancotinho | 22:2c6161c96a76 | 303 | |
pancotinho | 22:2c6161c96a76 | 304 | void read_sensors (void) |
mbed_official | 14:de95c96e3305 | 305 | { |
pancotinho | 22:2c6161c96a76 | 306 | int32_t float_to_32; |
pancotinho | 22:2c6161c96a76 | 307 | |
pancotinho | 22:2c6161c96a76 | 308 | |
pancotinho | 22:2c6161c96a76 | 309 | |
pancotinho | 22:2c6161c96a76 | 310 | if (sensor_light.open() && sens==0) { |
pancotinho | 22:2c6161c96a76 | 311 | float_to_32=0; |
pancotinho | 22:2c6161c96a76 | 312 | sensor_get = sensor_light.get_light_level(); |
pancotinho | 22:2c6161c96a76 | 313 | float_to_32 = sensor_get/10; |
pancotinho | 22:2c6161c96a76 | 314 | //float_to_32 = __rev( float_to_32 ); |
pancotinho | 22:2c6161c96a76 | 315 | float_to_32 = (float_to_32 < 0 ? 0 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 316 | lux = (float_to_32 > 255 ? 255 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 317 | sens=1; |
pancotinho | 22:2c6161c96a76 | 318 | printf("Lux = %f\n", sensor_get); |
pancotinho | 22:2c6161c96a76 | 319 | |
pancotinho | 22:2c6161c96a76 | 320 | } |
pancotinho | 22:2c6161c96a76 | 321 | if (sens==1) { |
pancotinho | 22:2c6161c96a76 | 322 | float_to_32=0; |
pancotinho | 22:2c6161c96a76 | 323 | sensor_amb.initialize(); |
pancotinho | 22:2c6161c96a76 | 324 | sensor_get = sensor_amb.getTemperature(); |
pancotinho | 22:2c6161c96a76 | 325 | float_to_32 = sensor_get*10 -103 ; |
pancotinho | 22:2c6161c96a76 | 326 | float_to_32 = (float_to_32 < 0 ? 0 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 327 | amb = (float_to_32 > 255 ? 255 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 328 | sens=2; |
pancotinho | 22:2c6161c96a76 | 329 | printf("Amb = %f\n", sensor_get); |
pancotinho | 22:2c6161c96a76 | 330 | } |
pancotinho | 22:2c6161c96a76 | 331 | if (sens==2) { |
pancotinho | 22:2c6161c96a76 | 332 | |
pancotinho | 22:2c6161c96a76 | 333 | i2c.frequency(20000); |
pancotinho | 22:2c6161c96a76 | 334 | |
pancotinho | 22:2c6161c96a76 | 335 | /*Le os Registradores do Acelerometro*/ |
pancotinho | 22:2c6161c96a76 | 336 | i2c_reg_buffer[0] = 0x12; |
pancotinho | 22:2c6161c96a76 | 337 | i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); |
pancotinho | 22:2c6161c96a76 | 338 | i2c.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false); |
pancotinho | 22:2c6161c96a76 | 339 | |
pancotinho | 22:2c6161c96a76 | 340 | /*Le os Registradores do Giroscopio*/ |
pancotinho | 22:2c6161c96a76 | 341 | i2c_reg_buffer[0] = 0x0C; |
pancotinho | 22:2c6161c96a76 | 342 | i2c.write(BMI160_ADDR, i2c_reg_buffer, 1, true); |
pancotinho | 22:2c6161c96a76 | 343 | i2c.read(BMI160_ADDR, (char *)&gyr_sample_buffer, sizeof(gyr_sample_buffer), false); |
pancotinho | 22:2c6161c96a76 | 344 | |
pancotinho | 22:2c6161c96a76 | 345 | /*Ajusta dados brutos Acelerometro em unidades de g */ |
pancotinho | 22:2c6161c96a76 | 346 | acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0); |
pancotinho | 22:2c6161c96a76 | 347 | acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0); |
pancotinho | 22:2c6161c96a76 | 348 | acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0); |
pancotinho | 22:2c6161c96a76 | 349 | |
pancotinho | 22:2c6161c96a76 | 350 | /*Ajusta dados Brutos do Giroscopio em unidades de deg/s */ |
pancotinho | 22:2c6161c96a76 | 351 | gyr_result_buffer[0] = (gyr_sample_buffer[0]/131.2); |
pancotinho | 22:2c6161c96a76 | 352 | gyr_result_buffer[1] = (gyr_sample_buffer[1]/131.2); |
pancotinho | 22:2c6161c96a76 | 353 | |
pancotinho | 22:2c6161c96a76 | 354 | /*Calcula os Angulos de Inclinacao com valor do Acelerometro*/ |
pancotinho | 22:2c6161c96a76 | 355 | accel_ang_x=atan(acc_result_buffer[0]/sqrt(pow(acc_result_buffer[1],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; |
pancotinho | 22:2c6161c96a76 | 356 | accel_ang_y=atan(acc_result_buffer[1]/sqrt(pow(acc_result_buffer[0],2) + pow(acc_result_buffer[2],2)))*RAD_DEG; |
pancotinho | 22:2c6161c96a76 | 357 | |
pancotinho | 22:2c6161c96a76 | 358 | /*Calcula os Angulos de Rotacao com valor do Giroscopio e aplica filtro complementar realizando a fusao*/ |
pancotinho | 22:2c6161c96a76 | 359 | tiltx = (0.98*(tiltx_prev+(gyr_result_buffer[0]*0.001)))+(0.02*(accel_ang_x)); |
pancotinho | 22:2c6161c96a76 | 360 | tilty = (0.98*(tilty_prev+(gyr_result_buffer[1]*0.001)))+(0.02*(accel_ang_y)); |
pancotinho | 22:2c6161c96a76 | 361 | |
pancotinho | 20:86417f956e22 | 362 | |
pancotinho | 22:2c6161c96a76 | 363 | float_to_32 = tiltx * 100 + 127; |
pancotinho | 22:2c6161c96a76 | 364 | float_to_32 = (float_to_32 < 0 ? 0 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 365 | imux = (float_to_32 > 255 ? 255 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 366 | |
pancotinho | 22:2c6161c96a76 | 367 | float_to_32 = 0; |
pancotinho | 22:2c6161c96a76 | 368 | |
pancotinho | 22:2c6161c96a76 | 369 | float_to_32 = tilty * 100 + 127; |
pancotinho | 22:2c6161c96a76 | 370 | float_to_32 = (float_to_32 < 0 ? 0 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 371 | imuy = (float_to_32 > 255 ? 255 : float_to_32); |
pancotinho | 22:2c6161c96a76 | 372 | //imuz = acc_result_buffer[2]; |
pancotinho | 22:2c6161c96a76 | 373 | |
pancotinho | 22:2c6161c96a76 | 374 | |
pancotinho | 22:2c6161c96a76 | 375 | |
pancotinho | 22:2c6161c96a76 | 376 | |
pancotinho | 22:2c6161c96a76 | 377 | |
pancotinho | 22:2c6161c96a76 | 378 | |
pancotinho | 22:2c6161c96a76 | 379 | /*Imprime os dados ACC pre-formatados*/ |
pancotinho | 22:2c6161c96a76 | 380 | printf("%.3f,%.3f\n\r",tiltx, tilty); |
pancotinho | 22:2c6161c96a76 | 381 | |
pancotinho | 22:2c6161c96a76 | 382 | sens=0; |
pancotinho | 22:2c6161c96a76 | 383 | |
pancotinho | 22:2c6161c96a76 | 384 | // update_char(); |
pancotinho | 22:2c6161c96a76 | 385 | |
pancotinho | 22:2c6161c96a76 | 386 | } |
pancotinho | 22:2c6161c96a76 | 387 | |
pancotinho | 22:2c6161c96a76 | 388 | //update_char(); |
pancotinho | 22:2c6161c96a76 | 389 | |
pancotinho | 22:2c6161c96a76 | 390 | } |
pancotinho | 22:2c6161c96a76 | 391 | |
pancotinho | 22:2c6161c96a76 | 392 | |
pancotinho | 22:2c6161c96a76 | 393 | void update_char(void) |
pancotinho | 22:2c6161c96a76 | 394 | { |
pancotinho | 22:2c6161c96a76 | 395 | if (msg==0) {ble_error_t err = _lux_char.set(*_server, lux); |
pancotinho | 22:2c6161c96a76 | 396 | printf ("lux sensor value: %d\n", lux); |
mbed_official | 14:de95c96e3305 | 397 | if (err) { |
pancotinho | 22:2c6161c96a76 | 398 | printf("write of the lux value returned error %u\r\n", err); |
pancotinho | 22:2c6161c96a76 | 399 | return; |
pancotinho | 22:2c6161c96a76 | 400 | } |
pancotinho | 22:2c6161c96a76 | 401 | wait(0.1); |
pancotinho | 22:2c6161c96a76 | 402 | msg=1; } |
pancotinho | 22:2c6161c96a76 | 403 | |
pancotinho | 22:2c6161c96a76 | 404 | if (msg==1) {ble_error_t err = _amb_char.set(*_server, amb); |
pancotinho | 22:2c6161c96a76 | 405 | printf ("amb sensor value: %d\n", amb); |
pancotinho | 22:2c6161c96a76 | 406 | if (err) { |
pancotinho | 22:2c6161c96a76 | 407 | printf("write of the amb value returned error %u\r\n", err); |
pancotinho | 20:86417f956e22 | 408 | return; |
pancotinho | 20:86417f956e22 | 409 | } |
pancotinho | 22:2c6161c96a76 | 410 | wait(0.1); |
pancotinho | 22:2c6161c96a76 | 411 | msg=2;} |
pancotinho | 22:2c6161c96a76 | 412 | |
pancotinho | 22:2c6161c96a76 | 413 | if (msg==2) {ble_error_t err = _imux_char.set(*_server, imux); |
pancotinho | 22:2c6161c96a76 | 414 | printf ("imu_x sensor value: %d\n", imux); |
pancotinho | 22:2c6161c96a76 | 415 | if (err) { |
pancotinho | 22:2c6161c96a76 | 416 | printf("write of the imu_x value returned error %u\r\n", err); |
pancotinho | 22:2c6161c96a76 | 417 | return; |
pancotinho | 20:86417f956e22 | 418 | } |
pancotinho | 22:2c6161c96a76 | 419 | wait(0.1); |
pancotinho | 22:2c6161c96a76 | 420 | msg=3;} |
pancotinho | 22:2c6161c96a76 | 421 | |
pancotinho | 22:2c6161c96a76 | 422 | if (msg==3) {ble_error_t err = _imuy_char.set(*_server, imuy); |
pancotinho | 22:2c6161c96a76 | 423 | printf ("imu_y sensor value: %d\n", imuy); |
pancotinho | 20:86417f956e22 | 424 | if (err) { |
pancotinho | 22:2c6161c96a76 | 425 | printf("write of the imu_y value returned error %u\r\n", err); |
mbed_official | 14:de95c96e3305 | 426 | return; |
pancotinho | 22:2c6161c96a76 | 427 | } |
pancotinho | 22:2c6161c96a76 | 428 | wait(0.1); |
pancotinho | 22:2c6161c96a76 | 429 | msg=0;} |
pancotinho | 22:2c6161c96a76 | 430 | |
pancotinho | 22:2c6161c96a76 | 431 | // err = _imuz_char.set(*_server, imuz); |
pancotinho | 22:2c6161c96a76 | 432 | // printf ("imu_z sensor value: %d\n", imuz); |
pancotinho | 22:2c6161c96a76 | 433 | // if (err) { |
pancotinho | 22:2c6161c96a76 | 434 | // printf("write of the imu_z value returned error %u\r\n", err); |
pancotinho | 22:2c6161c96a76 | 435 | // return; |
pancotinho | 22:2c6161c96a76 | 436 | // } |
pancotinho | 22:2c6161c96a76 | 437 | |
pancotinho | 22:2c6161c96a76 | 438 | |
pancotinho | 20:86417f956e22 | 439 | } |
pancotinho | 22:2c6161c96a76 | 440 | |
pancotinho | 20:86417f956e22 | 441 | |
pancotinho | 20:86417f956e22 | 442 | void blink() { |
pancotinho | 20:86417f956e22 | 443 | _alive_led = !_alive_led; |
mbed_official | 14:de95c96e3305 | 444 | } |
mbed_official | 14:de95c96e3305 | 445 | |
mbed_official | 14:de95c96e3305 | 446 | /** |
pancotinho | 20:86417f956e22 | 447 | * Change led status. |
mbed_official | 14:de95c96e3305 | 448 | */ |
pancotinho | 20:86417f956e22 | 449 | void change_led(void) |
mbed_official | 14:de95c96e3305 | 450 | { |
pancotinho | 20:86417f956e22 | 451 | uint8_t led = 0; |
pancotinho | 20:86417f956e22 | 452 | ble_error_t err = _led_char.get(*_server, led); |
mbed_official | 14:de95c96e3305 | 453 | if (err) { |
pancotinho | 20:86417f956e22 | 454 | printf("read of the led value returned error %u\r\n", err); |
mbed_official | 14:de95c96e3305 | 455 | return; |
mbed_official | 14:de95c96e3305 | 456 | } |
mbed_official | 14:de95c96e3305 | 457 | } |
mbed_official | 14:de95c96e3305 | 458 | |
pancotinho | 20:86417f956e22 | 459 | |
mbed_official | 14:de95c96e3305 | 460 | |
mbed_official | 14:de95c96e3305 | 461 | private: |
mbed_official | 14:de95c96e3305 | 462 | /** |
mbed_official | 14:de95c96e3305 | 463 | * Helper that construct an event handler from a member function of this |
mbed_official | 14:de95c96e3305 | 464 | * instance. |
mbed_official | 14:de95c96e3305 | 465 | */ |
mbed_official | 14:de95c96e3305 | 466 | template<typename Arg> |
mbed_official | 14:de95c96e3305 | 467 | FunctionPointerWithContext<Arg> as_cb(void (Self::*member)(Arg)) |
mbed_official | 14:de95c96e3305 | 468 | { |
mbed_official | 14:de95c96e3305 | 469 | return makeFunctionPointer(this, member); |
mbed_official | 14:de95c96e3305 | 470 | } |
mbed_official | 14:de95c96e3305 | 471 | |
mbed_official | 14:de95c96e3305 | 472 | /** |
mbed_official | 14:de95c96e3305 | 473 | * Read, Write, Notify, Indicate Characteristic declaration helper. |
mbed_official | 14:de95c96e3305 | 474 | * |
mbed_official | 14:de95c96e3305 | 475 | * @tparam T type of data held by the characteristic. |
mbed_official | 14:de95c96e3305 | 476 | */ |
mbed_official | 14:de95c96e3305 | 477 | template<typename T> |
mbed_official | 14:de95c96e3305 | 478 | class ReadWriteNotifyIndicateCharacteristic : public GattCharacteristic { |
mbed_official | 14:de95c96e3305 | 479 | public: |
mbed_official | 14:de95c96e3305 | 480 | /** |
mbed_official | 14:de95c96e3305 | 481 | * Construct a characteristic that can be read or written and emit |
mbed_official | 14:de95c96e3305 | 482 | * notification or indication. |
mbed_official | 14:de95c96e3305 | 483 | * |
mbed_official | 14:de95c96e3305 | 484 | * @param[in] uuid The UUID of the characteristic. |
mbed_official | 14:de95c96e3305 | 485 | * @param[in] initial_value Initial value contained by the characteristic. |
mbed_official | 14:de95c96e3305 | 486 | */ |
mbed_official | 14:de95c96e3305 | 487 | ReadWriteNotifyIndicateCharacteristic(const UUID & uuid, const T& initial_value) : |
mbed_official | 14:de95c96e3305 | 488 | GattCharacteristic( |
mbed_official | 14:de95c96e3305 | 489 | /* UUID */ uuid, |
pancotinho | 22:2c6161c96a76 | 490 | /* Initial value */ (uint8_t *) &_value, |
mbed_official | 14:de95c96e3305 | 491 | /* Value size */ sizeof(_value), |
mbed_official | 14:de95c96e3305 | 492 | /* Value capacity */ sizeof(_value), |
mbed_official | 14:de95c96e3305 | 493 | /* Properties */ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
mbed_official | 14:de95c96e3305 | 494 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
mbed_official | 14:de95c96e3305 | 495 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | |
mbed_official | 14:de95c96e3305 | 496 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE, |
mbed_official | 14:de95c96e3305 | 497 | /* Descriptors */ NULL, |
mbed_official | 14:de95c96e3305 | 498 | /* Num descriptors */ 0, |
mbed_official | 14:de95c96e3305 | 499 | /* variable len */ false |
mbed_official | 14:de95c96e3305 | 500 | ), |
mbed_official | 14:de95c96e3305 | 501 | _value(initial_value) { |
mbed_official | 14:de95c96e3305 | 502 | } |
mbed_official | 14:de95c96e3305 | 503 | |
mbed_official | 14:de95c96e3305 | 504 | /** |
pancotinho | 20:86417f956e22 | 505 | * Get the value of this characteristic. |
mbed_official | 14:de95c96e3305 | 506 | * |
mbed_official | 14:de95c96e3305 | 507 | * @param[in] server GattServer instance that contain the characteristic |
mbed_official | 14:de95c96e3305 | 508 | * value. |
mbed_official | 14:de95c96e3305 | 509 | * @param[in] dst Variable that will receive the characteristic value. |
mbed_official | 14:de95c96e3305 | 510 | * |
mbed_official | 14:de95c96e3305 | 511 | * @return BLE_ERROR_NONE in case of success or an appropriate error code. |
mbed_official | 14:de95c96e3305 | 512 | */ |
mbed_official | 14:de95c96e3305 | 513 | ble_error_t get(GattServer &server, T& dst) const |
mbed_official | 14:de95c96e3305 | 514 | { |
mbed_official | 14:de95c96e3305 | 515 | uint16_t value_length = sizeof(dst); |
pancotinho | 22:2c6161c96a76 | 516 | return server.read(getValueHandle(), (uint8_t *) &dst, &value_length); |
mbed_official | 14:de95c96e3305 | 517 | } |
mbed_official | 14:de95c96e3305 | 518 | |
mbed_official | 14:de95c96e3305 | 519 | /** |
mbed_official | 14:de95c96e3305 | 520 | * Assign a new value to this characteristic. |
mbed_official | 14:de95c96e3305 | 521 | * |
mbed_official | 14:de95c96e3305 | 522 | * @param[in] server GattServer instance that will receive the new value. |
mbed_official | 14:de95c96e3305 | 523 | * @param[in] value The new value to set. |
mbed_official | 14:de95c96e3305 | 524 | * @param[in] local_only Flag that determine if the change should be kept |
mbed_official | 14:de95c96e3305 | 525 | * locally or forwarded to subscribed clients. |
mbed_official | 14:de95c96e3305 | 526 | */ |
pancotinho | 20:86417f956e22 | 527 | ble_error_t set( |
pancotinho | 22:2c6161c96a76 | 528 | GattServer &server, const uint8_t &value, bool local_only = false |
mbed_official | 14:de95c96e3305 | 529 | ) const { |
pancotinho | 22:2c6161c96a76 | 530 | return server.write(getValueHandle(), (uint8_t *) &value, sizeof(value), local_only); |
pancotinho | 22:2c6161c96a76 | 531 | } |
pancotinho | 22:2c6161c96a76 | 532 | |
pancotinho | 22:2c6161c96a76 | 533 | private: |
pancotinho | 22:2c6161c96a76 | 534 | uint32_t _value; |
pancotinho | 22:2c6161c96a76 | 535 | }; |
pancotinho | 22:2c6161c96a76 | 536 | |
pancotinho | 22:2c6161c96a76 | 537 | template<typename T> |
pancotinho | 22:2c6161c96a76 | 538 | class ReadWriteCharacteristic : public GattCharacteristic { |
pancotinho | 22:2c6161c96a76 | 539 | public: |
pancotinho | 22:2c6161c96a76 | 540 | /** |
pancotinho | 22:2c6161c96a76 | 541 | * Construct a characteristic that can be read or written and emit |
pancotinho | 22:2c6161c96a76 | 542 | * notification or indication. |
pancotinho | 22:2c6161c96a76 | 543 | * |
pancotinho | 22:2c6161c96a76 | 544 | * @param[in] uuid The UUID of the characteristic. |
pancotinho | 22:2c6161c96a76 | 545 | * @param[in] initial_value Initial value contained by the characteristic. |
pancotinho | 22:2c6161c96a76 | 546 | */ |
pancotinho | 22:2c6161c96a76 | 547 | ReadWriteCharacteristic(const UUID & uuid, const T& initial_value) : |
pancotinho | 22:2c6161c96a76 | 548 | GattCharacteristic( |
pancotinho | 22:2c6161c96a76 | 549 | /* UUID */ uuid, |
pancotinho | 22:2c6161c96a76 | 550 | /* Initial value */ (uint8_t *) &_value, |
pancotinho | 22:2c6161c96a76 | 551 | /* Value size */ sizeof(_value), |
pancotinho | 22:2c6161c96a76 | 552 | /* Value capacity */ sizeof(_value), |
pancotinho | 22:2c6161c96a76 | 553 | /* Properties */ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
pancotinho | 22:2c6161c96a76 | 554 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE , |
pancotinho | 22:2c6161c96a76 | 555 | /* Descriptors */ NULL, |
pancotinho | 22:2c6161c96a76 | 556 | /* Num descriptors */ 0, |
pancotinho | 22:2c6161c96a76 | 557 | /* variable len */ false |
pancotinho | 22:2c6161c96a76 | 558 | ), |
pancotinho | 22:2c6161c96a76 | 559 | _value(initial_value) { |
pancotinho | 22:2c6161c96a76 | 560 | } |
pancotinho | 22:2c6161c96a76 | 561 | |
pancotinho | 22:2c6161c96a76 | 562 | /** |
pancotinho | 22:2c6161c96a76 | 563 | * Get the value of this characteristic. |
pancotinho | 22:2c6161c96a76 | 564 | * |
pancotinho | 22:2c6161c96a76 | 565 | * @param[in] server GattServer instance that contain the characteristic |
pancotinho | 22:2c6161c96a76 | 566 | * value. |
pancotinho | 22:2c6161c96a76 | 567 | * @param[in] dst Variable that will receive the characteristic value. |
pancotinho | 22:2c6161c96a76 | 568 | * |
pancotinho | 22:2c6161c96a76 | 569 | * @return BLE_ERROR_NONE in case of success or an appropriate error code. |
pancotinho | 22:2c6161c96a76 | 570 | */ |
pancotinho | 22:2c6161c96a76 | 571 | ble_error_t get(GattServer &server, T& dst) const |
pancotinho | 22:2c6161c96a76 | 572 | { |
pancotinho | 22:2c6161c96a76 | 573 | uint16_t value_length = sizeof(dst); |
pancotinho | 22:2c6161c96a76 | 574 | return server.read(getValueHandle(), (uint8_t *) &dst, &value_length); |
pancotinho | 22:2c6161c96a76 | 575 | } |
pancotinho | 22:2c6161c96a76 | 576 | |
pancotinho | 22:2c6161c96a76 | 577 | /** |
pancotinho | 22:2c6161c96a76 | 578 | * Assign a new value to this characteristic. |
pancotinho | 22:2c6161c96a76 | 579 | * |
pancotinho | 22:2c6161c96a76 | 580 | * @param[in] server GattServer instance that will receive the new value. |
pancotinho | 22:2c6161c96a76 | 581 | * @param[in] value The new value to set. |
pancotinho | 22:2c6161c96a76 | 582 | * @param[in] local_only Flag that determine if the change should be kept |
pancotinho | 22:2c6161c96a76 | 583 | * locally or forwarded to subscribed clients. |
pancotinho | 22:2c6161c96a76 | 584 | */ |
pancotinho | 22:2c6161c96a76 | 585 | ble_error_t set( |
pancotinho | 22:2c6161c96a76 | 586 | GattServer &server, const uint8_t &value, bool local_only = false |
pancotinho | 22:2c6161c96a76 | 587 | ) const { |
pancotinho | 22:2c6161c96a76 | 588 | return server.write(getValueHandle(), (uint8_t *) &value, sizeof(value), local_only); |
mbed_official | 14:de95c96e3305 | 589 | } |
mbed_official | 14:de95c96e3305 | 590 | |
mbed_official | 14:de95c96e3305 | 591 | private: |
mbed_official | 14:de95c96e3305 | 592 | uint8_t _value; |
mbed_official | 14:de95c96e3305 | 593 | }; |
mbed_official | 14:de95c96e3305 | 594 | |
pancotinho | 22:2c6161c96a76 | 595 | ReadWriteNotifyIndicateCharacteristic<uint32_t> _amb_char; |
pancotinho | 22:2c6161c96a76 | 596 | ReadWriteCharacteristic<uint8_t> _led_char; |
pancotinho | 22:2c6161c96a76 | 597 | ReadWriteNotifyIndicateCharacteristic<uint32_t> _lux_char; |
pancotinho | 22:2c6161c96a76 | 598 | ReadWriteNotifyIndicateCharacteristic<uint32_t> _imux_char; |
pancotinho | 22:2c6161c96a76 | 599 | ReadWriteNotifyIndicateCharacteristic<uint32_t> _imuy_char; |
pancotinho | 22:2c6161c96a76 | 600 | // ReadWriteNotifyIndicateCharacteristic<uint32_t> _imuz_char; |
pancotinho | 20:86417f956e22 | 601 | // list of the characteristics of the My service |
pancotinho | 22:2c6161c96a76 | 602 | GattCharacteristic* _My_characteristics[5]; |
mbed_official | 14:de95c96e3305 | 603 | |
mbed_official | 14:de95c96e3305 | 604 | // demo service |
pancotinho | 20:86417f956e22 | 605 | GattService _My_service; |
mbed_official | 14:de95c96e3305 | 606 | |
mbed_official | 14:de95c96e3305 | 607 | GattServer* _server; |
mbed_official | 14:de95c96e3305 | 608 | events::EventQueue *_event_queue; |
pancotinho | 20:86417f956e22 | 609 | |
pancotinho | 20:86417f956e22 | 610 | DigitalOut _alive_led; |
pancotinho | 20:86417f956e22 | 611 | DigitalOut _actuated_led; |
mbed_official | 14:de95c96e3305 | 612 | }; |
mbed_official | 14:de95c96e3305 | 613 | |
mbed_official | 14:de95c96e3305 | 614 | int main() { |
pancotinho | 22:2c6161c96a76 | 615 | |
pancotinho | 22:2c6161c96a76 | 616 | |
pancotinho | 22:2c6161c96a76 | 617 | printf("ambient sensor initialized"); |
pancotinho | 22:2c6161c96a76 | 618 | wait(1) ; |
mbed_official | 14:de95c96e3305 | 619 | BLE &ble_interface = BLE::Instance(); |
mbed_official | 14:de95c96e3305 | 620 | events::EventQueue event_queue; |
pancotinho | 20:86417f956e22 | 621 | MyService demo_service; |
mbed_official | 14:de95c96e3305 | 622 | BLEProcess ble_process(event_queue, ble_interface); |
pancotinho | 20:86417f956e22 | 623 | |
pancotinho | 22:2c6161c96a76 | 624 | i2c.frequency(20000); |
pancotinho | 22:2c6161c96a76 | 625 | |
pancotinho | 22:2c6161c96a76 | 626 | /*Reset BMI160*/ |
pancotinho | 22:2c6161c96a76 | 627 | i2c_reg_buffer[0] = 0x7E; |
pancotinho | 22:2c6161c96a76 | 628 | i2c_reg_buffer[1] = 0xB6; |
pancotinho | 22:2c6161c96a76 | 629 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
pancotinho | 22:2c6161c96a76 | 630 | wait_ms(200); |
pancotinho | 22:2c6161c96a76 | 631 | printf("BMI160 Resetado\n\r"); |
pancotinho | 22:2c6161c96a76 | 632 | |
pancotinho | 22:2c6161c96a76 | 633 | /*Habilita o Acelerometro*/ |
pancotinho | 22:2c6161c96a76 | 634 | i2c_reg_buffer[0] = 0x7E; |
pancotinho | 22:2c6161c96a76 | 635 | i2c_reg_buffer[1] = 0x11; //PMU Normal |
pancotinho | 22:2c6161c96a76 | 636 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
pancotinho | 22:2c6161c96a76 | 637 | printf("Acc Habilitado\n\r"); |
pancotinho | 22:2c6161c96a76 | 638 | |
pancotinho | 22:2c6161c96a76 | 639 | /*Habilita o Giroscopio*/ |
pancotinho | 22:2c6161c96a76 | 640 | i2c_reg_buffer[0] = 0x7E; |
pancotinho | 22:2c6161c96a76 | 641 | i2c_reg_buffer[1] = 0x15; //PMU Normal |
pancotinho | 22:2c6161c96a76 | 642 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
pancotinho | 22:2c6161c96a76 | 643 | printf("Gyr Habilitado\n\r"); |
pancotinho | 22:2c6161c96a76 | 644 | |
pancotinho | 22:2c6161c96a76 | 645 | /*Config o Data Rate ACC em 1600Hz*/ |
pancotinho | 22:2c6161c96a76 | 646 | i2c_reg_buffer[0] = 0x40; |
pancotinho | 22:2c6161c96a76 | 647 | i2c_reg_buffer[1] = 0x2C; |
pancotinho | 22:2c6161c96a76 | 648 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
pancotinho | 22:2c6161c96a76 | 649 | printf("Data Rate ACC Selecionado a 1600Hz\n\r"); |
pancotinho | 22:2c6161c96a76 | 650 | |
pancotinho | 22:2c6161c96a76 | 651 | /*Config o Data Rate GYR em 1600Hz*/ |
pancotinho | 22:2c6161c96a76 | 652 | i2c_reg_buffer[0] = 0x42; |
pancotinho | 22:2c6161c96a76 | 653 | i2c_reg_buffer[1] = 0x2C; |
pancotinho | 22:2c6161c96a76 | 654 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
pancotinho | 22:2c6161c96a76 | 655 | printf("Data Rate GYR Selecionado a 1600Hz\n\r"); |
pancotinho | 22:2c6161c96a76 | 656 | |
pancotinho | 22:2c6161c96a76 | 657 | /*Config o Range GYR em 250º/s*/ |
pancotinho | 22:2c6161c96a76 | 658 | i2c_reg_buffer[0] = 0x43; |
pancotinho | 22:2c6161c96a76 | 659 | i2c_reg_buffer[1] = 0x03; |
pancotinho | 22:2c6161c96a76 | 660 | i2c.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false); |
pancotinho | 22:2c6161c96a76 | 661 | printf("Range GYR Selecionado a 250deg/s\n\r"); |
pancotinho | 22:2c6161c96a76 | 662 | |
pancotinho | 22:2c6161c96a76 | 663 | wait(0.1); |
pancotinho | 22:2c6161c96a76 | 664 | |
pancotinho | 22:2c6161c96a76 | 665 | printf("BMI160 Configurado\n\r"); |
mbed_official | 14:de95c96e3305 | 666 | |
pancotinho | 20:86417f956e22 | 667 | ble_process.on_init(callback(&demo_service, &MyService::start)); |
pancotinho | 20:86417f956e22 | 668 | |
mbed_official | 14:de95c96e3305 | 669 | |
mbed_official | 14:de95c96e3305 | 670 | // bind the event queue to the ble interface, initialize the interface |
mbed_official | 14:de95c96e3305 | 671 | // and start advertising |
mbed_official | 14:de95c96e3305 | 672 | ble_process.start(); |
mbed_official | 14:de95c96e3305 | 673 | |
mbed_official | 14:de95c96e3305 | 674 | // Process the event queue. |
mbed_official | 14:de95c96e3305 | 675 | event_queue.dispatch_forever(); |
mbed_official | 14:de95c96e3305 | 676 | |
mbed_official | 14:de95c96e3305 | 677 | return 0; |
mbed_official | 14:de95c96e3305 | 678 | } |