Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Files at this revision

API Documentation at this revision

Comitter:
dbartolovic
Date:
Tue Mar 20 15:13:51 2018 +0000
Parent:
7:ac8277568115
Child:
9:aef8bb3d13ed
Commit message:
Added lizzy service.

Changed in this revision

aconno_ble/aconno_ble.cpp Show annotated file Show diff for this revision Revisions of this file
aconno_ble/aconno_ble.h Show annotated file Show diff for this revision Revisions of this file
aconno_ble/lizzy_service.h Show annotated file Show diff for this revision Revisions of this file
bsp/bsp.h Show annotated file Show diff for this revision Revisions of this file
bsp/bsp_buzz.cpp Show annotated file Show diff for this revision Revisions of this file
bsp/bsp_buzz.h Show annotated file Show diff for this revision Revisions of this file
bsp/bsp_led.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
source/main.h Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
--- a/aconno_ble/aconno_ble.cpp	Wed Mar 14 08:48:26 2018 +0000
+++ b/aconno_ble/aconno_ble.cpp	Tue Mar 20 15:13:51 2018 +0000
@@ -4,7 +4,65 @@
  *
  */
 
+#include "main.h"
 #include "aconno_ble.h"
+#include "tasks.h"
+#include "lizzy_service.h"
+
+init_lizzy_t init_lizzy = {
+    .buzz = false,
+    .leds = {false ,false, false},
+    .acc_lsb = LSB_VALUE,
+    .acc_data = {0, 0, 0}
+};
+LizzyService *lizzy_service;
+
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    (lizzy_service->get_ble())->gap().startAdvertising();
+}
+
+void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){   
+    
+}
+
+void onDataWrittenCallback(const GattWriteCallbackParams *params) 
+{
+    if ((params->handle == lizzy_service->get_buzz_handle()) &&
+        (params->len == 1))
+    {
+        if ((uint8_t)true < *(params->data))
+            lizzy_service->set_buzz_state(true);
+            
+        updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
+    }
+    else if ((params->handle == lizzy_service->get_red_handle()) &&
+        (params->len == 1))
+    {
+        if ((uint8_t)true < *(params->data))
+            lizzy_service->set_red_state(true);
+            
+        updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
+    }
+    else if ((params->handle == lizzy_service->get_green_handle()) &&
+        (params->len == 1))
+    {
+        if ((uint8_t)true < *(params->data))
+            lizzy_service->set_green_state(true);
+            
+        updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
+    }
+    else if ((params->handle == lizzy_service->get_blue_handle()) &&
+        (params->len == 1))
+    {
+        if ((uint8_t)true < *(params->data))
+            lizzy_service->set_blue_state(true);
+            
+        updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
+    }
+}
+
 
 /**
 * Callback triggered when the ble initialization process has finished
@@ -25,6 +83,12 @@
         return;
     }
     
+    
+    lizzy_service = new LizzyService(ble, &init_lizzy);
+    ble.gap().onDisconnection(disconnectionCallback);
+    //ble.gap().onConnection(onConnectionCallback);         
+    ble.gattServer().onDataWritten(onDataWrittenCallback);
+    
     advertisementPacket.header = APPLICATION_ID;
     advertisementPacket.type = 0x00;
     advertisementPacket.gyroscope[0] = (int16_t)0;
--- a/aconno_ble/aconno_ble.h	Wed Mar 14 08:48:26 2018 +0000
+++ b/aconno_ble/aconno_ble.h	Tue Mar 20 15:13:51 2018 +0000
@@ -10,11 +10,14 @@
 #include "mbed.h"
 #include "ble/BLE.h"
 #include "GapAdvertisingData.h"
+#include "lizzy_service.h"
 
 #define MSD_SIZE_b          (10)
 #define ADV_INTERVAL_MS     (100)
 #define APPLICATION_ID      (0xCF170059) //(0xCF170059)
 
+#define LSB_VALUE           (0x0001)
+
 /* Global variables and constants */
 
 struct __attribute__((packed, aligned(1))) advertising_packet{
@@ -37,6 +40,8 @@
     };
 };
 
