Plymouth ELEC351 Group T
/
ELEC351_Group_T
FINAL PROJECT isn't it
Fork of ELEC351 by
main.cpp@22:eb4cc12087b2, 2017-12-29 (annotated)
- Committer:
- thomasmorris
- Date:
- Fri Dec 29 21:46:49 2017 +0000
- Revision:
- 22:eb4cc12087b2
- Parent:
- 21:3c078c799caa
- Child:
- 23:3c85d7f657a2
Start of Serial Commands not working Networking working LCD.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 6:97f586597310 | 1 | /* |
thomasmorris | 6:97f586597310 | 2 | ELEC 351 Group T |
thomasmorris | 6:97f586597310 | 3 | Team Members : Christopher Hills, Thomas Morris |
thomasmorris | 8:0e4481b64353 | 4 | Current Verision 3 |
thomasmorris | 6:97f586597310 | 5 | Overiew: Working Tasks 1,5,7 |
thomasmorris | 8:0e4481b64353 | 6 | |
thomasmorris | 12:536eca338ae8 | 7 | Last Revision: Added Mail Box to serial |
thomasmorris | 8:0e4481b64353 | 8 | Todo: |
thomasmorris | 12:536eca338ae8 | 9 | make a mailbox within SD card, LCD, network so they can be used from within |
thomasmorris | 12:536eca338ae8 | 10 | Fix the network so it uses the mail box data. |
thomasmorris | 12:536eca338ae8 | 11 | Place the LCD write functions in their own function |
thomasmorris | 6:97f586597310 | 12 | */ |
thomasmorris | 6:97f586597310 | 13 | |
thomasmorris | 8:0e4481b64353 | 14 | //Includes and Definitions |
noutram | 1:e1cf7663f5ff | 15 | #include "sample_hardware.hpp" |
noutram | 3:a88838ff33e7 | 16 | #include "Networkbits.hpp" |
thomasmorris | 5:2594b953f111 | 17 | #include "rtos.h" |
thomasmorris | 5:2594b953f111 | 18 | #include "LED.hpp" |
thomasmorris | 8:0e4481b64353 | 19 | #include "DATA.hpp" |
chills | 20:cbb71f84cff9 | 20 | #include "LCD.hpp" |
thomasmorris | 5:2594b953f111 | 21 | #define SamplingTime 1 |
thomasmorris | 5:2594b953f111 | 22 | #define NotSamplingTime 0 |
thomasmorris | 7:dfe19413fdc2 | 23 | #define Print_Time_to_LCD 1 |
thomasmorris | 7:dfe19413fdc2 | 24 | #define Dont_Print_Time_to_LCD 0 |
chills | 15:c1592fc1a501 | 25 | #define TimerInterval 1 //This is in seconds |
thomasmorris | 7:dfe19413fdc2 | 26 | #define EDGE_RISEN 1 |
thomasmorris | 7:dfe19413fdc2 | 27 | #define EDGE_FALLEN 0 |
chills | 16:067916791a25 | 28 | #define mailsize 5 |
thomasmorris | 22:eb4cc12087b2 | 29 | #include <iostream> |
thomasmorris | 5:2594b953f111 | 30 | Serial pc(USBTX, USBRX); |
thomasmorris | 21:3c078c799caa | 31 | //SW1+SW2 are declared as interrupt ins in sample hardware.cpp |
thomasmorris | 8:0e4481b64353 | 32 | |
thomasmorris | 8:0e4481b64353 | 33 | |
thomasmorris | 8:0e4481b64353 | 34 | //Thread IDs |
thomasmorris | 5:2594b953f111 | 35 | osThreadId idMain; |
thomasmorris | 5:2594b953f111 | 36 | osThreadId id1; |
thomasmorris | 5:2594b953f111 | 37 | osThreadId id2; |
thomasmorris | 5:2594b953f111 | 38 | osThreadId id3; |
thomasmorris | 5:2594b953f111 | 39 | osThreadId id4; |
noutram | 0:36e89e3ed7c4 | 40 | |
thomasmorris | 21:3c078c799caa | 41 | |
thomasmorris | 7:dfe19413fdc2 | 42 | Timeout sw1TimeOut;//Used to prevent switch bounce |
thomasmorris | 7:dfe19413fdc2 | 43 | |
thomasmorris | 5:2594b953f111 | 44 | LED Red_led(PE_15); |
thomasmorris | 5:2594b953f111 | 45 | LED Yellow_led(PB_10); |
thomasmorris | 5:2594b953f111 | 46 | LED Green_led(PB_11); |
thomasmorris | 5:2594b953f111 | 47 | |
chills | 20:cbb71f84cff9 | 48 | LCD LCD(D8,D9,D4,A0,A1,A2,A3,D7,D6,D3,D1); |
thomasmorris | 21:3c078c799caa | 49 | char LCD_buffer [50]; |
thomasmorris | 21:3c078c799caa | 50 | int LCD_sprintf; |
chills | 20:cbb71f84cff9 | 51 | |
thomasmorris | 8:0e4481b64353 | 52 | //Tickers |
thomasmorris | 8:0e4481b64353 | 53 | |
thomasmorris | 5:2594b953f111 | 54 | Ticker Sample_timer; |
thomasmorris | 8:0e4481b64353 | 55 | |
noutram | 3:a88838ff33e7 | 56 | //Threads |
thomasmorris | 5:2594b953f111 | 57 | Thread t1; |
thomasmorris | 5:2594b953f111 | 58 | Thread t2; |
thomasmorris | 21:3c078c799caa | 59 | Thread t3; |
thomasmorris | 21:3c078c799caa | 60 | Thread t4; |
thomasmorris | 21:3c078c799caa | 61 | Thread t180;//cos yolo |
thomasmorris | 7:dfe19413fdc2 | 62 | |
thomasmorris | 7:dfe19413fdc2 | 63 | double temp = 0; |
thomasmorris | 7:dfe19413fdc2 | 64 | double pressure = 0; |
thomasmorris | 7:dfe19413fdc2 | 65 | double lux = 0; |
chills | 10:46946784326d | 66 | string buffer_time = 0; |
thomasmorris | 7:dfe19413fdc2 | 67 | |
thomasmorris | 7:dfe19413fdc2 | 68 | char buffer[32]; |
chills | 15:c1592fc1a501 | 69 | char scom_time_buffer[32]; |
chills | 17:37d883f40c3d | 70 | char msel_time_buffer[32]; |
thomasmorris | 7:dfe19413fdc2 | 71 | |
thomasmorris | 7:dfe19413fdc2 | 72 | void SW1FallingEdge(); |
thomasmorris | 7:dfe19413fdc2 | 73 | void SW1TimeOutHandler(); |
thomasmorris | 7:dfe19413fdc2 | 74 | |
thomasmorris | 22:eb4cc12087b2 | 75 | |
thomasmorris | 22:eb4cc12087b2 | 76 | |
thomasmorris | 22:eb4cc12087b2 | 77 | using namespace std; |
thomasmorris | 22:eb4cc12087b2 | 78 | |
thomasmorris | 7:dfe19413fdc2 | 79 | int mode = 0; |
thomasmorris | 7:dfe19413fdc2 | 80 | |
chills | 17:37d883f40c3d | 81 | Mail<DATA, mailsize> mail_box; //Mail Queue, Type DATA, Capacity mailsize(defined above), name mail_box |
thomasmorris | 8:0e4481b64353 | 82 | |
chills | 13:db857b3744c6 | 83 | void Network() //Interrupt service routine for handling the timeout |
thomasmorris | 8:0e4481b64353 | 84 | { |
chills | 13:db857b3744c6 | 85 | osEvent evt_network = mail_box.get(); //Get the latest entry from "mail_box" |
chills | 13:db857b3744c6 | 86 | |
chills | 13:db857b3744c6 | 87 | if (evt_network.status == osEventMail) |
chills | 13:db857b3744c6 | 88 | { |
chills | 17:37d883f40c3d | 89 | DATA *Rec_Data_Network = (DATA*)evt_network.value.p; //Create pointer to mailbox |
chills | 17:37d883f40c3d | 90 | DATA msg_network; //Create temporary instance of DATA class |
chills | 13:db857b3744c6 | 91 | |
chills | 17:37d883f40c3d | 92 | msg_network.set_time(Rec_Data_Network->get_time()); //Copy time from mailbox to temporary instance |
chills | 17:37d883f40c3d | 93 | msg_network.set_temperature(Rec_Data_Network->get_temperature()); //Copy temperature from mailbox to temporary instance |
chills | 17:37d883f40c3d | 94 | msg_network.set_pressure(Rec_Data_Network->get_pressure()); //Copy pressure from mailbox to temporary instance |
chills | 17:37d883f40c3d | 95 | msg_network.set_light(Rec_Data_Network->get_light()); //Copy light from mailbox to temporary instance |
chills | 17:37d883f40c3d | 96 | mail_box.free(Rec_Data_Network); //Free space in the mailbox (delete earliest sample taken) |
chills | 13:db857b3744c6 | 97 | |
thomasmorris | 18:194a606ccd47 | 98 | networktest(); |
chills | 13:db857b3744c6 | 99 | } |
chills | 13:db857b3744c6 | 100 | } |
chills | 13:db857b3744c6 | 101 | |
thomasmorris | 8:0e4481b64353 | 102 | void SW1TimeOutHandler() |
thomasmorris | 8:0e4481b64353 | 103 | { |
thomasmorris | 7:dfe19413fdc2 | 104 | sw1TimeOut.detach(); //Stop the timeout counter firing |
thomasmorris | 7:dfe19413fdc2 | 105 | SW1.fall(&SW1FallingEdge); //Now wait for a falling edge |
thomasmorris | 7:dfe19413fdc2 | 106 | } |
thomasmorris | 12:536eca338ae8 | 107 | void SDWrite()//End of skype chat |
thomasmorris | 12:536eca338ae8 | 108 | { |
chills | 13:db857b3744c6 | 109 | osEvent evt_sd = mail_box.get(); //Get the latest entry from "mail_box" |
chills | 13:db857b3744c6 | 110 | |
chills | 13:db857b3744c6 | 111 | if (evt_sd.status == osEventMail) |
chills | 13:db857b3744c6 | 112 | { |
chills | 17:37d883f40c3d | 113 | DATA *Rec_Data_SD = (DATA*)evt_sd.value.p; //Create pointer to mailbox |
chills | 17:37d883f40c3d | 114 | DATA msg_sd; //Create temporary instance of DATA class |
chills | 13:db857b3744c6 | 115 | |
chills | 17:37d883f40c3d | 116 | msg_sd.set_time(Rec_Data_SD->get_time()); //Copy time from mailbox to temporary instance |
chills | 17:37d883f40c3d | 117 | msg_sd.set_temperature(Rec_Data_SD->get_temperature()); //Copy temperature from mailbox to temporary instance |
chills | 17:37d883f40c3d | 118 | msg_sd.set_pressure(Rec_Data_SD->get_pressure()); //Copy pressure from mailbox to temporary instance |
chills | 17:37d883f40c3d | 119 | msg_sd.set_light(Rec_Data_SD->get_light()); //Copy light from mailbox to temporary instance |
chills | 17:37d883f40c3d | 120 | mail_box.free(Rec_Data_SD); //Free space in the mailbox (delete earliest sample taken) |
chills | 13:db857b3744c6 | 121 | } |
thomasmorris | 12:536eca338ae8 | 122 | } |
thomasmorris | 7:dfe19413fdc2 | 123 | //Interrupt service routive for SW1 falling edge (release) |
thomasmorris | 19:54bc302a82ea | 124 | void SW1FallingEdge() |
thomasmorris | 19:54bc302a82ea | 125 | { |
thomasmorris | 7:dfe19413fdc2 | 126 | SW1.fall(NULL); //Disable this interrupt |
chills | 16:067916791a25 | 127 | Yellow_led.Toggle(); //Toggle LED |
thomasmorris | 7:dfe19413fdc2 | 128 | |
thomasmorris | 7:dfe19413fdc2 | 129 | mode = mode +1;//Cycles through modes |
thomasmorris | 7:dfe19413fdc2 | 130 | if(mode >1) |
thomasmorris | 7:dfe19413fdc2 | 131 | { |
thomasmorris | 7:dfe19413fdc2 | 132 | mode = 0; |
thomasmorris | 7:dfe19413fdc2 | 133 | } |
thomasmorris | 7:dfe19413fdc2 | 134 | |
thomasmorris | 7:dfe19413fdc2 | 135 | sw1TimeOut.attach(&SW1TimeOutHandler, 0.2); //Start timeout counter |
thomasmorris | 7:dfe19413fdc2 | 136 | } |
thomasmorris | 7:dfe19413fdc2 | 137 | void ModeSelection() |
thomasmorris | 7:dfe19413fdc2 | 138 | { |
thomasmorris | 7:dfe19413fdc2 | 139 | while(1){ |
chills | 13:db857b3744c6 | 140 | |
chills | 16:067916791a25 | 141 | //Thread::wait(1000); |
chills | 13:db857b3744c6 | 142 | |
chills | 13:db857b3744c6 | 143 | osEvent evt_lcd = mail_box.get(); //Get the latest entry from "mail_box" |
chills | 13:db857b3744c6 | 144 | |
chills | 13:db857b3744c6 | 145 | if (evt_lcd.status == osEventMail) |
thomasmorris | 7:dfe19413fdc2 | 146 | { |
chills | 17:37d883f40c3d | 147 | DATA *Rec_Data_LCD = (DATA*)evt_lcd.value.p; //Create pointer to mailbox |
chills | 17:37d883f40c3d | 148 | DATA msg_lcd; //Create temporary instance of DATA class |
chills | 13:db857b3744c6 | 149 | |
chills | 17:37d883f40c3d | 150 | msg_lcd.set_time(Rec_Data_LCD->get_time()); //Copy time from mailbox to temporary instance |
chills | 17:37d883f40c3d | 151 | msg_lcd.set_temperature(Rec_Data_LCD->get_temperature()); //Copy temperature from mailbox to temporary instance |
chills | 17:37d883f40c3d | 152 | msg_lcd.set_pressure(Rec_Data_LCD->get_pressure()); //Copy pressure from mailbox to temporary instance |
chills | 17:37d883f40c3d | 153 | msg_lcd.set_light(Rec_Data_LCD->get_light()); //Copy light from mailbox to temporary instance |
chills | 17:37d883f40c3d | 154 | mail_box.free(Rec_Data_LCD); //Free space in the mailbox (delete earliest sample taken) |
chills | 13:db857b3744c6 | 155 | |
chills | 13:db857b3744c6 | 156 | if(mode == 0)//Print values to the LCD |
chills | 13:db857b3744c6 | 157 | { |
chills | 13:db857b3744c6 | 158 | //Write new data to LCD (not fast!) |
thomasmorris | 21:3c078c799caa | 159 | |
thomasmorris | 21:3c078c799caa | 160 | /*LCD_sprintf = */sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting |
thomasmorris | 21:3c078c799caa | 161 | |
thomasmorris | 21:3c078c799caa | 162 | //LCD.Display_Clear(); |
thomasmorris | 21:3c078c799caa | 163 | LCD.DDRAM_Address(0x00); |
thomasmorris | 21:3c078c799caa | 164 | LCD.Write_String("Temp Pres li"); |
thomasmorris | 21:3c078c799caa | 165 | LCD.DDRAM_Address(0x40); |
thomasmorris | 21:3c078c799caa | 166 | |
thomasmorris | 21:3c078c799caa | 167 | LCD.Write_String(LCD_buffer); |
thomasmorris | 21:3c078c799caa | 168 | //LCD.Write_String("Temp Pres li\n"); |
thomasmorris | 21:3c078c799caa | 169 | //LCD.Write_String("%1.1f ", msg_lcd.get_temperature());//Print Temperature to LCD |
thomasmorris | 21:3c078c799caa | 170 | //LCD.Write_String("%1.1f ", msg_lcd.get_pressure());//Print Pressure to LCD |
thomasmorris | 21:3c078c799caa | 171 | //LCD.Write_String("%1.1f\n", msg_lcd.get_light()); //Print Light to LCD |
thomasmorris | 21:3c078c799caa | 172 | |
chills | 13:db857b3744c6 | 173 | } |
chills | 13:db857b3744c6 | 174 | else if(mode == 1)//Print the Time to the LCD |
chills | 13:db857b3744c6 | 175 | { |
chills | 17:37d883f40c3d | 176 | time_t msel_time = msg_lcd.get_time(); //Declare local variable for time |
thomasmorris | 18:194a606ccd47 | 177 | strftime(scom_time_buffer, 32, "%I:%M %p", localtime(&msel_time)); //Format time as a string |
thomasmorris | 21:3c078c799caa | 178 | LCD.Display_Clear(); |
thomasmorris | 21:3c078c799caa | 179 | LCD_sprintf = sprintf (LCD_buffer, "%s",scom_time_buffer); |
thomasmorris | 21:3c078c799caa | 180 | LCD.DDRAM_Address(0x00); |
thomasmorris | 21:3c078c799caa | 181 | LCD.Write_String("Current Time:"); |
thomasmorris | 21:3c078c799caa | 182 | LCD.DDRAM_Address(0x40); |
thomasmorris | 21:3c078c799caa | 183 | LCD.Write_String(LCD_buffer); |
thomasmorris | 21:3c078c799caa | 184 | |
chills | 13:db857b3744c6 | 185 | } |
chills | 13:db857b3744c6 | 186 | else |
chills | 13:db857b3744c6 | 187 | { |
chills | 13:db857b3744c6 | 188 | mode = 0; |
chills | 13:db857b3744c6 | 189 | } |
thomasmorris | 7:dfe19413fdc2 | 190 | } |
thomasmorris | 7:dfe19413fdc2 | 191 | } |
thomasmorris | 7:dfe19413fdc2 | 192 | } |
thomasmorris | 22:eb4cc12087b2 | 193 | void Serial_Commands() |
thomasmorris | 22:eb4cc12087b2 | 194 | { |
thomasmorris | 22:eb4cc12087b2 | 195 | string Serial_Input; |
thomasmorris | 22:eb4cc12087b2 | 196 | while(1) |
thomasmorris | 22:eb4cc12087b2 | 197 | { |
thomasmorris | 22:eb4cc12087b2 | 198 | cout << "Please type in a command" << endl; |
thomasmorris | 22:eb4cc12087b2 | 199 | |
thomasmorris | 22:eb4cc12087b2 | 200 | cin >> Serial_Input; |
thomasmorris | 22:eb4cc12087b2 | 201 | |
thomasmorris | 22:eb4cc12087b2 | 202 | if (Serial_Input == "Test") |
thomasmorris | 22:eb4cc12087b2 | 203 | { |
thomasmorris | 22:eb4cc12087b2 | 204 | |
thomasmorris | 22:eb4cc12087b2 | 205 | } |
thomasmorris | 22:eb4cc12087b2 | 206 | else if(Serial_Input == "READ ALL") |
thomasmorris | 22:eb4cc12087b2 | 207 | { |
thomasmorris | 22:eb4cc12087b2 | 208 | |
thomasmorris | 22:eb4cc12087b2 | 209 | } |
thomasmorris | 22:eb4cc12087b2 | 210 | else if(Serial_Input == "DELETE ALL") |
thomasmorris | 22:eb4cc12087b2 | 211 | { |
thomasmorris | 22:eb4cc12087b2 | 212 | |
thomasmorris | 22:eb4cc12087b2 | 213 | } |
thomasmorris | 22:eb4cc12087b2 | 214 | else if(Serial_Input == "READ <n>") |
thomasmorris | 22:eb4cc12087b2 | 215 | { |
thomasmorris | 22:eb4cc12087b2 | 216 | |
thomasmorris | 22:eb4cc12087b2 | 217 | } |
thomasmorris | 22:eb4cc12087b2 | 218 | else if(Serial_Input == "DELETE <n>") |
thomasmorris | 22:eb4cc12087b2 | 219 | { |
thomasmorris | 22:eb4cc12087b2 | 220 | |
thomasmorris | 22:eb4cc12087b2 | 221 | } |
thomasmorris | 22:eb4cc12087b2 | 222 | else if(Serial_Input == "SETDATE <dd> <mm> <yyyy>") |
thomasmorris | 22:eb4cc12087b2 | 223 | { |
thomasmorris | 22:eb4cc12087b2 | 224 | |
thomasmorris | 22:eb4cc12087b2 | 225 | } |
thomasmorris | 22:eb4cc12087b2 | 226 | else if(Serial_Input == "SETT <T>") |
thomasmorris | 22:eb4cc12087b2 | 227 | { |
thomasmorris | 22:eb4cc12087b2 | 228 | |
thomasmorris | 22:eb4cc12087b2 | 229 | } |
thomasmorris | 22:eb4cc12087b2 | 230 | else if(Serial_Input == "STATE <x>") |
thomasmorris | 22:eb4cc12087b2 | 231 | { |
thomasmorris | 22:eb4cc12087b2 | 232 | |
thomasmorris | 22:eb4cc12087b2 | 233 | } |
thomasmorris | 22:eb4cc12087b2 | 234 | else if(Serial_Input == "LOGGING <x>") |
thomasmorris | 22:eb4cc12087b2 | 235 | { |
thomasmorris | 22:eb4cc12087b2 | 236 | |
thomasmorris | 22:eb4cc12087b2 | 237 | } |
thomasmorris | 22:eb4cc12087b2 | 238 | else if(Serial_Input == "HELP")// Use this to display all of the availble commands |
thomasmorris | 22:eb4cc12087b2 | 239 | { |
thomasmorris | 22:eb4cc12087b2 | 240 | cout << "Avalible Commands are: "<< endl; |
thomasmorris | 22:eb4cc12087b2 | 241 | cout << "READ ALL , DELETE ALL READ<n> ,DELETE<n> , SETDATE<dd> <mm> <yyyy> , SETTIME <hh> <mm> <ss>, SETT <T> , STATE <x>, LOGGING <x>, HELP"<< endl; |
thomasmorris | 22:eb4cc12087b2 | 242 | } |
thomasmorris | 22:eb4cc12087b2 | 243 | else |
thomasmorris | 22:eb4cc12087b2 | 244 | { |
thomasmorris | 22:eb4cc12087b2 | 245 | cout << "Please enter a acceptable command" << endl; |
thomasmorris | 22:eb4cc12087b2 | 246 | } |
thomasmorris | 22:eb4cc12087b2 | 247 | } |
thomasmorris | 22:eb4cc12087b2 | 248 | } |
thomasmorris | 5:2594b953f111 | 249 | void Serial_Comms()//Thread for Serial Communications |
thomasmorris | 5:2594b953f111 | 250 | { |
thomasmorris | 22:eb4cc12087b2 | 251 | cout << "In Serial Comms" << endl; |
thomasmorris | 5:2594b953f111 | 252 | while(1) |
thomasmorris | 5:2594b953f111 | 253 | { |
chills | 13:db857b3744c6 | 254 | osEvent evt_serial = mail_box.get(); //Get the latest entry from "mail_box" |
chills | 10:46946784326d | 255 | |
chills | 13:db857b3744c6 | 256 | if (evt_serial.status == osEventMail) |
thomasmorris | 12:536eca338ae8 | 257 | { |
chills | 17:37d883f40c3d | 258 | DATA *Rec_Data_Serial = (DATA*)evt_serial.value.p; //Create pointer to mailbox |
chills | 17:37d883f40c3d | 259 | DATA msg_serial; //Create temporary instance of DATA class |
chills | 11:e7b5ed6cd3cf | 260 | |
chills | 17:37d883f40c3d | 261 | msg_serial.set_time(Rec_Data_Serial->get_time()); //Copy time from mailbox to temporary instance |
chills | 17:37d883f40c3d | 262 | msg_serial.set_temperature(Rec_Data_Serial->get_temperature()); //Copy teperature from mailbox to temporary instance |
chills | 17:37d883f40c3d | 263 | msg_serial.set_pressure(Rec_Data_Serial->get_pressure()); //Copy pressure from mailbox to temporary instance |
chills | 17:37d883f40c3d | 264 | msg_serial.set_light(Rec_Data_Serial->get_light()); //Copy light from mailbox to temporary instance |
chills | 17:37d883f40c3d | 265 | mail_box.free(Rec_Data_Serial); //Free space in the mailbox (delete earliest sample taken) |
chills | 11:e7b5ed6cd3cf | 266 | |
chills | 17:37d883f40c3d | 267 | time_t scom_time = msg_serial.get_time(); //Declare local variable for time |
chills | 17:37d883f40c3d | 268 | strftime(scom_time_buffer, 32, "%I:%M %p\t", localtime(&scom_time)); //Format time as a string |
chills | 17:37d883f40c3d | 269 | pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time |
chills | 15:c1592fc1a501 | 270 | |
chills | 13:db857b3744c6 | 271 | pc.printf("Temperature = %f\t", msg_serial.get_temperature()); //Print Temperature |
chills | 13:db857b3744c6 | 272 | pc.printf("Pressure = %f\t", msg_serial.get_pressure()); //Print Pressure |
chills | 13:db857b3744c6 | 273 | pc.printf("Light = %f\n\r", msg_serial.get_light()); //Print Light |
chills | 10:46946784326d | 274 | } |
thomasmorris | 7:dfe19413fdc2 | 275 | Green_led.Toggle(); |
chills | 16:067916791a25 | 276 | //Thread::wait(1000); |
thomasmorris | 5:2594b953f111 | 277 | } |
thomasmorris | 5:2594b953f111 | 278 | } |
thomasmorris | 5:2594b953f111 | 279 | |
chills | 10:46946784326d | 280 | void Sample_signal_set() //Sets the Signal for when to sample the sensors |
thomasmorris | 5:2594b953f111 | 281 | { |
chills | 10:46946784326d | 282 | t1.signal_set(SamplingTime); //Set the sampling thread signal high |
thomasmorris | 5:2594b953f111 | 283 | } |
thomasmorris | 5:2594b953f111 | 284 | void Sample()//Samples the hardware and prints the result to the LCD |
thomasmorris | 5:2594b953f111 | 285 | { |
thomasmorris | 5:2594b953f111 | 286 | while(1) |
thomasmorris | 5:2594b953f111 | 287 | { |
chills | 10:46946784326d | 288 | Thread::signal_wait(SamplingTime); //Set the time between samples |
chills | 10:46946784326d | 289 | |
chills | 10:46946784326d | 290 | temp = sensor.getTemperature(); //Read Temperature |
chills | 10:46946784326d | 291 | pressure = sensor.getPressure(); //Read Pressure |
chills | 10:46946784326d | 292 | lux = adcIn.read(); //Read Light |
chills | 16:067916791a25 | 293 | time_t buffer_time = time(NULL); //Read Time |
thomasmorris | 7:dfe19413fdc2 | 294 | |
chills | 16:067916791a25 | 295 | DATA *Send_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data |
thomasmorris | 5:2594b953f111 | 296 | |
chills | 16:067916791a25 | 297 | if (Send_Data == NULL){ //If Data is empty |
chills | 16:067916791a25 | 298 | //pc.printf("Out of memory\n\r"); //Print out of memory warning |
chills | 10:46946784326d | 299 | return; |
chills | 10:46946784326d | 300 | } |
chills | 10:46946784326d | 301 | |
chills | 10:46946784326d | 302 | Send_Data->set_time(buffer_time); //Pass in Time |
chills | 10:46946784326d | 303 | Send_Data->set_temperature(temp); //Pass in Temp |
chills | 10:46946784326d | 304 | Send_Data->set_pressure(pressure); //Pass in Pres |
chills | 10:46946784326d | 305 | Send_Data->set_light(lux); //Pass in Light |
chills | 10:46946784326d | 306 | |
chills | 10:46946784326d | 307 | osStatus stat = mail_box.put(Send_Data); //Puts "Send_Data" into the mailbox |
chills | 10:46946784326d | 308 | |
chills | 10:46946784326d | 309 | if (stat == osErrorResource){ //If mailbox overfills |
chills | 10:46946784326d | 310 | //pc.printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); //Print error message |
chills | 10:46946784326d | 311 | mail_box.free(Send_Data); //Free the mail box |
chills | 10:46946784326d | 312 | return; |
chills | 10:46946784326d | 313 | } |
thomasmorris | 5:2594b953f111 | 314 | Red_led.Toggle(); |
thomasmorris | 5:2594b953f111 | 315 | t1.signal_set(NotSamplingTime); |
thomasmorris | 5:2594b953f111 | 316 | } |
thomasmorris | 5:2594b953f111 | 317 | } |
thomasmorris | 5:2594b953f111 | 318 | int main() |
thomasmorris | 5:2594b953f111 | 319 | { |
thomasmorris | 22:eb4cc12087b2 | 320 | |
thomasmorris | 21:3c078c799caa | 321 | |
thomasmorris | 21:3c078c799caa | 322 | |
noutram | 1:e1cf7663f5ff | 323 | //Greeting |
chills | 16:067916791a25 | 324 | pc.printf("Test Start"); |
chills | 16:067916791a25 | 325 | pc.printf("\n\r"); |
chills | 16:067916791a25 | 326 | |
chills | 10:46946784326d | 327 | set_time(1512940530); //Set RTC time to December 10 2017 |
chills | 10:46946784326d | 328 | pc.baud(9600); //Sets the Serial Comms Baud Rate |
thomasmorris | 5:2594b953f111 | 329 | |
chills | 20:cbb71f84cff9 | 330 | LCD.Initialise(); |
chills | 20:cbb71f84cff9 | 331 | LCD.DDRAM_Address(0x40); |
thomasmorris | 21:3c078c799caa | 332 | |
thomasmorris | 21:3c078c799caa | 333 | |
chills | 10:46946784326d | 334 | post(); //Power on Self Test |
noutram | 1:e1cf7663f5ff | 335 | |
noutram | 3:a88838ff33e7 | 336 | //Initialise the SD card (this needs to move) |
noutram | 1:e1cf7663f5ff | 337 | if ( sd.init() != 0) { |
noutram | 1:e1cf7663f5ff | 338 | printf("Init failed \n"); |
noutram | 3:a88838ff33e7 | 339 | lcd.cls(); |
thomasmorris | 21:3c078c799caa | 340 | lcd.printf("CANNOT INIT SD"); //Change me |
noutram | 1:e1cf7663f5ff | 341 | errorCode(FATAL); |
noutram | 1:e1cf7663f5ff | 342 | } |
noutram | 1:e1cf7663f5ff | 343 | |
noutram | 1:e1cf7663f5ff | 344 | //Create a filing system for SD Card |
noutram | 1:e1cf7663f5ff | 345 | FATFileSystem fs("sd", &sd); |
noutram | 0:36e89e3ed7c4 | 346 | |
noutram | 1:e1cf7663f5ff | 347 | //Open to WRITE |
noutram | 1:e1cf7663f5ff | 348 | FILE* fp = fopen("/sd/test.csv","a"); |
noutram | 1:e1cf7663f5ff | 349 | if (fp == NULL) { |
noutram | 1:e1cf7663f5ff | 350 | error("Could not open file for write\n"); |
noutram | 3:a88838ff33e7 | 351 | lcd.cls(); |
thomasmorris | 21:3c078c799caa | 352 | lcd.printf("CANNOT OPEN FILE\n\n");//Change me |
noutram | 1:e1cf7663f5ff | 353 | errorCode(FATAL); |
noutram | 1:e1cf7663f5ff | 354 | } |
noutram | 3:a88838ff33e7 | 355 | |
noutram | 3:a88838ff33e7 | 356 | //Last message before sampling begins |
thomasmorris | 21:3c078c799caa | 357 | lcd.cls();//change me |
thomasmorris | 21:3c078c799caa | 358 | lcd.printf("READY\n\n");//change me |
thomasmorris | 21:3c078c799caa | 359 | |
thomasmorris | 5:2594b953f111 | 360 | |
thomasmorris | 5:2594b953f111 | 361 | //Run interrupt |
thomasmorris | 5:2594b953f111 | 362 | Sample_timer.attach(&Sample_signal_set,TimerInterval); |
thomasmorris | 7:dfe19413fdc2 | 363 | SW1.fall(&SW1FallingEdge); |
thomasmorris | 5:2594b953f111 | 364 | //Run Threads |
thomasmorris | 5:2594b953f111 | 365 | |
thomasmorris | 21:3c078c799caa | 366 | Thread t4(osPriorityAboveNormal);//Sets the Priority bigly |
thomasmorris | 5:2594b953f111 | 367 | t1.start(Sample); |
thomasmorris | 5:2594b953f111 | 368 | t2.start(Serial_Comms); |
thomasmorris | 21:3c078c799caa | 369 | t3.start(ModeSelection); |
thomasmorris | 21:3c078c799caa | 370 | t4.start(Network); |
thomasmorris | 21:3c078c799caa | 371 | |
thomasmorris | 21:3c078c799caa | 372 | |
thomasmorris | 5:2594b953f111 | 373 | //Main thread ID |
thomasmorris | 5:2594b953f111 | 374 | |
thomasmorris | 5:2594b953f111 | 375 | idMain = osThreadGetId(); //CMSIS RTOS call |
thomasmorris | 5:2594b953f111 | 376 | |
thomasmorris | 5:2594b953f111 | 377 | //Thread ID |
thomasmorris | 5:2594b953f111 | 378 | id1 = t1.gettid(); |
thomasmorris | 5:2594b953f111 | 379 | id2 = t2.gettid(); |
thomasmorris | 21:3c078c799caa | 380 | id3 = t3.gettid(); |
thomasmorris | 21:3c078c799caa | 381 | id4 = t4.gettid(); |
thomasmorris | 21:3c078c799caa | 382 | |
thomasmorris | 5:2594b953f111 | 383 | |
thomasmorris | 5:2594b953f111 | 384 | //Toggle Green LED after a button has been pressed |
noutram | 1:e1cf7663f5ff | 385 | //Press either switch to unmount |
thomasmorris | 7:dfe19413fdc2 | 386 | DigitalIn onBoardSwitch(USER_BUTTON); |
thomasmorris | 21:3c078c799caa | 387 | |
thomasmorris | 7:dfe19413fdc2 | 388 | while (onBoardSwitch == 0){ |
thomasmorris | 7:dfe19413fdc2 | 389 | |
noutram | 1:e1cf7663f5ff | 390 | } |
noutram | 1:e1cf7663f5ff | 391 | |
noutram | 1:e1cf7663f5ff | 392 | //Close File |
chills | 15:c1592fc1a501 | 393 | /* |
noutram | 1:e1cf7663f5ff | 394 | fclose(fp); |
noutram | 1:e1cf7663f5ff | 395 | |
noutram | 1:e1cf7663f5ff | 396 | //Close down |
noutram | 1:e1cf7663f5ff | 397 | sd.deinit(); |
noutram | 1:e1cf7663f5ff | 398 | printf("Unmounted...\n"); |
noutram | 1:e1cf7663f5ff | 399 | lcd.cls(); |
noutram | 1:e1cf7663f5ff | 400 | lcd.printf("Unmounted...\n\n"); |
chills | 15:c1592fc1a501 | 401 | */ |
noutram | 1:e1cf7663f5ff | 402 | |
noutram | 1:e1cf7663f5ff | 403 | while(true) { |
noutram | 1:e1cf7663f5ff | 404 | greenLED = 1; |
noutram | 1:e1cf7663f5ff | 405 | wait(0.5); |
noutram | 1:e1cf7663f5ff | 406 | greenLED = 0; |
noutram | 1:e1cf7663f5ff | 407 | wait(0.1); |
noutram | 0:36e89e3ed7c4 | 408 | } |
noutram | 0:36e89e3ed7c4 | 409 | } |
noutram | 0:36e89e3ed7c4 | 410 | |
noutram | 1:e1cf7663f5ff | 411 | |
noutram | 1:e1cf7663f5ff | 412 |