Demonstration of EventQueues

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

Revision:
10:3ab2495f24d9
Parent:
9:c46e831f8e4a
Child:
11:67cb33bc247e
diff -r c46e831f8e4a -r 3ab2495f24d9 main.cpp
--- a/main.cpp	Mon Apr 03 14:02:26 2017 +0000
+++ b/main.cpp	Mon Nov 20 10:51:32 2017 +0000
@@ -1,96 +1,69 @@
 #include "mbed.h"
-#include "rtos.h"
-#include "string.h"
-#include <stdio.h>
-#include <ctype.h>
-
-#define SWITCH1_RELEASE 1
+#include <iostream>
+#include "sample_hardware.hpp"
+#include "mbed_events.h"
+void buttonPressedISR();
+void addSample(float t);
+void heartBeat();
+ 
+DigitalOut led(LED1);
+InterruptIn btn(USER_BUTTON);
 
-void thread1();
-void thread2();
-void switchISR();
+//Creates a queue with the default size
+EventQueue mainQueue(20);
 
-//Digital outputs
-DigitalOut onBoardLED(LED1);
-DigitalOut redLED(D7);
-DigitalOut yellowLED(D6);
-DigitalOut greenLED(D5);
+Timer tmr1;
+Thread t1;
 
-//Serial Interface
-Serial pc(USBTX, USBRX);
-
-//Digital inputs
-DigitalIn  onBoardSwitch(USER_BUTTON);
-InterruptIn  sw1(D4);
-DigitalIn    sw2(D3);
+//Buffer
+float arrayOfTimes[10];
 
-//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
+int main() {
+    //Power on self test
+    post();
+    
+    //Set up ISR
+    btn.fall(buttonPressedISR);
+    
+    //Start timer
+    tmr1.start();
+    
+    //Background flash (backed by a Ticker)
+    mainQueue.call_every(500, heartBeat);
+    
+    // events are executed by the dispatch method
+    mainQueue.dispatch();
+    
+    //Unreachable code
+    printf("This should never appear!\n");
 }
 
-//High priority thread
-void thread1() 
-{
-    redLED = 1;
-    while (true) {
-         Thread::signal_wait(SWITCH1_RELEASE);
-         redLED = !redLED;
-         Thread::wait(1000);
-         redLED = !redLED;
-         Thread::wait(1000);  
-         t1->signal_clr(SWITCH1_RELEASE);   //Debounce
-    }
-}
-
-//This thread has normal priority
-void thread2() 
-{
-    greenLED = 1; 
-    while (true) {
-        //Thread::wait(2000);    
-        Thread::wait(500);          // WAIT FOR 0.5 SECONDS
-        greenLED = !greenLED;
-    }
+void buttonPressedISR() { 
+    float t = tmr1.read();  //Immediately read time in seconds
+    mainQueue.call(addSample, t);  
 }
 
-
-// Main thread
-int main() {
-    redLED    = 0;
-    yellowLED = 0;
-    greenLED  = 0;
-           
-    //Threads
-    t1 = new Thread(osPriorityRealtime);
-    t2 = new Thread(osPriorityNormal);              
-       
-    t1->start(thread1);
-    t2->start(thread2);
-           
-    // Thread IDs
-    tidMain = Thread::gettid();  
-    tid1    = t1->gettid();
-    tid2    = t2->gettid();
+// Add sample to buffer
+// Single parameter t, copy of latest switch time
+void addSample(float t) {
+    
+    //Manually shuffle the buffer
+    for (unsigned int n=9; n>0; n--) {
+        arrayOfTimes[n] = arrayOfTimes[n-1];
+    }
     
-    //Hook up interrupt
-    sw1.fall(switchISR);      
-        
-    pc.printf("Main Thread\n");
-    while (true) {
-        Thread::wait(osWaitForever);
+    //Add new sample
+    arrayOfTimes[0] = t;
+    
+    //Display deltas
+    for (unsigned int n=9; n>0; n--) {
+        float delta = (arrayOfTimes[n] - arrayOfTimes[n-1]);
+        printf("%5.1f ", delta);
     }
-
+    printf("\n");
 }
 
-   
+//Flashing LED 
+void heartBeat() {
+    led = !led;
+}