SEDO subject project

Dependencies:   ds3231 mbed-rtos mbed DHT

main_20170502.txt

Committer:
ValenSalLop
Date:
2017-05-15
Revision:
12:1d544cdab2cf
Parent:
9:5e9c4277151d

File content as of revision 12:1d544cdab2cf:

/*
 * Sensing operations
 */
#include "mbed.h"
#include "rtos.h"

#include "sensor.h"

extern CirQueue buffer;
extern DHT DHTSensor;
extern DigitalOut DHTEnable;
extern uint32_t initSensor(void const *args)
{

    buffer = CirQueue();
    DHTSensor = DHT(D6, DHT11);
    DHTEnable = DigitalOut(D7);
    // TODO: Establecer alarma RTC para ejecucin periodica

    return 1;
}
extern void sensor_thread(void const *args)
{
    
    int DHTError = 0;
    // TODO: gestion RTC adquirir hora
    time_t ts;
    while (true) {
        DHTError = 0;
        ts = rtc.get_epoch();
        if(ts<=0)
            // TODO : No hay hora
        SensorData measures;
        DHTError = readDHT(&measures);
        if(DHTError) {
            printf("DHTError: %d\n", DHTError);
        }
        buffer.push(measures);
        // TODO: inicio periodico segun RTC
        Thread::wait(5000);
    }
}

int DHTread(SensorData* measures)
{
    int error = 0;
    DHTEnable = 1;
    wait(500);
    error = DHTSensor.readData();
    if (0 == error) {
        measures->temperature = DHTSensor.ReadTemperature(CELCIUS);
        measures->humidity = DHTSensor.ReadHumidity();
        measures->dewPoint = DHTSensor.CalcdewPoint(*temperature, *humidity);
    }
    DHTEnable=0;
    return error;
}

// Circle queue implementation
CirQueue:: CirQueue()
{
    front =  0;
    rear  = -1;
    count =  0;
}

int CirQueue:: isFull()
{
    int full=0;

    if( count == SENSOR_QUEUE_LENGTH )
        full = 1;

    return full;
}

int CirQueue:: isEmpty()
{
    int empty=0;

    if( count == 0 )
        empty = 1;

    return empty;
}


int CirQueue:: pop(SensorData *item)
{
    if( isEmpty() ) {
        return -1;
    }
    *item = ele[front];
    front = (front+1) % SENSOR_QUEUE_LENGTH;
    count--;
    return 0;
}

int CirQueue:: push(SensorData item)
{
    int res = 0;
    if( isFull() ) {
        SensorData latest_data;
        pop(&latest_data);
        res = -1;
    }

    rear= (rear+1) % SENSOR_QUEUE_LENGTH;

    ele[rear] = item;
    count++;

    return res;
}