Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

tasks/tasks.cpp

Committer:
dbartolovic
Date:
2018-04-20
Branch:
axis_normal
Revision:
13:2e964fa2a632
Parent:
11:4dc690157508
Child:
15:c0c01188a29b

File content as of revision 13:2e964fa2a632:

#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 = LSB_VALUE;
        
        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]);
        */

        
        /* 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);
    }
}