![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Task651 Event Queue
Dependencies: ELEC350-Practicals-FZ429
Fork of Task622Solution-mbedos54 by
Diff: main.cpp
- Revision:
- 8:b28defacd894
- Parent:
- 7:cd015e83995a
- Child:
- 9:c46e831f8e4a
--- a/main.cpp Mon Mar 14 16:32:56 2016 +0000 +++ b/main.cpp Mon Mar 14 19:48:38 2016 +0000 @@ -4,7 +4,11 @@ #include <stdio.h> #include <ctype.h> -#define DELAY 200 +#define SWITCH1_RELEASE 1 + +void thread1( const void* ); +void thread2( const void* ); +void switchISR(); //Digital outputs DigitalOut onBoardLED(LED1); @@ -17,60 +21,68 @@ //Digital inputs DigitalIn onBoardSwitch(USER_BUTTON); -DigitalIn SW1(D4); -DigitalIn SW2(D3); +InterruptIn sw1(D4); +DigitalIn sw2(D3); + +//Threads +Thread *t1; +Thread *t2; //Thread ID for the Main function (CMSIS API) osThreadId tidMain; +osThreadId tid1; +osThreadId tid2; + +//Called on the falling edge of a switch +void switchISR() { + t1->signal_set(SWITCH1_RELEASE); //Very short +} + +//High priority thread void thread1( const void* arg ) { - pc.printf("Entering thread 1\n"); + redLED = 1; while (true) { - yellowLED = 1; - Thread::wait(DELAY); - yellowLED = 0; - Thread::wait(DELAY); + Thread::signal_wait(SWITCH1_RELEASE); + redLED = !redLED; + Thread::wait(1000); + redLED = !redLED; + Thread::wait(1000); + t1->signal_clr(SWITCH1_RELEASE); //Debounce } } -//This thread has higher priority +//This thread has normal priority void thread2( const void* arg ) { - pc.printf("Entering thread 2\n"); + greenLED = 1; while (true) { - redLED = 1; - if (SW1 == 1) { - - // 1) Select the 'type' of wait - - //wait_ms(osWaitForever); - Thread::wait(osWaitForever); - } else { - Thread::wait(DELAY); - } - redLED = 0; - Thread::wait(DELAY); + //Thread::wait(2000); + Thread::wait(500); // WAIT FOR 0.5 SECONDS + greenLED = !greenLED; } } -//Main thread +// Main thread int main() { redLED = 0; yellowLED = 0; greenLED = 0; - - //Main thread ID + + //Threads + t1 = new Thread(&thread1, NULL, osPriorityRealtime); + t2 = new Thread(&thread2, NULL, osPriorityNormal); + + // Thread IDs tidMain = Thread::gettid(); - - Thread t1(thread1, NULL, osPriorityNormal); + tid1 = t1->gettid(); + tid2 = t2->gettid(); - // 2) Select the Thread Priority - - //Thread t2(thread2, NULL, osPriorityNormal); - Thread t2(thread2, NULL, osPriorityAboveNormal); - + //Hook up interrupt + sw1.fall(switchISR); + pc.printf("Main Thread\n"); while (true) { Thread::wait(osWaitForever);