Mail Queue Task

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task633-mbedos54 by Nicholas Outram

Revision:
13:f05daa68720c
Parent:
12:4605f1c51824
Child:
14:03836b560d3d
--- a/main.cpp	Tue Nov 14 18:15:59 2017 +0000
+++ b/main.cpp	Tue Nov 21 11:36:41 2017 +0000
@@ -12,12 +12,13 @@
 
 
 //Threads
+Thread samplingThread(osPriorityRealtime);
 Thread t1;
 
 //Class type
 class message_t {
 public:    
-    float    adcValue;    
+    float adcValue;    
     int sw1State;
     int sw2State;
     
@@ -41,40 +42,47 @@
 
 // Call this on precise intervals
 void adcISR() {
-
-    
-    //Read sample - make a copy
-    float sample = adcIn;
-    //Grab switch state
-    uint32_t switch1State = SW1;
-    uint32_t switch2State = SW2;
-    
-    //Allocate a block from the memory pool
-    message_t *message = mail_box.alloc();
-    if (message == NULL) {
-        //Out of memory
-        printf("Out of memory\n\r");
-        redLED = 1;
-        return;   
-    }
+    //This is going to be brief
+    samplingThread.signal_set(1);    
+}
+void doSample() {
     
-    //Fill in the data
-    message->adcValue = sample;
-    message->sw1State = switch1State;
-    message->sw2State = switch2State;
-    
-    //Write to queue
-    osStatus stat = mail_box.put(message);    //Note we are sending the "pointer"
-    
-    //Check if succesful
-    if (stat == osErrorResource) {
-        redLED = 1; 
-        printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);   
-        mail_box.free(message);
-        return;
-    }
-    
-    
+    while (true) {
+        //Waiting for ISR in WAITING state
+        Thread::signal_wait(1);
+                
+        //Read sample - make a copy
+        float sample = adcIn;
+        //Grab switch state
+        uint32_t switch1State = SW1;
+        uint32_t switch2State = SW2;
+        
+        //Allocate a block from the memory pool
+        message_t *message = mail_box.alloc();
+        if (message == NULL) {
+            //Out of memory
+            printf("Out of memory\n\r");
+            redLED = 1;
+            return;   
+        }
+        
+        //Fill in the data
+        message->adcValue = sample;
+        message->sw1State = switch1State;
+        message->sw2State = switch2State;
+        
+        //Write to queue
+        osStatus stat = mail_box.put(message);    //Note we are sending the "pointer"
+        
+        //Check if succesful
+        if (stat == osErrorResource) {
+            redLED = 1; 
+            printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);   
+            mail_box.free(message);
+            return;
+        }
+        
+    } //end whie    
 }
 
 //Normal priority thread (consumer)
@@ -117,6 +125,7 @@
     timer.attach(&adcISR, 0.1);
                
     //Threads
+    samplingThread.start(doSample); //High Priority
     t1.start(thread1); 
     
     printf("Main Thread\n");