Lizzy project
Dependencies: aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT
tasks/tasks.cpp@16:482d8f81d6f3, 2018-08-29 (annotated)
- Committer:
- dbartolovic
- Date:
- Wed Aug 29 17:08:08 2018 +0000
- Branch:
- axis_normal
- Revision:
- 16:482d8f81d6f3
- Parent:
- 15:c0c01188a29b
- Child:
- 23:bf9c1dd2b5cb
Debuged wake on shake. If consumption in sleep is half mA make sure mbed-os version is 5.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dbartolovic | 8:7ba4f82de9b6 | 1 | #include "main.h" |
dbartolovic | 8:7ba4f82de9b6 | 2 | #include "bsp_buzz.h" |
dbartolovic | 8:7ba4f82de9b6 | 3 | #include "bsp_led.h" |
dbartolovic | 8:7ba4f82de9b6 | 4 | #include "aconno_ble.h" |
dbartolovic | 8:7ba4f82de9b6 | 5 | #include "tasks.h" |
dbartolovic | 8:7ba4f82de9b6 | 6 | #include "GapAdvertisingData.h" |
dbartolovic | 8:7ba4f82de9b6 | 7 | |
dbartolovic | 8:7ba4f82de9b6 | 8 | |
dbartolovic | 8:7ba4f82de9b6 | 9 | DigitalOut redLed(RED_LED_PIN); |
dbartolovic | 8:7ba4f82de9b6 | 10 | #if NANO_MODULE == 0 |
dbartolovic | 8:7ba4f82de9b6 | 11 | DigitalOut greenLed(GREEN_LED_PIN); |
dbartolovic | 8:7ba4f82de9b6 | 12 | DigitalOut blueLed(BLUE_LED_PIN); |
dbartolovic | 8:7ba4f82de9b6 | 13 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 14 | |
dbartolovic | 8:7ba4f82de9b6 | 15 | Buzz buzzer(NRF_PWM2, BUZZER_PIN); |
dbartolovic | 8:7ba4f82de9b6 | 16 | |
dbartolovic | 8:7ba4f82de9b6 | 17 | static advertising_packet advertisementPacket; |
dbartolovic | 8:7ba4f82de9b6 | 18 | static GapAdvertisingData adv_data = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 19 | |
dbartolovic | 8:7ba4f82de9b6 | 20 | |
dbartolovic | 8:7ba4f82de9b6 | 21 | #if TEST_LEDS_BUZZ |
dbartolovic | 8:7ba4f82de9b6 | 22 | Ticker test_ticker; |
dbartolovic | 8:7ba4f82de9b6 | 23 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 24 | |
dbartolovic | 8:7ba4f82de9b6 | 25 | |
dbartolovic | 8:7ba4f82de9b6 | 26 | void tasks_init() |
dbartolovic | 8:7ba4f82de9b6 | 27 | { |
dbartolovic | 8:7ba4f82de9b6 | 28 | redLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 29 | greenLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 30 | blueLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 31 | |
dbartolovic | 8:7ba4f82de9b6 | 32 | #if TEST_LEDS_BUZZ |
dbartolovic | 8:7ba4f82de9b6 | 33 | test_ticker.attach(led_tick, 0.5); |
dbartolovic | 8:7ba4f82de9b6 | 34 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 35 | } |
dbartolovic | 8:7ba4f82de9b6 | 36 | |
dbartolovic | 8:7ba4f82de9b6 | 37 | #if TEST_LEDS_BUZZ |
dbartolovic | 8:7ba4f82de9b6 | 38 | void buzz_tick() |
dbartolovic | 8:7ba4f82de9b6 | 39 | { |
dbartolovic | 8:7ba4f82de9b6 | 40 | static int start = 1; |
dbartolovic | 8:7ba4f82de9b6 | 41 | |
dbartolovic | 8:7ba4f82de9b6 | 42 | if (start) |
dbartolovic | 8:7ba4f82de9b6 | 43 | { |
dbartolovic | 8:7ba4f82de9b6 | 44 | buzzer.enable(); |
dbartolovic | 8:7ba4f82de9b6 | 45 | start = 0; |
dbartolovic | 8:7ba4f82de9b6 | 46 | } |
dbartolovic | 8:7ba4f82de9b6 | 47 | else |
dbartolovic | 8:7ba4f82de9b6 | 48 | { |
dbartolovic | 8:7ba4f82de9b6 | 49 | buzzer.disable(); |
dbartolovic | 8:7ba4f82de9b6 | 50 | start = 1; |
dbartolovic | 8:7ba4f82de9b6 | 51 | led_tick(); |
dbartolovic | 8:7ba4f82de9b6 | 52 | test_ticker.detach(); |
dbartolovic | 8:7ba4f82de9b6 | 53 | test_ticker.attach(led_tick, 0.5); |
dbartolovic | 8:7ba4f82de9b6 | 54 | } |
dbartolovic | 8:7ba4f82de9b6 | 55 | } |
dbartolovic | 8:7ba4f82de9b6 | 56 | |
dbartolovic | 8:7ba4f82de9b6 | 57 | void led_tick() |
dbartolovic | 8:7ba4f82de9b6 | 58 | { |
dbartolovic | 8:7ba4f82de9b6 | 59 | static int count = 0; |
dbartolovic | 8:7ba4f82de9b6 | 60 | |
dbartolovic | 8:7ba4f82de9b6 | 61 | switch(count) |
dbartolovic | 8:7ba4f82de9b6 | 62 | { |
dbartolovic | 8:7ba4f82de9b6 | 63 | case 0: |
dbartolovic | 8:7ba4f82de9b6 | 64 | redLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 65 | break; |
dbartolovic | 8:7ba4f82de9b6 | 66 | |
dbartolovic | 8:7ba4f82de9b6 | 67 | case 1: |
dbartolovic | 8:7ba4f82de9b6 | 68 | redLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 69 | greenLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 70 | break; |
dbartolovic | 8:7ba4f82de9b6 | 71 | |
dbartolovic | 8:7ba4f82de9b6 | 72 | case 2: |
dbartolovic | 8:7ba4f82de9b6 | 73 | greenLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 74 | blueLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 75 | break; |
dbartolovic | 8:7ba4f82de9b6 | 76 | |
dbartolovic | 8:7ba4f82de9b6 | 77 | default: |
dbartolovic | 8:7ba4f82de9b6 | 78 | blueLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 79 | count = -1; |
dbartolovic | 8:7ba4f82de9b6 | 80 | buzz_tick(); |
dbartolovic | 8:7ba4f82de9b6 | 81 | test_ticker.detach(); |
dbartolovic | 8:7ba4f82de9b6 | 82 | test_ticker.attach(buzz_tick, BUZZ_TIME_S); |
dbartolovic | 8:7ba4f82de9b6 | 83 | } |
dbartolovic | 8:7ba4f82de9b6 | 84 | |
dbartolovic | 8:7ba4f82de9b6 | 85 | count++; |
dbartolovic | 8:7ba4f82de9b6 | 86 | } |
dbartolovic | 8:7ba4f82de9b6 | 87 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 88 | |
dbartolovic | 15:c0c01188a29b | 89 | int untilSleep = ACTIVE_PERIOD / MEASURE_INTERVAL_MS; |
dbartolovic | 8:7ba4f82de9b6 | 90 | void measureF(Lis2dh12 *mems) |
dbartolovic | 8:7ba4f82de9b6 | 91 | { |
dbartolovic | 15:c0c01188a29b | 92 | while(1) |
dbartolovic | 8:7ba4f82de9b6 | 93 | { |
dbartolovic | 15:c0c01188a29b | 94 | Thread::signal_wait(START_MEAS); |
dbartolovic | 15:c0c01188a29b | 95 | Thread::signal_clr(START_MEAS); |
dbartolovic | 15:c0c01188a29b | 96 | |
dbartolovic | 15:c0c01188a29b | 97 | BLE::Instance().startAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 98 | |
dbartolovic | 15:c0c01188a29b | 99 | while (1) |
dbartolovic | 15:c0c01188a29b | 100 | { |
dbartolovic | 15:c0c01188a29b | 101 | advertisementPacket.header = APPLICATION_ID; |
dbartolovic | 15:c0c01188a29b | 102 | advertisementPacket.type = 0x00; |
dbartolovic | 15:c0c01188a29b | 103 | advertisementPacket.gyroscope[0] = (int16_t)0; |
dbartolovic | 15:c0c01188a29b | 104 | advertisementPacket.gyroscope[1] = (int16_t)0; |
dbartolovic | 15:c0c01188a29b | 105 | advertisementPacket.gyroscope[2] = (int16_t)0; |
dbartolovic | 15:c0c01188a29b | 106 | advertisementPacket.magnetometer[0] = (int16_t)0; |
dbartolovic | 15:c0c01188a29b | 107 | advertisementPacket.magnetometer[1] = (int16_t)0; |
dbartolovic | 15:c0c01188a29b | 108 | advertisementPacket.magnetometer[2] = (int16_t)0; |
dbartolovic | 15:c0c01188a29b | 109 | |
dbartolovic | 15:c0c01188a29b | 110 | #if NORMAL_AXIS == 1 |
dbartolovic | 15:c0c01188a29b | 111 | advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis(); |
dbartolovic | 15:c0c01188a29b | 112 | advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis(); |
dbartolovic | 15:c0c01188a29b | 113 | advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis(); |
dbartolovic | 15:c0c01188a29b | 114 | #else |
dbartolovic | 15:c0c01188a29b | 115 | advertisementPacket.accelerometer[0] = -(int16_t)mems->readYAxis(); |
dbartolovic | 15:c0c01188a29b | 116 | advertisementPacket.accelerometer[1] = (int16_t)mems->readXAxis(); |
dbartolovic | 15:c0c01188a29b | 117 | advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis(); |
dbartolovic | 15:c0c01188a29b | 118 | #endif |
dbartolovic | 15:c0c01188a29b | 119 | |
dbartolovic | 15:c0c01188a29b | 120 | advertisementPacket.acc_lsb_value = LSB_VALUE; |
dbartolovic | 15:c0c01188a29b | 121 | |
dbartolovic | 15:c0c01188a29b | 122 | updateServiceT.signal_set(MEAS_DONE); |
dbartolovic | 15:c0c01188a29b | 123 | bleT.signal_set(MEAS_DONE); |
dbartolovic | 15:c0c01188a29b | 124 | |
dbartolovic | 16:482d8f81d6f3 | 125 | //wait_ms(MEASURE_INTERVAL_MS); |
dbartolovic | 16:482d8f81d6f3 | 126 | Thread::wait(MEASURE_INTERVAL_MS); |
dbartolovic | 15:c0c01188a29b | 127 | untilSleep--; |
dbartolovic | 15:c0c01188a29b | 128 | |
dbartolovic | 16:482d8f81d6f3 | 129 | if( (untilSleep <= 0) && !bleIsClientConnected() ) |
dbartolovic | 15:c0c01188a29b | 130 | { |
dbartolovic | 15:c0c01188a29b | 131 | BLE::Instance().stopAdvertising(); |
dbartolovic | 15:c0c01188a29b | 132 | break; |
dbartolovic | 15:c0c01188a29b | 133 | } |
dbartolovic | 15:c0c01188a29b | 134 | } |
dbartolovic | 8:7ba4f82de9b6 | 135 | } |
dbartolovic | 8:7ba4f82de9b6 | 136 | } |
dbartolovic | 8:7ba4f82de9b6 | 137 | |
dbartolovic | 8:7ba4f82de9b6 | 138 | void updateServiceF() |
dbartolovic | 8:7ba4f82de9b6 | 139 | { |
dbartolovic | 8:7ba4f82de9b6 | 140 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 141 | { |
dbartolovic | 8:7ba4f82de9b6 | 142 | Thread::signal_wait(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 143 | updateServiceT.signal_clr(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 144 | |
dbartolovic | 8:7ba4f82de9b6 | 145 | lizzy_service->set_acc_data(advertisementPacket.accelerometer); |
dbartolovic | 8:7ba4f82de9b6 | 146 | } |
dbartolovic | 8:7ba4f82de9b6 | 147 | } |
dbartolovic | 8:7ba4f82de9b6 | 148 | |
dbartolovic | 8:7ba4f82de9b6 | 149 | void updateBuzzLedsF() |
dbartolovic | 8:7ba4f82de9b6 | 150 | { |
dbartolovic | 8:7ba4f82de9b6 | 151 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 152 | { |
dbartolovic | 8:7ba4f82de9b6 | 153 | Thread::signal_wait(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 154 | updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 155 | |
dbartolovic | 8:7ba4f82de9b6 | 156 | if (buzzer.get_state() != (lizzy_service->get_buzz_state())) |
dbartolovic | 8:7ba4f82de9b6 | 157 | { |
dbartolovic | 8:7ba4f82de9b6 | 158 | if (lizzy_service->get_buzz_state()) |
dbartolovic | 8:7ba4f82de9b6 | 159 | buzzer.enable(); |
dbartolovic | 8:7ba4f82de9b6 | 160 | else |
dbartolovic | 8:7ba4f82de9b6 | 161 | buzzer.disable(); |
dbartolovic | 8:7ba4f82de9b6 | 162 | } |
dbartolovic | 8:7ba4f82de9b6 | 163 | if (!redLed != (lizzy_service->get_red_state())) |
dbartolovic | 8:7ba4f82de9b6 | 164 | { |
dbartolovic | 8:7ba4f82de9b6 | 165 | redLed = !(lizzy_service->get_red_state()); |
dbartolovic | 8:7ba4f82de9b6 | 166 | } |
dbartolovic | 8:7ba4f82de9b6 | 167 | if (!greenLed != (lizzy_service->get_green_state())) |
dbartolovic | 8:7ba4f82de9b6 | 168 | { |
dbartolovic | 8:7ba4f82de9b6 | 169 | greenLed = !(lizzy_service->get_green_state()); |
dbartolovic | 8:7ba4f82de9b6 | 170 | } |
dbartolovic | 8:7ba4f82de9b6 | 171 | if (!blueLed != (lizzy_service->get_blue_state())) |
dbartolovic | 8:7ba4f82de9b6 | 172 | { |
dbartolovic | 8:7ba4f82de9b6 | 173 | blueLed = !(lizzy_service->get_blue_state()); |
dbartolovic | 8:7ba4f82de9b6 | 174 | } |
dbartolovic | 8:7ba4f82de9b6 | 175 | } |
dbartolovic | 8:7ba4f82de9b6 | 176 | } |
dbartolovic | 8:7ba4f82de9b6 | 177 | |
dbartolovic | 8:7ba4f82de9b6 | 178 | void bleF(BLE *ble) |
dbartolovic | 8:7ba4f82de9b6 | 179 | { |
dbartolovic | 8:7ba4f82de9b6 | 180 | while(true) |
dbartolovic | 8:7ba4f82de9b6 | 181 | { |
dbartolovic | 8:7ba4f82de9b6 | 182 | Thread::signal_wait(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 183 | bleT.signal_clr(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 184 | |
dbartolovic | 10:3c1e5bf6a00b | 185 | /* |
dbartolovic | 8:7ba4f82de9b6 | 186 | printf("%6d\t", advertisementPacket.accelerometer[0]); |
dbartolovic | 8:7ba4f82de9b6 | 187 | printf("%6d\t", advertisementPacket.accelerometer[1]); |
dbartolovic | 8:7ba4f82de9b6 | 188 | printf("%6d\r\n", advertisementPacket.accelerometer[2]); |
dbartolovic | 10:3c1e5bf6a00b | 189 | */ |
dbartolovic | 8:7ba4f82de9b6 | 190 | |
dbartolovic | 8:7ba4f82de9b6 | 191 | |
dbartolovic | 8:7ba4f82de9b6 | 192 | /* setup advertising */ |
dbartolovic | 8:7ba4f82de9b6 | 193 | |
dbartolovic | 8:7ba4f82de9b6 | 194 | |
dbartolovic | 8:7ba4f82de9b6 | 195 | /* |
dbartolovic | 8:7ba4f82de9b6 | 196 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 197 | advetisementData = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 198 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
dbartolovic | 8:7ba4f82de9b6 | 199 | ble->setAdvertisingData(advetisementData); |
dbartolovic | 8:7ba4f82de9b6 | 200 | */ |
dbartolovic | 8:7ba4f82de9b6 | 201 | adv_data = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 202 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
dbartolovic | 8:7ba4f82de9b6 | 203 | ble->setAdvertisingData(adv_data); |
dbartolovic | 8:7ba4f82de9b6 | 204 | |
dbartolovic | 8:7ba4f82de9b6 | 205 | |
dbartolovic | 8:7ba4f82de9b6 | 206 | //printf("Ble advertisement is ON.\r\n"); |
dbartolovic | 8:7ba4f82de9b6 | 207 | |
dbartolovic | 8:7ba4f82de9b6 | 208 | #if DEBUG_LED |
dbartolovic | 8:7ba4f82de9b6 | 209 | redLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 210 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 211 | //ble->gap().startAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 212 | //wait_ms(1000); |
dbartolovic | 8:7ba4f82de9b6 | 213 | |
dbartolovic | 8:7ba4f82de9b6 | 214 | //wait_ms(MEASURE_INTERVAL_MS); |
dbartolovic | 8:7ba4f82de9b6 | 215 | //wait_ms(1000); |
dbartolovic | 8:7ba4f82de9b6 | 216 | |
dbartolovic | 8:7ba4f82de9b6 | 217 | //printf("Ble advertisement is OFF.\r\n\r\n"); |
dbartolovic | 8:7ba4f82de9b6 | 218 | #if DEBUG_LED |
dbartolovic | 8:7ba4f82de9b6 | 219 | redLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 220 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 221 | //ble->gap().stopAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 222 | /* |
dbartolovic | 8:7ba4f82de9b6 | 223 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 224 | advetisementData = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 225 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
dbartolovic | 8:7ba4f82de9b6 | 226 | ble->setAdvertisingData(advetisementData); |
dbartolovic | 8:7ba4f82de9b6 | 227 | */ |
dbartolovic | 8:7ba4f82de9b6 | 228 | //wait_ms(100); |
dbartolovic | 8:7ba4f82de9b6 | 229 | } |
dbartolovic | 16:482d8f81d6f3 | 230 | } |
dbartolovic | 16:482d8f81d6f3 | 231 | |
dbartolovic | 16:482d8f81d6f3 | 232 | void servLoopC(void) |
dbartolovic | 16:482d8f81d6f3 | 233 | { |
dbartolovic | 16:482d8f81d6f3 | 234 | while(1) |
dbartolovic | 16:482d8f81d6f3 | 235 | { |
dbartolovic | 16:482d8f81d6f3 | 236 | getBLEEventQueue()->dispatch_forever(); |
dbartolovic | 16:482d8f81d6f3 | 237 | Thread::wait(0xFFFFFFFF); |
dbartolovic | 16:482d8f81d6f3 | 238 | } |
dbartolovic | 8:7ba4f82de9b6 | 239 | } |