Lizzy project
Dependencies: aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT
tasks/tasks.cpp@10:3c1e5bf6a00b, 2018-03-21 (annotated)
- Committer:
- dbartolovic
- Date:
- Wed Mar 21 13:52:02 2018 +0000
- Revision:
- 10:3c1e5bf6a00b
- Parent:
- 9:aef8bb3d13ed
- Child:
- 11:4dc690157508
Minor tweaks
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 | 8:7ba4f82de9b6 | 89 | void measureF(Lis2dh12 *mems) |
dbartolovic | 8:7ba4f82de9b6 | 90 | { |
dbartolovic | 8:7ba4f82de9b6 | 91 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 92 | { |
dbartolovic | 8:7ba4f82de9b6 | 93 | advertisementPacket.header = APPLICATION_ID; |
dbartolovic | 8:7ba4f82de9b6 | 94 | advertisementPacket.type = 0x00; |
dbartolovic | 8:7ba4f82de9b6 | 95 | advertisementPacket.gyroscope[0] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 96 | advertisementPacket.gyroscope[1] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 97 | advertisementPacket.gyroscope[2] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 98 | advertisementPacket.magnetometer[0] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 99 | advertisementPacket.magnetometer[1] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 100 | advertisementPacket.magnetometer[2] = (int16_t)0; |
dbartolovic | 8:7ba4f82de9b6 | 101 | |
dbartolovic | 8:7ba4f82de9b6 | 102 | |
dbartolovic | 8:7ba4f82de9b6 | 103 | advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis(); |
dbartolovic | 8:7ba4f82de9b6 | 104 | advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis(); |
dbartolovic | 8:7ba4f82de9b6 | 105 | advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis(); |
dbartolovic | 8:7ba4f82de9b6 | 106 | |
dbartolovic | 8:7ba4f82de9b6 | 107 | //advertisementPacket.acc_lsb_value = 0xC000; |
dbartolovic | 9:aef8bb3d13ed | 108 | //advertisementPacket.acc_lsb_value = 0x0100; |
dbartolovic | 9:aef8bb3d13ed | 109 | advertisementPacket.acc_lsb_value = LSB_VALUE; |
dbartolovic | 8:7ba4f82de9b6 | 110 | |
dbartolovic | 8:7ba4f82de9b6 | 111 | updateServiceT.signal_set(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 112 | bleT.signal_set(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 113 | |
dbartolovic | 8:7ba4f82de9b6 | 114 | wait_ms(MEASURE_INTERVAL_MS); |
dbartolovic | 8:7ba4f82de9b6 | 115 | } |
dbartolovic | 8:7ba4f82de9b6 | 116 | } |
dbartolovic | 8:7ba4f82de9b6 | 117 | |
dbartolovic | 8:7ba4f82de9b6 | 118 | void updateServiceF() |
dbartolovic | 8:7ba4f82de9b6 | 119 | { |
dbartolovic | 8:7ba4f82de9b6 | 120 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 121 | { |
dbartolovic | 8:7ba4f82de9b6 | 122 | Thread::signal_wait(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 123 | updateServiceT.signal_clr(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 124 | |
dbartolovic | 8:7ba4f82de9b6 | 125 | lizzy_service->set_acc_data(advertisementPacket.accelerometer); |
dbartolovic | 8:7ba4f82de9b6 | 126 | } |
dbartolovic | 8:7ba4f82de9b6 | 127 | } |
dbartolovic | 8:7ba4f82de9b6 | 128 | |
dbartolovic | 8:7ba4f82de9b6 | 129 | void updateBuzzLedsF() |
dbartolovic | 8:7ba4f82de9b6 | 130 | { |
dbartolovic | 8:7ba4f82de9b6 | 131 | while (1) |
dbartolovic | 8:7ba4f82de9b6 | 132 | { |
dbartolovic | 8:7ba4f82de9b6 | 133 | Thread::signal_wait(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 134 | updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS); |
dbartolovic | 8:7ba4f82de9b6 | 135 | |
dbartolovic | 8:7ba4f82de9b6 | 136 | if (buzzer.get_state() != (lizzy_service->get_buzz_state())) |
dbartolovic | 8:7ba4f82de9b6 | 137 | { |
dbartolovic | 8:7ba4f82de9b6 | 138 | if (lizzy_service->get_buzz_state()) |
dbartolovic | 8:7ba4f82de9b6 | 139 | buzzer.enable(); |
dbartolovic | 8:7ba4f82de9b6 | 140 | else |
dbartolovic | 8:7ba4f82de9b6 | 141 | buzzer.disable(); |
dbartolovic | 8:7ba4f82de9b6 | 142 | } |
dbartolovic | 8:7ba4f82de9b6 | 143 | if (!redLed != (lizzy_service->get_red_state())) |
dbartolovic | 8:7ba4f82de9b6 | 144 | { |
dbartolovic | 8:7ba4f82de9b6 | 145 | redLed = !(lizzy_service->get_red_state()); |
dbartolovic | 8:7ba4f82de9b6 | 146 | } |
dbartolovic | 8:7ba4f82de9b6 | 147 | if (!greenLed != (lizzy_service->get_green_state())) |
dbartolovic | 8:7ba4f82de9b6 | 148 | { |
dbartolovic | 8:7ba4f82de9b6 | 149 | greenLed = !(lizzy_service->get_green_state()); |
dbartolovic | 8:7ba4f82de9b6 | 150 | } |
dbartolovic | 8:7ba4f82de9b6 | 151 | if (!blueLed != (lizzy_service->get_blue_state())) |
dbartolovic | 8:7ba4f82de9b6 | 152 | { |
dbartolovic | 8:7ba4f82de9b6 | 153 | blueLed = !(lizzy_service->get_blue_state()); |
dbartolovic | 8:7ba4f82de9b6 | 154 | } |
dbartolovic | 8:7ba4f82de9b6 | 155 | } |
dbartolovic | 8:7ba4f82de9b6 | 156 | } |
dbartolovic | 8:7ba4f82de9b6 | 157 | |
dbartolovic | 8:7ba4f82de9b6 | 158 | void bleF(BLE *ble) |
dbartolovic | 8:7ba4f82de9b6 | 159 | { |
dbartolovic | 8:7ba4f82de9b6 | 160 | while(true) |
dbartolovic | 8:7ba4f82de9b6 | 161 | { |
dbartolovic | 8:7ba4f82de9b6 | 162 | Thread::signal_wait(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 163 | bleT.signal_clr(MEAS_DONE); |
dbartolovic | 8:7ba4f82de9b6 | 164 | |
dbartolovic | 10:3c1e5bf6a00b | 165 | /* |
dbartolovic | 8:7ba4f82de9b6 | 166 | printf("%6d\t", advertisementPacket.accelerometer[0]); |
dbartolovic | 8:7ba4f82de9b6 | 167 | printf("%6d\t", advertisementPacket.accelerometer[1]); |
dbartolovic | 8:7ba4f82de9b6 | 168 | printf("%6d\r\n", advertisementPacket.accelerometer[2]); |
dbartolovic | 10:3c1e5bf6a00b | 169 | */ |
dbartolovic | 8:7ba4f82de9b6 | 170 | |
dbartolovic | 8:7ba4f82de9b6 | 171 | //updatePayload(ble, &advertisementPacket); |
dbartolovic | 8:7ba4f82de9b6 | 172 | |
dbartolovic | 8:7ba4f82de9b6 | 173 | /* setup advertising */ |
dbartolovic | 8:7ba4f82de9b6 | 174 | |
dbartolovic | 8:7ba4f82de9b6 | 175 | |
dbartolovic | 8:7ba4f82de9b6 | 176 | /* |
dbartolovic | 8:7ba4f82de9b6 | 177 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 178 | advetisementData = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 179 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
dbartolovic | 8:7ba4f82de9b6 | 180 | ble->setAdvertisingData(advetisementData); |
dbartolovic | 8:7ba4f82de9b6 | 181 | */ |
dbartolovic | 8:7ba4f82de9b6 | 182 | adv_data = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 183 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
dbartolovic | 8:7ba4f82de9b6 | 184 | ble->setAdvertisingData(adv_data); |
dbartolovic | 8:7ba4f82de9b6 | 185 | |
dbartolovic | 8:7ba4f82de9b6 | 186 | |
dbartolovic | 8:7ba4f82de9b6 | 187 | //printf("Ble advertisement is ON.\r\n"); |
dbartolovic | 8:7ba4f82de9b6 | 188 | |
dbartolovic | 8:7ba4f82de9b6 | 189 | #if DEBUG_LED |
dbartolovic | 8:7ba4f82de9b6 | 190 | redLed = 0; |
dbartolovic | 8:7ba4f82de9b6 | 191 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 192 | //ble->gap().startAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 193 | //wait_ms(1000); |
dbartolovic | 8:7ba4f82de9b6 | 194 | |
dbartolovic | 8:7ba4f82de9b6 | 195 | //wait_ms(MEASURE_INTERVAL_MS); |
dbartolovic | 8:7ba4f82de9b6 | 196 | //wait_ms(1000); |
dbartolovic | 8:7ba4f82de9b6 | 197 | |
dbartolovic | 8:7ba4f82de9b6 | 198 | //printf("Ble advertisement is OFF.\r\n\r\n"); |
dbartolovic | 8:7ba4f82de9b6 | 199 | #if DEBUG_LED |
dbartolovic | 8:7ba4f82de9b6 | 200 | redLed = 1; |
dbartolovic | 8:7ba4f82de9b6 | 201 | #endif |
dbartolovic | 8:7ba4f82de9b6 | 202 | //ble->gap().stopAdvertising(); |
dbartolovic | 8:7ba4f82de9b6 | 203 | /* |
dbartolovic | 8:7ba4f82de9b6 | 204 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 205 | advetisementData = ble->getAdvertisingData(); |
dbartolovic | 8:7ba4f82de9b6 | 206 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
dbartolovic | 8:7ba4f82de9b6 | 207 | ble->setAdvertisingData(advetisementData); |
dbartolovic | 8:7ba4f82de9b6 | 208 | */ |
dbartolovic | 8:7ba4f82de9b6 | 209 | //wait_ms(100); |
dbartolovic | 8:7ba4f82de9b6 | 210 | } |
dbartolovic | 8:7ba4f82de9b6 | 211 | } |