...

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
trixrabbit
Date:
2014-01-28
Revision:
2:e5e49d27768a
Parent:
1:92ebe0707661
Child:
3:cd25d0efe0e1

File content as of revision 2:e5e49d27768a:

#include "mbed.h"
#include "rtos.h"

#define NUM_DELAY 100
#define ANN_DELAY 250
#define ANN_THRESOLD 125

#define SWITCH_1 0
#define SWITCH_2 1
#define POT_1 2
#define POT_2 3

const char* eventType[4] = {"Switch 1", "Switch 2", "Pot 1", "Pot 2 "};

Serial pc(USBTX, USBRX);
DigitalIn evenNum1(p15);
DigitalIn evenNum2(p16);
AnalogIn evenAnn1(p19);
AnalogIn evenAnn2(p20);

struct Event
{
       short type;
       time_t time;
};

typedef Event Event_t;

Queue<Event_t, 16> eventQueue;

Mutex queue_mutex; 

void RTC_Init()
{
     set_time(1391097600);  // Set RTC time to Thursday, 30 Jan 2014 16:00:00   
}

short Average(short values[5])
{
    return ((values[0] + values[1] + values[2] + values[3] + values[4]) /5);
}

void samplingNum(void const *n)
{
    static bool stateChanged[2] = {false, false};
    static bool value[2] = {1, 1};
    
    stateChanged[0] = (value[0] != evenNum1.read());
    stateChanged[1] = (value[1] != evenNum2.read());
    
    if((stateChanged[0] == true) || (stateChanged[1] == true))
    {
        wait_ms(50);
        stateChanged[0] = (value[0] != evenNum1.read());
        stateChanged[1] = (value[1] != evenNum2.read());
        
        if(stateChanged[0])
        {
            Event_t *event1 = new Event_t();
            event1->type = SWITCH_1;
            event1->time = time(NULL);
            queue_mutex.lock();
            eventQueue.put(event1);
            queue_mutex.unlock();
            value[0] = !value[0];
        }
        if(stateChanged[1])
        {
            Event_t *event2 = new Event_t();
            event2->type = SWITCH_2;
            event2->time = time(NULL);
            queue_mutex.lock();
            eventQueue.put(event2);
            queue_mutex.unlock();
            value[1] = !value[1];
        }
    }
}

void samplingAnn(void const *n)
{
    static short tabPot1[5] = {0, 0, 0, 0, 0};
    static short tabPot2[5] = {0, 0, 0, 0, 0};
    static short currentValue[2] = {0, 0};
    short avg[2]= {0, 0};
    static short tabPtr = 0;
    static bool ready = false;
    
    currentValue[0] = evenAnn1.read() * 1000;
    avg[0] = Average(tabPot1);
    
    currentValue[1] = evenAnn2.read() * 1000;
    avg[1] = Average(tabPot2);
            
    if(ready)
    {
        if((currentValue[0] < (avg[0] - ANN_THRESOLD)) || (currentValue[0] > (avg[0] + ANN_THRESOLD)))
        {
            Event_t *event1 = new Event_t();
            event1->type = POT_1;
            event1->time = time(NULL);
            queue_mutex.lock();
            eventQueue.put(event1);
            queue_mutex.unlock();
        }
        
        if((currentValue[1] < (avg[1] - ANN_THRESOLD)) || (currentValue[1] > (avg[1] + ANN_THRESOLD)))
        {
            Event_t *event2 = new Event_t();
            event2->type = POT_2;
            event2->time = time(NULL);
            queue_mutex.lock();
            eventQueue.put(event2);
            queue_mutex.unlock();
        }
    }
    
    tabPot1[tabPtr] = currentValue[0];
    tabPot2[tabPtr] = currentValue[1];
    
    tabPtr++;
    if(tabPtr == 5)
    {
        tabPtr = 0;
        ready = true;
    }
}


void NumEvent_thread(void const *args)
{
    RtosTimer samplingTimer(samplingNum, osTimerPeriodic, (void *)0);
    samplingTimer.start(NUM_DELAY);
    while (true){}
}

void AnnEvent_thread(void const *args)
{
    RtosTimer samplingTimer(samplingAnn, osTimerPeriodic, (void *)0);
    samplingTimer.start(ANN_DELAY);
    while (true){}
}

void Collector_thread(void const *args)
{    
    Event_t *event;
    osEvent evt;
    
    while(true)
    {
        evt = eventQueue.get();
        if (evt.status == osEventMessage) 
        {
            event = (Event_t*)evt.value.p;
        }
        pc.printf("Event Type = %s    Time = %s \n\r", eventType[event->type], ctime(&event->time));
        
        delete event;   
    }
}

int main() 
{
    RTC_Init();
    Thread thread1(NumEvent_thread);
    Thread thread2(AnnEvent_thread);
    Thread thread3(Collector_thread);
    
    /*
    thread1.set_priority(osPriorityRealtime);
    thread2.set_priority(osPriorityRealtime);
    thread3.set_priority(osPriorityNormal);
    */
    
    while (true){} 
 }