GPS to Pulga
Diff: source/main.cpp
- Revision:
- 20:86417f956e22
- Parent:
- 14:de95c96e3305
- Child:
- 21:6cf50085f9f3
diff -r 622c672f6d5f -r 86417f956e22 source/main.cpp --- a/source/main.cpp Mon Mar 25 15:01:13 2019 +0000 +++ b/source/main.cpp Wed Jul 10 17:50:57 2019 +0000 @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + +#include <events/mbed_events.h> #include <stdio.h> +#include "mbed.h" #include "platform/Callback.h" #include "events/EventQueue.h" @@ -27,46 +29,53 @@ #include "ble/GapAdvertisingData.h" #include "ble/GattServer.h" #include "BLEProcess.h" +#include "Si1133.h" + using mbed::callback; + Si1133 sensor_light(P0_13, P0_15); + /** - * A Clock service that demonstrate the GattServer features. + * A My service that demonstrate the GattServer features. * - * The clock service host three characteristics that model the current hour, - * minute and second of the clock. The value of the second characteristic is + * The My service host three characteristics that model the current hour, + * led and count of the My. The value of the count characteristic is * incremented automatically by the system. * - * A client can subscribe to updates of the clock characteristics and get + * A client can subscribe to updates of the My characteristics and get * notified when one of the value is changed. Clients can also change value of - * the second, minute and hour characteristric. + * the count, led and hour characteristric. */ -class ClockService { - typedef ClockService Self; +class MyService { + typedef MyService Self; public: - ClockService() : - _hour_char("485f4145-52b9-4644-af1f-7a6b9322490f", 0), - _minute_char("0a924ca7-87cd-4699-a3bd-abdcd9cf126a", 0), - _second_char("8dd6a1b7-bc75-4741-8a26-264af75807de", 0), - _clock_service( + MyService() : + // _hour_char("485f4145-52b9-4644-af1f-7a6b9322490f", 0), + _led_char("0a924ca7-87cd-4699-a3bd-abdcd9cf126a", 0), + _count_char("8dd6a1b7-bc75-4741-8a26-264af75807de", 0), + _My_service( /* uuid */ "51311102-030e-485f-b122-f8f381aa84ed", - /* characteristics */ _clock_characteristics, - /* numCharacteristics */ sizeof(_clock_characteristics) / - sizeof(_clock_characteristics[0]) + /* characteristics */ _My_characteristics, + /* numCharacteristics */ sizeof(_My_characteristics) / + sizeof(_My_characteristics[0]) ), _server(NULL), - _event_queue(NULL) + _event_queue(NULL), + _alive_led(P1_13, 1), + _actuated_led(P1_14,0) + { // update internal pointers (value, descriptors and characteristics array) - _clock_characteristics[0] = &_hour_char; - _clock_characteristics[1] = &_minute_char; - _clock_characteristics[2] = &_second_char; + // _My_characteristics[0] = &_hour_char; + _My_characteristics[1] = &_led_char; + _My_characteristics[0] = &_count_char; // setup authorization handlers - _hour_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); - _minute_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); - _second_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); + //_hour_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); + _led_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); + _count_char.setWriteAuthorizationCallback(this, &Self::authorize_client_write); } @@ -80,9 +89,10 @@ _server = &ble_interface.gattServer(); _event_queue = &event_queue; + // register the service printf("Adding demo service\r\n"); - ble_error_t err = _server->addService(_clock_service); + ble_error_t err = _server->addService(_My_service); if (err) { printf("Error %u during demo service registration.\r\n", err); @@ -100,13 +110,15 @@ _server->onConfirmationReceived(as_cb(&Self::when_confirmation_received)); // print the handles - printf("clock service registered\r\n"); - printf("service handle: %u\r\n", _clock_service.getHandle()); - printf("\thour characteristic value handle %u\r\n", _hour_char.getValueHandle()); - printf("\tminute characteristic value handle %u\r\n", _minute_char.getValueHandle()); - printf("\tsecond characteristic value handle %u\r\n", _second_char.getValueHandle()); + printf("My service registered\r\n"); + printf("service handle: %u\r\n", _My_service.getHandle()); + //printf("\thour characteristic value handle %u\r\n", _hour_char.getValueHandle()); + // printf("\tled characteristic value handle %u\r\n", _led_char.getValueHandle()); + printf("\tcount characteristic value handle %u\r\n", _count_char.getValueHandle()); - _event_queue->call_every(1000 /* ms */, callback(this, &Self::increment_second)); + _event_queue->call_every(1000 /* ms */, callback(this, &Self::increment_count)); + _event_queue->call_every(500 /* ms */, callback(this, &Self::blink)); + } private: @@ -127,12 +139,14 @@ printf("data written:\r\n"); printf("\tconnection handle: %u\r\n", e->connHandle); printf("\tattribute handle: %u", e->handle); - if (e->handle == _hour_char.getValueHandle()) { - printf(" (hour characteristic)\r\n"); - } else if (e->handle == _minute_char.getValueHandle()) { - printf(" (minute characteristic)\r\n"); - } else if (e->handle == _second_char.getValueHandle()) { - printf(" (second characteristic)\r\n"); + //if (e->handle == _hour_char.getValueHandle()) { + // printf(" (hour characteristic)\r\n"); + // } else + if (e->handle == _led_char.getValueHandle()) { + printf(" (led characteristic)\r\n"); + _actuated_led = *(e->data); + } else if (e->handle == _count_char.getValueHandle()) { + printf(" (count characteristic)\r\n"); } else { printf("\r\n"); } @@ -156,12 +170,13 @@ printf("data read:\r\n"); printf("\tconnection handle: %u\r\n", e->connHandle); printf("\tattribute handle: %u", e->handle); - if (e->handle == _hour_char.getValueHandle()) { - printf(" (hour characteristic)\r\n"); - } else if (e->handle == _minute_char.getValueHandle()) { - printf(" (minute characteristic)\r\n"); - } else if (e->handle == _second_char.getValueHandle()) { - printf(" (second characteristic)\r\n"); + // if (e->handle == _hour_char.getValueHandle()) { + // printf(" (hour characteristic)\r\n"); + // } else + if (e->handle == _led_char.getValueHandle()) { + printf(" (led characteristic)\r\n"); + } else if (e->handle == _count_char.getValueHandle()) { + printf(" (count characteristic)\r\n"); } else { printf("\r\n"); } @@ -221,69 +236,71 @@ return; } - if ((e->data[0] >= 60) || - ((e->data[0] >= 24) && (e->handle == _hour_char.getValueHandle()))) { - printf("Error invalid data\r\n"); - e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED; - return; - } + //if ((e->data[0] >= 60) || + // ((e->data[0] >= 24) && (e->handle == _hour_char.getValueHandle()))) { + // printf("Error invalid data\r\n"); + // e->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED; + // return; + // } e->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; } /** - * Increment the second counter. + * Increment the count counter. */ - void increment_second(void) + void increment_count(void) { - uint8_t second = 0; - ble_error_t err = _second_char.get(*_server, second); + uint8_t count = 0; + + + ble_error_t err = _count_char.get(*_server, count); if (err) { - printf("read of the second value returned error %u\r\n", err); + printf("read of the count value returned error %u\r\n", err); + return; + } + //count ++; + + + if (sensor_light.open()) { + printf("Device detected!\n"); + //Print the current light level + printf("Lux = %.3f\n", (float)sensor_light.get_light_level()); + //Print the current UV index + printf("UV index = %.3f\n", (float)sensor_light.get_uv_index()); + //Sleep for 0.5 seconds + count= static_cast<uint8_t>(sensor_light.get_light_level()); + + _actuated_led = !_actuated_led; + } + + err = _count_char.set(*_server, count); + if (err) { + printf("write of the count value returned error %u\r\n", err); return; } - second = (second + 1) % 60; - - err = _second_char.set(*_server, second); - if (err) { - printf("write of the second value returned error %u\r\n", err); - return; - } - - if (second == 0) { - increment_minute(); - } + } + + void blink() { + _alive_led = !_alive_led; } /** - * Increment the minute counter. + * Change led status. */ - void increment_minute(void) + void change_led(void) { - uint8_t minute = 0; - ble_error_t err = _minute_char.get(*_server, minute); + uint8_t led = 0; + ble_error_t err = _led_char.get(*_server, led); if (err) { - printf("read of the minute value returned error %u\r\n", err); + printf("read of the led value returned error %u\r\n", err); return; } - - minute = (minute + 1) % 60; - - err = _minute_char.set(*_server, minute); - if (err) { - printf("write of the minute value returned error %u\r\n", err); - return; - } - - if (minute == 0) { - increment_hour(); - } } - /** - * Increment the hour counter. - */ + /* * Increment the hour counter. + void increment_hour(void) { uint8_t hour = 0; @@ -293,7 +310,7 @@ return; } - hour = (hour + 1) % 24; + hour = (hour + 1) % 24; err = _hour_char.set(*_server, hour); if (err) { @@ -301,7 +318,7 @@ return; } } - +*/ private: /** * Helper that construct an event handler from a member function of this @@ -346,7 +363,7 @@ } /** - * Get the value of this characteristic. + * Get the value of this characteristic. * * @param[in] server GattServer instance that contain the characteristic * value. @@ -368,8 +385,8 @@ * @param[in] local_only Flag that determine if the change should be kept * locally or forwarded to subscribed clients. */ - ble_error_t set( - GattServer &server, const uint8_t &value, bool local_only = false + ble_error_t set( + GattServer &server, const uint8_t &value, bool local_only = false ) const { return server.write(getValueHandle(), &value, sizeof(value), local_only); } @@ -378,27 +395,32 @@ uint8_t _value; }; - ReadWriteNotifyIndicateCharacteristic<uint8_t> _hour_char; - ReadWriteNotifyIndicateCharacteristic<uint8_t> _minute_char; - ReadWriteNotifyIndicateCharacteristic<uint8_t> _second_char; + //ReadWriteNotifyIndicateCharacteristic<uint8_t> _hour_char; + ReadWriteNotifyIndicateCharacteristic<uint8_t> _led_char; + ReadWriteNotifyIndicateCharacteristic<uint8_t> _count_char; - // list of the characteristics of the clock service - GattCharacteristic* _clock_characteristics[3]; + // list of the characteristics of the My service + GattCharacteristic* _My_characteristics[2]; // demo service - GattService _clock_service; + GattService _My_service; GattServer* _server; events::EventQueue *_event_queue; + + DigitalOut _alive_led; + DigitalOut _actuated_led; }; int main() { BLE &ble_interface = BLE::Instance(); events::EventQueue event_queue; - ClockService demo_service; + MyService demo_service; BLEProcess ble_process(event_queue, ble_interface); + - ble_process.on_init(callback(&demo_service, &ClockService::start)); + ble_process.on_init(callback(&demo_service, &MyService::start)); + // bind the event queue to the ble interface, initialize the interface // and start advertising