Lizzy project
Dependencies: aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT
aconno_ble/aconno_ble.cpp@15:c0c01188a29b, 2018-08-29 (annotated)
- Committer:
- dbartolovic
- Date:
- Wed Aug 29 14:48:43 2018 +0000
- Branch:
- axis_normal
- Revision:
- 15:c0c01188a29b
- Parent:
- 8:7ba4f82de9b6
- Child:
- 16:482d8f81d6f3
Brought consumption to 500uA
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:7ba4f82de9b6 | 24 | |
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 | 8:7ba4f82de9b6 | 39 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
dbartolovic | 8:7ba4f82de9b6 | 40 | { |
dbartolovic | 8:7ba4f82de9b6 | 41 | (lizzy_service->get_ble())->gap().startAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 42 | } |
dbartolovic | 8:7ba4f82de9b6 | 43 | |
dbartolovic | 8:7ba4f82de9b6 | 44 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){ |
dbartolovic | 8:7ba4f82de9b6 | 45 | |
dbartolovic | 8:7ba4f82de9b6 | 46 | } |
dbartolovic | 8:7ba4f82de9b6 | 47 | |
dbartolovic | 8:7ba4f82de9b6 | 48 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
dbartolovic | 8:7ba4f82de9b6 | 49 | { |
dbartolovic | 8:7ba4f82de9b6 | 50 | if ((params->handle == lizzy_service->get_buzz_handle()) && |
dbartolovic | 8:7ba4f82de9b6 | 51 | (params->len == 1)) |
dbartolovic | 8:7ba4f82de9b6 | 52 | { |
dbartolovic | 8:7ba4f82de9b6 | 53 | if ((uint8_t)true < *(params->data)) |
dbartolovic | 8:7ba4f82de9b6 | 54 | lizzy_service->set_buzz_state(true); |
dbartolovic | 8:7ba4f82de9b6 | 55 | |
dbartolovic | 8:7ba4f82de9b6 | 56 | updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 57 | } |
dbartolovic | 8:7ba4f82de9b6 | 58 | else if ((params->handle == lizzy_service->get_red_handle()) && |
dbartolovic | 8:7ba4f82de9b6 | 59 | (params->len == 1)) |
dbartolovic | 8:7ba4f82de9b6 | 60 | { |
dbartolovic | 8:7ba4f82de9b6 | 61 | if ((uint8_t)true < *(params->data)) |
dbartolovic | 8:7ba4f82de9b6 | 62 | lizzy_service->set_red_state(true); |
dbartolovic | 8:7ba4f82de9b6 | 63 | |
dbartolovic | 8:7ba4f82de9b6 | 64 | updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 65 | } |
dbartolovic | 8:7ba4f82de9b6 | 66 | else if ((params->handle == lizzy_service->get_green_handle()) && |
dbartolovic | 8:7ba4f82de9b6 | 67 | (params->len == 1)) |
dbartolovic | 8:7ba4f82de9b6 | 68 | { |
dbartolovic | 8:7ba4f82de9b6 | 69 | if ((uint8_t)true < *(params->data)) |
dbartolovic | 8:7ba4f82de9b6 | 70 | lizzy_service->set_green_state(true); |
dbartolovic | 8:7ba4f82de9b6 | 71 | |
dbartolovic | 8:7ba4f82de9b6 | 72 | updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 73 | } |
dbartolovic | 8:7ba4f82de9b6 | 74 | else if ((params->handle == lizzy_service->get_blue_handle()) && |
dbartolovic | 8:7ba4f82de9b6 | 75 | (params->len == 1)) |
dbartolovic | 8:7ba4f82de9b6 | 76 | { |
dbartolovic | 8:7ba4f82de9b6 | 77 | if ((uint8_t)true < *(params->data)) |
dbartolovic | 8:7ba4f82de9b6 | 78 | lizzy_service->set_blue_state(true); |
dbartolovic | 8:7ba4f82de9b6 | 79 | |
dbartolovic | 8:7ba4f82de9b6 | 80 | updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 81 | } |
dbartolovic | 8:7ba4f82de9b6 | 82 | } |
dbartolovic | 8:7ba4f82de9b6 | 83 | |
jurica238814 | 0:fc77522f4d28 | 84 | |
jurica238814 | 0:fc77522f4d28 | 85 | /** |
jurica238814 | 0:fc77522f4d28 | 86 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:fc77522f4d28 | 87 | */ |
jurica238814 | 0:fc77522f4d28 | 88 | |
jurica238814 | 0:fc77522f4d28 | 89 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
jurica238814 | 0:fc77522f4d28 | 90 | { |
jurica238814 | 0:fc77522f4d28 | 91 | advertising_packet advertisementPacket; |
jurica238814 | 0:fc77522f4d28 | 92 | BLE& ble = params->ble; |
jurica238814 | 0:fc77522f4d28 | 93 | ble_error_t error = params->error; |
jurica238814 | 0:fc77522f4d28 | 94 | |
jurica238814 | 0:fc77522f4d28 | 95 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:fc77522f4d28 | 96 | return; |
jurica238814 | 0:fc77522f4d28 | 97 | } |
jurica238814 | 0:fc77522f4d28 | 98 | |
jurica238814 | 0:fc77522f4d28 | 99 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:fc77522f4d28 | 100 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:fc77522f4d28 | 101 | return; |
jurica238814 | 0:fc77522f4d28 | 102 | } |
jurica238814 | 0:fc77522f4d28 | 103 | |
dbartolovic | 8:7ba4f82de9b6 | 104 | |
dbartolovic | 8:7ba4f82de9b6 | 105 | lizzy_service = new LizzyService(ble, &init_lizzy); |
dbartolovic | 8:7ba4f82de9b6 | 106 | ble.gap().onDisconnection(disconnectionCallback); |
dbartolovic | 8:7ba4f82de9b6 | 107 | //ble.gap().onConnection(onConnectionCallback); |
dbartolovic | 8:7ba4f82de9b6 | 108 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
dbartolovic | 8:7ba4f82de9b6 | 109 | |
dbartolovic | 15:c0c01188a29b | 110 | // Setup event handling. This is needed only with services. |
dbartolovic | 15:c0c01188a29b | 111 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
dbartolovic | 15:c0c01188a29b | 112 | |
jurica238814 | 0:fc77522f4d28 | 113 | advertisementPacket.header = APPLICATION_ID; |
jurica238814 | 0:fc77522f4d28 | 114 | advertisementPacket.type = 0x00; |
jurica238814 | 0:fc77522f4d28 | 115 | advertisementPacket.gyroscope[0] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 116 | advertisementPacket.gyroscope[1] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 117 | advertisementPacket.gyroscope[2] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 118 | advertisementPacket.accelerometer[0] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 119 | advertisementPacket.accelerometer[1] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 120 | advertisementPacket.accelerometer[2] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 121 | advertisementPacket.magnetometer[0] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 122 | advertisementPacket.magnetometer[1] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 123 | advertisementPacket.magnetometer[2] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 124 | |
jurica238814 | 0:fc77522f4d28 | 125 | /* setup advertising */ |
jurica238814 | 0:fc77522f4d28 | 126 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jurica238814 | 0:fc77522f4d28 | 127 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(uint8_t)*11); |
jurica238814 | 0:fc77522f4d28 | 128 | ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS); |
jurica238814 | 0:fc77522f4d28 | 129 | ble.gap().startAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 130 | } |
jurica238814 | 0:fc77522f4d28 | 131 | |
jurica238814 | 0:fc77522f4d28 | 132 | void updatePayload(BLE *ble, advertising_packet *advertisementPacket) |
jurica238814 | 0:fc77522f4d28 | 133 | { |
jurica238814 | 0:fc77522f4d28 | 134 | ble->gap().stopAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 135 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 136 | advetisementData = ble->getAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 137 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)advertisementPacket, sizeof(advertising_packet)); |
jurica238814 | 0:fc77522f4d28 | 138 | ble->setAdvertisingData(advetisementData); |
jurica238814 | 0:fc77522f4d28 | 139 | //ble->gap().startAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 140 | } |