Repo. for the ELEC351 Coursework - Oliver Thompson

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

SerialComms.hpp

Committer:
O_Thom
Date:
2018-12-08
Revision:
17:da78a552339d
Parent:
16:73af0e3ddcaa
Child:
18:a036c2e5ff89

File content as of revision 17:da78a552339d:

#include "mbed.h"
#include "mbed_events.h"
#include <stdio.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

#define cmdRow 36
#define cmdCol 37
Serial pc(USBTX, USBRX);   // USB Tx and Rx Connections 

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

struct Timestamp
{
 int Day;
 int Month;
 int Year;
 int Second;
 int Minute;
 int Hour;
};

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;
         int rx_in; 
         //char rx_buffer[buffer_size + 1];
         int cmdCount;
         int rxInputFlag;
         char RxIn[buffer_size+1];
         char clearingArray[buffer_size+1];
         char timestampArray[buffer_size+1];
   public:
        EventQueue SERIAL_Queue;                    //Initialise the EventQueue
        void displayFrame()
        { 
            printf("\033[2J"); // Clear screen
            printf("\033[0;0H"); // Home Positon Reset
            printf("*********************************************************************************\n"
                   "*                    ELEC351 COURSEWORK ENVIRONMENTAL SYSTEM                    *\n"
                   "*********************************************************************************\n"
                   "*        Sensor Readout         *                     Terminal                  *\n"
                   "*********************************************************************************\n"
                   "* Timestamp:                    *                                               *\n" // Col 5 Row 13
                   "* Temperature:                  *                                               *\n" // Col 6 Row 15
                   "* Pressure:                     *                                               *\n" // Col 7 Row 12
                   "* Light Level:                  *                                               *\n" // Col 8 Row15
                   "*********************************                                               *\n"
                   "*         Error Codes           *                                               *\n" //Col 10                  
                   "*********************************                                               *\n" // Col 11 Row 2
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*********************************                                               *\n"
                   "*         Thread Health         *                                               *\n"
                   "*********************************                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n" 
                   "*                               *                                               *\n"    // cmd position r:36 c:37 
                   "*********************************************************************************\n\n"    
                   );          
            printf("\033[%d;%dfcmd: ", cmdRow, cmdCol);

        }  
        
        Serialcomms()
        {
            pc.baud(9600);
            rx_in = 0; 
            cmdCount = 0;
            fTemp = 0;
            fPressure = 0;
            fLDR = 0;
            for (int i = 0; i < buffer_size+1; i++)
            {
                clearingArray[i] = ' ';
                timestampArray[i] = 0;
                rx_buffer[i] = 0;
            }
            displayFrame();
            SERIAL_Queue.call_every(1000, callback(this, &Serialcomms::updateTerminal));
            SERIAL_Queue.call_every(50, callback(this, &Serialcomms::ReadData));           // Start the periodic event queue to check the buffer
        }
        
        ~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[6;16f%s", timestampArray);  
            printf("\033[7;16f%5.2f", fTemp);
            printf("\033[8;16f%5.2f", fPressure);
            printf("\033[9;16f%5.2f", fLDR);
            if (ErrorCodes.size() == 0)
            {
                printf("\033[13;3fNo Error Codes Raised\n");
            }
            else
            {
             for (int idx = 0; idx < ErrorCodes.size(); idx++)
             {
                printf("\033[%d;3: %d\n",(11+idx),ErrorCodes[idx]);                    
             }
            }
            // Add code to receive feedback from watchdog and place into thread safe section
        }
 
         
        void printstringtoTerminal(char Data[])
        {
            printf("\033[%d;%df%s", (cmdRow-(cmdCount+1)),(cmdCol+5), Data);  // Confirmation of Command
            cmdCount++; 
        }
        void printintegertoTerminal(int Data)
        {
            printf("\033[%d;%df%d", (cmdRow-(cmdCount+1)),(cmdCol+5), Data);  // Confirmation of Command   
            cmdCount++;              
        }
        
              
        int searchInput(char stream[], char command[])
        {
            int match = 0;
            const int commandLength = strlen(command); 
            for (int i = 0; i < commandLength; i++)
            {
                if  (stream[i] == command[i])
                {
                    match = i;              // Return the position of the last command character
                }
                else
                {
                    match = 0;
                    i = commandLength-1;
                }
            }           
            return match;
        }
        

        
        void handleInput()
        {   
            if (int readall = searchInput(RxIn, "READ ALL") > 0)     
            {
                printstringtoTerminal("READING ALL");
            }
            else if(int readall = searchInput(RxIn, "DELETE ALL") > 0)
            {
                printf("\033[%d;%df%s", (cmdRow-(cmdCount+1)),(cmdCol+5), "DELETING ALL");  // Confirmation of Command
                cmdCount++;
            }           
            else if(int read = searchInput(RxIn, "READ") > 0)
            {
                read = searchInput(RxIn, "READ");
                char count[buffer_size] = {0};
                for (int i = read; i < strlen(RxIn); i++)
                {
                    count[i-read] = RxIn[i+1];
                }
            }
            else if(int del = searchInput(RxIn, "DELETE") > 0)
            {
                del = searchInput(RxIn, "DELETE");
                char count[buffer_size] = {0};
                for (int i = del; i < strlen(RxIn); i++)
                {
                    count[i-del] = RxIn[i+1];
                }
            }         
            else if(int setdate = searchInput(RxIn, "SETDATE") > 0) //<dd> <mm> <yyyy>
            {
                setdate = searchInput(RxIn, "SETDATE");
                char count[buffer_size] = {0};
                for (int i = setdate; i < strlen(RxIn); i++)
                {
                    count[i-setdate] = RxIn[i+1];
                    timestampArray[i-setdate] = RxIn[i+1];
                }
            }     
            else if(int settime = searchInput(RxIn, "SETTIME") > 0) //<hh> <mm> <ss>
            {
                settime = searchInput(RxIn, "SETTIME");
                char count[buffer_size] = {0};
                for (int i = settime; i < strlen(RxIn); i++)
                {
                    count[i-settime] = RxIn[i+1];
                    timestampArray[i-settime+6] = RxIn[i+1];
                }           
            }        
            else if(int sett = searchInput(RxIn, "SETT") > 0)
            {
            }           
            else if(int state = searchInput(RxIn, "STATE") > 0)       
            {
            }      
            else if(int logging = searchInput(RxIn, "LOGGING") > 0)     // Verbose logging
            {
            }
            else if(int clearall = searchInput(RxIn, "CLEAR ALL") > 0)
            {
                cmdCount = 0;
                for (int i = 0; i < 22; i++)
                {
                    printstringtoTerminal(clearingArray);
                }
                cmdCount = 0;
            }
            else
            {
                printstringtoTerminal("UNKNOWN COMMAND");
            }

            for (int i = 0; i < (buffer_size+1); i++)   // Init
            {
                RxIn[i] = 0;   
            }
            printf("\033[%d;%dfcmd: ", cmdRow, cmdCol);  // Reset to cmd location      
        }
 

        void ReadData()
        {
            while(pc.readable())                // While there's data in the Rx buffer
            {
                char c = pc.getc();             // Take a character off the buffer
                if (c == 0x0D)                  // Enter ASCII Code
                {
                    printf("\033[%d;%df%s", (cmdRow-(cmdCount+1)),(cmdCol+5), rx_buffer);     // Echo the Command back to the terminal
                    rx_in = 0;                  // Reset the buffer indexer
                    int stringLength = strlen(rx_buffer); //finds length of the array
                    for (int i = 0; i < stringLength; i++) {
                        RxIn[i] = rx_buffer[i]; // Copies buffer into global // stringLength-1-i
                    }
                    RxIn[stringLength] = '\0'; //adds NULL character
                    SERIAL_Queue.call(callback(this, &Serialcomms::handleInput));
                    
                    for (int i = 0 ; i < stringLength; i++)
                    {
                        rx_buffer[i] = 0;       // Clear the buffer with spaces
                    }
                    printf("\033[%d;%df%s", cmdRow, (cmdCol+5), clearingArray);                 // Clear the Command entry space
                    //printf("\033[%d;%df%s", (cmdRow -(cmdCount+2)), cmdCol,  clearingArray);  // Clear above the oldest record Command

                    if (cmdCount >= 20)
                    {
                        cmdCount = 0;
                    }
                    cmdCount++;

                    break;
                }
                rx_buffer[rx_in] = c;
                printf("\033[%d;%df%s",cmdRow, (cmdCol+5), rx_buffer);  // Echo     
                rx_in += 1;    // Increase received indexer
            }
        }

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

};