Version 5

Dependencies:   BMP280 TextLCD BME280

Sampler.hpp

Committer:
O_Thom
Date:
2018-11-25
Revision:
2:38d31b2e0956
Parent:
1:f89c930c6491
Child:
3:82612f4ae4c5

File content as of revision 2:38d31b2e0956:

#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
{
private:
        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)
    {
        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 activate()
    {
        t1.signal_set(Activate_Flag);   // Signal the sampling thread to move from WAITING to READY
    }

    void samplingThread()
    {
        while(1)
        {
            Thread::signal_wait(Activate_Flag);
            printf("\033[2J"); // Clear screen
            printf("\033[H"); //  Home Position
            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();
            #ifdef BME
            float humidity = sensor.getHumidity();
            #endif
            printf("Temperature: %5.1f\n", temp);
            printf("Pressure: %5.1f\n", pressure);
            #ifdef BME
            printf("Pressure: %5.1f\n", humidity);
            #endif            
            mailqueuePush(temp, pressure, sw1State, sw2state);  // Place onto the mailqueue
        }
    }
        
    Sampler() 
    {   //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(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() 
    {
        
    }
};