Version 5

Dependencies:   BMP280 TextLCD BME280

Revision:
2:38d31b2e0956
Parent:
1:f89c930c6491
Child:
3:82612f4ae4c5
diff -r f89c930c6491 -r 38d31b2e0956 Sampler.hpp
--- a/Sampler.hpp	Sun Nov 25 16:19:10 2018 +0000
+++ b/Sampler.hpp	Sun Nov 25 17:49:47 2018 +0000
@@ -1,42 +1,77 @@
 #include "mbed.h"
 #define Activate_Flag 1
 
+// Class for Sampled Data
+class message_t 
+{
+public:
+    float temp;
+    float pressure;
+    int sw1State;
+    int sw2State;
+    message_t(float f1, float f2, int s1, int s2)
+    {
+        temp = f1;
+        pressure = f2;
+        sw1State = s1;
+        sw2State = s2;
+    }
+};
+
+class MailQueue     
+// Potentially place all of the mail queue code into a class. Include pushing and popping functions
+// Circular buffer management also - Rewrite the oldest sample -> Include in the mail queue or in each respective thread??
+{
+ private:
+ 
+ public:
+    
+};
+
 class Sampler
 {
-friend class Ticker;            // Share the private and protected sections to the non-member function
-
 private:
-        Ticker t;               // Time Initialisation
-        Thread t1;              // Sample Thread
-public:
-
-    void start()
+        Thread t1;                              // Sample Thread
+        MemoryPool<message_t, 20> mpool;        //Memory Pool has 20 data blocks
+        Queue<message_t, 20> queue;             //Message queue
+public: 
+    void mailqueuePush(float tsample, float psample, int switch1State, int switch2State)
     {
-        t.attach(Callback<void()>(this, &Sampler::doISR), 15);
-        post();                 // Hardware Testing     
+        message_t *message = mpool.alloc();         // Allocate a block from the memory pool
+        if (message == NULL)                        // Catch the error if the pool is full
+        {                  
+            printf("Memory Full");                  // Complete the handling of this
+            return;
+        }
+        message->temp = tsample;                    // Load data into the message object
+        message->pressure = psample;
+        message->sw1State = switch1State;
+        message->sw2State = switch2State;
+        osStatus stat = queue.put(message);         // Write the pointer of the message to the queue
+        if (stat == osErrorResource)                // Catch the error if the 'put' failed
+        {
+            printf("queue->put() Error code: %4Xh\r\n", stat);
+            mpool.free(message);
+            return;       
+        }
     }
     
-    void doISR()
+    void activate()
     {
         t1.signal_set(Activate_Flag);   // Signal the sampling thread to move from WAITING to READY
     }
-    
-    void mailqueuePush()
+
+    void samplingThread()
     {
-        
-    }
-
-    static void samplingThread()
-    {
-        int idx = 0;
         while(1)
         {
             Thread::signal_wait(Activate_Flag);
-            idx++;
             printf("\033[2J"); // Clear screen
             printf("\033[H"); //  Home Position
-            printf("**********Sample %d**********\n", idx);          
-            printf("SW1: %d\tSW2: %d\n\r", SW1.read(), SW2.read());    
+            printf("**********Sample**********\n");  
+            int sw1State = SW1.read();
+            int sw2state = SW2.read();        
+            printf("SW1: %d\tSW2: %d\n\r", sw1State, sw2state);    
             printf("LDR: %3.5f\n\r", adcIn.read()*4095);
             float temp = sensor.getTemperature();
             float pressure = sensor.getPressure();
@@ -47,25 +82,25 @@
             printf("Pressure: %5.1f\n", pressure);
             #ifdef BME
             printf("Pressure: %5.1f\n", humidity);
-            #endif
-            puts("**********POST END**********");  
-        } 
+            #endif            
+            mailqueuePush(temp, pressure, sw1State, sw2state);  // Place onto the mailqueue
+        }
     }
         
     Sampler() 
-    {                     //Constructor 
+    {   //Constructor 
         // IDs
         osThreadId idMain;
         osThreadId idSample;
-        idMain = osThreadGetId();   // CMSIS RTOS Call
-        idSample = t1.gettid();     // Assign the id to the thread handle (Check this)
-        t1.start(samplingThread);   // Start the sampling thread
+        idMain = osThreadGetId();               // CMSIS RTOS Call
+        idSample = t1.gettid();                 // Assign the id to the thread handle (Check this)
+        t1.start(this, &Sampler::samplingThread);               // Start the sampling thread
         // NVIC_SetPriority(TIMER0_IRQn,osPriorityHigh);     // Uncomment for priority setting in the NVIC
     } 
     
     //Destructor - should the instance go out of scope, this is called
     ~Sampler() 
     {
-        t.detach();
+        
     }
 };
\ No newline at end of file