Lizzy project
Dependencies: aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT
tasks/tasks.cpp@19:2681edc2f2b9, 2018-09-13 (annotated)
- Committer:
- jurica238814
- Date:
- Thu Sep 13 12:20:35 2018 +0200
- Branch:
- mbedOsUpdate
- Revision:
- 19:2681edc2f2b9
- Parent:
- 11:4dc690157508
- Child:
- 22:7dae8496b97c
Work in progress
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 19:2681edc2f2b9 | 1 | /** |
jurica238814 | 19:2681edc2f2b9 | 2 | * Aconno.de |
jurica238814 | 19:2681edc2f2b9 | 3 | */ |
jurica238814 | 19:2681edc2f2b9 | 4 | |
dbartolovic | 8:7ba4f82de9b6 | 5 | #include "bsp_buzz.h" |
jurica238814 | 19:2681edc2f2b9 | 6 | #include "bspLed.h" |
dbartolovic | 8:7ba4f82de9b6 | 7 | #include "aconno_ble.h" |
dbartolovic | 8:7ba4f82de9b6 | 8 | #include "tasks.h" |
dbartolovic | 8:7ba4f82de9b6 | 9 | #include "GapAdvertisingData.h" |
jurica238814 | 19:2681edc2f2b9 | 10 | #include "aconnoConfig.h" |
jurica238814 | 19:2681edc2f2b9 | 11 | #include "bsp.h" |
dbartolovic | 8:7ba4f82de9b6 | 12 | |
dbartolovic | 8:7ba4f82de9b6 | 13 | Buzz buzzer(NRF_PWM2, BUZZER_PIN); |
dbartolovic | 8:7ba4f82de9b6 | 14 | |
dbartolovic | 8:7ba4f82de9b6 | 15 | static advertising_packet advertisementPacket; |
dbartolovic | 8:7ba4f82de9b6 | 16 | static GapAdvertisingData adv_data = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 17 | |
dbartolovic | 8:7ba4f82de9b6 | 18 | |
dbartolovic | 8:7ba4f82de9b6 | 19 | #if TEST_LEDS_BUZZ |
dbartolovic | 8:7ba4f82de9b6 | 20 | Ticker test_ticker; |
dbartolovic | 8:7ba4f82de9b6 | 21 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 22 | |
dbartolovic | 8:7ba4f82de9b6 | 23 | |
dbartolovic | 8:7ba4f82de9b6 | 24 | void tasks_init() |
dbartolovic | 8:7ba4f82de9b6 | 25 | { |
jurica238814 | 19:2681edc2f2b9 | 26 | |
dbartolovic | 8:7ba4f82de9b6 | 27 | #if TEST_LEDS_BUZZ |
dbartolovic | 8:7ba4f82de9b6 | 28 | test_ticker.attach(led_tick, 0.5); |
dbartolovic | 8:7ba4f82de9b6 | 29 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 30 | } |
dbartolovic | 8:7ba4f82de9b6 | 31 | |
dbartolovic | 8:7ba4f82de9b6 | 32 | #if TEST_LEDS_BUZZ |
dbartolovic | 8:7ba4f82de9b6 | 33 | void buzz_tick() |
dbartolovic | 8:7ba4f82de9b6 | 34 | { |
dbartolovic | 8:7ba4f82de9b6 | 35 | static int start = 1; |
jurica238814 | 19:2681edc2f2b9 | 36 | |
dbartolovic | 8:7ba4f82de9b6 | 37 | if (start) |
dbartolovic | 8:7ba4f82de9b6 | 38 | { |
dbartolovic | 8:7ba4f82de9b6 | 39 | buzzer.enable(); |
dbartolovic | 8:7ba4f82de9b6 | 40 | start = 0; |
dbartolovic | 8:7ba4f82de9b6 | 41 | } |
dbartolovic | 8:7ba4f82de9b6 | 42 | else |
dbartolovic | 8:7ba4f82de9b6 | 43 | { |
dbartolovic | 8:7ba4f82de9b6 | 44 | buzzer.disable(); |
dbartolovic | 8:7ba4f82de9b6 | 45 | start = 1; |
dbartolovic | 8:7ba4f82de9b6 | 46 | led_tick(); |
dbartolovic | 8:7ba4f82de9b6 | 47 | test_ticker.detach(); |
dbartolovic | 8:7ba4f82de9b6 | 48 | test_ticker.attach(led_tick, 0.5); |
dbartolovic | 8:7ba4f82de9b6 | 49 | } |
dbartolovic | 8:7ba4f82de9b6 | 50 | } |
dbartolovic | 8:7ba4f82de9b6 | 51 | |
dbartolovic | 8:7ba4f82de9b6 | 52 | void led_tick() |
dbartolovic | 8:7ba4f82de9b6 | 53 | { |
dbartolovic | 8:7ba4f82de9b6 | 54 | static int count = 0; |
jurica238814 | 19:2681edc2f2b9 | 55 | |
dbartolovic | 8:7ba4f82de9b6 | 56 | switch(count) |
dbartolovic | 8:7ba4f82de9b6 | 57 | { |
dbartolovic | 8:7ba4f82de9b6 | 58 | case 0: |
dbartolovic | 8:7ba4f82de9b6 | 59 | redLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 60 | break; |
jurica238814 | 19:2681edc2f2b9 | 61 | |
dbartolovic | 8:7ba4f82de9b6 | 62 | case 1: |
dbartolovic | 8:7ba4f82de9b6 | 63 | redLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 64 | greenLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 65 | break; |
jurica238814 | 19:2681edc2f2b9 | 66 | |
dbartolovic | 8:7ba4f82de9b6 | 67 | case 2: |
dbartolovic | 8:7ba4f82de9b6 | 68 | greenLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 69 | blueLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 70 | break; |
jurica238814 | 19:2681edc2f2b9 | 71 | |
dbartolovic | 8:7ba4f82de9b6 | 72 | default: |
dbartolovic | 8:7ba4f82de9b6 | 73 | blueLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 74 | count = -1; |
dbartolovic | 8:7ba4f82de9b6 | 75 | buzz_tick(); |
dbartolovic | 8:7ba4f82de9b6 | 76 | test_ticker.detach(); |
dbartolovic | 8:7ba4f82de9b6 | 77 | test_ticker.attach(buzz_tick, BUZZ_TIME_S); |
dbartolovic | 8:7ba4f82de9b6 | 78 | } |
jurica238814 | 19:2681edc2f2b9 | 79 | |
dbartolovic | 8:7ba4f82de9b6 | 80 | count++; |
dbartolovic | 8:7ba4f82de9b6 | 81 | } |
dbartolovic | 8:7ba4f82de9b6 | 82 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 83 | |
dbartolovic | 8:7ba4f82de9b6 | 84 | void measureF(Lis2dh12 *mems) |
dbartolovic | 8:7ba4f82de9b6 | 85 | { |
dbartolovic | 8:7ba4f82de9b6 | 86 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 87 | { |
dbartolovic | 8:7ba4f82de9b6 | 88 | advertisementPacket.header = APPLICATION_ID; |
dbartolovic | 8:7ba4f82de9b6 | 89 | advertisementPacket.type = 0x00; |
dbartolovic | 8:7ba4f82de9b6 | 90 | advertisementPacket.gyroscope[0] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 91 | advertisementPacket.gyroscope[1] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 92 | advertisementPacket.gyroscope[2] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 93 | advertisementPacket.magnetometer[0] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 94 | advertisementPacket.magnetometer[1] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 95 | advertisementPacket.magnetometer[2] = (int16_t)0; |
jurica238814 | 19:2681edc2f2b9 | 96 | |
jurica238814 | 19:2681edc2f2b9 | 97 | lisPowerCtrl(LIS_ON); |
jurica238814 | 19:2681edc2f2b9 | 98 | wait_ms(POWER_UP_DELAY_MS); |
jurica238814 | 19:2681edc2f2b9 | 99 | /* |
jurica238814 | 19:2681edc2f2b9 | 100 | //mems.setMode(LOW_POWER); |
jurica238814 | 19:2681edc2f2b9 | 101 | mems.setMode(HIGH_RES); |
jurica238814 | 19:2681edc2f2b9 | 102 | mems.enableAxes(X_axis); |
jurica238814 | 19:2681edc2f2b9 | 103 | mems.enableAxes(Y_axis); |
jurica238814 | 19:2681edc2f2b9 | 104 | mems.enableAxes(Z_axis); |
jurica238814 | 19:2681edc2f2b9 | 105 | mems.setODR(ODR_50Hz); |
jurica238814 | 19:2681edc2f2b9 | 106 | //mems.setScale(_16g); |
jurica238814 | 19:2681edc2f2b9 | 107 | mems.setScale(_2g); |
jurica238814 | 19:2681edc2f2b9 | 108 | mems.int1Setup(0x40); // IntActivity 1 driven to INT1 pad |
jurica238814 | 19:2681edc2f2b9 | 109 | mems.int1Latch(0x01); // Enable int1 latch |
jurica238814 | 19:2681edc2f2b9 | 110 | mems.int1Threshold(INT1_THRESHOLD); |
jurica238814 | 19:2681edc2f2b9 | 111 | mems.int1Duration(INT1_DUR); |
jurica238814 | 19:2681edc2f2b9 | 112 | mems.int1Config(0x2A); // Enable XHigh, YHigh and ZHigh |
jurica238814 | 19:2681edc2f2b9 | 113 | */ |
jurica238814 | 19:2681edc2f2b9 | 114 | |
dbartolovic | 8:7ba4f82de9b6 | 115 | advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis(); |
dbartolovic | 8:7ba4f82de9b6 | 116 | advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis(); |
dbartolovic | 8:7ba4f82de9b6 | 117 | advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis(); |
jurica238814 | 19:2681edc2f2b9 | 118 | |
dbartolovic | 9:aef8bb3d13ed | 119 | advertisementPacket.acc_lsb_value = LSB_VALUE; |
jurica238814 | 19:2681edc2f2b9 | 120 | |
dbartolovic | 8:7ba4f82de9b6 | 121 | updateServiceT.signal_set(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 122 | bleT.signal_set(MEAS_DONE); |
jurica238814 | 19:2681edc2f2b9 | 123 | |
jurica238814 | 19:2681edc2f2b9 | 124 | lisPowerCtrl(LIS_OFF); |
jurica238814 | 19:2681edc2f2b9 | 125 | Thread::wait(MEASURE_INTERVAL_MS); |
dbartolovic | 8:7ba4f82de9b6 | 126 | } |
dbartolovic | 8:7ba4f82de9b6 | 127 | } |
dbartolovic | 8:7ba4f82de9b6 | 128 | |
dbartolovic | 8:7ba4f82de9b6 | 129 | void updateServiceF() |
dbartolovic | 8:7ba4f82de9b6 | 130 | { |
dbartolovic | 8:7ba4f82de9b6 | 131 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 132 | { |
dbartolovic | 8:7ba4f82de9b6 | 133 | Thread::signal_wait(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 134 | updateServiceT.signal_clr(MEAS_DONE); |
jurica238814 | 19:2681edc2f2b9 | 135 | |
dbartolovic | 8:7ba4f82de9b6 | 136 | lizzy_service->set_acc_data(advertisementPacket.accelerometer); |
dbartolovic | 8:7ba4f82de9b6 | 137 | } |
dbartolovic | 8:7ba4f82de9b6 | 138 | } |
dbartolovic | 8:7ba4f82de9b6 | 139 | |
dbartolovic | 8:7ba4f82de9b6 | 140 | void updateBuzzLedsF() |
dbartolovic | 8:7ba4f82de9b6 | 141 | { |
dbartolovic | 8:7ba4f82de9b6 | 142 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 143 | { |
dbartolovic | 8:7ba4f82de9b6 | 144 | Thread::signal_wait(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 145 | updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS); |
jurica238814 | 19:2681edc2f2b9 | 146 | |
dbartolovic | 8:7ba4f82de9b6 | 147 | if (buzzer.get_state() != (lizzy_service->get_buzz_state())) |
dbartolovic | 8:7ba4f82de9b6 | 148 | { |
dbartolovic | 8:7ba4f82de9b6 | 149 | if (lizzy_service->get_buzz_state()) |
dbartolovic | 8:7ba4f82de9b6 | 150 | buzzer.enable(); |
dbartolovic | 8:7ba4f82de9b6 | 151 | else |
dbartolovic | 8:7ba4f82de9b6 | 152 | buzzer.disable(); |
dbartolovic | 8:7ba4f82de9b6 | 153 | } |
dbartolovic | 8:7ba4f82de9b6 | 154 | if (!redLed != (lizzy_service->get_red_state())) |
dbartolovic | 8:7ba4f82de9b6 | 155 | { |
dbartolovic | 8:7ba4f82de9b6 | 156 | redLed = !(lizzy_service->get_red_state()); |
dbartolovic | 8:7ba4f82de9b6 | 157 | } |
dbartolovic | 8:7ba4f82de9b6 | 158 | if (!greenLed != (lizzy_service->get_green_state())) |
dbartolovic | 8:7ba4f82de9b6 | 159 | { |
dbartolovic | 8:7ba4f82de9b6 | 160 | greenLed = !(lizzy_service->get_green_state()); |
dbartolovic | 8:7ba4f82de9b6 | 161 | } |
dbartolovic | 8:7ba4f82de9b6 | 162 | if (!blueLed != (lizzy_service->get_blue_state())) |
dbartolovic | 8:7ba4f82de9b6 | 163 | { |
dbartolovic | 8:7ba4f82de9b6 | 164 | blueLed = !(lizzy_service->get_blue_state()); |
dbartolovic | 8:7ba4f82de9b6 | 165 | } |
dbartolovic | 8:7ba4f82de9b6 | 166 | } |
dbartolovic | 8:7ba4f82de9b6 | 167 | } |
dbartolovic | 8:7ba4f82de9b6 | 168 | |
dbartolovic | 8:7ba4f82de9b6 | 169 | void bleF(BLE *ble) |
dbartolovic | 8:7ba4f82de9b6 | 170 | { |
dbartolovic | 8:7ba4f82de9b6 | 171 | while(true) |
dbartolovic | 8:7ba4f82de9b6 | 172 | { |
dbartolovic | 8:7ba4f82de9b6 | 173 | Thread::signal_wait(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 174 | bleT.signal_clr(MEAS_DONE); |
jurica238814 | 19:2681edc2f2b9 | 175 | |
dbartolovic | 10:3c1e5bf6a00b | 176 | /* |
dbartolovic | 8:7ba4f82de9b6 | 177 | printf("%6d\t", advertisementPacket.accelerometer[0]); |
dbartolovic | 8:7ba4f82de9b6 | 178 | printf("%6d\t", advertisementPacket.accelerometer[1]); |
dbartolovic | 8:7ba4f82de9b6 | 179 | printf("%6d\r\n", advertisementPacket.accelerometer[2]); |
dbartolovic | 10:3c1e5bf6a00b | 180 | */ |
dbartolovic | 8:7ba4f82de9b6 | 181 | |
dbartolovic | 8:7ba4f82de9b6 | 182 | //updatePayload(ble, &advertisementPacket); |
jurica238814 | 19:2681edc2f2b9 | 183 | |
dbartolovic | 8:7ba4f82de9b6 | 184 | /* setup advertising */ |
jurica238814 | 19:2681edc2f2b9 | 185 | |
jurica238814 | 19:2681edc2f2b9 | 186 | |
dbartolovic | 8:7ba4f82de9b6 | 187 | /* |
dbartolovic | 8:7ba4f82de9b6 | 188 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 189 | advetisementData = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 190 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
dbartolovic | 8:7ba4f82de9b6 | 191 | ble->setAdvertisingData(advetisementData); |
dbartolovic | 8:7ba4f82de9b6 | 192 | */ |
dbartolovic | 8:7ba4f82de9b6 | 193 | adv_data = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 194 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
dbartolovic | 8:7ba4f82de9b6 | 195 | ble->setAdvertisingData(adv_data); |
jurica238814 | 19:2681edc2f2b9 | 196 | |
jurica238814 | 19:2681edc2f2b9 | 197 | |
dbartolovic | 8:7ba4f82de9b6 | 198 | //printf("Ble advertisement is ON.\r\n"); |
jurica238814 | 19:2681edc2f2b9 | 199 | |
dbartolovic | 8:7ba4f82de9b6 | 200 | #if DEBUG_LED |
dbartolovic | 8:7ba4f82de9b6 | 201 | redLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 202 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 203 | //ble->gap().startAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 204 | //wait_ms(1000); |
jurica238814 | 19:2681edc2f2b9 | 205 | |
dbartolovic | 8:7ba4f82de9b6 | 206 | //wait_ms(MEASURE_INTERVAL_MS); |
dbartolovic | 8:7ba4f82de9b6 | 207 | //wait_ms(1000); |
jurica238814 | 19:2681edc2f2b9 | 208 | |
dbartolovic | 8:7ba4f82de9b6 | 209 | //printf("Ble advertisement is OFF.\r\n\r\n"); |
dbartolovic | 8:7ba4f82de9b6 | 210 | #if DEBUG_LED |
dbartolovic | 8:7ba4f82de9b6 | 211 | redLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 212 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 213 | //ble->gap().stopAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 214 | /* |
dbartolovic | 8:7ba4f82de9b6 | 215 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 216 | advetisementData = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 217 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
dbartolovic | 8:7ba4f82de9b6 | 218 | ble->setAdvertisingData(advetisementData); |
dbartolovic | 8:7ba4f82de9b6 | 219 | */ |
dbartolovic | 8:7ba4f82de9b6 | 220 | //wait_ms(100); |
dbartolovic | 8:7ba4f82de9b6 | 221 | } |
jurica238814 | 19:2681edc2f2b9 | 222 | } |