Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

tasks/tasks.cpp

Committer:
jurica238814
Date:
2018-09-13
Branch:
mbedOsUpdate
Revision:
19:2681edc2f2b9
Parent:
11:4dc690157508
Child:
22:7dae8496b97c

File content as of revision 19:2681edc2f2b9:

/**
 * Aconno.de
 */

#include "bsp_buzz.h"
#include "bspLed.h"
#include "aconno_ble.h"
#include "tasks.h"
#include "GapAdvertisingData.h"
#include "aconnoConfig.h"
#include "bsp.h"

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()
{

#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;

		lisPowerCtrl(LIS_ON);
		wait_ms(POWER_UP_DELAY_MS);
		/*
		//mems.setMode(LOW_POWER);
	    mems.setMode(HIGH_RES);
	    mems.enableAxes(X_axis);
	    mems.enableAxes(Y_axis);
	    mems.enableAxes(Z_axis);
	    mems.setODR(ODR_50Hz);
	    //mems.setScale(_16g);
	    mems.setScale(_2g);
	    mems.int1Setup(0x40);       // IntActivity 1 driven to INT1 pad
	    mems.int1Latch(0x01);       // Enable int1 latch
	    mems.int1Threshold(INT1_THRESHOLD);
	    mems.int1Duration(INT1_DUR);
	    mems.int1Config(0x2A);      // Enable XHigh, YHigh and ZHigh
		*/

        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);

		lisPowerCtrl(LIS_OFF);
        Thread::wait(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);
    }
}