Oliver Thompson / Mbed OS ELEC351-Coursework

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

Revision:
13:37a7c57f4641
Parent:
12:88d33b87ecb2
Child:
14:dbb0741ce576
--- a/SerialComms.hpp	Tue Dec 04 18:15:49 2018 +0000
+++ b/SerialComms.hpp	Wed Dec 05 19:53:40 2018 +0000
@@ -9,9 +9,11 @@
 #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 
 
-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
@@ -21,24 +23,29 @@
          float fPressure;  //current pressure of sensor
          float fLDR;       //current light level from LDR
          vector<int> ErrorCodes;
-         string RxIn;           // Init
+         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), Serial::RxIrq);        // Interrupt if data is received
+            pc.attach(callback(this, &Serialcomms::Rx_Interrupt));        // Interrupt if data is received
         }
+        
         ~Serialcomms()
         {
             printf("Closing Serial Comms.");
@@ -59,7 +66,6 @@
             msg.ldr = fLDR;
             return msg;
         }
-        
         void updateTerminal()                   // Print internal values of sensors
         {            
             //printf("\033[2J");
@@ -86,19 +92,13 @@
         }
         
         void displayFrame()
-        {
-            
-            
+        {  
         }
-        
-
-        
-  
-        void handleInput(string RxIn)
+    
+        void handleInput()
         {
-            printf("Echo: %s", RxIn);           // Debug
-            
-            if (RxIn == "READ ALL")    // Sends a comma seperate list of all measurements.
+            printf("Echo: %s \n", RxIn);  // Debug
+            if (RxIn == "READ ALL")       // Sends a comma seperate list of all measurements.
             {
             }
             else if(RxIn == "DELETE ALL")
@@ -128,22 +128,31 @@
             RxIn = "";                      // Reset the input string         
         }
  
-       void ReadData()
+
+        void ReadData()
         {
-            char c = pc.getc();
-
-            if (c != '\n')
+            __disable_irq();
+            char rx_buffer[buffer_size + 1] = {0};
+            char c;
+            rx_in = 0; 
+            while(1)
             {
-                RxIn.append(1,c);         // Push the next character in the buffer onto the vector
+                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
             }
-            else                          // If the command is terminated
-            {
-                SERIAL_Queue.call(callback(this, &Serialcomms::handleInput), RxIn);   // Process the inpuT
-            }
+            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
@@ -151,7 +160,6 @@
                 ErrorCodes.push_back(ErrorsIn[idx]);
             }
         }
-    
         void updateTimeDate()
         {
         }