Task 641

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task641-mbedos54 by Nicholas Outram

Files at this revision

API Documentation at this revision

Comitter:
noutram
Date:
Thu Nov 23 14:13:34 2017 +0000
Parent:
11:09fc22b7f251
Commit message:
Task 641

Changed in this revision

ELEC350-Practicals-FZ429.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ELEC350-Practicals-FZ429.lib	Thu Nov 23 14:13:34 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/University-of-Plymouth-Stage-2-and-3/code/ELEC350-Practicals-FZ429/#24eb98cf2376
--- a/main.cpp	Mon Apr 03 15:02:50 2017 +0000
+++ b/main.cpp	Thu Nov 23 14:13:34 2017 +0000
@@ -1,36 +1,24 @@
 #include "mbed.h"
-#include "rtos.h"
 #include "string.h"
 #include <stdio.h>
 #include <ctype.h>
+#include "sample_hardware.hpp"
 
 #define SWITCH1_RELEASE 1
 
+void getSample();
 void thread1();
-void thread2();
 void switchISR();
 
-//Analogue inputs
-AnalogIn adcIn(A0);
+//Threads
+Thread samplingThread(osPriorityRealtime);
+Thread t1;
 
-//Digital outputs
-DigitalOut onBoardLED(LED1);
-DigitalOut redLED(D7);
-DigitalOut yellowLED(D6);
-DigitalOut greenLED(D5);
-
-//Digital inputs
-DigitalIn  onBoardSwitch(USER_BUTTON);
-DigitalIn  sw1(D4);                         //CONSIDER CHANGING THIS TO AN INTERRUPT
-DigitalIn  sw2(D3);
-
-//Threads
-Thread *t1;
 
 //Class type
 class message_t {
 public:    
-    float    adcValue;    
+    float adcValue;    
     int sw1State;
     int sw2State;
     
@@ -48,40 +36,54 @@
 //Message queue - matched to the memory pool
 Queue<message_t, 16> queue;
 
+
 // 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 = mpool.alloc();
-    if (message == NULL) {
-        //Out of memory
-        redLED = 1;
-        return;   
-    }
+    //This is going to be brief ;)
+    samplingThread.signal_set(1);
+}
+
+//High priority thread dedicated to sampling
+void getSample() {
     
-    //Fill in the data
-    message->adcValue = sample;
-    message->sw1State = switch1State;
-    message->sw2State = switch2State;
-    
-    //Write to queue
-    osStatus stat = queue.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);   
-        mpool.free(message);
-        return;
-    }
+    while(1) {
+        //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 = mpool.alloc();
+        if (message == NULL) {
+            //Out of memory
+            redLED = 1;
+            return;   
+        }
+        
+        //Fill in the data
+        message->adcValue = sample;
+        message->sw1State = switch1State;
+        message->sw2State = switch2State;
+        
+        //Write to queue
+        osStatus stat = queue.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);   
+            mpool.free(message);
+            return;
+        }        
+    } //end while
 }
 
+
+
 //Normal priority thread (consumer)
 void thread1() 
 {      
@@ -92,15 +94,15 @@
         //Check status
         if (evt.status == osEventMessage) {
             message_t *pMessage = (message_t*)evt.value.p;  //This is the pointer (address)
-            //Make a copy
+            //Construct a COPY
             message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State);
             //We are done with this, so give back the memory to the pool
             mpool.free(pMessage);
             
             //Echo to the terminal
             printf("ADC Value: %.2f\t",    msg.adcValue);
-            printf("SW1: %u\t",             msg.sw1State);
-            printf("SW2: %u\n\r",             msg.sw2State);
+            printf("SW1: %u\t",            msg.sw1State);
+            printf("SW2: %u\t\r",          msg.sw2State);
         }
 
              
@@ -122,8 +124,8 @@
     timer.attach(&adcISR, 0.1);
                
     //Threads
-    t1 = new Thread();
-    t1->start(thread1); 
+    samplingThread.start(getSample);
+    t1.start(thread1); 
     
     printf("Main Thread\n");
     while (true) {
--- a/mbed-os.lib	Mon Apr 03 15:02:50 2017 +0000
+++ b/mbed-os.lib	Thu Nov 23 14:13:34 2017 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#50b3418e45484ebf442b88cd935a2d5355402d7d
+https://github.com/ARMmbed/mbed-os/#34b61d1612aaec2e5d61a8a43c9764bc2fe8aac0