Lizzy project
Dependencies: aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT
aconno_ble/aconno_ble.cpp@22:7dae8496b97c, 2018-09-13 (annotated)
- Committer:
- jurica238814
- Date:
- Thu Sep 13 15:14:14 2018 +0200
- Branch:
- SimpleGATTExample
- Revision:
- 22:7dae8496b97c
- Parent:
- 19:2681edc2f2b9
LAUT example program
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 | |
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" |
jurica238814 | 22:7dae8496b97c | 11 | #include "bsp.h" |
jurica238814 | 22:7dae8496b97c | 12 | |
jurica238814 | 22:7dae8496b97c | 13 | const static char deviceName[] = "LAUT"; |
dbartolovic | 8:7ba4f82de9b6 | 14 | |
dbartolovic | 8:7ba4f82de9b6 | 15 | init_lizzy_t init_lizzy = { |
dbartolovic | 8:7ba4f82de9b6 | 16 | .buzz = false, |
dbartolovic | 8:7ba4f82de9b6 | 17 | .leds = {false ,false, false}, |
jurica238814 | 22:7dae8496b97c | 18 | .acc_lsb = 023, |
dbartolovic | 8:7ba4f82de9b6 | 19 | .acc_data = {0, 0, 0} |
dbartolovic | 8:7ba4f82de9b6 | 20 | }; |
jurica238814 | 22:7dae8496b97c | 21 | |
dbartolovic | 8:7ba4f82de9b6 | 22 | LizzyService *lizzy_service; |
dbartolovic | 8:7ba4f82de9b6 | 23 | |
dbartolovic | 8:7ba4f82de9b6 | 24 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
dbartolovic | 8:7ba4f82de9b6 | 25 | { |
dbartolovic | 8:7ba4f82de9b6 | 26 | (lizzy_service->get_ble())->gap().startAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 27 | } |
dbartolovic | 8:7ba4f82de9b6 | 28 | |
jurica238814 | 19:2681edc2f2b9 | 29 | void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){ |
jurica238814 | 19:2681edc2f2b9 | 30 | |
dbartolovic | 8:7ba4f82de9b6 | 31 | } |
dbartolovic | 8:7ba4f82de9b6 | 32 | |
jurica238814 | 19:2681edc2f2b9 | 33 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
dbartolovic | 8:7ba4f82de9b6 | 34 | { |
jurica238814 | 22:7dae8496b97c | 35 | |
jurica238814 | 22:7dae8496b97c | 36 | if ((params->handle == lizzy_service->get_red_handle()) && |
dbartolovic | 8:7ba4f82de9b6 | 37 | (params->len == 1)) |
dbartolovic | 8:7ba4f82de9b6 | 38 | { |
jurica238814 | 22:7dae8496b97c | 39 | if (*(params->data)) |
jurica238814 | 22:7dae8496b97c | 40 | { |
jurica238814 | 22:7dae8496b97c | 41 | lizzy_service->set_red_state(LED_ON); |
jurica238814 | 22:7dae8496b97c | 42 | } |
jurica238814 | 19:2681edc2f2b9 | 43 | |
dbartolovic | 8:7ba4f82de9b6 | 44 | updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 45 | } |
dbartolovic | 8:7ba4f82de9b6 | 46 | else if ((params->handle == lizzy_service->get_green_handle()) && |
dbartolovic | 8:7ba4f82de9b6 | 47 | (params->len == 1)) |
dbartolovic | 8:7ba4f82de9b6 | 48 | { |
jurica238814 | 22:7dae8496b97c | 49 | if (*(params->data)) |
jurica238814 | 22:7dae8496b97c | 50 | { |
jurica238814 | 22:7dae8496b97c | 51 | lizzy_service->set_green_state(LED_ON); |
jurica238814 | 22:7dae8496b97c | 52 | } |
dbartolovic | 8:7ba4f82de9b6 | 53 | updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 54 | } |
dbartolovic | 8:7ba4f82de9b6 | 55 | } |
dbartolovic | 8:7ba4f82de9b6 | 56 | |
jurica238814 | 0:fc77522f4d28 | 57 | |
jurica238814 | 0:fc77522f4d28 | 58 | /** |
jurica238814 | 0:fc77522f4d28 | 59 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:fc77522f4d28 | 60 | */ |
jurica238814 | 0:fc77522f4d28 | 61 | |
jurica238814 | 0:fc77522f4d28 | 62 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
jurica238814 | 0:fc77522f4d28 | 63 | { |
jurica238814 | 0:fc77522f4d28 | 64 | advertising_packet advertisementPacket; |
jurica238814 | 0:fc77522f4d28 | 65 | BLE& ble = params->ble; |
jurica238814 | 0:fc77522f4d28 | 66 | ble_error_t error = params->error; |
jurica238814 | 0:fc77522f4d28 | 67 | |
jurica238814 | 0:fc77522f4d28 | 68 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:fc77522f4d28 | 69 | return; |
jurica238814 | 0:fc77522f4d28 | 70 | } |
jurica238814 | 0:fc77522f4d28 | 71 | |
jurica238814 | 0:fc77522f4d28 | 72 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:fc77522f4d28 | 73 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:fc77522f4d28 | 74 | return; |
jurica238814 | 0:fc77522f4d28 | 75 | } |
jurica238814 | 19:2681edc2f2b9 | 76 | |
jurica238814 | 19:2681edc2f2b9 | 77 | |
dbartolovic | 8:7ba4f82de9b6 | 78 | lizzy_service = new LizzyService(ble, &init_lizzy); |
dbartolovic | 8:7ba4f82de9b6 | 79 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 19:2681edc2f2b9 | 80 | //ble.gap().onConnection(onConnectionCallback); |
dbartolovic | 8:7ba4f82de9b6 | 81 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
jurica238814 | 19:2681edc2f2b9 | 82 | |
jurica238814 | 0:fc77522f4d28 | 83 | advertisementPacket.header = APPLICATION_ID; |
jurica238814 | 0:fc77522f4d28 | 84 | advertisementPacket.type = 0x00; |
jurica238814 | 19:2681edc2f2b9 | 85 | |
jurica238814 | 0:fc77522f4d28 | 86 | /* setup advertising */ |
jurica238814 | 22:7dae8496b97c | 87 | ble.gap().accumulateAdvertisingPayload( |
jurica238814 | 22:7dae8496b97c | 88 | GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jurica238814 | 22:7dae8496b97c | 89 | ble.gap().accumulateAdvertisingPayload( |
jurica238814 | 22:7dae8496b97c | 90 | GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, |
jurica238814 | 22:7dae8496b97c | 91 | (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
jurica238814 | 22:7dae8496b97c | 92 | ble.gap().accumulateAdvertisingPayload( |
jurica238814 | 22:7dae8496b97c | 93 | GapAdvertisingData::COMPLETE_LOCAL_NAME, |
jurica238814 | 22:7dae8496b97c | 94 | (uint8_t *)deviceName, sizeof(deviceName)); |
jurica238814 | 0:fc77522f4d28 | 95 | ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS); |
jurica238814 | 0:fc77522f4d28 | 96 | ble.gap().startAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 97 | } |
jurica238814 | 0:fc77522f4d28 | 98 | |
jurica238814 | 0:fc77522f4d28 | 99 | void updatePayload(BLE *ble, advertising_packet *advertisementPacket) |
jurica238814 | 0:fc77522f4d28 | 100 | { |
jurica238814 | 0:fc77522f4d28 | 101 | ble->gap().stopAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 102 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 103 | advetisementData = ble->getAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 104 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)advertisementPacket, sizeof(advertising_packet)); |
jurica238814 | 0:fc77522f4d28 | 105 | ble->setAdvertisingData(advetisementData); |
jurica238814 | 0:fc77522f4d28 | 106 | //ble->gap().startAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 107 | } |