Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280 ELEC350-Practicals-FZ429- TextLCD watchdog_RTOS BME280 ntp-client
SerialComms.hpp@12:88d33b87ecb2, 2018-12-04 (annotated)
- Committer:
- O_Thom
- Date:
- Tue Dec 04 18:15:49 2018 +0000
- Revision:
- 12:88d33b87ecb2
- Parent:
- 11:b8e8630c7e3b
- Child:
- 13:37a7c57f4641
Serial Not Working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
O_Thom | 6:b7f6e0c0f646 | 1 | #include "mbed.h" |
O_Thom | 10:08c366434f2b | 2 | #include<string> |
O_Thom | 11:b8e8630c7e3b | 3 | #include <iostream> |
O_Thom | 9:654e14de9d74 | 4 | #include <vector> |
O_Thom | 10:08c366434f2b | 5 | |
O_Thom | 9:654e14de9d74 | 6 | #define ERROR_LCD_EXIT 1 |
O_Thom | 9:654e14de9d74 | 7 | #define ERROR_SERIAL_EXIT 2 |
O_Thom | 9:654e14de9d74 | 8 | #define ERROR_SD_EXIT 3 |
O_Thom | 9:654e14de9d74 | 9 | #define ERROR_NET_EXIT 4 |
O_Thom | 9:654e14de9d74 | 10 | #define ERROR_SAMPLER_EXIT 5 |
O_Thom | 9:654e14de9d74 | 11 | |
O_Thom | 9:654e14de9d74 | 12 | //enum ErrorCodes {ERROR_LCD_EXIT,ERROR_SERIAL_EXIT,ERROR_SD_EXIT,ERROR_NET_EXIT}; |
O_Thom | 6:b7f6e0c0f646 | 13 | |
O_Thom | 10:08c366434f2b | 14 | Serial pc(USBTX, USBRX); // USB Tx and Rx Connections |
O_Thom | 11:b8e8630c7e3b | 15 | using namespace std; |
O_Thom | 10:08c366434f2b | 16 | |
O_Thom | 6:b7f6e0c0f646 | 17 | class Serialcomms |
O_Thom | 1:f89c930c6491 | 18 | { |
O_Thom | 1:f89c930c6491 | 19 | private: |
O_Thom | 8:7d218affea71 | 20 | float fTemp; //current temperature of sensor |
O_Thom | 8:7d218affea71 | 21 | float fPressure; //current pressure of sensor |
O_Thom | 9:654e14de9d74 | 22 | float fLDR; //current light level from LDR |
O_Thom | 9:654e14de9d74 | 23 | vector<int> ErrorCodes; |
O_Thom | 11:b8e8630c7e3b | 24 | string RxIn; // Init |
O_Thom | 11:b8e8630c7e3b | 25 | |
O_Thom | 11:b8e8630c7e3b | 26 | public: |
O_Thom | 10:08c366434f2b | 27 | |
O_Thom | 9:654e14de9d74 | 28 | EventQueue SERIAL_Queue; //Initialise the EventQueue |
O_Thom | 12:88d33b87ecb2 | 29 | |
O_Thom | 12:88d33b87ecb2 | 30 | void Rx_Interrupt() |
O_Thom | 10:08c366434f2b | 31 | { |
O_Thom | 12:88d33b87ecb2 | 32 | SERIAL_Queue.call(callback(this, &Serialcomms::ReadData)); // Read data from the serial buffer -> Pushed onto the event queue |
O_Thom | 10:08c366434f2b | 33 | } |
O_Thom | 12:88d33b87ecb2 | 34 | |
O_Thom | 10:08c366434f2b | 35 | Serialcomms() |
O_Thom | 10:08c366434f2b | 36 | { |
O_Thom | 10:08c366434f2b | 37 | pc.baud(9600); |
O_Thom | 10:08c366434f2b | 38 | printf("Serial Comms Initialised, Baud: 9600\n"); |
O_Thom | 11:b8e8630c7e3b | 39 | printf("COMMAND: \n"); |
O_Thom | 10:08c366434f2b | 40 | pc.attach(callback(this, &Serialcomms::Rx_Interrupt), Serial::RxIrq); // Interrupt if data is received |
O_Thom | 10:08c366434f2b | 41 | } |
O_Thom | 10:08c366434f2b | 42 | ~Serialcomms() |
O_Thom | 10:08c366434f2b | 43 | { |
O_Thom | 11:b8e8630c7e3b | 44 | printf("Closing Serial Comms."); |
O_Thom | 10:08c366434f2b | 45 | } |
O_Thom | 10:08c366434f2b | 46 | |
O_Thom | 6:b7f6e0c0f646 | 47 | void setsampledata(sample_message msg) // Update internal values |
O_Thom | 6:b7f6e0c0f646 | 48 | { |
O_Thom | 6:b7f6e0c0f646 | 49 | fTemp = msg.temp; |
O_Thom | 6:b7f6e0c0f646 | 50 | fPressure = msg.pressure; |
O_Thom | 6:b7f6e0c0f646 | 51 | fLDR = msg.ldr; |
O_Thom | 6:b7f6e0c0f646 | 52 | } |
O_Thom | 6:b7f6e0c0f646 | 53 | |
O_Thom | 9:654e14de9d74 | 54 | sample_message getsampledata() // Retrieves the data |
O_Thom | 6:b7f6e0c0f646 | 55 | { |
O_Thom | 6:b7f6e0c0f646 | 56 | sample_message msg; |
O_Thom | 6:b7f6e0c0f646 | 57 | msg.temp = fTemp; |
O_Thom | 6:b7f6e0c0f646 | 58 | msg.pressure = fPressure; |
O_Thom | 6:b7f6e0c0f646 | 59 | msg.ldr = fLDR; |
O_Thom | 6:b7f6e0c0f646 | 60 | return msg; |
O_Thom | 6:b7f6e0c0f646 | 61 | } |
O_Thom | 6:b7f6e0c0f646 | 62 | |
O_Thom | 6:b7f6e0c0f646 | 63 | void updateTerminal() // Print internal values of sensors |
O_Thom | 7:8664a45f5ce1 | 64 | { |
O_Thom | 9:654e14de9d74 | 65 | //printf("\033[2J"); |
O_Thom | 9:654e14de9d74 | 66 | printf("\033[H"); |
O_Thom | 6:b7f6e0c0f646 | 67 | printf("======= Sensor Update ========\n"); |
O_Thom | 6:b7f6e0c0f646 | 68 | printf("Temperate: %5.2f\n", fTemp); |
O_Thom | 6:b7f6e0c0f646 | 69 | printf("Pressure: %5.2f\n", fPressure); |
O_Thom | 6:b7f6e0c0f646 | 70 | printf("Light Level: %5.2f\n", fLDR); |
O_Thom | 9:654e14de9d74 | 71 | printf("==============================\n"); |
O_Thom | 9:654e14de9d74 | 72 | printf("Error Codes: "); |
O_Thom | 9:654e14de9d74 | 73 | if (ErrorCodes.size() == 0) |
O_Thom | 9:654e14de9d74 | 74 | { |
O_Thom | 9:654e14de9d74 | 75 | printf("No Error Codes Raised\n"); |
O_Thom | 9:654e14de9d74 | 76 | } |
O_Thom | 9:654e14de9d74 | 77 | else |
O_Thom | 9:654e14de9d74 | 78 | { |
O_Thom | 9:654e14de9d74 | 79 | for (int idx = 0; idx < ErrorCodes.size(); idx++) |
O_Thom | 9:654e14de9d74 | 80 | { |
O_Thom | 9:654e14de9d74 | 81 | printf("%d: %d\n", idx, ErrorCodes[idx]); |
O_Thom | 9:654e14de9d74 | 82 | } |
O_Thom | 9:654e14de9d74 | 83 | } |
O_Thom | 9:654e14de9d74 | 84 | printf("Thread Health: \n\n"); |
O_Thom | 9:654e14de9d74 | 85 | // Add code to receive feedback from watchdog |
O_Thom | 9:654e14de9d74 | 86 | } |
O_Thom | 9:654e14de9d74 | 87 | |
O_Thom | 9:654e14de9d74 | 88 | void displayFrame() |
O_Thom | 9:654e14de9d74 | 89 | { |
O_Thom | 9:654e14de9d74 | 90 | |
O_Thom | 9:654e14de9d74 | 91 | |
O_Thom | 9:654e14de9d74 | 92 | } |
O_Thom | 9:654e14de9d74 | 93 | |
O_Thom | 10:08c366434f2b | 94 | |
O_Thom | 10:08c366434f2b | 95 | |
O_Thom | 10:08c366434f2b | 96 | |
O_Thom | 11:b8e8630c7e3b | 97 | void handleInput(string RxIn) |
O_Thom | 9:654e14de9d74 | 98 | { |
O_Thom | 11:b8e8630c7e3b | 99 | printf("Echo: %s", RxIn); // Debug |
O_Thom | 10:08c366434f2b | 100 | |
O_Thom | 11:b8e8630c7e3b | 101 | if (RxIn == "READ ALL") // Sends a comma seperate list of all measurements. |
O_Thom | 9:654e14de9d74 | 102 | { |
O_Thom | 9:654e14de9d74 | 103 | } |
O_Thom | 11:b8e8630c7e3b | 104 | else if(RxIn == "DELETE ALL") |
O_Thom | 9:654e14de9d74 | 105 | { |
O_Thom | 9:654e14de9d74 | 106 | } |
O_Thom | 11:b8e8630c7e3b | 107 | else if(RxIn == "READ") |
O_Thom | 9:654e14de9d74 | 108 | { |
O_Thom | 9:654e14de9d74 | 109 | } |
O_Thom | 11:b8e8630c7e3b | 110 | else if(RxIn == "DELETE") |
O_Thom | 9:654e14de9d74 | 111 | { |
O_Thom | 9:654e14de9d74 | 112 | } |
O_Thom | 11:b8e8630c7e3b | 113 | else if(RxIn == "SETDATE") |
O_Thom | 9:654e14de9d74 | 114 | { |
O_Thom | 9:654e14de9d74 | 115 | } |
O_Thom | 11:b8e8630c7e3b | 116 | else if(RxIn == "SETTIME") |
O_Thom | 9:654e14de9d74 | 117 | { |
O_Thom | 9:654e14de9d74 | 118 | } |
O_Thom | 11:b8e8630c7e3b | 119 | else if(RxIn == "SETT") |
O_Thom | 9:654e14de9d74 | 120 | { |
O_Thom | 9:654e14de9d74 | 121 | } |
O_Thom | 11:b8e8630c7e3b | 122 | else if(RxIn == "STATE") |
O_Thom | 9:654e14de9d74 | 123 | { |
O_Thom | 9:654e14de9d74 | 124 | } |
O_Thom | 11:b8e8630c7e3b | 125 | else if(RxIn == "LOGGING") // Verbose logging |
O_Thom | 9:654e14de9d74 | 126 | { |
O_Thom | 11:b8e8630c7e3b | 127 | } |
O_Thom | 11:b8e8630c7e3b | 128 | RxIn = ""; // Reset the input string |
O_Thom | 9:654e14de9d74 | 129 | } |
O_Thom | 10:08c366434f2b | 130 | |
O_Thom | 10:08c366434f2b | 131 | void ReadData() |
O_Thom | 10:08c366434f2b | 132 | { |
O_Thom | 11:b8e8630c7e3b | 133 | char c = pc.getc(); |
O_Thom | 11:b8e8630c7e3b | 134 | |
O_Thom | 11:b8e8630c7e3b | 135 | if (c != '\n') |
O_Thom | 10:08c366434f2b | 136 | { |
O_Thom | 11:b8e8630c7e3b | 137 | RxIn.append(1,c); // Push the next character in the buffer onto the vector |
O_Thom | 11:b8e8630c7e3b | 138 | } |
O_Thom | 11:b8e8630c7e3b | 139 | else // If the command is terminated |
O_Thom | 11:b8e8630c7e3b | 140 | { |
O_Thom | 11:b8e8630c7e3b | 141 | SERIAL_Queue.call(callback(this, &Serialcomms::handleInput), RxIn); // Process the inpuT |
O_Thom | 11:b8e8630c7e3b | 142 | } |
O_Thom | 10:08c366434f2b | 143 | } |
O_Thom | 10:08c366434f2b | 144 | |
O_Thom | 9:654e14de9d74 | 145 | |
O_Thom | 9:654e14de9d74 | 146 | |
O_Thom | 9:654e14de9d74 | 147 | void updateErrors(vector<int> ErrorsIn) |
O_Thom | 9:654e14de9d74 | 148 | { |
O_Thom | 9:654e14de9d74 | 149 | for (int idx = 0; idx < ErrorsIn.size() ; idx++) // Add the Error Codes to the vector |
O_Thom | 9:654e14de9d74 | 150 | { |
O_Thom | 9:654e14de9d74 | 151 | ErrorCodes.push_back(ErrorsIn[idx]); |
O_Thom | 9:654e14de9d74 | 152 | } |
O_Thom | 6:b7f6e0c0f646 | 153 | } |
O_Thom | 1:f89c930c6491 | 154 | |
O_Thom | 6:b7f6e0c0f646 | 155 | void updateTimeDate() |
O_Thom | 6:b7f6e0c0f646 | 156 | { |
O_Thom | 6:b7f6e0c0f646 | 157 | } |
O_Thom | 6:b7f6e0c0f646 | 158 | |
O_Thom | 10:08c366434f2b | 159 | |
O_Thom | 1:f89c930c6491 | 160 | }; |
O_Thom | 7:8664a45f5ce1 | 161 |