Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Committer:
dbartolovic
Date:
Thu Sep 20 13:20:13 2018 +0000
Branch:
master
Revision:
26:6101bb09f70d
Parent:
24:7539258b015c
Child:
29:b021b33cf666
Added vodafone compatibility, it can be enabled or disabled with a macro

Who changed what in which revision?

UserRevisionLine numberNew 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 26:6101bb09f70d 173 #if VODAFONE_COMPATIBILITY == 1
dbartolovic 26:6101bb09f70d 174 const uint8_t startBuzz = 0xBA;
dbartolovic 26:6101bb09f70d 175 #endif
dbartolovic 26:6101bb09f70d 176
dbartolovic 8:7ba4f82de9b6 177 while (1)
dbartolovic 8:7ba4f82de9b6 178 {
dbartolovic 8:7ba4f82de9b6 179 Thread::signal_wait(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 180 updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS);
dbartolovic 26:6101bb09f70d 181
dbartolovic 26:6101bb09f70d 182 #if VODAFONE_COMPATIBILITY == 1
dbartolovic 26:6101bb09f70d 183 uint8_t tmpBuzzState[4];
dbartolovic 26:6101bb09f70d 184 if( buzzer.get_state() !=
dbartolovic 26:6101bb09f70d 185 (lizzy_service->getBuzz(tmpBuzzState)[0] == startBuzz) )
dbartolovic 26:6101bb09f70d 186 #else
dbartolovic 26:6101bb09f70d 187 if( buzzer.get_state() != (lizzy_service->getBuzz()) )
dbartolovic 26:6101bb09f70d 188 #endif
dbartolovic 8:7ba4f82de9b6 189 {
dbartolovic 26:6101bb09f70d 190 #if VODAFONE_COMPATIBILITY == 1
dbartolovic 26:6101bb09f70d 191 if( tmpBuzzState[0] == startBuzz )
dbartolovic 26:6101bb09f70d 192 #else
dbartolovic 26:6101bb09f70d 193 if( lizzy_service->getBuzz() )
dbartolovic 26:6101bb09f70d 194 #endif
dbartolovic 8:7ba4f82de9b6 195 buzzer.enable();
dbartolovic 8:7ba4f82de9b6 196 else
dbartolovic 8:7ba4f82de9b6 197 buzzer.disable();
dbartolovic 8:7ba4f82de9b6 198 }
dbartolovic 26:6101bb09f70d 199 #if VODAFONE_COMPATIBILITY == 1
dbartolovic 26:6101bb09f70d 200 #else
dbartolovic 26:6101bb09f70d 201 if( !redLed != (lizzy_service->getRedLed()) )
dbartolovic 8:7ba4f82de9b6 202 {
dbartolovic 26:6101bb09f70d 203 redLed = !(lizzy_service->getRedLed());
dbartolovic 8:7ba4f82de9b6 204 }
dbartolovic 26:6101bb09f70d 205 #endif
dbartolovic 26:6101bb09f70d 206 if( !greenLed != (lizzy_service->get_green_state()) )
dbartolovic 8:7ba4f82de9b6 207 {
dbartolovic 8:7ba4f82de9b6 208 greenLed = !(lizzy_service->get_green_state());
dbartolovic 8:7ba4f82de9b6 209 }
dbartolovic 26:6101bb09f70d 210 if( !blueLed != (lizzy_service->get_blue_state()) )
dbartolovic 8:7ba4f82de9b6 211 {
dbartolovic 8:7ba4f82de9b6 212 blueLed = !(lizzy_service->get_blue_state());
dbartolovic 8:7ba4f82de9b6 213 }
dbartolovic 8:7ba4f82de9b6 214 }
dbartolovic 8:7ba4f82de9b6 215 }
dbartolovic 8:7ba4f82de9b6 216
dbartolovic 8:7ba4f82de9b6 217 void bleF(BLE *ble)
dbartolovic 8:7ba4f82de9b6 218 {
dbartolovic 8:7ba4f82de9b6 219 while(true)
dbartolovic 8:7ba4f82de9b6 220 {
dbartolovic 8:7ba4f82de9b6 221 Thread::signal_wait(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 222 bleT.signal_clr(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 223
dbartolovic 26:6101bb09f70d 224 #if VODAFONE_COMPATIBILITY == 1
dbartolovic 26:6101bb09f70d 225 #else
dbartolovic 8:7ba4f82de9b6 226 adv_data = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 227 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
dbartolovic 8:7ba4f82de9b6 228 ble->setAdvertisingData(adv_data);
dbartolovic 26:6101bb09f70d 229 #endif
dbartolovic 26:6101bb09f70d 230
dbartolovic 8:7ba4f82de9b6 231 }
dbartolovic 16:482d8f81d6f3 232 }
dbartolovic 16:482d8f81d6f3 233
dbartolovic 16:482d8f81d6f3 234 void servLoopC(void)
dbartolovic 16:482d8f81d6f3 235 {
dbartolovic 16:482d8f81d6f3 236 while(1)
dbartolovic 16:482d8f81d6f3 237 {
dbartolovic 16:482d8f81d6f3 238 getBLEEventQueue()->dispatch_forever();
dbartolovic 16:482d8f81d6f3 239 Thread::wait(0xFFFFFFFF);
dbartolovic 16:482d8f81d6f3 240 }
dbartolovic 24:7539258b015c 241 }
dbartolovic 24:7539258b015c 242
dbartolovic 24:7539258b015c 243 void longDelaysC(void)
dbartolovic 24:7539258b015c 244 {
dbartolovic 24:7539258b015c 245 mems.clearIntFlag();
dbartolovic 24:7539258b015c 246
dbartolovic 24:7539258b015c 247 INT1.enable_irq();
dbartolovic 24:7539258b015c 248
dbartolovic 24:7539258b015c 249 stopLongDelay();
dbartolovic 8:7ba4f82de9b6 250 }