FINAL PROJECT isn't it

Fork of ELEC351 by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Sat Jan 06 17:43:17 2018 +0000
Revision:
30:4cde05cc7c4f
Parent:
29:64b1f95a807c
Child:
31:4a88bf97b53e
Latest Revision LCD time setting

Who changed what in which revision?

UserRevisionLine numberNew 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 30:4cde05cc7c4f 4 Current Verision 13
thomasmorris 29:64b1f95a807c 5 Overiew: Working Tasks 1,2,4,5,6,7,8,10,11,12,13
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 6:97f586597310 11 */
thomasmorris 6:97f586597310 12
thomasmorris 25:36699ed589ab 13 #include "SETUP.hpp"
chills 24:7d2da96e05ad 14
thomasmorris 29:64b1f95a807c 15 void Network()//Interrupt service routine for handling the timeout
thomasmorris 8:0e4481b64353 16 {
thomasmorris 29:64b1f95a807c 17 osEvent evt_network = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 25:36699ed589ab 18
thomasmorris 25:36699ed589ab 19 if (evt_network.status == osEventMail) {
chills 17:37d883f40c3d 20 DATA *Rec_Data_Network = (DATA*)evt_network.value.p; //Create pointer to mailbox
chills 17:37d883f40c3d 21 DATA msg_network; //Create temporary instance of DATA class
thomasmorris 25:36699ed589ab 22
chills 17:37d883f40c3d 23 msg_network.set_time(Rec_Data_Network->get_time()); //Copy time from mailbox to temporary instance
chills 17:37d883f40c3d 24 msg_network.set_temperature(Rec_Data_Network->get_temperature()); //Copy temperature from mailbox to temporary instance
chills 17:37d883f40c3d 25 msg_network.set_pressure(Rec_Data_Network->get_pressure()); //Copy pressure from mailbox to temporary instance
thomasmorris 25:36699ed589ab 26 msg_network.set_light(Rec_Data_Network->get_light()); //Copy light from mailbox to temporary instance
chills 17:37d883f40c3d 27 mail_box.free(Rec_Data_Network); //Free space in the mailbox (delete earliest sample taken)
thomasmorris 25:36699ed589ab 28
chills 24:7d2da96e05ad 29 networktest();
thomasmorris 25:36699ed589ab 30 }
chills 13:db857b3744c6 31 }
thomasmorris 30:4cde05cc7c4f 32 void SW2TimeOutHandler()
thomasmorris 30:4cde05cc7c4f 33 {
thomasmorris 30:4cde05cc7c4f 34 sw2TimeOut.detach();//Stop the timeout counter firing
thomasmorris 30:4cde05cc7c4f 35 SW2.rise(&SW2RisingEdge);//Now wait for a rising edge
thomasmorris 30:4cde05cc7c4f 36 }
thomasmorris 25:36699ed589ab 37 void SW1TimeOutHandler()
thomasmorris 8:0e4481b64353 38 {
thomasmorris 7:dfe19413fdc2 39 sw1TimeOut.detach(); //Stop the timeout counter firing
thomasmorris 30:4cde05cc7c4f 40 SW1.rise(&SW1RisingEdge); //Now wait for a rising edge
thomasmorris 7:dfe19413fdc2 41 }
thomasmorris 29:64b1f95a807c 42 void SDWrite()
thomasmorris 12:536eca338ae8 43 {
thomasmorris 29:64b1f95a807c 44 osEvent evt_sd = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 25:36699ed589ab 45
thomasmorris 25:36699ed589ab 46 if (evt_sd.status == osEventMail) {
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
thomasmorris 25:36699ed589ab 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)
thomasmorris 25:36699ed589ab 55 }
thomasmorris 12:536eca338ae8 56 }
thomasmorris 30:4cde05cc7c4f 57 //Interrupt service routive for SW1 rising edge (release)
thomasmorris 30:4cde05cc7c4f 58 void SW1RisingEdge()
thomasmorris 19:54bc302a82ea 59 {
thomasmorris 30:4cde05cc7c4f 60 sw1TimeOut.attach(&SW1TimeOutHandler, 0.35); //Start timeout counter
thomasmorris 30:4cde05cc7c4f 61 wait(0.1);
thomasmorris 30:4cde05cc7c4f 62 if(SW2 == 1 & SW1 == 1)
thomasmorris 30:4cde05cc7c4f 63 {
thomasmorris 30:4cde05cc7c4f 64 wait(0.2); //Wait 200ms
thomasmorris 30:4cde05cc7c4f 65 if(SW1 == 0 & SW2 == 0)
thomasmorris 30:4cde05cc7c4f 66 {
thomasmorris 30:4cde05cc7c4f 67 if(mode < 7)
thomasmorris 30:4cde05cc7c4f 68 {
thomasmorris 30:4cde05cc7c4f 69 mode = mode + 1;
thomasmorris 30:4cde05cc7c4f 70 }
thomasmorris 30:4cde05cc7c4f 71 else if (mode == 7)
thomasmorris 30:4cde05cc7c4f 72 {
thomasmorris 30:4cde05cc7c4f 73 mode = 0;
thomasmorris 30:4cde05cc7c4f 74 }
thomasmorris 30:4cde05cc7c4f 75 }
thomasmorris 7:dfe19413fdc2 76 }
thomasmorris 30:4cde05cc7c4f 77 }
thomasmorris 25:36699ed589ab 78
thomasmorris 30:4cde05cc7c4f 79 void SW2RisingEdge()
thomasmorris 30:4cde05cc7c4f 80 {
thomasmorris 30:4cde05cc7c4f 81
thomasmorris 30:4cde05cc7c4f 82 while (SW1 == 0) { }
thomasmorris 30:4cde05cc7c4f 83
thomasmorris 30:4cde05cc7c4f 84 //Insert a small delay to reduce switch bounce effects
thomasmorris 30:4cde05cc7c4f 85 wait(0.05);
thomasmorris 30:4cde05cc7c4f 86
thomasmorris 30:4cde05cc7c4f 87 // Wait for SW1 to be released
thomasmorris 30:4cde05cc7c4f 88 while (SW1 == 1) { }
thomasmorris 30:4cde05cc7c4f 89
thomasmorris 30:4cde05cc7c4f 90 mode = mode + 1;
thomasmorris 7:dfe19413fdc2 91 }
thomasmorris 30:4cde05cc7c4f 92
thomasmorris 30:4cde05cc7c4f 93 void LCD_Output()
thomasmorris 30:4cde05cc7c4f 94 {
thomasmorris 30:4cde05cc7c4f 95 while(1)
thomasmorris 29:64b1f95a807c 96 {
thomasmorris 30:4cde05cc7c4f 97 Thread::wait(10);//Dont Delete
chills 13:db857b3744c6 98 osEvent evt_lcd = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 30:4cde05cc7c4f 99 DATA msg_lcd;
thomasmorris 30:4cde05cc7c4f 100 if (evt_lcd.status == osEventMail)
thomasmorris 30:4cde05cc7c4f 101 {
chills 17:37d883f40c3d 102 DATA *Rec_Data_LCD = (DATA*)evt_lcd.value.p; //Create pointer to mailbox
thomasmorris 30:4cde05cc7c4f 103 //Create temporary instance of DATA class
thomasmorris 25:36699ed589ab 104
chills 17:37d883f40c3d 105 msg_lcd.set_time(Rec_Data_LCD->get_time()); //Copy time from mailbox to temporary instance
chills 17:37d883f40c3d 106 msg_lcd.set_temperature(Rec_Data_LCD->get_temperature()); //Copy temperature from mailbox to temporary instance
chills 17:37d883f40c3d 107 msg_lcd.set_pressure(Rec_Data_LCD->get_pressure()); //Copy pressure from mailbox to temporary instance
chills 17:37d883f40c3d 108 msg_lcd.set_light(Rec_Data_LCD->get_light()); //Copy light from mailbox to temporary instance
chills 17:37d883f40c3d 109 mail_box.free(Rec_Data_LCD); //Free space in the mailbox (delete earliest sample taken)
thomasmorris 30:4cde05cc7c4f 110 }
thomasmorris 30:4cde05cc7c4f 111 if(mode == 0)//Default mode
thomasmorris 30:4cde05cc7c4f 112 {
thomasmorris 30:4cde05cc7c4f 113 cout << "In mode 0" << endl;
thomasmorris 30:4cde05cc7c4f 114 SW1.rise(&SW1RisingEdge);//Now wait for a rising edg
thomasmorris 30:4cde05cc7c4f 115 //SW2.rise(&SW2RisingEdge);//Now wait for a rising edg
thomasmorris 30:4cde05cc7c4f 116 Thread::wait(3000);
thomasmorris 30:4cde05cc7c4f 117
thomasmorris 30:4cde05cc7c4f 118 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 30:4cde05cc7c4f 119 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 120 LCD.Write_String("Temp Pres li");
thomasmorris 30:4cde05cc7c4f 121 LCD.DDRAM_Address(0x40);
thomasmorris 30:4cde05cc7c4f 122 LCD.Write_String(LCD_buffer);
thomasmorris 25:36699ed589ab 123
thomasmorris 30:4cde05cc7c4f 124 Thread::wait(3000);
thomasmorris 30:4cde05cc7c4f 125
thomasmorris 30:4cde05cc7c4f 126 time_t msel_time = msg_lcd.get_time(); //Declare local variable for time
thomasmorris 30:4cde05cc7c4f 127 strftime(scom_time_buffer, 32, "%I:%M %p", localtime(&msel_time)); //Format time as a string
thomasmorris 30:4cde05cc7c4f 128 LCD.Display_Clear();
thomasmorris 30:4cde05cc7c4f 129 LCD_sprintf = sprintf (LCD_buffer, "%s",scom_time_buffer);
thomasmorris 30:4cde05cc7c4f 130 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 131 LCD.Write_String("Current Time:");
thomasmorris 30:4cde05cc7c4f 132 LCD.DDRAM_Address(0x40);
thomasmorris 30:4cde05cc7c4f 133 LCD.Write_String(LCD_buffer);
thomasmorris 30:4cde05cc7c4f 134 }
thomasmorris 30:4cde05cc7c4f 135 else if(mode == 1)//Choose either date setting or time setting
thomasmorris 30:4cde05cc7c4f 136 {
thomasmorris 30:4cde05cc7c4f 137 SW1.rise(NULL);
thomasmorris 30:4cde05cc7c4f 138 SW2.rise(NULL);
thomasmorris 30:4cde05cc7c4f 139 Thread::wait(10);
thomasmorris 30:4cde05cc7c4f 140 cout << "In Mode 1" << endl;
thomasmorris 30:4cde05cc7c4f 141 LCD.Display_Clear();
thomasmorris 30:4cde05cc7c4f 142 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 143 LCD.Write_String("Date Time");
thomasmorris 30:4cde05cc7c4f 144 LCD.DDRAM_Address(0x40);
thomasmorris 30:4cde05cc7c4f 145 LCD.Write_String("< >");
thomasmorris 30:4cde05cc7c4f 146
thomasmorris 30:4cde05cc7c4f 147
thomasmorris 21:3c078c799caa 148
thomasmorris 30:4cde05cc7c4f 149 }
thomasmorris 30:4cde05cc7c4f 150 else if(mode == 2)//Set the Year
thomasmorris 30:4cde05cc7c4f 151 {
thomasmorris 30:4cde05cc7c4f 152 cout << "In Mode 2" << endl;
thomasmorris 30:4cde05cc7c4f 153 }
thomasmorris 30:4cde05cc7c4f 154 else if(mode == 3)//Set the Month
thomasmorris 30:4cde05cc7c4f 155 {
thomasmorris 30:4cde05cc7c4f 156 cout << "In Mode 3" << endl;
thomasmorris 30:4cde05cc7c4f 157 }
thomasmorris 30:4cde05cc7c4f 158 else if(mode == 4)//Set the Day
thomasmorris 30:4cde05cc7c4f 159 {
thomasmorris 30:4cde05cc7c4f 160 cout << "In Mode 4" << endl;
thomasmorris 30:4cde05cc7c4f 161 }
thomasmorris 30:4cde05cc7c4f 162 else if(mode == 5)//Set the Hour
thomasmorris 30:4cde05cc7c4f 163 {
thomasmorris 30:4cde05cc7c4f 164 cout << "In Mode 5" << endl;
thomasmorris 30:4cde05cc7c4f 165 }
thomasmorris 30:4cde05cc7c4f 166 else if(mode == 6)//Set the Minute
thomasmorris 30:4cde05cc7c4f 167 {
thomasmorris 30:4cde05cc7c4f 168 cout << "In Mode 6" << endl;
thomasmorris 30:4cde05cc7c4f 169 }
thomasmorris 30:4cde05cc7c4f 170 else if(mode == 7)//Set the Seconds
thomasmorris 30:4cde05cc7c4f 171 {
thomasmorris 30:4cde05cc7c4f 172 cout << "In Mode 7" << endl;
thomasmorris 30:4cde05cc7c4f 173 }
thomasmorris 30:4cde05cc7c4f 174 else
thomasmorris 30:4cde05cc7c4f 175 {
thomasmorris 30:4cde05cc7c4f 176 mode == 0;
thomasmorris 7:dfe19413fdc2 177 }
thomasmorris 7:dfe19413fdc2 178 }
thomasmorris 7:dfe19413fdc2 179 }
thomasmorris 29:64b1f95a807c 180 void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
thomasmorris 22:eb4cc12087b2 181 {
chills 24:7d2da96e05ad 182 string Serial_Input;
thomasmorris 25:36699ed589ab 183 while(1) {
thomasmorris 25:36699ed589ab 184 Serial_Input = "Blank";
chills 24:7d2da96e05ad 185 cout << "Please type in a command" << endl;
thomasmorris 25:36699ed589ab 186
thomasmorris 22:eb4cc12087b2 187 cin >> Serial_Input;
thomasmorris 25:36699ed589ab 188
thomasmorris 22:eb4cc12087b2 189 if (Serial_Input == "Test")
thomasmorris 22:eb4cc12087b2 190 {
chills 24:7d2da96e05ad 191 cout << "Test Confirmed" << endl;
thomasmorris 30:4cde05cc7c4f 192 cout << "This is the time " << get_current_time() << endl;
thomasmorris 25:36699ed589ab 193 }
thomasmorris 25:36699ed589ab 194 else if(Serial_Input == "READALL")
thomasmorris 22:eb4cc12087b2 195 {
chills 26:78f5e454e59f 196 cout << "Read All Confirmed " << Sample_Rate << endl;
chills 26:78f5e454e59f 197 Console_Output_Timer.attach(&Console_Output_ISR,Sample_Rate);
thomasmorris 25:36699ed589ab 198 }
chills 26:78f5e454e59f 199 else if(Serial_Input == "DELETEALL")
chills 26:78f5e454e59f 200 {
chills 27:807dd5660c4b 201 Sampling_Timer.detach();
chills 27:807dd5660c4b 202 Console_Output_Timer.detach();
thomasmorris 29:64b1f95a807c 203 for (int x = 0; x < mailsize; x++)
thomasmorris 29:64b1f95a807c 204 {
chills 27:807dd5660c4b 205 DATA *Delete_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
chills 27:807dd5660c4b 206 mail_box.free(Delete_Data); //Puts "Send_Data" into the mailbox
chills 27:807dd5660c4b 207 }
chills 27:807dd5660c4b 208 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 27:807dd5660c4b 209 cout << "Sampling Restarted" << endl;
thomasmorris 25:36699ed589ab 210 }
chills 27:807dd5660c4b 211 else if(Serial_Input == "READ")
thomasmorris 22:eb4cc12087b2 212 {
thomasmorris 29:64b1f95a807c 213 cout << "Please enter in the number of samples to read" <<endl;
thomasmorris 29:64b1f95a807c 214 int Loops; cin >> Loops;
thomasmorris 29:64b1f95a807c 215 for (int x = 0; x < Loops; x++)
thomasmorris 29:64b1f95a807c 216 {
thomasmorris 29:64b1f95a807c 217 Serial_Comms_Stealth_Mode();
thomasmorris 29:64b1f95a807c 218 }
thomasmorris 25:36699ed589ab 219 }
chills 26:78f5e454e59f 220 else if(Serial_Input == "DELETE")
thomasmorris 22:eb4cc12087b2 221 {
chills 26:78f5e454e59f 222 Sampling_Timer.detach();
chills 26:78f5e454e59f 223 Console_Output_Timer.detach();
chills 26:78f5e454e59f 224 cout << "Number of samples to delete:" << endl;
chills 26:78f5e454e59f 225 int Loops; cin >> Loops;
chills 26:78f5e454e59f 226 for (int x = 0; x < Loops; x++){
chills 26:78f5e454e59f 227 DATA *Delete_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
chills 26:78f5e454e59f 228 mail_box.free(Delete_Data); //Puts "Send_Data" into the mailbox
chills 26:78f5e454e59f 229 }
chills 26:78f5e454e59f 230 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 26:78f5e454e59f 231 cout << "Sampling Restarted" << endl;
thomasmorris 25:36699ed589ab 232 }
thomasmorris 25:36699ed589ab 233 else if(Serial_Input == "SETDATE")
chills 26:78f5e454e59f 234 {
thomasmorris 25:36699ed589ab 235 set_time(SETDATE());
chills 26:78f5e454e59f 236 }
thomasmorris 25:36699ed589ab 237 else if(Serial_Input == "SETT")
thomasmorris 22:eb4cc12087b2 238 {
chills 26:78f5e454e59f 239 float Temp_Sample_Rate = SETT();
chills 26:78f5e454e59f 240
chills 26:78f5e454e59f 241 if(Temp_Sample_Rate >= 1){Sample_Rate = Temp_Sample_Rate;}
chills 26:78f5e454e59f 242
chills 26:78f5e454e59f 243 Sampling_Timer.detach();
chills 26:78f5e454e59f 244 Console_Output_Timer.detach();
chills 26:78f5e454e59f 245 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
thomasmorris 25:36699ed589ab 246 }
chills 27:807dd5660c4b 247 else if(Serial_Input == "STATE")
chills 27:807dd5660c4b 248 {
chills 27:807dd5660c4b 249 Sampling_Timer.detach(); Console_Output_Timer.detach();
chills 27:807dd5660c4b 250 cout << "Turn Sampling On or Off:" << endl;
chills 27:807dd5660c4b 251 string State; cin >> State;
chills 27:807dd5660c4b 252
chills 27:807dd5660c4b 253 if(State == "On"){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}
chills 27:807dd5660c4b 254 if(State == "Off"){Sampling_Timer.detach();}
thomasmorris 25:36699ed589ab 255 }
thomasmorris 25:36699ed589ab 256 else if(Serial_Input == "LOGGING.<x>")
chills 26:78f5e454e59f 257 {
thomasmorris 25:36699ed589ab 258
thomasmorris 25:36699ed589ab 259 }
thomasmorris 25:36699ed589ab 260 else if(Serial_Input == "HELP")// Use this to display all of the availble commands
thomasmorris 25:36699ed589ab 261 {
thomasmorris 25:36699ed589ab 262 HELP();
thomasmorris 22:eb4cc12087b2 263 }
chills 26:78f5e454e59f 264 else if(Serial_Input == "STOP")
chills 26:78f5e454e59f 265 {
thomasmorris 28:09b5c46c8afd 266 Console_Output_Timer.detach(); //Stops Sampling
chills 26:78f5e454e59f 267 }
thomasmorris 30:4cde05cc7c4f 268 else if(Serial_Input == "Blank Blank")
thomasmorris 30:4cde05cc7c4f 269 {
thomasmorris 30:4cde05cc7c4f 270 cout << "Blank Blank complete" << endl;
thomasmorris 30:4cde05cc7c4f 271 }
thomasmorris 25:36699ed589ab 272 else
thomasmorris 22:eb4cc12087b2 273 {
chills 26:78f5e454e59f 274 cout << "Please enter an acceptable command" << endl;
thomasmorris 22:eb4cc12087b2 275 }
thomasmorris 22:eb4cc12087b2 276 }
thomasmorris 22:eb4cc12087b2 277 }
thomasmorris 29:64b1f95a807c 278 void Serial_Comms_Stealth_Mode()//Change this name
thomasmorris 29:64b1f95a807c 279 {
thomasmorris 29:64b1f95a807c 280 osEvent evt_serial = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 29:64b1f95a807c 281
thomasmorris 29:64b1f95a807c 282 DATA *Rec_Data_Serial = (DATA*)evt_serial.value.p; //Create pointer to mailbox
thomasmorris 29:64b1f95a807c 283 DATA msg_serial; //Create temporary instance of DATA class
thomasmorris 29:64b1f95a807c 284
thomasmorris 29:64b1f95a807c 285 msg_serial.set_time(Rec_Data_Serial->get_time()); //Copy time from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 286 msg_serial.set_temperature(Rec_Data_Serial->get_temperature()); //Copy teperature from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 287 msg_serial.set_pressure(Rec_Data_Serial->get_pressure()); //Copy pressure from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 288 msg_serial.set_light(Rec_Data_Serial->get_light()); //Copy light from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 289 mail_box.free(Rec_Data_Serial); //Free space in the mailbox (delete earliest sample taken)
thomasmorris 29:64b1f95a807c 290 time_t scom_time = msg_serial.get_time(); //Declare local variable for time
thomasmorris 29:64b1f95a807c 291 strftime(scom_time_buffer, 32, "%I:%M %p\t", localtime(&scom_time)); //Format time as a string
thomasmorris 29:64b1f95a807c 292 pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time
thomasmorris 29:64b1f95a807c 293 pc.printf("Temperature = %f\t", msg_serial.get_temperature()); //Print Temperature
thomasmorris 29:64b1f95a807c 294 pc.printf("Pressure = %f\t", msg_serial.get_pressure()); //Print Pressure
thomasmorris 29:64b1f95a807c 295 pc.printf("Light = %f\n\r", msg_serial.get_light()); //Print Light
thomasmorris 29:64b1f95a807c 296
thomasmorris 29:64b1f95a807c 297 Green_led.Toggle();
thomasmorris 29:64b1f95a807c 298 }
chills 24:7d2da96e05ad 299 void Serial_Comms()//Thread for Serial Communications
thomasmorris 5:2594b953f111 300 {
thomasmorris 25:36699ed589ab 301 while(1) {
thomasmorris 28:09b5c46c8afd 302 Thread::signal_wait(SerialCommsTime);//Stupid 0 Bug
thomasmorris 29:64b1f95a807c 303 Serial_Comms_Stealth_Mode();
thomasmorris 5:2594b953f111 304 }
thomasmorris 5:2594b953f111 305 }
thomasmorris 5:2594b953f111 306
thomasmorris 28:09b5c46c8afd 307 void Console_Output_ISR() {t2.signal_set(SerialCommsTime);}
chills 26:78f5e454e59f 308 void Sampling_ISR() {t1.signal_set(SamplingTime);}
chills 26:78f5e454e59f 309
chills 26:78f5e454e59f 310
thomasmorris 5:2594b953f111 311 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 312 {
thomasmorris 30:4cde05cc7c4f 313 while(1)
thomasmorris 30:4cde05cc7c4f 314 {
chills 10:46946784326d 315 Thread::signal_wait(SamplingTime); //Set the time between samples
thomasmorris 25:36699ed589ab 316
chills 10:46946784326d 317 temp = sensor.getTemperature(); //Read Temperature
chills 10:46946784326d 318 pressure = sensor.getPressure(); //Read Pressure
chills 10:46946784326d 319 lux = adcIn.read(); //Read Light
chills 16:067916791a25 320 time_t buffer_time = time(NULL); //Read Time
thomasmorris 25:36699ed589ab 321
chills 16:067916791a25 322 DATA *Send_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
thomasmorris 25:36699ed589ab 323
thomasmorris 25:36699ed589ab 324 if (Send_Data == NULL) { //If Data is empty
chills 16:067916791a25 325 //pc.printf("Out of memory\n\r"); //Print out of memory warning
chills 10:46946784326d 326 return;
chills 10:46946784326d 327 }
chills 10:46946784326d 328 Send_Data->set_time(buffer_time); //Pass in Time
chills 10:46946784326d 329 Send_Data->set_temperature(temp); //Pass in Temp
chills 10:46946784326d 330 Send_Data->set_pressure(pressure); //Pass in Pres
chills 10:46946784326d 331 Send_Data->set_light(lux); //Pass in Light
thomasmorris 25:36699ed589ab 332
chills 10:46946784326d 333 osStatus stat = mail_box.put(Send_Data); //Puts "Send_Data" into the mailbox
thomasmorris 25:36699ed589ab 334
thomasmorris 25:36699ed589ab 335 if (stat == osErrorResource) { //If mailbox overfills
chills 10:46946784326d 336 mail_box.free(Send_Data); //Free the mail box
chills 10:46946784326d 337 }
thomasmorris 5:2594b953f111 338 Red_led.Toggle();
thomasmorris 5:2594b953f111 339 t1.signal_set(NotSamplingTime);
thomasmorris 5:2594b953f111 340 }
thomasmorris 25:36699ed589ab 341 }
thomasmorris 25:36699ed589ab 342 int main()
thomasmorris 25:36699ed589ab 343 {
noutram 1:e1cf7663f5ff 344 //Greeting
chills 16:067916791a25 345 pc.printf("Test Start");
chills 16:067916791a25 346 pc.printf("\n\r");
thomasmorris 25:36699ed589ab 347
chills 10:46946784326d 348 set_time(1512940530); //Set RTC time to December 10 2017
thomasmorris 30:4cde05cc7c4f 349
chills 10:46946784326d 350 pc.baud(9600); //Sets the Serial Comms Baud Rate
thomasmorris 25:36699ed589ab 351
chills 20:cbb71f84cff9 352 LCD.Initialise();
thomasmorris 28:09b5c46c8afd 353 LCD.DDRAM_Address(0x00);
thomasmorris 29:64b1f95a807c 354
thomasmorris 25:36699ed589ab 355 post(); //Power on Self Test
thomasmorris 25:36699ed589ab 356
noutram 3:a88838ff33e7 357 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 358 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 359 printf("Init failed \n");
thomasmorris 28:09b5c46c8afd 360 LCD.Display_Clear();
thomasmorris 28:09b5c46c8afd 361 LCD.Write_String("CANNOT INIT SD"); //Change me
noutram 1:e1cf7663f5ff 362 errorCode(FATAL);
thomasmorris 25:36699ed589ab 363 }
noutram 1:e1cf7663f5ff 364 //Create a filing system for SD Card
thomasmorris 25:36699ed589ab 365 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 366
noutram 1:e1cf7663f5ff 367 //Open to WRITE
noutram 1:e1cf7663f5ff 368 FILE* fp = fopen("/sd/test.csv","a");
noutram 1:e1cf7663f5ff 369 if (fp == NULL) {
noutram 1:e1cf7663f5ff 370 error("Could not open file for write\n");
thomasmorris 28:09b5c46c8afd 371 LCD.Display_Clear();
thomasmorris 28:09b5c46c8afd 372 LCD.Write_String("CANNOT OPEN FILE\n\n");//Change me
noutram 1:e1cf7663f5ff 373 errorCode(FATAL);
noutram 1:e1cf7663f5ff 374 }
noutram 3:a88838ff33e7 375 //Last message before sampling begins
thomasmorris 28:09b5c46c8afd 376 LCD.Display_Clear();
thomasmorris 30:4cde05cc7c4f 377 LCD.Write_String("READY");
thomasmorris 25:36699ed589ab 378
thomasmorris 25:36699ed589ab 379
thomasmorris 25:36699ed589ab 380 Sample_Rate = TimerInterval;
chills 24:7d2da96e05ad 381 //Run interrupt
chills 26:78f5e454e59f 382 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 24:7d2da96e05ad 383
thomasmorris 5:2594b953f111 384 t1.start(Sample);
chills 26:78f5e454e59f 385 t2.start(Serial_Comms);
thomasmorris 30:4cde05cc7c4f 386 t3.start(LCD_Output);
chills 24:7d2da96e05ad 387 //t4.start(Network);
thomasmorris 23:3c85d7f657a2 388 t5.start(Serial_Commands);
thomasmorris 25:36699ed589ab 389
thomasmorris 5:2594b953f111 390 //Main thread ID
thomasmorris 25:36699ed589ab 391
thomasmorris 5:2594b953f111 392 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 25:36699ed589ab 393
thomasmorris 5:2594b953f111 394 //Thread ID
thomasmorris 5:2594b953f111 395 id1 = t1.gettid();
thomasmorris 5:2594b953f111 396 id2 = t2.gettid();
thomasmorris 21:3c078c799caa 397 id3 = t3.gettid();
thomasmorris 21:3c078c799caa 398 id4 = t4.gettid();
chills 24:7d2da96e05ad 399 id5 = t5.gettid();
thomasmorris 25:36699ed589ab 400
thomasmorris 25:36699ed589ab 401 while (onBoardSwitch == 0) {
thomasmorris 25:36699ed589ab 402
noutram 1:e1cf7663f5ff 403 }
thomasmorris 25:36699ed589ab 404
noutram 1:e1cf7663f5ff 405 //Close File
chills 15:c1592fc1a501 406 /*
noutram 1:e1cf7663f5ff 407 fclose(fp);
thomasmorris 25:36699ed589ab 408
noutram 1:e1cf7663f5ff 409 //Close down
noutram 1:e1cf7663f5ff 410 sd.deinit();
noutram 1:e1cf7663f5ff 411 printf("Unmounted...\n");
noutram 1:e1cf7663f5ff 412 lcd.cls();
noutram 1:e1cf7663f5ff 413 lcd.printf("Unmounted...\n\n");
chills 15:c1592fc1a501 414 */
thomasmorris 25:36699ed589ab 415
noutram 1:e1cf7663f5ff 416 while(true) {
noutram 1:e1cf7663f5ff 417 greenLED = 1;
noutram 1:e1cf7663f5ff 418 wait(0.5);
noutram 1:e1cf7663f5ff 419 greenLED = 0;
thomasmorris 25:36699ed589ab 420 wait(0.1);
noutram 0:36e89e3ed7c4 421 }
noutram 0:36e89e3ed7c4 422 }