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