Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Committer:
dbartolovic
Date:
Tue Mar 20 15:13:51 2018 +0000
Revision:
8:7ba4f82de9b6
Child:
9:aef8bb3d13ed
Added lizzy service.

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 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 8:7ba4f82de9b6 108 advertisementPacket.acc_lsb_value = 0x0100;
dbartolovic 8:7ba4f82de9b6 109
dbartolovic 8:7ba4f82de9b6 110 updateServiceT.signal_set(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 111 bleT.signal_set(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 112
dbartolovic 8:7ba4f82de9b6 113 wait_ms(MEASURE_INTERVAL_MS);
dbartolovic 8:7ba4f82de9b6 114 }
dbartolovic 8:7ba4f82de9b6 115 }
dbartolovic 8:7ba4f82de9b6 116
dbartolovic 8:7ba4f82de9b6 117 void updateServiceF()
dbartolovic 8:7ba4f82de9b6 118 {
dbartolovic 8:7ba4f82de9b6 119 while (1)
dbartolovic 8:7ba4f82de9b6 120 {
dbartolovic 8:7ba4f82de9b6 121 Thread::signal_wait(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 122 updateServiceT.signal_clr(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 123
dbartolovic 8:7ba4f82de9b6 124 lizzy_service->set_acc_data(advertisementPacket.accelerometer);
dbartolovic 8:7ba4f82de9b6 125 }
dbartolovic 8:7ba4f82de9b6 126 }
dbartolovic 8:7ba4f82de9b6 127
dbartolovic 8:7ba4f82de9b6 128 void updateBuzzLedsF()
dbartolovic 8:7ba4f82de9b6 129 {
dbartolovic 8:7ba4f82de9b6 130 while (1)
dbartolovic 8:7ba4f82de9b6 131 {
dbartolovic 8:7ba4f82de9b6 132 Thread::signal_wait(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 133 updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS);
dbartolovic 8:7ba4f82de9b6 134
dbartolovic 8:7ba4f82de9b6 135 if (buzzer.get_state() != (lizzy_service->get_buzz_state()))
dbartolovic 8:7ba4f82de9b6 136 {
dbartolovic 8:7ba4f82de9b6 137 if (lizzy_service->get_buzz_state())
dbartolovic 8:7ba4f82de9b6 138 buzzer.enable();
dbartolovic 8:7ba4f82de9b6 139 else
dbartolovic 8:7ba4f82de9b6 140 buzzer.disable();
dbartolovic 8:7ba4f82de9b6 141 }
dbartolovic 8:7ba4f82de9b6 142 if (!redLed != (lizzy_service->get_red_state()))
dbartolovic 8:7ba4f82de9b6 143 {
dbartolovic 8:7ba4f82de9b6 144 redLed = !(lizzy_service->get_red_state());
dbartolovic 8:7ba4f82de9b6 145 }
dbartolovic 8:7ba4f82de9b6 146 if (!greenLed != (lizzy_service->get_green_state()))
dbartolovic 8:7ba4f82de9b6 147 {
dbartolovic 8:7ba4f82de9b6 148 greenLed = !(lizzy_service->get_green_state());
dbartolovic 8:7ba4f82de9b6 149 }
dbartolovic 8:7ba4f82de9b6 150 if (!blueLed != (lizzy_service->get_blue_state()))
dbartolovic 8:7ba4f82de9b6 151 {
dbartolovic 8:7ba4f82de9b6 152 blueLed = !(lizzy_service->get_blue_state());
dbartolovic 8:7ba4f82de9b6 153 }
dbartolovic 8:7ba4f82de9b6 154 }
dbartolovic 8:7ba4f82de9b6 155 }
dbartolovic 8:7ba4f82de9b6 156
dbartolovic 8:7ba4f82de9b6 157 void bleF(BLE *ble)
dbartolovic 8:7ba4f82de9b6 158 {
dbartolovic 8:7ba4f82de9b6 159 while(true)
dbartolovic 8:7ba4f82de9b6 160 {
dbartolovic 8:7ba4f82de9b6 161 Thread::signal_wait(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 162 bleT.signal_clr(MEAS_DONE);
dbartolovic 8:7ba4f82de9b6 163
dbartolovic 8:7ba4f82de9b6 164
dbartolovic 8:7ba4f82de9b6 165 printf("%6d\t", advertisementPacket.accelerometer[0]);
dbartolovic 8:7ba4f82de9b6 166 printf("%6d\t", advertisementPacket.accelerometer[1]);
dbartolovic 8:7ba4f82de9b6 167 printf("%6d\r\n", advertisementPacket.accelerometer[2]);
dbartolovic 8:7ba4f82de9b6 168
dbartolovic 8:7ba4f82de9b6 169 //updatePayload(ble, &advertisementPacket);
dbartolovic 8:7ba4f82de9b6 170
dbartolovic 8:7ba4f82de9b6 171 /* setup advertising */
dbartolovic 8:7ba4f82de9b6 172
dbartolovic 8:7ba4f82de9b6 173
dbartolovic 8:7ba4f82de9b6 174 /*
dbartolovic 8:7ba4f82de9b6 175 GapAdvertisingData advetisementData = GapAdvertisingData();
dbartolovic 8:7ba4f82de9b6 176 advetisementData = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 177 advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
dbartolovic 8:7ba4f82de9b6 178 ble->setAdvertisingData(advetisementData);
dbartolovic 8:7ba4f82de9b6 179 */
dbartolovic 8:7ba4f82de9b6 180 adv_data = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 181 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
dbartolovic 8:7ba4f82de9b6 182 ble->setAdvertisingData(adv_data);
dbartolovic 8:7ba4f82de9b6 183
dbartolovic 8:7ba4f82de9b6 184
dbartolovic 8:7ba4f82de9b6 185 //printf("Ble advertisement is ON.\r\n");
dbartolovic 8:7ba4f82de9b6 186
dbartolovic 8:7ba4f82de9b6 187 #if DEBUG_LED
dbartolovic 8:7ba4f82de9b6 188 redLed = 0;
dbartolovic 8:7ba4f82de9b6 189 #endif
dbartolovic 8:7ba4f82de9b6 190 //ble->gap().startAdvertising();
dbartolovic 8:7ba4f82de9b6 191 //wait_ms(1000);
dbartolovic 8:7ba4f82de9b6 192
dbartolovic 8:7ba4f82de9b6 193 //wait_ms(MEASURE_INTERVAL_MS);
dbartolovic 8:7ba4f82de9b6 194 //wait_ms(1000);
dbartolovic 8:7ba4f82de9b6 195
dbartolovic 8:7ba4f82de9b6 196 //printf("Ble advertisement is OFF.\r\n\r\n");
dbartolovic 8:7ba4f82de9b6 197 #if DEBUG_LED
dbartolovic 8:7ba4f82de9b6 198 redLed = 1;
dbartolovic 8:7ba4f82de9b6 199 #endif
dbartolovic 8:7ba4f82de9b6 200 //ble->gap().stopAdvertising();
dbartolovic 8:7ba4f82de9b6 201 /*
dbartolovic 8:7ba4f82de9b6 202 GapAdvertisingData advetisementData = GapAdvertisingData();
dbartolovic 8:7ba4f82de9b6 203 advetisementData = ble->getAdvertisingData();
dbartolovic 8:7ba4f82de9b6 204 advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
dbartolovic 8:7ba4f82de9b6 205 ble->setAdvertisingData(advetisementData);
dbartolovic 8:7ba4f82de9b6 206 */
dbartolovic 8:7ba4f82de9b6 207 //wait_ms(100);
dbartolovic 8:7ba4f82de9b6 208 }
dbartolovic 8:7ba4f82de9b6 209 }