Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Committer:
jurica238814
Date:
Thu Sep 13 12:20:35 2018 +0200
Branch:
mbedOsUpdate
Revision:
19:2681edc2f2b9
Parent:
8:7ba4f82de9b6
Child:
22:7dae8496b97c
Work in progress

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
jurica238814 0:fc77522f4d28 7 #include "aconno_ble.h"
dbartolovic 8:7ba4f82de9b6 8 #include "tasks.h"
dbartolovic 8:7ba4f82de9b6 9 #include "lizzy_service.h"
jurica238814 19:2681edc2f2b9 10 #include "aconnoConfig.h"
dbartolovic 8:7ba4f82de9b6 11
dbartolovic 8:7ba4f82de9b6 12 init_lizzy_t init_lizzy = {
dbartolovic 8:7ba4f82de9b6 13 .buzz = false,
dbartolovic 8:7ba4f82de9b6 14 .leds = {false ,false, false},
dbartolovic 8:7ba4f82de9b6 15 .acc_lsb = LSB_VALUE,
dbartolovic 8:7ba4f82de9b6 16 .acc_data = {0, 0, 0}
dbartolovic 8:7ba4f82de9b6 17 };
dbartolovic 8:7ba4f82de9b6 18 LizzyService *lizzy_service;
dbartolovic 8:7ba4f82de9b6 19
dbartolovic 8:7ba4f82de9b6 20
dbartolovic 8:7ba4f82de9b6 21 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
dbartolovic 8:7ba4f82de9b6 22 {
dbartolovic 8:7ba4f82de9b6 23 (lizzy_service->get_ble())->gap().startAdvertising();
dbartolovic 8:7ba4f82de9b6 24 }
dbartolovic 8:7ba4f82de9b6 25
jurica238814 19:2681edc2f2b9 26 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
jurica238814 19:2681edc2f2b9 27
dbartolovic 8:7ba4f82de9b6 28 }
dbartolovic 8:7ba4f82de9b6 29
jurica238814 19:2681edc2f2b9 30 void onDataWrittenCallback(const GattWriteCallbackParams *params)
dbartolovic 8:7ba4f82de9b6 31 {
dbartolovic 8:7ba4f82de9b6 32 if ((params->handle == lizzy_service->get_buzz_handle()) &&
dbartolovic 8:7ba4f82de9b6 33 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 34 {
dbartolovic 8:7ba4f82de9b6 35 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 36 lizzy_service->set_buzz_state(true);
jurica238814 19:2681edc2f2b9 37
dbartolovic 8:7ba4f82de9b6 38 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 39 }
dbartolovic 8:7ba4f82de9b6 40 else if ((params->handle == lizzy_service->get_red_handle()) &&
dbartolovic 8:7ba4f82de9b6 41 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 42 {
dbartolovic 8:7ba4f82de9b6 43 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 44 lizzy_service->set_red_state(true);
jurica238814 19:2681edc2f2b9 45
dbartolovic 8:7ba4f82de9b6 46 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 47 }
dbartolovic 8:7ba4f82de9b6 48 else if ((params->handle == lizzy_service->get_green_handle()) &&
dbartolovic 8:7ba4f82de9b6 49 (params->len == 1))
dbartolovic 8:7ba4f82de9b6 50 {
dbartolovic 8:7ba4f82de9b6 51 if ((uint8_t)true < *(params->data))
dbartolovic 8:7ba4f82de9b6 52 lizzy_service->set_green_state(true);
jurica238814 19:2681edc2f2b9 53
dbartolovic 8:7ba4f82de9b6 54 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 55 }
dbartolovic 8:7ba4f82de9b6 56 else if ((params->handle == lizzy_service->get_blue_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_blue_state(true);
jurica238814 19:2681edc2f2b9 61
dbartolovic 8:7ba4f82de9b6 62 updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 63 }
dbartolovic 8:7ba4f82de9b6 64 }
dbartolovic 8:7ba4f82de9b6 65
jurica238814 0:fc77522f4d28 66
jurica238814 0:fc77522f4d28 67 /**
jurica238814 0:fc77522f4d28 68 * Callback triggered when the ble initialization process has finished
jurica238814 0:fc77522f4d28 69 */
jurica238814 0:fc77522f4d28 70
jurica238814 0:fc77522f4d28 71 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
jurica238814 0:fc77522f4d28 72 {
jurica238814 0:fc77522f4d28 73 advertising_packet advertisementPacket;
jurica238814 0:fc77522f4d28 74 BLE& ble = params->ble;
jurica238814 0:fc77522f4d28 75 ble_error_t error = params->error;
jurica238814 0:fc77522f4d28 76
jurica238814 0:fc77522f4d28 77 if (error != BLE_ERROR_NONE) {
jurica238814 0:fc77522f4d28 78 return;
jurica238814 0:fc77522f4d28 79 }
jurica238814 0:fc77522f4d28 80
jurica238814 0:fc77522f4d28 81 /* Ensure that it is the default instance of BLE */
jurica238814 0:fc77522f4d28 82 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:fc77522f4d28 83 return;
jurica238814 0:fc77522f4d28 84 }
jurica238814 19:2681edc2f2b9 85
jurica238814 19:2681edc2f2b9 86
dbartolovic 8:7ba4f82de9b6 87 lizzy_service = new LizzyService(ble, &init_lizzy);
dbartolovic 8:7ba4f82de9b6 88 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 19:2681edc2f2b9 89 //ble.gap().onConnection(onConnectionCallback);
dbartolovic 8:7ba4f82de9b6 90 ble.gattServer().onDataWritten(onDataWrittenCallback);
jurica238814 19:2681edc2f2b9 91
jurica238814 0:fc77522f4d28 92 advertisementPacket.header = APPLICATION_ID;
jurica238814 0:fc77522f4d28 93 advertisementPacket.type = 0x00;
jurica238814 0:fc77522f4d28 94 advertisementPacket.gyroscope[0] = (int16_t)0;
jurica238814 0:fc77522f4d28 95 advertisementPacket.gyroscope[1] = (int16_t)0;
jurica238814 0:fc77522f4d28 96 advertisementPacket.gyroscope[2] = (int16_t)0;
jurica238814 0:fc77522f4d28 97 advertisementPacket.accelerometer[0] = (int16_t)0;
jurica238814 0:fc77522f4d28 98 advertisementPacket.accelerometer[1] = (int16_t)0;
jurica238814 0:fc77522f4d28 99 advertisementPacket.accelerometer[2] = (int16_t)0;
jurica238814 0:fc77522f4d28 100 advertisementPacket.magnetometer[0] = (int16_t)0;
jurica238814 0:fc77522f4d28 101 advertisementPacket.magnetometer[1] = (int16_t)0;
jurica238814 0:fc77522f4d28 102 advertisementPacket.magnetometer[2] = (int16_t)0;
jurica238814 19:2681edc2f2b9 103
jurica238814 0:fc77522f4d28 104 /* setup advertising */
jurica238814 0:fc77522f4d28 105 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 0:fc77522f4d28 106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(uint8_t)*11);
jurica238814 0:fc77522f4d28 107 ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
jurica238814 0:fc77522f4d28 108 ble.gap().startAdvertising();
jurica238814 0:fc77522f4d28 109 }
jurica238814 0:fc77522f4d28 110
jurica238814 0:fc77522f4d28 111 void updatePayload(BLE *ble, advertising_packet *advertisementPacket)
jurica238814 0:fc77522f4d28 112 {
jurica238814 0:fc77522f4d28 113 ble->gap().stopAdvertising();
jurica238814 0:fc77522f4d28 114 GapAdvertisingData advetisementData = GapAdvertisingData();
jurica238814 0:fc77522f4d28 115 advetisementData = ble->getAdvertisingData();
jurica238814 0:fc77522f4d28 116 advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)advertisementPacket, sizeof(advertising_packet));
jurica238814 0:fc77522f4d28 117 ble->setAdvertisingData(advetisementData);
jurica238814 0:fc77522f4d28 118 //ble->gap().startAdvertising();
jurica238814 0:fc77522f4d28 119 }