Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Committer:
jurica238814
Date:
Thu Jan 31 15:50:52 2019 +0100
Branch:
master
Revision:
29:b021b33cf666
Parent:
28:0cc8a58195cb
Scaling factor hardcoded for acnSENSA format

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