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-05
Revision:
13:37a7c57f4641
Parent:
12:88d33b87ecb2
Child:
14:dbb0741ce576

File content as of revision 13:37a7c57f4641:

#include "mbed.h"
#include<string> 
#include <iostream>
#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

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

const int buffer_size = 255;
char rx_buffer[buffer_size+1];

using namespace std;

class Serialcomms
{
    private:
         float fTemp;      //current temperature of sensor
         float fPressure;  //current pressure of sensor
         float fLDR;       //current light level from LDR
         vector<int> ErrorCodes;
         string RxIn;      // Init
         int rx_in;


   public:
       
        EventQueue SERIAL_Queue;                    //Initialise the EventQueue
        
        void Rx_Interrupt()
        {
            SERIAL_Queue.call(callback(this, &Serialcomms::ReadData));               // Read data from the serial buffer -> Pushed onto the event queue
//              ReadData();
        }    
        Serialcomms()
        {
            pc.baud(9600);
            RxIn = "";
            rx_in = 0; 
            printf("Serial Comms Initialised, Baud: 9600\n");
            printf("COMMAND: \n");
            pc.attach(callback(this, &Serialcomms::Rx_Interrupt));        // Interrupt if data is received
        }
        
        ~Serialcomms()
        {
            printf("Closing Serial Comms.");
        }  
        
        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()
        {
            printf("Echo: %s \n", RxIn);  // Debug
            if (RxIn == "READ ALL")       // Sends a comma seperate list of all measurements.
            {
            }
            else if(RxIn == "DELETE ALL")
            {
            }           
            else if(RxIn == "READ")
            {
            } 
            else if(RxIn == "DELETE")
            {
            }         
            else if(RxIn == "SETDATE")
            {
            }     
            else if(RxIn == "SETTIME")
            {
            }       
            else if(RxIn == "SETT")
            {
            }           
            else if(RxIn == "STATE")       
            {
            }      
            else if(RxIn == "LOGGING")     // Verbose logging
            {
            }     
            RxIn = "";                      // Reset the input string         
        }
 

        void ReadData()
        {
            __disable_irq();
            char rx_buffer[buffer_size + 1] = {0};
            char c;
            rx_in = 0; 
            while(1)
            {
                c = pc.getc();
                if (c == 0x0D)  // Enter ASCII Code
                {
                    break;
                }
                printf("Receiving Character: %c \n", c);      
                rx_buffer[rx_in] = c;
                rx_in += 1;    // Increase received indexer
            }
            RxIn = rx_buffer;
            printf("Received: ");
            printf("%s \n", rx_buffer);
           // SERIAL_Queue.call(callback(this, &Serialcomms::handleInput));   // Process the input
            __enable_irq();            
        }

        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()
        {
        }
        

};