Repo. for the ELEC351 Coursework - Oliver Thompson

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

Committer:
O_Thom
Date:
Thu Dec 06 18:39:04 2018 +0000
Revision:
14:dbb0741ce576
Parent:
13:37a7c57f4641
Child:
15:f8748fd0c5b1
Serial working - Terminal Formatting and Input Placement Pending

Who changed what in which revision?

UserRevisionLine numberNew contents of line
O_Thom 6:b7f6e0c0f646 1 #include "mbed.h"
O_Thom 14:dbb0741ce576 2 #include "mbed_events.h"
O_Thom 14:dbb0741ce576 3 #include <stdio.h>
O_Thom 14:dbb0741ce576 4
O_Thom 10:08c366434f2b 5 #include<string>
O_Thom 11:b8e8630c7e3b 6 #include <iostream>
O_Thom 9:654e14de9d74 7 #include <vector>
O_Thom 10:08c366434f2b 8
O_Thom 9:654e14de9d74 9 #define ERROR_LCD_EXIT 1
O_Thom 9:654e14de9d74 10 #define ERROR_SERIAL_EXIT 2
O_Thom 9:654e14de9d74 11 #define ERROR_SD_EXIT 3
O_Thom 9:654e14de9d74 12 #define ERROR_NET_EXIT 4
O_Thom 9:654e14de9d74 13 #define ERROR_SAMPLER_EXIT 5
O_Thom 9:654e14de9d74 14
O_Thom 13:37a7c57f4641 15 Serial pc(USBTX, USBRX); // USB Tx and Rx Connections
O_Thom 6:b7f6e0c0f646 16
O_Thom 13:37a7c57f4641 17 const int buffer_size = 255;
O_Thom 13:37a7c57f4641 18 char rx_buffer[buffer_size+1];
O_Thom 13:37a7c57f4641 19
O_Thom 11:b8e8630c7e3b 20 using namespace std;
O_Thom 10:08c366434f2b 21
O_Thom 6:b7f6e0c0f646 22 class Serialcomms
O_Thom 1:f89c930c6491 23 {
O_Thom 1:f89c930c6491 24 private:
O_Thom 8:7d218affea71 25 float fTemp; //current temperature of sensor
O_Thom 8:7d218affea71 26 float fPressure; //current pressure of sensor
O_Thom 9:654e14de9d74 27 float fLDR; //current light level from LDR
O_Thom 9:654e14de9d74 28 vector<int> ErrorCodes;
O_Thom 13:37a7c57f4641 29 string RxIn; // Init
O_Thom 13:37a7c57f4641 30 int rx_in;
O_Thom 14:dbb0741ce576 31
O_Thom 11:b8e8630c7e3b 32
O_Thom 11:b8e8630c7e3b 33 public:
O_Thom 10:08c366434f2b 34
O_Thom 9:654e14de9d74 35 EventQueue SERIAL_Queue; //Initialise the EventQueue
O_Thom 14:dbb0741ce576 36
O_Thom 14:dbb0741ce576 37 void displayFrame()
O_Thom 14:dbb0741ce576 38 {
O_Thom 14:dbb0741ce576 39 // ESC[#;#H Move the positon (Line/Column) ("\033
O_Thom 14:dbb0741ce576 40 // - Position the Cursor:
O_Thom 14:dbb0741ce576 41 // \033[<L>;<C>H
O_Thom 14:dbb0741ce576 42 // Or
O_Thom 14:dbb0741ce576 43 // \033[<L>;<C>f
O_Thom 14:dbb0741ce576 44 // puts the cursor at line L and column C.
O_Thom 14:dbb0741ce576 45 //- Move the cursor up N lines:
O_Thom 14:dbb0741ce576 46 // \033[<N>A
O_Thom 14:dbb0741ce576 47 //- Move the cursor down N lines:
O_Thom 14:dbb0741ce576 48 // \033[<N>B
O_Thom 14:dbb0741ce576 49 //- Move the cursor forward N columns:
O_Thom 14:dbb0741ce576 50 // \033[<N>C
O_Thom 14:dbb0741ce576 51 //- Move the cursor backward N columns:
O_Thom 14:dbb0741ce576 52 // \033[<N>D
O_Thom 14:dbb0741ce576 53 //
O_Thom 14:dbb0741ce576 54 //- Clear the screen, move to (0,0):
O_Thom 14:dbb0741ce576 55 // \033[2J
O_Thom 14:dbb0741ce576 56 //- Erase to end of line:
O_Thom 14:dbb0741ce576 57 // \033[K
O_Thom 14:dbb0741ce576 58 //
O_Thom 14:dbb0741ce576 59 //- Save cursor position:
O_Thom 14:dbb0741ce576 60 // \033[s
O_Thom 14:dbb0741ce576 61 //- Restore cursor position:
O_Thom 14:dbb0741ce576 62 // \033[u
O_Thom 13:37a7c57f4641 63
O_Thom 14:dbb0741ce576 64 printf("\033[2J"); // Clear screen
O_Thom 14:dbb0741ce576 65 printf("\033[0;0H"); // Home Positon Reset
O_Thom 14:dbb0741ce576 66 printf("*************************************************\n"
O_Thom 14:dbb0741ce576 67 "* Environmental System *\n"
O_Thom 14:dbb0741ce576 68 "*************************************************\n"
O_Thom 14:dbb0741ce576 69 "* Sensor Readout * Terminal *\n"
O_Thom 14:dbb0741ce576 70 "*************************************************\n"
O_Thom 14:dbb0741ce576 71 "* Timestamp: * *\n"
O_Thom 14:dbb0741ce576 72 "* Pressure: * *\n"
O_Thom 14:dbb0741ce576 73 "* Light Level: * *\n"
O_Thom 14:dbb0741ce576 74 "************************ *\n"
O_Thom 14:dbb0741ce576 75 "* Error Codes * *\n"
O_Thom 14:dbb0741ce576 76 "************************ *\n"
O_Thom 14:dbb0741ce576 77 "* * *\n"
O_Thom 14:dbb0741ce576 78 "* * *\n"
O_Thom 14:dbb0741ce576 79 "* * *\n"
O_Thom 14:dbb0741ce576 80 "* * *\n"
O_Thom 14:dbb0741ce576 81 "* * *\n"
O_Thom 14:dbb0741ce576 82 "************************ *\n"
O_Thom 14:dbb0741ce576 83 "* Thread Health * *\n"
O_Thom 14:dbb0741ce576 84 "************************ *\n"
O_Thom 14:dbb0741ce576 85 "* * *\n"
O_Thom 14:dbb0741ce576 86 "* * *\n"
O_Thom 14:dbb0741ce576 87 "* * *\n"
O_Thom 14:dbb0741ce576 88 "* * cmd: *\n" // cmd position r:23 c:30
O_Thom 14:dbb0741ce576 89 "*************************************************\n"
O_Thom 14:dbb0741ce576 90 );
O_Thom 14:dbb0741ce576 91 }
O_Thom 14:dbb0741ce576 92
O_Thom 10:08c366434f2b 93 Serialcomms()
O_Thom 10:08c366434f2b 94 {
O_Thom 10:08c366434f2b 95 pc.baud(9600);
O_Thom 13:37a7c57f4641 96 RxIn = "";
O_Thom 14:dbb0741ce576 97 char rx_buffer[buffer_size + 1] = {0};
O_Thom 13:37a7c57f4641 98 rx_in = 0;
O_Thom 14:dbb0741ce576 99 displayFrame();
O_Thom 14:dbb0741ce576 100 printf("\033[23;30f");
O_Thom 14:dbb0741ce576 101 SERIAL_Queue.call_every(100, callback(this, &Serialcomms::ReadData)); // Start the periodic event queue to check the buffer
O_Thom 10:08c366434f2b 102 }
O_Thom 13:37a7c57f4641 103
O_Thom 10:08c366434f2b 104 ~Serialcomms()
O_Thom 10:08c366434f2b 105 {
O_Thom 11:b8e8630c7e3b 106 printf("Closing Serial Comms.");
O_Thom 10:08c366434f2b 107 }
O_Thom 10:08c366434f2b 108
O_Thom 6:b7f6e0c0f646 109 void setsampledata(sample_message msg) // Update internal values
O_Thom 6:b7f6e0c0f646 110 {
O_Thom 6:b7f6e0c0f646 111 fTemp = msg.temp;
O_Thom 6:b7f6e0c0f646 112 fPressure = msg.pressure;
O_Thom 6:b7f6e0c0f646 113 fLDR = msg.ldr;
O_Thom 6:b7f6e0c0f646 114 }
O_Thom 6:b7f6e0c0f646 115
O_Thom 9:654e14de9d74 116 sample_message getsampledata() // Retrieves the data
O_Thom 6:b7f6e0c0f646 117 {
O_Thom 6:b7f6e0c0f646 118 sample_message msg;
O_Thom 6:b7f6e0c0f646 119 msg.temp = fTemp;
O_Thom 6:b7f6e0c0f646 120 msg.pressure = fPressure;
O_Thom 6:b7f6e0c0f646 121 msg.ldr = fLDR;
O_Thom 6:b7f6e0c0f646 122 return msg;
O_Thom 6:b7f6e0c0f646 123 }
O_Thom 6:b7f6e0c0f646 124 void updateTerminal() // Print internal values of sensors
O_Thom 7:8664a45f5ce1 125 {
O_Thom 9:654e14de9d74 126 printf("\033[H");
O_Thom 6:b7f6e0c0f646 127 printf("======= Sensor Update ========\n");
O_Thom 6:b7f6e0c0f646 128 printf("Temperate: %5.2f\n", fTemp);
O_Thom 6:b7f6e0c0f646 129 printf("Pressure: %5.2f\n", fPressure);
O_Thom 6:b7f6e0c0f646 130 printf("Light Level: %5.2f\n", fLDR);
O_Thom 9:654e14de9d74 131 printf("==============================\n");
O_Thom 9:654e14de9d74 132 printf("Error Codes: ");
O_Thom 9:654e14de9d74 133 if (ErrorCodes.size() == 0)
O_Thom 9:654e14de9d74 134 {
O_Thom 9:654e14de9d74 135 printf("No Error Codes Raised\n");
O_Thom 9:654e14de9d74 136 }
O_Thom 9:654e14de9d74 137 else
O_Thom 9:654e14de9d74 138 {
O_Thom 9:654e14de9d74 139 for (int idx = 0; idx < ErrorCodes.size(); idx++)
O_Thom 9:654e14de9d74 140 {
O_Thom 9:654e14de9d74 141 printf("%d: %d\n", idx, ErrorCodes[idx]);
O_Thom 9:654e14de9d74 142 }
O_Thom 9:654e14de9d74 143 }
O_Thom 9:654e14de9d74 144 printf("Thread Health: \n\n");
O_Thom 9:654e14de9d74 145 // Add code to receive feedback from watchdog
O_Thom 9:654e14de9d74 146 }
O_Thom 9:654e14de9d74 147
O_Thom 14:dbb0741ce576 148
O_Thom 13:37a7c57f4641 149
O_Thom 13:37a7c57f4641 150 void handleInput()
O_Thom 9:654e14de9d74 151 {
O_Thom 14:dbb0741ce576 152 //printf("Echo: %s", RxIn); // Debug
O_Thom 13:37a7c57f4641 153 if (RxIn == "READ ALL") // Sends a comma seperate list of all measurements.
O_Thom 14:dbb0741ce576 154 {
O_Thom 9:654e14de9d74 155 }
O_Thom 11:b8e8630c7e3b 156 else if(RxIn == "DELETE ALL")
O_Thom 9:654e14de9d74 157 {
O_Thom 9:654e14de9d74 158 }
O_Thom 11:b8e8630c7e3b 159 else if(RxIn == "READ")
O_Thom 9:654e14de9d74 160 {
O_Thom 9:654e14de9d74 161 }
O_Thom 11:b8e8630c7e3b 162 else if(RxIn == "DELETE")
O_Thom 9:654e14de9d74 163 {
O_Thom 9:654e14de9d74 164 }
O_Thom 11:b8e8630c7e3b 165 else if(RxIn == "SETDATE")
O_Thom 9:654e14de9d74 166 {
O_Thom 9:654e14de9d74 167 }
O_Thom 11:b8e8630c7e3b 168 else if(RxIn == "SETTIME")
O_Thom 9:654e14de9d74 169 {
O_Thom 9:654e14de9d74 170 }
O_Thom 11:b8e8630c7e3b 171 else if(RxIn == "SETT")
O_Thom 9:654e14de9d74 172 {
O_Thom 9:654e14de9d74 173 }
O_Thom 11:b8e8630c7e3b 174 else if(RxIn == "STATE")
O_Thom 9:654e14de9d74 175 {
O_Thom 9:654e14de9d74 176 }
O_Thom 11:b8e8630c7e3b 177 else if(RxIn == "LOGGING") // Verbose logging
O_Thom 9:654e14de9d74 178 {
O_Thom 11:b8e8630c7e3b 179 }
O_Thom 11:b8e8630c7e3b 180 RxIn = ""; // Reset the input string
O_Thom 9:654e14de9d74 181 }
O_Thom 10:08c366434f2b 182
O_Thom 13:37a7c57f4641 183
O_Thom 13:37a7c57f4641 184 void ReadData()
O_Thom 10:08c366434f2b 185 {
O_Thom 14:dbb0741ce576 186 while(pc.readable())
O_Thom 10:08c366434f2b 187 {
O_Thom 14:dbb0741ce576 188 char c = pc.getc();
O_Thom 13:37a7c57f4641 189 if (c == 0x0D) // Enter ASCII Code
O_Thom 13:37a7c57f4641 190 {
O_Thom 14:dbb0741ce576 191 rx_in = 0;
O_Thom 14:dbb0741ce576 192 RxIn = rx_buffer;
O_Thom 14:dbb0741ce576 193 SERIAL_Queue.call(callback(this, &Serialcomms::handleInput)); // Process the input
O_Thom 14:dbb0741ce576 194 char rx_buffer[buffer_size + 1] = {0};
O_Thom 13:37a7c57f4641 195 }
O_Thom 13:37a7c57f4641 196 rx_buffer[rx_in] = c;
O_Thom 13:37a7c57f4641 197 rx_in += 1; // Increase received indexer
O_Thom 11:b8e8630c7e3b 198 }
O_Thom 10:08c366434f2b 199 }
O_Thom 13:37a7c57f4641 200
O_Thom 9:654e14de9d74 201 void updateErrors(vector<int> ErrorsIn)
O_Thom 9:654e14de9d74 202 {
O_Thom 9:654e14de9d74 203 for (int idx = 0; idx < ErrorsIn.size() ; idx++) // Add the Error Codes to the vector
O_Thom 9:654e14de9d74 204 {
O_Thom 9:654e14de9d74 205 ErrorCodes.push_back(ErrorsIn[idx]);
O_Thom 9:654e14de9d74 206 }
O_Thom 6:b7f6e0c0f646 207 }
O_Thom 6:b7f6e0c0f646 208 void updateTimeDate()
O_Thom 6:b7f6e0c0f646 209 {
O_Thom 6:b7f6e0c0f646 210 }
O_Thom 6:b7f6e0c0f646 211
O_Thom 10:08c366434f2b 212
O_Thom 1:f89c930c6491 213 };
O_Thom 7:8664a45f5ce1 214