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