APP Team
/
app2
...
Diff: main.cpp
- Revision:
- 3:cd25d0efe0e1
- Parent:
- 2:e5e49d27768a
- Child:
- 5:3b94e16f0675
diff -r e5e49d27768a -r cd25d0efe0e1 main.cpp --- a/main.cpp Tue Jan 28 18:59:41 2014 +0000 +++ b/main.cpp Tue Jan 28 19:36:56 2014 +0000 @@ -1,33 +1,6 @@ -#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; +#include "main.h" Queue<Event_t, 16> eventQueue; - Mutex queue_mutex; void RTC_Init() @@ -35,130 +8,6 @@ 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();