Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Committer:
dbartolovic
Date:
Wed Aug 29 14:48:43 2018 +0000
Branch:
axis_normal
Revision:
15:c0c01188a29b
Parent:
13:2e964fa2a632
Child:
16:482d8f81d6f3
Brought consumption to 500uA

Who changed what in which revision?

UserRevisionLine numberNew 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 15:c0c01188a29b 125 wait_ms(MEASURE_INTERVAL_MS);
dbartolovic 15:c0c01188a29b 126 untilSleep--;
dbartolovic 15:c0c01188a29b 127
dbartolovic 15:c0c01188a29b 128 if(untilSleep <= 0)
dbartolovic 15:c0c01188a29b 129 {
dbartolovic 15:c0c01188a29b 130 BLE::Instance().stopAdvertising();
dbartolovic 15:c0c01188a29b 131 break;
dbartolovic 15:c0c01188a29b 132 }
dbartolovic 15:c0c01188a29b 133 }
dbartolovic 8:7ba4f82de9b6 134 }
dbartolovic 8:7ba4f82de9b6 135 }
dbartolovic 8:7ba4f82de9b6 136
dbartolovic 8:7ba4f82de9b6 137 void updateServiceF()
dbartolovic 8:7ba4f82de9b6 138 {
dbartolovic 8:7ba4f82de9b6 139 while (1)
dbartolovic 8:7ba4f82de9b6 140 {
dbartolovic 8:7ba4f82de9b6 141 Thread::signal_wait(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 142 updateServiceT.signal_clr(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 143
dbartolovic 8:7ba4f82de9b6 144 lizzy_service->set_acc_data(advertisementPacket.accelerometer);
dbartolovic 8:7ba4f82de9b6 145 }
dbartolovic 8:7ba4f82de9b6 146 }
dbartolovic 8:7ba4f82de9b6 147
dbartolovic 8:7ba4f82de9b6 148 void updateBuzzLedsF()
dbartolovic 8:7ba4f82de9b6 149 {
dbartolovic 8:7ba4f82de9b6 150 while (1)
dbartolovic 8:7ba4f82de9b6 151 {
dbartolovic 8:7ba4f82de9b6 152 Thread::signal_wait(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 153 updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 154
dbartolovic 8:7ba4f82de9b6 155 if (buzzer.get_state() != (lizzy_service->get_buzz_state()))
dbartolovic 8:7ba4f82de9b6 156 {
dbartolovic 8:7ba4f82de9b6 157 if (lizzy_service->get_buzz_state())
dbartolovic 8:7ba4f82de9b6 158 buzzer.enable();
dbartolovic 8:7ba4f82de9b6 159 else
dbartolovic 8:7ba4f82de9b6 160 buzzer.disable();
dbartolovic 8:7ba4f82de9b6 161 }
dbartolovic 8:7ba4f82de9b6 162 if (!redLed != (lizzy_service->get_red_state()))
dbartolovic 8:7ba4f82de9b6 163 {
dbartolovic 8:7ba4f82de9b6 164 redLed = !(lizzy_service->get_red_state());
dbartolovic 8:7ba4f82de9b6 165 }
dbartolovic 8:7ba4f82de9b6 166 if (!greenLed != (lizzy_service->get_green_state()))
dbartolovic 8:7ba4f82de9b6 167 {
dbartolovic 8:7ba4f82de9b6 168 greenLed = !(lizzy_service->get_green_state());
dbartolovic 8:7ba4f82de9b6 169 }
dbartolovic 8:7ba4f82de9b6 170 if (!blueLed != (lizzy_service->get_blue_state()))
dbartolovic 8:7ba4f82de9b6 171 {
dbartolovic 8:7ba4f82de9b6 172 blueLed = !(lizzy_service->get_blue_state());
dbartolovic 8:7ba4f82de9b6 173 }
dbartolovic 8:7ba4f82de9b6 174 }
dbartolovic 8:7ba4f82de9b6 175 }
dbartolovic 8:7ba4f82de9b6 176
dbartolovic 8:7ba4f82de9b6 177 void bleF(BLE *ble)
dbartolovic 8:7ba4f82de9b6 178 {
dbartolovic 8:7ba4f82de9b6 179 while(true)
dbartolovic 8:7ba4f82de9b6 180 {
dbartolovic 8:7ba4f82de9b6 181 Thread::signal_wait(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 182 bleT.signal_clr(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 183
dbartolovic 10:3c1e5bf6a00b 184 /*
dbartolovic 8:7ba4f82de9b6 185 printf("%6d\t", advertisementPacket.accelerometer[0]);
dbartolovic 8:7ba4f82de9b6 186 printf("%6d\t", advertisementPacket.accelerometer[1]);
dbartolovic 8:7ba4f82de9b6 187 printf("%6d\r\n", advertisementPacket.accelerometer[2]);
dbartolovic 10:3c1e5bf6a00b 188 */
dbartolovic 8:7ba4f82de9b6 189
dbartolovic 8:7ba4f82de9b6 190
dbartolovic 8:7ba4f82de9b6 191 /* setup advertising */
dbartolovic 8:7ba4f82de9b6 192
dbartolovic 8:7ba4f82de9b6 193
dbartolovic 8:7ba4f82de9b6 194 /*
dbartolovic 8:7ba4f82de9b6 195 GapAdvertisingData advetisementData = GapAdvertisingData();
dbartolovic 8:7ba4f82de9b6 196 advetisementData = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 197 advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
dbartolovic 8:7ba4f82de9b6 198 ble->setAdvertisingData(advetisementData);
dbartolovic 8:7ba4f82de9b6 199 */
dbartolovic 8:7ba4f82de9b6 200 adv_data = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 201 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
dbartolovic 8:7ba4f82de9b6 202 ble->setAdvertisingData(adv_data);
dbartolovic 8:7ba4f82de9b6 203
dbartolovic 8:7ba4f82de9b6 204
dbartolovic 8:7ba4f82de9b6 205 //printf("Ble advertisement is ON.\r\n");
dbartolovic 8:7ba4f82de9b6 206
dbartolovic 8:7ba4f82de9b6 207 #if DEBUG_LED
dbartolovic 8:7ba4f82de9b6 208 redLed = 0;
dbartolovic 8:7ba4f82de9b6 209 #endif
dbartolovic 8:7ba4f82de9b6 210 //ble->gap().startAdvertising();
dbartolovic 8:7ba4f82de9b6 211 //wait_ms(1000);
dbartolovic 8:7ba4f82de9b6 212
dbartolovic 8:7ba4f82de9b6 213 //wait_ms(MEASURE_INTERVAL_MS);
dbartolovic 8:7ba4f82de9b6 214 //wait_ms(1000);
dbartolovic 8:7ba4f82de9b6 215
dbartolovic 8:7ba4f82de9b6 216 //printf("Ble advertisement is OFF.\r\n\r\n");
dbartolovic 8:7ba4f82de9b6 217 #if DEBUG_LED
dbartolovic 8:7ba4f82de9b6 218 redLed = 1;
dbartolovic 8:7ba4f82de9b6 219 #endif
dbartolovic 8:7ba4f82de9b6 220 //ble->gap().stopAdvertising();
dbartolovic 8:7ba4f82de9b6 221 /*
dbartolovic 8:7ba4f82de9b6 222 GapAdvertisingData advetisementData = GapAdvertisingData();
dbartolovic 8:7ba4f82de9b6 223 advetisementData = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 224 advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
dbartolovic 8:7ba4f82de9b6 225 ble->setAdvertisingData(advetisementData);
dbartolovic 8:7ba4f82de9b6 226 */
dbartolovic 8:7ba4f82de9b6 227 //wait_ms(100);
dbartolovic 8:7ba4f82de9b6 228 }
dbartolovic 8:7ba4f82de9b6 229 }