Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tasks.cpp Source File

tasks.cpp

00001 #include "main.h"
00002 #include "bsp.h"
00003 #include "bsp_buzz.h"
00004 #include "bsp_led.h"
00005 #include "aconno_ble.h"
00006 #include "tasks.h"
00007 #include "GapAdvertisingData.h"
00008 
00009 
00010 DigitalOut redLed(RED_LED_PIN);
00011 #if NANO_MODULE == 0
00012 DigitalOut greenLed(GREEN_LED_PIN);
00013 DigitalOut blueLed(BLUE_LED_PIN);
00014 #endif
00015 
00016 extern Lis2dh12 mems;
00017 extern InterruptIn INT1;
00018 
00019 Buzz buzzer(NRF_PWM2, BUZZER_PIN);
00020 
00021 static advertising_packet advertisementPacket;
00022 static GapAdvertisingData adv_data = GapAdvertisingData();
00023 
00024 
00025 #if TEST_LEDS_BUZZ
00026 Ticker test_ticker;
00027 #endif
00028 
00029 LowPowerTicker longDelaysT;
00030 
00031 
00032 inline void startLongDelay(uint32_t delay)
00033 {
00034     longDelaysT.attach(longDelaysC, delay);
00035 }
00036 
00037 inline void stopLongDelay()
00038 {
00039     longDelaysT.detach();
00040 }
00041 
00042 
00043 void tasks_init()
00044 {
00045     redLed = 1;
00046     greenLed = 1;
00047     blueLed = 1;
00048     
00049 #if TEST_LEDS_BUZZ
00050     test_ticker.attach(led_tick, 0.5);
00051 #endif
00052 }
00053 
00054 #if TEST_LEDS_BUZZ
00055 void buzz_tick()
00056 {
00057     static int start = 1;
00058     
00059     if (start)
00060     {
00061         buzzer.enable();
00062         start = 0;
00063     }
00064     else
00065     {
00066         buzzer.disable();
00067         start = 1;
00068         led_tick();
00069         test_ticker.detach();
00070         test_ticker.attach(led_tick, 0.5);
00071     }
00072 }
00073 
00074 void led_tick()
00075 {
00076     static int count = 0;
00077     
00078     switch(count)
00079     {
00080         case 0:
00081             redLed = 0;
00082             break;
00083             
00084         case 1:
00085             redLed = 1;
00086             greenLed = 0;
00087             break;
00088             
00089         case 2:
00090             greenLed = 1;
00091             blueLed = 0;
00092             break;
00093         
00094         default:
00095             blueLed = 1;
00096             count = -1;
00097             buzz_tick();
00098             test_ticker.detach();
00099             test_ticker.attach(buzz_tick, BUZZ_TIME_S);
00100     }
00101     
00102     count++;
00103 }
00104 #endif
00105 
00106 void measureF(Lis2dh12 *mems)
00107 {
00108     int untilSleep;
00109     
00110     while(1)
00111     {
00112         Thread::signal_wait(START_MEAS);
00113         Thread::signal_clr(START_MEAS);
00114         
00115         INT1.disable_irq();
00116         
00117         untilSleep = ACTIVE_PERIOD_MS / MEASURE_INTERVAL_MS;
00118         
00119         BLE::Instance().startAdvertising();
00120         
00121         while (1)
00122         {
00123             advertisementPacket.header = APPLICATION_ID;
00124             advertisementPacket.type = 0x00;
00125             advertisementPacket.gyroscope[0] = (int16_t)0;
00126             advertisementPacket.gyroscope[1] = (int16_t)0;
00127             advertisementPacket.gyroscope[2] = (int16_t)0;
00128             advertisementPacket.magnetometer[0] = (int16_t)0;
00129             advertisementPacket.magnetometer[1] = (int16_t)0;
00130             advertisementPacket.magnetometer[2] = (int16_t)0;
00131             
00132 #if NORMAL_AXIS == 1
00133             advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis();
00134             advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis();
00135             advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
00136 #else
00137             advertisementPacket.accelerometer[0] = -(int16_t)mems->readYAxis();
00138             advertisementPacket.accelerometer[1] = (int16_t)mems->readXAxis();
00139             advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
00140 #endif
00141             advertisementPacket.accelerometer[0] *= 60; 
00142             advertisementPacket.accelerometer[1] *= 60;
00143             advertisementPacket.accelerometer[2] *= 60;
00144             
00145             printf("X: %d", advertisementPacket.accelerometer[0]);
00146             printf("\tY: %d", advertisementPacket.accelerometer[1]);
00147             printf("\tZ: %d\n", advertisementPacket.accelerometer[2]);
00148 
00149             advertisementPacket.acc_lsb_value = LSB_VALUE;
00150             
00151             updateServiceT.signal_set(MEAS_DONE);
00152             bleT.signal_set(MEAS_DONE);
00153             
00154             wait_ms(MEASURE_INTERVAL_MS);
00155             untilSleep--;
00156             
00157             if( (untilSleep <= 0) && !bleIsClientConnected() )
00158             {
00159                 BLE::Instance().stopAdvertising();
00160                 startLongDelay(LONG_SLEEP_S);
00161                 break;
00162             }
00163         }
00164     }
00165 }
00166 
00167 void updateServiceF()
00168 {
00169     while (1)
00170     {
00171         Thread::signal_wait(MEAS_DONE);
00172         updateServiceT.signal_clr(MEAS_DONE);
00173         
00174         lizzy_service->set_acc_data(advertisementPacket.accelerometer);
00175     }
00176 }
00177 
00178 void updateBuzzLedsF()
00179 {
00180 #if VODAFONE_COMPATIBILITY == 1
00181     const uint8_t startBuzz = 0xBA;
00182 #endif    
00183     
00184     while (1)
00185     {
00186         Thread::signal_wait(UPDATE_BUZZ_LEDS);
00187         updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS);
00188 
00189 #if VODAFONE_COMPATIBILITY == 1
00190         uint8_t tmpBuzzState[4];
00191         if( buzzer.get_state() != 
00192             (lizzy_service->getBuzz(tmpBuzzState)[0] == startBuzz) )
00193 #else
00194         if( buzzer.get_state() != (lizzy_service->getBuzz()) )
00195 #endif
00196         {
00197 #if VODAFONE_COMPATIBILITY == 1
00198             if( tmpBuzzState[0] == startBuzz )
00199 #else
00200             if( lizzy_service->getBuzz() )
00201 #endif
00202                 buzzer.enable();
00203             else
00204                 buzzer.disable();
00205         }
00206 #if VODAFONE_COMPATIBILITY == 1
00207 #else
00208         if( !redLed != (lizzy_service->getRedLed()) )
00209         {
00210             redLed = !(lizzy_service->getRedLed());
00211         }
00212 #endif
00213         if( !greenLed != (lizzy_service->get_green_state()) )
00214         {
00215             greenLed = !(lizzy_service->get_green_state());
00216         }
00217         if( !blueLed != (lizzy_service->get_blue_state()) )
00218         {
00219             blueLed = !(lizzy_service->get_blue_state());
00220         }
00221     }
00222 }
00223 
00224 void bleF(BLE *ble)
00225 {
00226     while(true)
00227     {
00228         Thread::signal_wait(MEAS_DONE);
00229         bleT.signal_clr(MEAS_DONE);
00230 
00231 #if VODAFONE_COMPATIBILITY == 1
00232 #else
00233         adv_data = ble->getAdvertisingData();
00234         adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
00235         ble->setAdvertisingData(adv_data);
00236 #endif
00237 
00238     }
00239 }
00240 
00241 void servLoopC(void)
00242 {
00243     while(1)
00244     {
00245         getBLEEventQueue()->dispatch_forever();
00246         Thread::wait(0xFFFFFFFF);
00247     }
00248 }
00249 
00250 void longDelaysC(void)
00251 {
00252     mems.clearIntFlag();
00253     
00254     INT1.enable_irq();
00255     
00256     stopLongDelay();
00257 }