GPS to Pulga
source/main.cpp@20:86417f956e22, 2019-07-10 (annotated)
- Committer:
- pancotinho
- Date:
- Wed Jul 10 17:50:57 2019 +0000
- Revision:
- 20:86417f956e22
- Parent:
- 14:de95c96e3305
- Child:
- 21:6cf50085f9f3
Frst commit
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 | 20:86417f956e22 | 33 | |
mbed_official | 14:de95c96e3305 | 34 | |
mbed_official | 14:de95c96e3305 | 35 | using mbed::callback; |
pancotinho | 20:86417f956e22 | 36 | Si1133 sensor_light(P0_13, P0_15); |
pancotinho | 20:86417f956e22 | 37 | |
mbed_official | 14:de95c96e3305 | 38 | |
mbed_official | 14:de95c96e3305 | 39 | /** |
pancotinho | 20:86417f956e22 | 40 | * A My service that demonstrate the GattServer features. |
mbed_official | 14:de95c96e3305 | 41 | * |
pancotinho | 20:86417f956e22 | 42 | * The My service host three characteristics that model the current hour, |
pancotinho | 20:86417f956e22 | 43 | * led and count of the My. The value of the count characteristic is |
mbed_official | 14:de95c96e3305 | 44 | * incremented automatically by the system. |
mbed_official | 14:de95c96e3305 | 45 | * |
pancotinho | 20:86417f956e22 | 46 | * A client can subscribe to updates of the My characteristics and get |
mbed_official | 14:de95c96e3305 | 47 | * notified when one of the value is changed. Clients can also change value of |
pancotinho | 20:86417f956e22 | 48 | * the count, led and hour characteristric. |
mbed_official | 14:de95c96e3305 | 49 | */ |
pancotinho | 20:86417f956e22 | 50 | class MyService { |
pancotinho | 20:86417f956e22 | 51 | typedef MyService Self; |
mbed_official | 14:de95c96e3305 | 52 | |
mbed_official | 14:de95c96e3305 | 53 | public: |
pancotinho | 20:86417f956e22 | 54 | MyService() : |
pancotinho | 20:86417f956e22 | 55 | // _hour_char("485f4145-52b9-4644-af1f-7a6b9322490f", 0), |
pancotinho | 20:86417f956e22 | 56 | _led_char("0a924ca7-87cd-4699-a3bd-abdcd9cf126a", 0), |
pancotinho | 20:86417f956e22 | 57 | _count_char("8dd6a1b7-bc75-4741-8a26-264af75807de", 0), |
pancotinho | 20:86417f956e22 | 58 | _My_service( |
mbed_official | 14:de95c96e3305 | 59 | /* uuid */ "51311102-030e-485f-b122-f8f381aa84ed", |
pancotinho | 20:86417f956e22 | 60 | /* characteristics */ _My_characteristics, |
pancotinho | 20:86417f956e22 | 61 | /* numCharacteristics */ sizeof(_My_characteristics) / |
pancotinho | 20:86417f956e22 | 62 | sizeof(_My_characteristics[0]) |
mbed_official | 14:de95c96e3305 | 63 | ), |
mbed_official | 14:de95c96e3305 | 64 | _server(NULL), |
pancotinho | 20:86417f956e22 | 65 | _event_queue(NULL), |
pancotinho | 20:86417f956e22 | 66 | _alive_led(P1_13, 1), |
pancotinho | 20:86417f956e22 | 67 | _actuated_led(P1_14,0) |
pancotinho | 20:86417f956e22 | 68 | |
mbed_official | 14:de95c96e3305 | 69 | { |
mbed_official | 14:de95c96e3305 | 70 | // update internal pointers (value, descriptors and characteristics array) |
pancotinho | 20:86417f956e22 | 71 | // _My_characteristics[0] = &_hour_char; |
pancotinho | 20:86417f956e22 | 72 | _My_characteristics[1] = &_led_char; |
pancotinho | 20:86417f956e22 | 73 | _My_characteristics[0] = &_count_char; |
mbed_official | 14:de95c96e3305 | 74 | |
mbed_official | 14:de95c96e3305 | 75 | // setup authorization handlers |
pancotinho | 20:86417f956e22 | 76 | //_hour_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 20:86417f956e22 | 77 | _led_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
pancotinho | 20:86417f956e22 | 78 | _count_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); |
mbed_official | 14:de95c96e3305 | 79 | } |
mbed_official | 14:de95c96e3305 | 80 | |
mbed_official | 14:de95c96e3305 | 81 | |
mbed_official | 14:de95c96e3305 | 82 | |
mbed_official | 14:de95c96e3305 | 83 | void start(BLE &ble_interface, events::EventQueue &event_queue) |
mbed_official | 14:de95c96e3305 | 84 | { |
mbed_official | 14:de95c96e3305 | 85 | if (_event_queue) { |
mbed_official | 14:de95c96e3305 | 86 | return; |
mbed_official | 14:de95c96e3305 | 87 | } |
mbed_official | 14:de95c96e3305 | 88 | |
mbed_official | 14:de95c96e3305 | 89 | _server = &ble_interface.gattServer(); |
mbed_official | 14:de95c96e3305 | 90 | _event_queue = &event_queue; |
mbed_official | 14:de95c96e3305 | 91 | |
pancotinho | 20:86417f956e22 | 92 | |
mbed_official | 14:de95c96e3305 | 93 | // register the service |
mbed_official | 14:de95c96e3305 | 94 | printf("Adding demo service\r\n"); |
pancotinho | 20:86417f956e22 | 95 | ble_error_t err = _server->addService(_My_service); |
mbed_official | 14:de95c96e3305 | 96 | |
mbed_official | 14:de95c96e3305 | 97 | if (err) { |
mbed_official | 14:de95c96e3305 | 98 | printf("Error %u during demo service registration.\r\n", err); |
mbed_official | 14:de95c96e3305 | 99 | return; |
mbed_official | 14:de95c96e3305 | 100 | } |
mbed_official | 14:de95c96e3305 | 101 | |
mbed_official | 14:de95c96e3305 | 102 | // read write handler |
mbed_official | 14:de95c96e3305 | 103 | _server->onDataSent(as_cb(&Self::when_data_sent)); |
mbed_official | 14:de95c96e3305 | 104 | _server->onDataWritten(as_cb(&Self::when_data_written)); |
mbed_official | 14:de95c96e3305 | 105 | _server->onDataRead(as_cb(&Self::when_data_read)); |
mbed_official | 14:de95c96e3305 | 106 | |
mbed_official | 14:de95c96e3305 | 107 | // updates subscribtion handlers |
mbed_official | 14:de95c96e3305 | 108 | _server->onUpdatesEnabled(as_cb(&Self::when_update_enabled)); |
mbed_official | 14:de95c96e3305 | 109 | _server->onUpdatesDisabled(as_cb(&Self::when_update_disabled)); |
mbed_official | 14:de95c96e3305 | 110 | _server->onConfirmationReceived(as_cb(&Self::when_confirmation_received)); |
mbed_official | 14:de95c96e3305 | 111 | |
mbed_official | 14:de95c96e3305 | 112 | // print the handles |
pancotinho | 20:86417f956e22 | 113 | printf("My service registered\r\n"); |
pancotinho | 20:86417f956e22 | 114 | printf("service handle: %u\r\n", _My_service.getHandle()); |
pancotinho | 20:86417f956e22 | 115 | //printf("\thour characteristic value handle %u\r\n", _hour_char.getValueHandle()); |
pancotinho | 20:86417f956e22 | 116 | // printf("\tled characteristic value handle %u\r\n", _led_char.getValueHandle()); |
pancotinho | 20:86417f956e22 | 117 | printf("\tcount characteristic value handle %u\r\n", _count_char.getValueHandle()); |
mbed_official | 14:de95c96e3305 | 118 | |
pancotinho | 20:86417f956e22 | 119 | _event_queue->call_every(1000 /* ms */, callback(this, &Self::increment_count)); |
pancotinho | 20:86417f956e22 | 120 | _event_queue->call_every(500 /* ms */, callback(this, &Self::blink)); |
pancotinho | 20:86417f956e22 | 121 | |
mbed_official | 14:de95c96e3305 | 122 | } |
mbed_official | 14:de95c96e3305 | 123 | |
mbed_official | 14:de95c96e3305 | 124 | private: |
mbed_official | 14:de95c96e3305 | 125 | |
mbed_official | 14:de95c96e3305 | 126 | /** |
mbed_official | 14:de95c96e3305 | 127 | * Handler called when a notification or an indication has been sent. |
mbed_official | 14:de95c96e3305 | 128 | */ |
mbed_official | 14:de95c96e3305 | 129 | void when_data_sent(unsigned count) |
mbed_official | 14:de95c96e3305 | 130 | { |
mbed_official | 14:de95c96e3305 | 131 | printf("sent %u updates\r\n", count); |
mbed_official | 14:de95c96e3305 | 132 | } |
mbed_official | 14:de95c96e3305 | 133 | |
mbed_official | 14:de95c96e3305 | 134 | /** |
mbed_official | 14:de95c96e3305 | 135 | * Handler called after an attribute has been written. |
mbed_official | 14:de95c96e3305 | 136 | */ |
mbed_official | 14:de95c96e3305 | 137 | void when_data_written(const GattWriteCallbackParams *e) |
mbed_official | 14:de95c96e3305 | 138 | { |
mbed_official | 14:de95c96e3305 | 139 | printf("data written:\r\n"); |
mbed_official | 14:de95c96e3305 | 140 | printf("\tconnection handle: %u\r\n", e->connHandle); |
mbed_official | 14:de95c96e3305 | 141 | printf("\tattribute handle: %u", e->handle); |
pancotinho | 20:86417f956e22 | 142 | //if (e->handle == _hour_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 143 | // printf(" (hour characteristic)\r\n"); |
pancotinho | 20:86417f956e22 | 144 | // } else |
pancotinho | 20:86417f956e22 | 145 | if (e->handle == _led_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 146 | printf(" (led characteristic)\r\n"); |
pancotinho | 20:86417f956e22 | 147 | _actuated_led = *(e->data); |
pancotinho | 20:86417f956e22 | 148 | } else if (e->handle == _count_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 149 | printf(" (count characteristic)\r\n"); |
mbed_official | 14:de95c96e3305 | 150 | } else { |
mbed_official | 14:de95c96e3305 | 151 | printf("\r\n"); |
mbed_official | 14:de95c96e3305 | 152 | } |
mbed_official | 14:de95c96e3305 | 153 | printf("\twrite operation: %u\r\n", e->writeOp); |
mbed_official | 14:de95c96e3305 | 154 | printf("\toffset: %u\r\n", e->offset); |
mbed_official | 14:de95c96e3305 | 155 | printf("\tlength: %u\r\n", e->len); |
mbed_official | 14:de95c96e3305 | 156 | printf("\t data: "); |
mbed_official | 14:de95c96e3305 | 157 | |
mbed_official | 14:de95c96e3305 | 158 | for (size_t i = 0; i < e->len; ++i) { |
mbed_official | 14:de95c96e3305 | 159 | printf("%02X", e->data[i]); |
mbed_official | 14:de95c96e3305 | 160 | } |
mbed_official | 14:de95c96e3305 | 161 | |
mbed_official | 14:de95c96e3305 | 162 | printf("\r\n"); |
mbed_official | 14:de95c96e3305 | 163 | } |
mbed_official | 14:de95c96e3305 | 164 | |
mbed_official | 14:de95c96e3305 | 165 | /** |
mbed_official | 14:de95c96e3305 | 166 | * Handler called after an attribute has been read. |
mbed_official | 14:de95c96e3305 | 167 | */ |
mbed_official | 14:de95c96e3305 | 168 | void when_data_read(const GattReadCallbackParams *e) |
mbed_official | 14:de95c96e3305 | 169 | { |
mbed_official | 14:de95c96e3305 | 170 | printf("data read:\r\n"); |
mbed_official | 14:de95c96e3305 | 171 | printf("\tconnection handle: %u\r\n", e->connHandle); |
mbed_official | 14:de95c96e3305 | 172 | printf("\tattribute handle: %u", e->handle); |
pancotinho | 20:86417f956e22 | 173 | // if (e->handle == _hour_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 174 | // printf(" (hour characteristic)\r\n"); |
pancotinho | 20:86417f956e22 | 175 | // } else |
pancotinho | 20:86417f956e22 | 176 | if (e->handle == _led_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 177 | printf(" (led characteristic)\r\n"); |
pancotinho | 20:86417f956e22 | 178 | } else if (e->handle == _count_char.getValueHandle()) { |
pancotinho | 20:86417f956e22 | 179 | printf(" (count characteristic)\r\n"); |
mbed_official | 14:de95c96e3305 | 180 | } else { |
mbed_official | 14:de95c96e3305 | 181 | printf("\r\n"); |
mbed_official | 14:de95c96e3305 | 182 | } |
mbed_official | 14:de95c96e3305 | 183 | } |
mbed_official | 14:de95c96e3305 | 184 | |
mbed_official | 14:de95c96e3305 | 185 | /** |
mbed_official | 14:de95c96e3305 | 186 | * Handler called after a client has subscribed to notification or indication. |
mbed_official | 14:de95c96e3305 | 187 | * |
mbed_official | 14:de95c96e3305 | 188 | * @param handle Handle of the characteristic value affected by the change. |
mbed_official | 14:de95c96e3305 | 189 | */ |
mbed_official | 14:de95c96e3305 | 190 | void when_update_enabled(GattAttribute::Handle_t handle) |
mbed_official | 14:de95c96e3305 | 191 | { |
mbed_official | 14:de95c96e3305 | 192 | printf("update enabled on handle %d\r\n", handle); |
mbed_official | 14:de95c96e3305 | 193 | } |
mbed_official | 14:de95c96e3305 | 194 | |
mbed_official | 14:de95c96e3305 | 195 | /** |
mbed_official | 14:de95c96e3305 | 196 | * Handler called after a client has cancelled his subscription from |
mbed_official | 14:de95c96e3305 | 197 | * notification or indication. |
mbed_official | 14:de95c96e3305 | 198 | * |
mbed_official | 14:de95c96e3305 | 199 | * @param handle Handle of the characteristic value affected by the change. |
mbed_official | 14:de95c96e3305 | 200 | */ |
mbed_official | 14:de95c96e3305 | 201 | void when_update_disabled(GattAttribute::Handle_t handle) |
mbed_official | 14:de95c96e3305 | 202 | { |
mbed_official | 14:de95c96e3305 | 203 | printf("update disabled on handle %d\r\n", handle); |
mbed_official | 14:de95c96e3305 | 204 | } |
mbed_official | 14:de95c96e3305 | 205 | |
mbed_official | 14:de95c96e3305 | 206 | /** |
mbed_official | 14:de95c96e3305 | 207 | * Handler called when an indication confirmation has been received. |
mbed_official | 14:de95c96e3305 | 208 | * |
mbed_official | 14:de95c96e3305 | 209 | * @param handle Handle of the characteristic value that has emitted the |
mbed_official | 14:de95c96e3305 | 210 | * indication. |
mbed_official | 14:de95c96e3305 | 211 | */ |
mbed_official | 14:de95c96e3305 | 212 | void when_confirmation_received(GattAttribute::Handle_t handle) |
mbed_official | 14:de95c96e3305 | 213 | { |
mbed_official | 14:de95c96e3305 | 214 | printf("confirmation received on handle %d\r\n", handle); |
mbed_official | 14:de95c96e3305 | 215 | } |
mbed_official | 14:de95c96e3305 | 216 | |
mbed_official | 14:de95c96e3305 | 217 | /** |
mbed_official | 14:de95c96e3305 | 218 | * Handler called when a write request is received. |
mbed_official | 14:de95c96e3305 | 219 | * |
mbed_official | 14:de95c96e3305 | 220 | * This handler verify that the value submitted by the client is valid before |
mbed_official | 14:de95c96e3305 | 221 | * authorizing the operation. |
mbed_official | 14:de95c96e3305 | 222 | */ |
mbed_official | 14:de95c96e3305 | 223 | void authorize_client_write(GattWriteAuthCallbackParams *e) |
mbed_official | 14:de95c96e3305 | 224 | { |
mbed_official | 14:de95c96e3305 | 225 | printf("characteristic %u write authorization\r\n", e->handle); |
mbed_official | 14:de95c96e3305 | 226 | |
mbed_official | 14:de95c96e3305 | 227 | if (e->offset != 0) { |
mbed_official | 14:de95c96e3305 | 228 | printf("Error invalid offset\r\n"); |
mbed_official | 14:de95c96e3305 | 229 | e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; |
mbed_official | 14:de95c96e3305 | 230 | return; |
mbed_official | 14:de95c96e3305 | 231 | } |
mbed_official | 14:de95c96e3305 | 232 | |
mbed_official | 14:de95c96e3305 | 233 | if (e->len != 1) { |
mbed_official | 14:de95c96e3305 | 234 | printf("Error invalid len\r\n"); |
mbed_official | 14:de95c96e3305 | 235 | e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; |
mbed_official | 14:de95c96e3305 | 236 | return; |
mbed_official | 14:de95c96e3305 | 237 | } |
mbed_official | 14:de95c96e3305 | 238 | |
pancotinho | 20:86417f956e22 | 239 | //if ((e->data[0] >= 60) || |
pancotinho | 20:86417f956e22 | 240 | // ((e->data[0] >= 24) && (e->handle == _hour_char.getValueHandle()))) { |
pancotinho | 20:86417f956e22 | 241 | // printf("Error invalid data\r\n"); |
pancotinho | 20:86417f956e22 | 242 | // e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED; |
pancotinho | 20:86417f956e22 | 243 | // return; |
pancotinho | 20:86417f956e22 | 244 | // } |
mbed_official | 14:de95c96e3305 | 245 | |
mbed_official | 14:de95c96e3305 | 246 | e->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
mbed_official | 14:de95c96e3305 | 247 | } |
mbed_official | 14:de95c96e3305 | 248 | |
mbed_official | 14:de95c96e3305 | 249 | /** |
pancotinho | 20:86417f956e22 | 250 | * Increment the count counter. |
mbed_official | 14:de95c96e3305 | 251 | */ |
pancotinho | 20:86417f956e22 | 252 | void increment_count(void) |
mbed_official | 14:de95c96e3305 | 253 | { |
pancotinho | 20:86417f956e22 | 254 | uint8_t count = 0; |
pancotinho | 20:86417f956e22 | 255 | |
pancotinho | 20:86417f956e22 | 256 | |
pancotinho | 20:86417f956e22 | 257 | ble_error_t err = _count_char.get(*_server, count); |
mbed_official | 14:de95c96e3305 | 258 | if (err) { |
pancotinho | 20:86417f956e22 | 259 | printf("read of the count value returned error %u\r\n", err); |
pancotinho | 20:86417f956e22 | 260 | return; |
pancotinho | 20:86417f956e22 | 261 | } |
pancotinho | 20:86417f956e22 | 262 | //count ++; |
pancotinho | 20:86417f956e22 | 263 | |
pancotinho | 20:86417f956e22 | 264 | |
pancotinho | 20:86417f956e22 | 265 | if (sensor_light.open()) { |
pancotinho | 20:86417f956e22 | 266 | printf("Device detected!\n"); |
pancotinho | 20:86417f956e22 | 267 | //Print the current light level |
pancotinho | 20:86417f956e22 | 268 | printf("Lux = %.3f\n", (float)sensor_light.get_light_level()); |
pancotinho | 20:86417f956e22 | 269 | //Print the current UV index |
pancotinho | 20:86417f956e22 | 270 | printf("UV index = %.3f\n", (float)sensor_light.get_uv_index()); |
pancotinho | 20:86417f956e22 | 271 | //Sleep for 0.5 seconds |
pancotinho | 20:86417f956e22 | 272 | count= static_cast<uint8_t>(sensor_light.get_light_level()); |
pancotinho | 20:86417f956e22 | 273 | |
pancotinho | 20:86417f956e22 | 274 | _actuated_led = !_actuated_led; |
pancotinho | 20:86417f956e22 | 275 | } |
pancotinho | 20:86417f956e22 | 276 | |
pancotinho | 20:86417f956e22 | 277 | err = _count_char.set(*_server, count); |
pancotinho | 20:86417f956e22 | 278 | if (err) { |
pancotinho | 20:86417f956e22 | 279 | printf("write of the count value returned error %u\r\n", err); |
mbed_official | 14:de95c96e3305 | 280 | return; |
mbed_official | 14:de95c96e3305 | 281 | } |
mbed_official | 14:de95c96e3305 | 282 | |
pancotinho | 20:86417f956e22 | 283 | } |
pancotinho | 20:86417f956e22 | 284 | |
pancotinho | 20:86417f956e22 | 285 | void blink() { |
pancotinho | 20:86417f956e22 | 286 | _alive_led = !_alive_led; |
mbed_official | 14:de95c96e3305 | 287 | } |
mbed_official | 14:de95c96e3305 | 288 | |
mbed_official | 14:de95c96e3305 | 289 | /** |
pancotinho | 20:86417f956e22 | 290 | * Change led status. |
mbed_official | 14:de95c96e3305 | 291 | */ |
pancotinho | 20:86417f956e22 | 292 | void change_led(void) |
mbed_official | 14:de95c96e3305 | 293 | { |
pancotinho | 20:86417f956e22 | 294 | uint8_t led = 0; |
pancotinho | 20:86417f956e22 | 295 | ble_error_t err = _led_char.get(*_server, led); |
mbed_official | 14:de95c96e3305 | 296 | if (err) { |
pancotinho | 20:86417f956e22 | 297 | printf("read of the led value returned error %u\r\n", err); |
mbed_official | 14:de95c96e3305 | 298 | return; |
mbed_official | 14:de95c96e3305 | 299 | } |
mbed_official | 14:de95c96e3305 | 300 | } |
mbed_official | 14:de95c96e3305 | 301 | |
pancotinho | 20:86417f956e22 | 302 | /* * Increment the hour counter. |
pancotinho | 20:86417f956e22 | 303 | |
mbed_official | 14:de95c96e3305 | 304 | void increment_hour(void) |
mbed_official | 14:de95c96e3305 | 305 | { |
mbed_official | 14:de95c96e3305 | 306 | uint8_t hour = 0; |
mbed_official | 14:de95c96e3305 | 307 | ble_error_t err = _hour_char.get(*_server, hour); |
mbed_official | 14:de95c96e3305 | 308 | if (err) { |
mbed_official | 14:de95c96e3305 | 309 | printf("read of the hour value returned error %u\r\n", err); |
mbed_official | 14:de95c96e3305 | 310 | return; |
mbed_official | 14:de95c96e3305 | 311 | } |
mbed_official | 14:de95c96e3305 | 312 | |
pancotinho | 20:86417f956e22 | 313 | hour = (hour + 1) % 24; |
mbed_official | 14:de95c96e3305 | 314 | |
mbed_official | 14:de95c96e3305 | 315 | err = _hour_char.set(*_server, hour); |
mbed_official | 14:de95c96e3305 | 316 | if (err) { |
mbed_official | 14:de95c96e3305 | 317 | printf("write of the hour value returned error %u\r\n", err); |
mbed_official | 14:de95c96e3305 | 318 | return; |
mbed_official | 14:de95c96e3305 | 319 | } |
mbed_official | 14:de95c96e3305 | 320 | } |
pancotinho | 20:86417f956e22 | 321 | */ |
mbed_official | 14:de95c96e3305 | 322 | private: |
mbed_official | 14:de95c96e3305 | 323 | /** |
mbed_official | 14:de95c96e3305 | 324 | * Helper that construct an event handler from a member function of this |
mbed_official | 14:de95c96e3305 | 325 | * instance. |
mbed_official | 14:de95c96e3305 | 326 | */ |
mbed_official | 14:de95c96e3305 | 327 | template<typename Arg> |
mbed_official | 14:de95c96e3305 | 328 | FunctionPointerWithContext<Arg> as_cb(void (Self::*member)(Arg)) |
mbed_official | 14:de95c96e3305 | 329 | { |
mbed_official | 14:de95c96e3305 | 330 | return makeFunctionPointer(this, member); |
mbed_official | 14:de95c96e3305 | 331 | } |
mbed_official | 14:de95c96e3305 | 332 | |
mbed_official | 14:de95c96e3305 | 333 | /** |
mbed_official | 14:de95c96e3305 | 334 | * Read, Write, Notify, Indicate Characteristic declaration helper. |
mbed_official | 14:de95c96e3305 | 335 | * |
mbed_official | 14:de95c96e3305 | 336 | * @tparam T type of data held by the characteristic. |
mbed_official | 14:de95c96e3305 | 337 | */ |
mbed_official | 14:de95c96e3305 | 338 | template<typename T> |
mbed_official | 14:de95c96e3305 | 339 | class ReadWriteNotifyIndicateCharacteristic : public GattCharacteristic { |
mbed_official | 14:de95c96e3305 | 340 | public: |
mbed_official | 14:de95c96e3305 | 341 | /** |
mbed_official | 14:de95c96e3305 | 342 | * Construct a characteristic that can be read or written and emit |
mbed_official | 14:de95c96e3305 | 343 | * notification or indication. |
mbed_official | 14:de95c96e3305 | 344 | * |
mbed_official | 14:de95c96e3305 | 345 | * @param[in] uuid The UUID of the characteristic. |
mbed_official | 14:de95c96e3305 | 346 | * @param[in] initial_value Initial value contained by the characteristic. |
mbed_official | 14:de95c96e3305 | 347 | */ |
mbed_official | 14:de95c96e3305 | 348 | ReadWriteNotifyIndicateCharacteristic(const UUID & uuid, const T& initial_value) : |
mbed_official | 14:de95c96e3305 | 349 | GattCharacteristic( |
mbed_official | 14:de95c96e3305 | 350 | /* UUID */ uuid, |
mbed_official | 14:de95c96e3305 | 351 | /* Initial value */ &_value, |
mbed_official | 14:de95c96e3305 | 352 | /* Value size */ sizeof(_value), |
mbed_official | 14:de95c96e3305 | 353 | /* Value capacity */ sizeof(_value), |
mbed_official | 14:de95c96e3305 | 354 | /* Properties */ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
mbed_official | 14:de95c96e3305 | 355 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
mbed_official | 14:de95c96e3305 | 356 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | |
mbed_official | 14:de95c96e3305 | 357 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE, |
mbed_official | 14:de95c96e3305 | 358 | /* Descriptors */ NULL, |
mbed_official | 14:de95c96e3305 | 359 | /* Num descriptors */ 0, |
mbed_official | 14:de95c96e3305 | 360 | /* variable len */ false |
mbed_official | 14:de95c96e3305 | 361 | ), |
mbed_official | 14:de95c96e3305 | 362 | _value(initial_value) { |
mbed_official | 14:de95c96e3305 | 363 | } |
mbed_official | 14:de95c96e3305 | 364 | |
mbed_official | 14:de95c96e3305 | 365 | /** |
pancotinho | 20:86417f956e22 | 366 | * Get the value of this characteristic. |
mbed_official | 14:de95c96e3305 | 367 | * |
mbed_official | 14:de95c96e3305 | 368 | * @param[in] server GattServer instance that contain the characteristic |
mbed_official | 14:de95c96e3305 | 369 | * value. |
mbed_official | 14:de95c96e3305 | 370 | * @param[in] dst Variable that will receive the characteristic value. |
mbed_official | 14:de95c96e3305 | 371 | * |
mbed_official | 14:de95c96e3305 | 372 | * @return BLE_ERROR_NONE in case of success or an appropriate error code. |
mbed_official | 14:de95c96e3305 | 373 | */ |
mbed_official | 14:de95c96e3305 | 374 | ble_error_t get(GattServer &server, T& dst) const |
mbed_official | 14:de95c96e3305 | 375 | { |
mbed_official | 14:de95c96e3305 | 376 | uint16_t value_length = sizeof(dst); |
mbed_official | 14:de95c96e3305 | 377 | return server.read(getValueHandle(), &dst, &value_length); |
mbed_official | 14:de95c96e3305 | 378 | } |
mbed_official | 14:de95c96e3305 | 379 | |
mbed_official | 14:de95c96e3305 | 380 | /** |
mbed_official | 14:de95c96e3305 | 381 | * Assign a new value to this characteristic. |
mbed_official | 14:de95c96e3305 | 382 | * |
mbed_official | 14:de95c96e3305 | 383 | * @param[in] server GattServer instance that will receive the new value. |
mbed_official | 14:de95c96e3305 | 384 | * @param[in] value The new value to set. |
mbed_official | 14:de95c96e3305 | 385 | * @param[in] local_only Flag that determine if the change should be kept |
mbed_official | 14:de95c96e3305 | 386 | * locally or forwarded to subscribed clients. |
mbed_official | 14:de95c96e3305 | 387 | */ |
pancotinho | 20:86417f956e22 | 388 | ble_error_t set( |
pancotinho | 20:86417f956e22 | 389 | GattServer &server, const uint8_t &value, bool local_only = false |
mbed_official | 14:de95c96e3305 | 390 | ) const { |
mbed_official | 14:de95c96e3305 | 391 | return server.write(getValueHandle(), &value, sizeof(value), local_only); |
mbed_official | 14:de95c96e3305 | 392 | } |
mbed_official | 14:de95c96e3305 | 393 | |
mbed_official | 14:de95c96e3305 | 394 | private: |
mbed_official | 14:de95c96e3305 | 395 | uint8_t _value; |
mbed_official | 14:de95c96e3305 | 396 | }; |
mbed_official | 14:de95c96e3305 | 397 | |
pancotinho | 20:86417f956e22 | 398 | //ReadWriteNotifyIndicateCharacteristic<uint8_t> _hour_char; |
pancotinho | 20:86417f956e22 | 399 | ReadWriteNotifyIndicateCharacteristic<uint8_t> _led_char; |
pancotinho | 20:86417f956e22 | 400 | ReadWriteNotifyIndicateCharacteristic<uint8_t> _count_char; |
mbed_official | 14:de95c96e3305 | 401 | |
pancotinho | 20:86417f956e22 | 402 | // list of the characteristics of the My service |
pancotinho | 20:86417f956e22 | 403 | GattCharacteristic* _My_characteristics[2]; |
mbed_official | 14:de95c96e3305 | 404 | |
mbed_official | 14:de95c96e3305 | 405 | // demo service |
pancotinho | 20:86417f956e22 | 406 | GattService _My_service; |
mbed_official | 14:de95c96e3305 | 407 | |
mbed_official | 14:de95c96e3305 | 408 | GattServer* _server; |
mbed_official | 14:de95c96e3305 | 409 | events::EventQueue *_event_queue; |
pancotinho | 20:86417f956e22 | 410 | |
pancotinho | 20:86417f956e22 | 411 | DigitalOut _alive_led; |
pancotinho | 20:86417f956e22 | 412 | DigitalOut _actuated_led; |
mbed_official | 14:de95c96e3305 | 413 | }; |
mbed_official | 14:de95c96e3305 | 414 | |
mbed_official | 14:de95c96e3305 | 415 | int main() { |
mbed_official | 14:de95c96e3305 | 416 | BLE &ble_interface = BLE::Instance(); |
mbed_official | 14:de95c96e3305 | 417 | events::EventQueue event_queue; |
pancotinho | 20:86417f956e22 | 418 | MyService demo_service; |
mbed_official | 14:de95c96e3305 | 419 | BLEProcess ble_process(event_queue, ble_interface); |
pancotinho | 20:86417f956e22 | 420 | |
mbed_official | 14:de95c96e3305 | 421 | |
pancotinho | 20:86417f956e22 | 422 | ble_process.on_init(callback(&demo_service, &MyService::start)); |
pancotinho | 20:86417f956e22 | 423 | |
mbed_official | 14:de95c96e3305 | 424 | |
mbed_official | 14:de95c96e3305 | 425 | // bind the event queue to the ble interface, initialize the interface |
mbed_official | 14:de95c96e3305 | 426 | // and start advertising |
mbed_official | 14:de95c96e3305 | 427 | ble_process.start(); |
mbed_official | 14:de95c96e3305 | 428 | |
mbed_official | 14:de95c96e3305 | 429 | // Process the event queue. |
mbed_official | 14:de95c96e3305 | 430 | event_queue.dispatch_forever(); |
mbed_official | 14:de95c96e3305 | 431 | |
mbed_official | 14:de95c96e3305 | 432 | return 0; |
mbed_official | 14:de95c96e3305 | 433 | } |