Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Committer:
dbartolovic
Date:
Wed Aug 29 17:08:08 2018 +0000
Branch:
axis_normal
Revision:
16:482d8f81d6f3
Parent:
15:c0c01188a29b
Child:
26:6101bb09f70d
Debuged wake on shake. If consumption in sleep is half mA make sure mbed-os version is 5.8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:fc77522f4d28 1 /*
jurica238814 0:fc77522f4d28 2 * Made by Jurica Resetar @ aconno
jurica238814 0:fc77522f4d28 3 * More info @ aconno.de
jurica238814 0:fc77522f4d28 4 *
jurica238814 0:fc77522f4d28 5 */
jurica238814 0:fc77522f4d28 6
dbartolovic 8:7ba4f82de9b6 7 #include "main.h"
jurica238814 0:fc77522f4d28 8 #include "aconno_ble.h"
dbartolovic 8:7ba4f82de9b6 9 #include "tasks.h"
dbartolovic 8:7ba4f82de9b6 10 #include "lizzy_service.h"
dbartolovic 8:7ba4f82de9b6 11
dbartolovic 15:c0c01188a29b 12
dbartolovic 15:c0c01188a29b 13 /* BLE event queue size */
dbartolovic 15:c0c01188a29b 14 #define BLE_EVENT_COUNT (4)
dbartolovic 15:c0c01188a29b 15
dbartolovic 8:7ba4f82de9b6 16 init_lizzy_t init_lizzy = {
dbartolovic 8:7ba4f82de9b6 17 .buzz = false,
dbartolovic 8:7ba4f82de9b6 18 .leds = {false ,false, false},
dbartolovic 8:7ba4f82de9b6 19 .acc_lsb = LSB_VALUE,
dbartolovic 8:7ba4f82de9b6 20 .acc_data = {0, 0, 0}
dbartolovic 8:7ba4f82de9b6 21 };
dbartolovic 8:7ba4f82de9b6 22 LizzyService *lizzy_service;
dbartolovic 8:7ba4f82de9b6 23
dbartolovic 16:482d8f81d6f3 24 static bool clientConected = false;
dbartolovic 15:c0c01188a29b 25 static EventQueue eventQueue( BLE_EVENT_COUNT * EVENTS_EVENT_SIZE );
dbartolovic 15:c0c01188a29b 26
dbartolovic 15:c0c01188a29b 27
dbartolovic 15:c0c01188a29b 28 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
dbartolovic 15:c0c01188a29b 29 {
dbartolovic 15:c0c01188a29b 30 BLE &ble = context->ble;
dbartolovic 15:c0c01188a29b 31 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
dbartolovic 15:c0c01188a29b 32 }
dbartolovic 15:c0c01188a29b 33
dbartolovic 15:c0c01188a29b 34 EventQueue *getBLEEventQueue(void)
dbartolovic 15:c0c01188a29b 35 {
dbartolovic 15:c0c01188a29b 36 return &eventQueue;
dbartolovic 15:c0c01188a29b 37 }
dbartolovic 15:c0c01188a29b 38
dbartolovic 16:482d8f81d6f3 39 bool bleIsClientConnected(void)
dbartolovic 16:482d8f81d6f3 40 {
dbartolovic 16:482d8f81d6f3 41 return clientConected;
dbartolovic 16:482d8f81d6f3 42 }
dbartolovic 16:482d8f81d6f3 43
dbartolovic 8:7ba4f82de9b6 44 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
dbartolovic 8:7ba4f82de9b6 45 {
dbartolovic 16:482d8f81d6f3 46 clientConected = false;
dbartolovic 8:7ba4f82de9b6 47 (lizzy_service->get_ble())->gap().startAdvertising();
dbartolovic 8:7ba4f82de9b6 48 }
dbartolovic 8:7ba4f82de9b6 49
dbartolovic 8:7ba4f82de9b6 50 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
dbartolovic 16:482d8f81d6f3 51 clientConected = true;
dbartolovic 8:7ba4f82de9b6 52 }
dbartolovic 8:7ba4f82de9b6 53
dbartolovic 8:7ba4f82de9b6 54 void onDataWrittenCallback(const GattWriteCallbackParams *params)
dbartolovic 8:7ba4f82de9b6 55 {
dbartolovic 8:7ba4f82de9b6 56 if ((params->handle == lizzy_service->get_buzz_handle()) &&
dbartolovic 8:7ba4f82de9b6 57 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 58 {
dbartolovic 8:7ba4f82de9b6 59 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 60 lizzy_service->set_buzz_state(true);
dbartolovic 8:7ba4f82de9b6 61
dbartolovic 8:7ba4f82de9b6 62 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 63 }
dbartolovic 8:7ba4f82de9b6 64 else if ((params->handle == lizzy_service->get_red_handle()) &&
dbartolovic 8:7ba4f82de9b6 65 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 66 {
dbartolovic 8:7ba4f82de9b6 67 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 68 lizzy_service->set_red_state(true);
dbartolovic 8:7ba4f82de9b6 69
dbartolovic 8:7ba4f82de9b6 70 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 71 }
dbartolovic 8:7ba4f82de9b6 72 else if ((params->handle == lizzy_service->get_green_handle()) &&
dbartolovic 8:7ba4f82de9b6 73 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 74 {
dbartolovic 8:7ba4f82de9b6 75 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 76 lizzy_service->set_green_state(true);
dbartolovic 8:7ba4f82de9b6 77
dbartolovic 8:7ba4f82de9b6 78 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 79 }
dbartolovic 8:7ba4f82de9b6 80 else if ((params->handle == lizzy_service->get_blue_handle()) &&
dbartolovic 8:7ba4f82de9b6 81 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 82 {
dbartolovic 8:7ba4f82de9b6 83 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 84 lizzy_service->set_blue_state(true);
dbartolovic 8:7ba4f82de9b6 85
dbartolovic 8:7ba4f82de9b6 86 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 87 }
dbartolovic 8:7ba4f82de9b6 88 }
dbartolovic 8:7ba4f82de9b6 89
jurica238814 0:fc77522f4d28 90
jurica238814 0:fc77522f4d28 91 /**
jurica238814 0:fc77522f4d28 92 * Callback triggered when the ble initialization process has finished
jurica238814 0:fc77522f4d28 93 */
jurica238814 0:fc77522f4d28 94
jurica238814 0:fc77522f4d28 95 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
jurica238814 0:fc77522f4d28 96 {
jurica238814 0:fc77522f4d28 97 advertising_packet advertisementPacket;
jurica238814 0:fc77522f4d28 98 BLE& ble = params->ble;
jurica238814 0:fc77522f4d28 99 ble_error_t error = params->error;
jurica238814 0:fc77522f4d28 100
jurica238814 0:fc77522f4d28 101 if (error != BLE_ERROR_NONE) {
jurica238814 0:fc77522f4d28 102 return;
jurica238814 0:fc77522f4d28 103 }
jurica238814 0:fc77522f4d28 104
jurica238814 0:fc77522f4d28 105 /* Ensure that it is the default instance of BLE */
jurica238814 0:fc77522f4d28 106 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:fc77522f4d28 107 return;
jurica238814 0:fc77522f4d28 108 }
jurica238814 0:fc77522f4d28 109
dbartolovic 8:7ba4f82de9b6 110
dbartolovic 8:7ba4f82de9b6 111 lizzy_service = new LizzyService(ble, &init_lizzy);
dbartolovic 8:7ba4f82de9b6 112 ble.gap().onDisconnection(disconnectionCallback);
dbartolovic 16:482d8f81d6f3 113 ble.gap().onConnection(onConnectionCallback);
dbartolovic 8:7ba4f82de9b6 114 ble.gattServer().onDataWritten(onDataWrittenCallback);
dbartolovic 8:7ba4f82de9b6 115
dbartolovic 15:c0c01188a29b 116 // Setup event handling. This is needed only with services.
dbartolovic 15:c0c01188a29b 117 ble.onEventsToProcess(scheduleBleEventsProcessing);
dbartolovic 15:c0c01188a29b 118
jurica238814 0:fc77522f4d28 119 advertisementPacket.header = APPLICATION_ID;
jurica238814 0:fc77522f4d28 120 advertisementPacket.type = 0x00;
jurica238814 0:fc77522f4d28 121 advertisementPacket.gyroscope[0] = (int16_t)0;
jurica238814 0:fc77522f4d28 122 advertisementPacket.gyroscope[1] = (int16_t)0;
jurica238814 0:fc77522f4d28 123 advertisementPacket.gyroscope[2] = (int16_t)0;
jurica238814 0:fc77522f4d28 124 advertisementPacket.accelerometer[0] = (int16_t)0;
jurica238814 0:fc77522f4d28 125 advertisementPacket.accelerometer[1] = (int16_t)0;
jurica238814 0:fc77522f4d28 126 advertisementPacket.accelerometer[2] = (int16_t)0;
jurica238814 0:fc77522f4d28 127 advertisementPacket.magnetometer[0] = (int16_t)0;
jurica238814 0:fc77522f4d28 128 advertisementPacket.magnetometer[1] = (int16_t)0;
jurica238814 0:fc77522f4d28 129 advertisementPacket.magnetometer[2] = (int16_t)0;
jurica238814 0:fc77522f4d28 130
jurica238814 0:fc77522f4d28 131 /* setup advertising */
jurica238814 0:fc77522f4d28 132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 0:fc77522f4d28 133 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(uint8_t)*11);
jurica238814 0:fc77522f4d28 134 ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
dbartolovic 16:482d8f81d6f3 135 //ble.gap().startAdvertising();
jurica238814 0:fc77522f4d28 136 }
jurica238814 0:fc77522f4d28 137
jurica238814 0:fc77522f4d28 138 void updatePayload(BLE *ble, advertising_packet *advertisementPacket)
jurica238814 0:fc77522f4d28 139 {
jurica238814 0:fc77522f4d28 140 ble->gap().stopAdvertising();
jurica238814 0:fc77522f4d28 141 GapAdvertisingData advetisementData = GapAdvertisingData();
jurica238814 0:fc77522f4d28 142 advetisementData = ble->getAdvertisingData();
jurica238814 0:fc77522f4d28 143 advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)advertisementPacket, sizeof(advertising_packet));
jurica238814 0:fc77522f4d28 144 ble->setAdvertisingData(advetisementData);
jurica238814 0:fc77522f4d28 145 //ble->gap().startAdvertising();
jurica238814 0:fc77522f4d28 146 }