gdfgdh
Dependencies: ELEC350-Practicals-FZ429
Fork of Task633-mbedos-FZ429 by
Diff: main.cpp
- Revision:
- 9:31031bbb59c7
- Parent:
- 8:c5663f5fa848
- Child:
- 10:3a3d2a571c8f
diff -r c5663f5fa848 -r 31031bbb59c7 main.cpp --- a/main.cpp Mon Mar 14 19:48:24 2016 +0000 +++ b/main.cpp Mon Mar 21 13:50:11 2016 +0000 @@ -10,15 +10,15 @@ void thread2( const void* ); void switchISR(); +//Analogue inputs +AnalogIn adcIn(A0); + //Digital outputs DigitalOut onBoardLED(LED1); DigitalOut redLED(D7); DigitalOut yellowLED(D6); DigitalOut greenLED(D5); -//Serial Interface -Serial pc(USBTX, USBRX); - //Digital inputs DigitalIn onBoardSwitch(USER_BUTTON); DigitalIn sw1(D4); //CONSIDER CHANGING THIS TO AN INTERRUPT @@ -26,50 +26,60 @@ //Threads Thread *t1; -Thread *t2; -//Thread ID for the Main function (CMSIS API) -osThreadId tidMain; -osThreadId tid1; -osThreadId tid2; +//Queues - "A message can be a integer or pointer value to a certain type T that is sent to a thread or interrupt service routine." +Queue<uint32_t, 5> *queue; -//TBD: Call this on the falling edge of SW1 -void switchISR() { - //TBD +// Call this on precise intervals +void adcISR() { + + //Option to starve the queue + if (sw2 == 1) return; + + //Read sample - make a copy + uint32_t sample = (uint32_t)(4095*adcIn.read()); + + //Write to queue + osStatus stat = queue->put((uint32_t*)sample); + + //Check if succesful + if (stat == osErrorResource) { + redLED = 1; + printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); + } + } -//High priority thread +//Normal priority thread (consumer) void thread1( const void* arg ) -{ - redLED = 1; +{ while (true) { - - // THIS IS BAD BAD BAD BAD BAD BAD - // BLOCK ON FALLING EDGE OF SW1 BY RAPID POLLING (SPINNING) - // Thread is blocked in the RUNNING state - while (sw1 == 0); - wait_ms(200); //Wait for debounce - while (sw1 == 1); - // TODO: FIX THIS! GET THE INTERRUPT TO SIGNAL THIS THREAD - - redLED = !redLED; - Thread::wait(1000); //Thread in WAITING state - redLED = !redLED; - Thread::wait(1000); //Thread in WAITING state - } -} - -// This thread has normal priority -// It is supposed to flash the green LED every second. -// THIS IS NOT WORKING because it is currently being starved by thread1 (while polling the switch) -void thread2( const void* arg ) -{ - greenLED = 1; - while (true) { - Thread::wait(500); - greenLED = !greenLED; - } + //Read queue - block (with timeout) + osEvent evt = queue->get(5000); //With timeout + + //Check status of get() + switch (evt.status) { + case osEventMessage: + //Normal status + printf("value = %d\n\r", evt.value.v); + greenLED = !greenLED; + break; + case osEventTimeout: + //Timeout + printf("queue->get() returned %02x status (timeout)\n\r", evt.status); + break; + default: + //All other errors (see cmsis_os.h for meaning of error code) + printf("queue->get() returned %02x status\n\r", evt.status); + break; + } + + //Block up consumer if switch is held down + //Will fill the queue if held long enough + while (sw1 == 1); + + } //end while } @@ -79,20 +89,23 @@ yellowLED = 0; greenLED = 0; + //Start message + printf("Welcome\n"); + + //Queue + queue = new Queue<uint32_t,5>(); + + //Hook up timer interrupt + Ticker timer; + timer.attach(&adcISR, 1.0); + //Threads - t1 = new Thread(&thread1, NULL, osPriorityRealtime); //HIGH PRIORITY - t2 = new Thread(&thread2, NULL, osPriorityNormal); - - // Thread IDs - tidMain = Thread::gettid(); - tid1 = t1->gettid(); - tid2 = t2->gettid(); + t1 = new Thread(&thread1); - //TBD: Hook up interrupt - - pc.printf("Main Thread\n"); + printf("Main Thread\n"); while (true) { - Thread::wait(osWaitForever); + Thread::wait(5000); + puts("Main Thread Alive"); } }