Version 5

Dependencies:   BMP280 TextLCD BME280

Sampler.hpp

Committer:
O_Thom
Date:
2018-11-23
Revision:
0:f9a18207d99c
Child:
1:f89c930c6491

File content as of revision 0:f9a18207d99c:

#include "mbed.h"
#define Activate_Flag 1

class Sampler
{
    friend class Ticker;
    
private:
        Ticker t;               // Time Initialisation
        Thread t1;              // Sample Thread
public:

    void start()
    {
       // t.attach(Callback(this,&doISR), 2);        // Start the ticker to 0.5 seconds to test   
        t.attach(Callback<void()>(this, &Sampler::doISR), 2);
        post();                     // Hardware Testing     
    }
    
    void doISR()
    {
        t1.signal_set(Activate_Flag);   // Signal the sampling thread to move from WAITING to READY
    }
    

        
    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("LDR: %f\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
            puts("**********POST END**********");  
        } 
    }
        
    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(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();
    }
};