+extern LizzyService *lizzy_service;
+
 /* Function declarations */
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
 void updatePayload(BLE *ble, advertising_packet *advertisementPacket);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_ble/lizzy_service.h	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,126 @@
+#ifndef __LIZZY_SERVICE_H__
+#define __LIZZY_SERVICE_H__
+
+#include "mbed.h"
+#include "ble/BLE.h"
+
+#define NUM_LEDS        (3)
+#define NUM_AXIS        (3)
+
+
+typedef struct
+{
+    bool buzz;
+    bool leds[NUM_LEDS];
+    uint16_t acc_lsb;
+    int16_t acc_data[NUM_AXIS];
+} init_lizzy_t;
+
+
+class LizzyService{
+    public:
+        const static uint16_t LIZZY_SERVICE_UUID = 0xA000;
+        const static uint16_t BUZZ_UUID = 0xA001;
+        const static uint16_t RED_UUID = 0xA002;
+        const static uint16_t GREEN_UUID = 0xA003;
+        const static uint16_t BLUE_UUID = 0xA004;
+        const static uint16_t ACC_LSB_UUID = 0xA005;
+        const static uint16_t ACC_DATA_UUID = 0xA006;
+
+        LizzyService(BLEDevice &_ble, init_lizzy_t *init) :
+              ble(_ble),
+              buzz(BUZZ_UUID, &(init->buzz), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+              red_led(RED_UUID, &(init->leds[0]), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+              green_led(GREEN_UUID, &(init->leds[1]), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+              blue_led(BLUE_UUID, &(init->leds[2]), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+              acc_lsb(ACC_LSB_UUID, &(init->acc_lsb), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+              acc_data(ACC_DATA_UUID, init->acc_data, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+        {
+            GattCharacteristic *charTable[] = {&buzz, &red_led, &green_led, &blue_led, &acc_lsb, &acc_data};     // Add characteristick in table
+            GattService AckService(LIZZY_SERVICE_UUID, charTable, sizeof(charTable)/sizeof(GattCharacteristic *));   
+            ble.gattServer().addService(AckService); // Add service in the BLE
+        }
+        
+        inline BLEDevice *get_ble(){
+            return &ble;
+        }
+        
+        // Characteristic setters.
+        inline void set_buzz_state(bool new_state){
+            ble.gattServer().write(buzz.getValueHandle(), (uint8_t*)&new_state, sizeof(new_state));
+        }
+        inline void set_red_state(bool new_state){
+            ble.gattServer().write(red_led.getValueHandle(), (uint8_t*)&new_state, sizeof(new_state));
+        }
+        inline void set_green_state(bool new_state){
+            ble.gattServer().write(green_led.getValueHandle(), (uint8_t*)&new_state, sizeof(new_state));
+        }
+        inline void set_blue_state(bool new_state){
+            ble.gattServer().write(blue_led.getValueHandle(), (uint8_t*)&new_state, sizeof(new_state));
+        }
+        
+        inline void set_acc_lsb(uint16_t new_value){
+            ble.gattServer().write(acc_lsb.getValueHandle(), (uint8_t*)&new_value, sizeof(new_value));
+        }
+        inline void set_acc_data(int16_t *new_values){
+            ble.gattServer().write(acc_data.getValueHandle(), (uint8_t*)new_values, sizeof(int16_t)*NUM_AXIS);
+        }
+        
+        // Characteristic getters.
+        inline bool get_buzz_state(){
+            bool tmp;
+            uint16_t size = sizeof(tmp);
+            ble.gattServer().read(buzz.getValueHandle(), (uint8_t*)&tmp, &size);
+            return tmp;
+        }
+        inline bool get_red_state(){
+            bool tmp;
+            uint16_t size = sizeof(tmp);
+            ble.gattServer().read(red_led.getValueHandle(), (uint8_t*)&tmp, &size);
+            return tmp;
+        }
+        inline bool get_green_state(){
+            bool tmp;
+            uint16_t size = sizeof(tmp);
+            ble.gattServer().read(green_led.getValueHandle(), (uint8_t*)&tmp, &size);
+            return tmp;
+        }
+        inline bool get_blue_state(){
+            bool tmp;
+            uint16_t size = sizeof(tmp);
+            ble.gattServer().read(blue_led.getValueHandle(), (uint8_t*)&tmp, &size);
+            return tmp;
+        }
+        
+        inline GattAttribute::Handle_t get_buzz_handle(){
+            return buzz.getValueHandle();
+        }
+        inline GattAttribute::Handle_t get_red_handle(){
+            return red_led.getValueHandle();
+        }
+        inline GattAttribute::Handle_t get_green_handle(){
+            return green_led.getValueHandle();
+        }
+        inline GattAttribute::Handle_t get_blue_handle(){
+            return blue_led.getValueHandle();
+        }
+        
+        inline GattAttribute::Handle_t get_acc_lsb_handle(){
+            return acc_lsb.getValueHandle();
+        }inline GattAttribute::Handle_t get_acc_data_handle(){
+            return acc_data.getValueHandle();
+        }
+        
+    private:
+        BLEDevice &ble;
+        // Create new characteristics
+        ReadWriteGattCharacteristic<bool> buzz;
+        ReadWriteGattCharacteristic<bool> red_led;
+        ReadWriteGattCharacteristic<bool> green_led;
+        ReadWriteGattCharacteristic<bool> blue_led;
+        
+        ReadOnlyGattCharacteristic<uint16_t> acc_lsb;
+        ReadOnlyArrayGattCharacteristic<int16_t, NUM_AXIS> acc_data;
+};
+
+#endif //__LIZZY_SERVICE_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bsp.h	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,25 @@
+#ifndef __BSP_H__
+#define __BSP_H__
+
+#include "main.h"
+
+#if NANO_MODULE
+    #define I2C_DATA            (p2)
+    #define I2C_CLK             (p3)
+    #define INT_PIN1            (p10)
+    #define INT_PIN2            (p9)
+    #define ACC_POWER_PIN       (p13)
+    #define RED_LED_PIN         (p12)
+#else
+    #define I2C_DATA            (p20)
+    #define I2C_CLK             (p17)
+    #define INT_PIN1            (p16)
+    #define INT_PIN2            (p15)
+    #define ACC_POWER_PIN       (p11)
+    #define RED_LED_PIN         (p31)   //p22
+    #define GREEN_LED_PIN       (p2)
+    #define BLUE_LED_PIN        (p3)
+    #define BUZZER_PIN          (p18)
+#endif
+
+#endif //__BSP_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bsp_buzz.cpp	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,20 @@
+#include "bsp_buzz.h"
+
+
+void Buzz::enable()
+{
+    buzzer.enable(BUZZER_FREQUENCY_HZ);
+    buzzer.enableChannel(0, pwm_pin);
+    buzzer.setDuty(0,0.5f);
+    
+    state = true;
+}
+
+void Buzz::disable()
+{
+    buzzer.enable(0);
+    buzzer.setDuty(0, 0);
+    buzzer.disable();
+    
+    state = false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bsp_buzz.h	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,37 @@
+#ifndef __BSP_BUZZ_H__
+#define __BSP_BUZZ_H__
+
+#include "main.h"
+#include "acn_nrf52_pwm.h"
+
+#if NANO_MODULE
+#else
+    #define BUZZER_PIN          (p18)
+#endif
+
+#define BUZZER_FREQUENCY_HZ       (4000)
+#define BUZZ_TIME_S               (2)     /* Buzz time in s */
+
+class Buzz
+{
+    typedef typeof(NRF_PWM0) NRF_PWM_TYPE;
+    
+    public:
+        
+        Buzz(NRF_PWM_TYPE pwmInstance, uint8_t pin) :
+        buzzer(pwmInstance), pwm_pin(pin), state(false){}
+        
+        void enable();
+        void disable();
+        
+        inline bool get_state(){ return state; }
+        
+    private:
+        
+        uint8_t pwm_pin;
+        NRF52_PWM buzzer;
+        
+        bool state;
+};
+
+#endif //__BSP_BUZZ_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bsp_led.h	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,14 @@
+#ifndef __BSP_LED_H__
+#define __BSP_LED_H__
+
+#include "main.h"
+
+#if NANO_MODULE
+    #define RED_LED_PIN         (p12)
+#else
+    #define RED_LED_PIN         (p31)   //p22
+    #define GREEN_LED_PIN       (p2)
+    #define BLUE_LED_PIN        (p3)
+#endif
+
+#endif //__BSP_LED_H__
\ No newline at end of file
--- a/source/main.cpp	Wed Mar 14 08:48:26 2018 +0000
+++ b/source/main.cpp	Tue Mar 20 15:13:51 2018 +0000
@@ -6,9 +6,24 @@
  */
 
 #include "main.h"
+#include "bsp.h"
+#include "tasks.h"
 
-static advertising_packet advertisementPacket;
-static GapAdvertisingData adv_data = GapAdvertisingData();
+
+char memsI2CAddress = I2C_ADDRESS;
+Mutex uartM;
+Thread uartT;
+Thread bleT;
+Thread measureT;
+Thread updateServiceT;
+Thread updateBuzzLedsT;
+
+DigitalOut AccVcc(ACC_POWER_PIN);
+InterruptIn INT1(INT_PIN1);
+//InterruptIn INT2(INT_PIN2);
+I2C i2c(I2C_DATA,I2C_CLK);
+Ticker bleTicker;
+
 
 void disableI2C(){
     NRF_TWI0->ENABLE = 0;
@@ -27,135 +42,10 @@
     bar = 1;
 }
 
-void bleF(BLE *ble)
-{
-    while(true)
-    {
-
-        advertisementPacket.header = APPLICATION_ID;
-        advertisementPacket.type = 0x00;
-        advertisementPacket.gyroscope[0] = (int16_t)0;
-        advertisementPacket.gyroscope[1] = (int16_t)0;
-        advertisementPacket.gyroscope[2] = (int16_t)0;
-        advertisementPacket.magnetometer[0] = (int16_t)0;
-        advertisementPacket.magnetometer[1] = (int16_t)0;
-        advertisementPacket.magnetometer[2] = (int16_t)0;
-        
-        
-        advertisementPacket.accelerometer[0] = (int16_t)mems.readXAxis();
-        advertisementPacket.accelerometer[1] = (int16_t)mems.readYAxis();
-        advertisementPacket.accelerometer[2] = (int16_t)mems.readZAxis();
-        
-        //advertisementPacket.acc_lsb_value = 0xC000;
-        advertisementPacket.acc_lsb_value = 0x0100;
-        
-        
-        printf("%6d\t", advertisementPacket.accelerometer[0]);
-        printf("%6d\t", advertisementPacket.accelerometer[1]);
-        printf("%6d\r\n", advertisementPacket.accelerometer[2]);
-
-        //updatePayload(ble, &advertisementPacket);
-        
-        /* setup advertising */
-        
-        
-        /*
-        GapAdvertisingData advetisementData = GapAdvertisingData();
-        advetisementData = ble->getAdvertisingData();
-        advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
-        ble->setAdvertisingData(advetisementData);
-        */
-        adv_data = ble->getAdvertisingData();
-        adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
-        ble->setAdvertisingData(adv_data);
-        
-        
-        //printf("Ble advertisement is ON.\r\n");
-        
-        #if DEBUG_LED
-            redLed = 0;
-        #endif
-        ble->gap().startAdvertising();
-        //wait_ms(1000);
-        
-        wait_ms(MEASURE_INTERVAL_MS);
-        //wait_ms(1000);
-        
-        //printf("Ble advertisement is OFF.\r\n\r\n");
-        #if DEBUG_LED
-            redLed = 1;
-        #endif
-        //ble->gap().stopAdvertising();
-        /*
-        GapAdvertisingData advetisementData = GapAdvertisingData();
-        advetisementData = ble->getAdvertisingData();
-        advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
-        ble->setAdvertisingData(advetisementData);
-        */
-        //wait_ms(100);
-    }
-}
-
-void led_tick();
-
-void buzz_tick()
-{
-    static int start = 1;
+int main(){
+    Lis2dh12 mems(&i2c, memsI2CAddress);
     
-    if (start)
-    {
-        buzzer.enable(BUZZER_FREQUENCY_HZ);
-        buzzer.enableChannel(0, BUZZER_PIN);
-        buzzer.setDuty(0,0.5f);
-        start = 0;
-    }
-    else
-    {
-        buzzer.enable(0);
-        buzzer.setDuty(0, 0);
-        buzzer.disable();
-        start = 1;
-        led_tick();
-        test_ticker.detach();
-        test_ticker.attach(led_tick, 0.5);
-    }
-}
-
-
-void led_tick()
-{
-    static int count = 0;
-    
-    switch(count)
-    {
-        case 0:
-            redLed = 0;
-            break;
-            
-        case 1:
-            redLed = 1;
-            greenLed = 0;
-            break;
-            
-        case 2:
-            greenLed = 1;
-            blueLed = 0;
-            break;
-        
-        default:
-            blueLed = 1;
-            count = -1;
-            buzz_tick();
-            test_ticker.detach();
-            test_ticker.attach(buzz_tick, BUZZ_TIME_S);
-    }
-    
-    count++;
-}
-
-int main(){
-    
-    test_ticker.attach(led_tick, 0.5);
+    tasks_init();
     
     printf("Main program started.\r\n");
     
@@ -164,11 +54,7 @@
     NRF_NVMC->CONFIG = 0x00000001;      // Write enable UICR
     NRF_UICR->NFCPINS = 0xFFFFFFFE;     // Change NFC to GPIO function
     
-    redLed = 0;
     AccVcc = 1;
-    
-    greenLed = 1;
-    blueLed = 1;
     wait_ms(POWER_UP_DELAY_MS);
 
     /*
@@ -194,10 +80,13 @@
     mems.int1Config(0x2A);      // Enable XHigh, YHigh and ZHigh
     
     bleT.start(callback(bleF, &ble));   // Start bleT
+    measureT.start(callback(measureF, &mems));
+    updateServiceT.start(updateServiceF);
+    updateBuzzLedsT.start(updateBuzzLedsF);
     
     
     while(1){
         //disableI2C();
-        Thread::wait(0xFFFFFFFF);
+        ble.waitForEvent();
     }
 }
--- a/source/main.h	Wed Mar 14 08:48:26 2018 +0000
+++ b/source/main.h	Tue Mar 20 15:13:51 2018 +0000
@@ -14,42 +14,17 @@
 #include "ble/BLE.h"
 #include "GapAdvertisingData.h"
 #include "acd52832_bsp.h"
-#include "acn_nrf52_pwm.h"
 
 #define DEBUG               (0)
-#define PRINT_ON_RTT        (1)
+#define PRINT_ON_RTT        (0)
 #define DEBUG_LED           (0)
 #define NANO_MODULE         (0)
 //#define LSB_VALUE           (192)
-#define LSB_VALUE           (1)
-
-#if NANO_MODULE
-    #define I2C_DATA            (p2)
-    #define I2C_CLK             (p3)
-    #define INT_PIN1            (p10)
-    #define INT_PIN2            (p9)
-    #define ACC_POWER_PIN       (p13)
-    #define RED_LED_PIN         (p12)
-#else
-    #define I2C_DATA            (p20)
-    #define I2C_CLK             (p17)
-    #define INT_PIN1            (p16)
-    #define INT_PIN2            (p15)
-    #define ACC_POWER_PIN       (p11)
-    #define RED_LED_PIN         (p31)   //p22
-    #define GREEN_LED_PIN       (p2)
-    #define BLUE_LED_PIN        (p3)
-    #define BUZZER_PIN          (p18)
-#endif
 
 #define BLE_ACTIVE_TIME_S   (0.4)
 #define POWER_UP_DELAY_MS   (200)
-#define MEASURE_INTERVAL_MS (100)
 
 
-#define BUZZER_FREQUENCY_HZ       (4000)
-#define BUZZ_TIME_S               (2)     /* Buzz time in s */
-
 #if PRINT_ON_RTT
     #include "SEGGER_RTT.h"
     #define printf(...)                      SEGGER_RTT_printf(0, __VA_ARGS__)
@@ -71,25 +46,10 @@
     #define SEND(...)
 #endif
 
-char memsI2CAddress = I2C_ADDRESS;
-Mutex uartM;
-Thread uartT;
-Thread bleT;
-Thread measureT;
 
-DigitalOut AccVcc(ACC_POWER_PIN);
-DigitalOut redLed(RED_LED_PIN);
-#if NANO_MODULE == 0
-DigitalOut greenLed(GREEN_LED_PIN);
-DigitalOut blueLed(BLUE_LED_PIN);
-#endif
-InterruptIn INT1(INT_PIN1);
-//InterruptIn INT2(INT_PIN2);
-I2C i2c(I2C_DATA,I2C_CLK);
-Lis2dh12 mems(&i2c, memsI2CAddress);
-Ticker bleTicker;
-Ticker test_ticker;
-
-NRF52_PWM buzzer(NRF_PWM2);
+extern Thread bleT;
+extern Thread measureT;
+extern Thread updateServiceT;
+extern Thread updateBuzzLedsT;
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.cpp	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,209 @@
+#include "main.h"
+#include "bsp_buzz.h"
+#include "bsp_led.h"
+#include "aconno_ble.h"
+#include "tasks.h"
+#include "GapAdvertisingData.h"
+
+
+DigitalOut redLed(RED_LED_PIN);
+#if NANO_MODULE == 0
+DigitalOut greenLed(GREEN_LED_PIN);
+DigitalOut blueLed(BLUE_LED_PIN);
+#endif
+
+Buzz buzzer(NRF_PWM2, BUZZER_PIN);
+
+static advertising_packet advertisementPacket;
+static GapAdvertisingData adv_data = GapAdvertisingData();
+
+
+#if TEST_LEDS_BUZZ
+Ticker test_ticker;
+#endif
+
+
+void tasks_init()
+{
+    redLed = 1;
+    greenLed = 1;
+    blueLed = 1;
+    
+#if TEST_LEDS_BUZZ
+    test_ticker.attach(led_tick, 0.5);
+#endif
+}
+
+#if TEST_LEDS_BUZZ
+void buzz_tick()
+{
+    static int start = 1;
+    
+    if (start)
+    {
+        buzzer.enable();
+        start = 0;
+    }
+    else
+    {
+        buzzer.disable();
+        start = 1;
+        led_tick();
+        test_ticker.detach();
+        test_ticker.attach(led_tick, 0.5);
+    }
+}
+
+void led_tick()
+{
+    static int count = 0;
+    
+    switch(count)
+    {
+        case 0:
+            redLed = 0;
+            break;
+            
+        case 1:
+            redLed = 1;
+            greenLed = 0;
+            break;
+            
+        case 2:
+            greenLed = 1;
+            blueLed = 0;
+            break;
+        
+        default:
+            blueLed = 1;
+            count = -1;
+            buzz_tick();
+            test_ticker.detach();
+            test_ticker.attach(buzz_tick, BUZZ_TIME_S);
+    }
+    
+    count++;
+}
+#endif
+
+void measureF(Lis2dh12 *mems)
+{
+    while (1)
+    {
+        advertisementPacket.header = APPLICATION_ID;
+        advertisementPacket.type = 0x00;
+        advertisementPacket.gyroscope[0] = (int16_t)0;
+        advertisementPacket.gyroscope[1] = (int16_t)0;
+        advertisementPacket.gyroscope[2] = (int16_t)0;
+        advertisementPacket.magnetometer[0] = (int16_t)0;
+        advertisementPacket.magnetometer[1] = (int16_t)0;
+        advertisementPacket.magnetometer[2] = (int16_t)0;
+        
+        
+        advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis();
+        advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis();
+        advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
+        
+        //advertisementPacket.acc_lsb_value = 0xC000;
+        advertisementPacket.acc_lsb_value = 0x0100;
+        
+        updateServiceT.signal_set(MEAS_DONE);
+        bleT.signal_set(MEAS_DONE);
+        
+        wait_ms(MEASURE_INTERVAL_MS);
+    }
+}
+
+void updateServiceF()
+{
+    while (1)
+    {
+        Thread::signal_wait(MEAS_DONE);
+        updateServiceT.signal_clr(MEAS_DONE);
+        
+        lizzy_service->set_acc_data(advertisementPacket.accelerometer);
+    }
+}
+
+void updateBuzzLedsF()
+{
+    while (1)
+    {
+        Thread::signal_wait(UPDATE_BUZZ_LEDS);
+        updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS);
+        
+        if (buzzer.get_state() != (lizzy_service->get_buzz_state()))
+        {
+            if (lizzy_service->get_buzz_state())
+                buzzer.enable();
+            else
+                buzzer.disable();
+        }
+        if (!redLed != (lizzy_service->get_red_state()))
+        {
+            redLed = !(lizzy_service->get_red_state());
+        }
+        if (!greenLed != (lizzy_service->get_green_state()))
+        {
+            greenLed = !(lizzy_service->get_green_state());
+        }
+        if (!blueLed != (lizzy_service->get_blue_state()))
+        {
+            blueLed = !(lizzy_service->get_blue_state());
+        }
+    }
+}
+
+void bleF(BLE *ble)
+{
+    while(true)
+    {
+        Thread::signal_wait(MEAS_DONE);
+        bleT.signal_clr(MEAS_DONE);
+        
+        
+        printf("%6d\t", advertisementPacket.accelerometer[0]);
+        printf("%6d\t", advertisementPacket.accelerometer[1]);
+        printf("%6d\r\n", advertisementPacket.accelerometer[2]);
+
+        //updatePayload(ble, &advertisementPacket);
+        
+        /* setup advertising */
+        
+        
+        /*
+        GapAdvertisingData advetisementData = GapAdvertisingData();
+        advetisementData = ble->getAdvertisingData();
+        advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
+        ble->setAdvertisingData(advetisementData);
+        */
+        adv_data = ble->getAdvertisingData();
+        adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
+        ble->setAdvertisingData(adv_data);
+        
+        
+        //printf("Ble advertisement is ON.\r\n");
+        
+        #if DEBUG_LED
+            redLed = 0;
+        #endif
+        //ble->gap().startAdvertising();
+        //wait_ms(1000);
+        
+        //wait_ms(MEASURE_INTERVAL_MS);
+        //wait_ms(1000);
+        
+        //printf("Ble advertisement is OFF.\r\n\r\n");
+        #if DEBUG_LED
+            redLed = 1;
+        #endif
+        //ble->gap().stopAdvertising();
+        /*
+        GapAdvertisingData advetisementData = GapAdvertisingData();
+        advetisementData = ble->getAdvertisingData();
+        advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet));
+        ble->setAdvertisingData(advetisementData);
+        */
+        //wait_ms(100);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.h	Tue Mar 20 15:13:51 2018 +0000
@@ -0,0 +1,25 @@
+#ifndef __TASKS_H__
+#define __TASKS_H__
+
+#include "Lis2dh12.h"
+
+#define TEST_LEDS_BUZZ          (0)
+
+#define MEASURE_INTERVAL_MS     (100)
+
+#define MEAS_DONE               (0x00001000)
+#define UPDATE_BUZZ_LEDS        (0x00001001)
+
+void tasks_init();
+
+#if TEST_LEDS_BUZZ
+void led_tick();
+void buzz_tick();
+#endif
+
+void measureF(Lis2dh12 *mems);
+void updateServiceF();
+void updateBuzzLedsF();
+void bleF(BLE *ble);
+
+#endif//__TASKS_H__
\ No newline at end of file