Oliver Thompson / Mbed OS ELEC351-Coursework

Dependencies:   BMP280 ELEC350-Practicals-FZ429- TextLCD watchdog_RTOS BME280 ntp-client

SerialComms.hpp

Committer:
O_Thom
Date:
2018-12-04
Revision:
10:08c366434f2b
Parent:
9:654e14de9d74
Child:
11:b8e8630c7e3b

File content as of revision 10:08c366434f2b:

#include "mbed.h"
#include<string> 
#include <vector>

#define ERROR_LCD_EXIT 1
#define ERROR_SERIAL_EXIT 2
#define ERROR_SD_EXIT 3
#define ERROR_NET_EXIT 4
#define ERROR_SAMPLER_EXIT 5

//enum ErrorCodes {ERROR_LCD_EXIT,ERROR_SERIAL_EXIT,ERROR_SD_EXIT,ERROR_NET_EXIT};

Serial pc(USBTX, USBRX);   // USB Tx and Rx Connections 


class Serialcomms
{
    friend class Serial;
    private:
         float fTemp;      //current temperature of sensor
         float fPressure;  //current pressure of sensor
         float fLDR;       //current light level from LDR
         vector<int> ErrorCodes;
    public:
       
        EventQueue SERIAL_Queue;                    //Initialise the EventQueue
        
        void Rx_Interrupt(void)
        {
           SERIAL_Queue.call(callback(this, &Serialcomms::ReadData));               // Read data from the serial buffer -> Pushed onto the event queue
        }      
        
        
        Serialcomms()
        {
            pc.baud(9600);
            printf("Serial Comms Initialised, Baud: 9600\n");
            pc.attach(callback(this, &Serialcomms::Rx_Interrupt), Serial::RxIrq);        // Interrupt if data is received
        }
        ~Serialcomms()
        {
        }  
        
        void setsampledata(sample_message msg)      // Update internal values
        {
            fTemp = msg.temp;
            fPressure = msg.pressure;
            fLDR = msg.ldr;   
        }
        
        sample_message getsampledata()              // Retrieves the data
        {
            sample_message msg;
            msg.temp = fTemp;
            msg.pressure = fPressure;
            msg.ldr = fLDR;
            return msg;
        }
        
        void updateTerminal()                   // Print internal values of sensors
        {            
            //printf("\033[2J");
            printf("\033[H");
            printf("======= Sensor Update ========\n");
            printf("Temperate: %5.2f\n", fTemp);
            printf("Pressure: %5.2f\n", fPressure);
            printf("Light Level: %5.2f\n", fLDR);
            printf("==============================\n");
            printf("Error Codes: ");
            if (ErrorCodes.size() == 0)
            {
                printf("No Error Codes Raised\n");
            }
            else
            {
             for (int idx = 0; idx < ErrorCodes.size(); idx++)
             {
                printf("%d: %d\n", idx, ErrorCodes[idx]);                    
             }
            }
            printf("Thread Health: \n\n"); 
            // Add code to receive feedback from watchdog
        }
        
        void displayFrame()
        {
            
            
        }
        

        
  
        void handleInput(vector<char> RxIn)
        {
            string Input = ( RxIn.begin(), RxIn.end() );    // Place into a string
            printf("Inputted String: %s", Input);           // Debug
            
            if (Input == "READ ALL")    // Sends a comma seperate list of all measurements.
            {
            }
            else if(Input == "DELETE ALL")
            {
            }           
            else if(Input == "READ")
            {
            } 
            else if(Input == "DELETE")
            {
            }         
            else if(Input == "SETDATE")
            {
            }     
            else if(Input == "SETTIME")
            {
            }       
            else if(Input == "SETT")
            {
            }           
            else if(Input == "STATE")       
            {
            }      
            else if(Input == "LOGGING")     // Verbose logging
            {
            }                      
        }
 
       void ReadData()
        {
            char c;
            vector<char> RxIn;
            do                          // While there is data in the buffer
            {
                c = pc.getc();          // Receive the character from the serial buffer
                RxIn.push_back(c);      // Push the next character in the buffer onto the vector
            } while(c != '\n');         // BLOCKING
            SERIAL_Queue.call(callback(this, &Serialcomms::handleInput), RxIn);   // Process the input
        }
               
        
        
        void updateErrors(vector<int> ErrorsIn)
        {
            for (int idx = 0; idx < ErrorsIn.size() ; idx++)    // Add the Error Codes to the vector
            {
                ErrorCodes.push_back(ErrorsIn[idx]);
            }
        }
    
        void updateTimeDate()
        {
        }
        

};

Serialcomms m_oSerial;