Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
chills
Date:
Sun Jan 07 00:45:11 2018 +0000
Revision:
33:3b5096f0126a
Parent:
32:8f2795716e97
Child:
34:c0b8705f183d
2018_01_07 00:41; Write using in-built time class;

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 32:8f2795716e97 4 Current Verision 15
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
chills 33:3b5096f0126a 15 Mutex Time_Lock_Main;
chills 33:3b5096f0126a 16
chills 33:3b5096f0126a 17 void Network() //Interrupt service routine for handling the timeout
thomasmorris 8:0e4481b64353 18 {
chills 33:3b5096f0126a 19 osEvent evt_network = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 25:36699ed589ab 20
thomasmorris 25:36699ed589ab 21 if (evt_network.status == osEventMail) {
chills 17:37d883f40c3d 22 DATA *Rec_Data_Network = (DATA*)evt_network.value.p; //Create pointer to mailbox
chills 17:37d883f40c3d 23 DATA msg_network; //Create temporary instance of DATA class
thomasmorris 25:36699ed589ab 24
chills 17:37d883f40c3d 25 msg_network.set_time(Rec_Data_Network->get_time()); //Copy time from mailbox to temporary instance
chills 17:37d883f40c3d 26 msg_network.set_temperature(Rec_Data_Network->get_temperature()); //Copy temperature from mailbox to temporary instance
chills 17:37d883f40c3d 27 msg_network.set_pressure(Rec_Data_Network->get_pressure()); //Copy pressure from mailbox to temporary instance
thomasmorris 25:36699ed589ab 28 msg_network.set_light(Rec_Data_Network->get_light()); //Copy light from mailbox to temporary instance
chills 17:37d883f40c3d 29 mail_box.free(Rec_Data_Network); //Free space in the mailbox (delete earliest sample taken)
thomasmorris 25:36699ed589ab 30
chills 24:7d2da96e05ad 31 networktest();
thomasmorris 25:36699ed589ab 32 }
chills 13:db857b3744c6 33 }
thomasmorris 31:4a88bf97b53e 34 void LCD_Write_Year()
thomasmorris 12:536eca338ae8 35 {
chills 33:3b5096f0126a 36 Time_Lock_Main.lock();
chills 33:3b5096f0126a 37 time_t Time = time(NULL);
chills 33:3b5096f0126a 38 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 39 int Years = 1900 + Time_Pointer->tm_year;
chills 33:3b5096f0126a 40 Time_Lock_Main.unlock();
thomasmorris 31:4a88bf97b53e 41 stringstream ss;
thomasmorris 31:4a88bf97b53e 42 ss << Years;
thomasmorris 31:4a88bf97b53e 43 string Year_String = ss.str();
thomasmorris 31:4a88bf97b53e 44 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 45 LCD.Write_String("Set Year");
thomasmorris 31:4a88bf97b53e 46 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 47 LCD.Write_String(Year_String);
thomasmorris 30:4cde05cc7c4f 48 }
thomasmorris 25:36699ed589ab 49
thomasmorris 31:4a88bf97b53e 50 void LCD_Write_Month()
thomasmorris 31:4a88bf97b53e 51 {
chills 33:3b5096f0126a 52 Time_Lock_Main.lock();
chills 33:3b5096f0126a 53 time_t Time = time(NULL);
chills 33:3b5096f0126a 54 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 55 int Months = 1 + Time_Pointer->tm_mon;
chills 33:3b5096f0126a 56 Time_Lock_Main.unlock();
thomasmorris 31:4a88bf97b53e 57 stringstream ss;
thomasmorris 31:4a88bf97b53e 58 ss << Months;
thomasmorris 31:4a88bf97b53e 59 string Month_String = ss.str();
thomasmorris 31:4a88bf97b53e 60 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 61 LCD.Write_String("Set Month");
thomasmorris 31:4a88bf97b53e 62 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 63 LCD.Write_String(Month_String);
thomasmorris 31:4a88bf97b53e 64 }
thomasmorris 31:4a88bf97b53e 65 void LCD_Write_Day()
thomasmorris 31:4a88bf97b53e 66 {
chills 33:3b5096f0126a 67 Time_Lock_Main.lock();
chills 33:3b5096f0126a 68 time_t Time = time(NULL);
chills 33:3b5096f0126a 69 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 70 int Days = Time_Pointer->tm_mday;
chills 33:3b5096f0126a 71 Time_Lock_Main.unlock();
thomasmorris 31:4a88bf97b53e 72 stringstream ss;
thomasmorris 31:4a88bf97b53e 73 ss << Days;
thomasmorris 31:4a88bf97b53e 74 string Day_String = ss.str();
thomasmorris 31:4a88bf97b53e 75 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 76 LCD.Write_String("Set Day");
thomasmorris 31:4a88bf97b53e 77 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 78 LCD.Write_String(Day_String);
thomasmorris 31:4a88bf97b53e 79 }
thomasmorris 31:4a88bf97b53e 80
thomasmorris 31:4a88bf97b53e 81
thomasmorris 31:4a88bf97b53e 82 void LCD_Write_Hour()
thomasmorris 30:4cde05cc7c4f 83 {
chills 33:3b5096f0126a 84 Time_Lock_Main.lock();
chills 33:3b5096f0126a 85 time_t Time = time(NULL);
chills 33:3b5096f0126a 86 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 87 int Hours = Time_Pointer->tm_hour;
chills 33:3b5096f0126a 88 Time_Lock_Main.unlock();
thomasmorris 31:4a88bf97b53e 89 stringstream ss;
thomasmorris 31:4a88bf97b53e 90 ss << Hours;
thomasmorris 31:4a88bf97b53e 91 string Hour_String = ss.str();
thomasmorris 31:4a88bf97b53e 92 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 93 LCD.Write_String("Set Hour");
thomasmorris 31:4a88bf97b53e 94 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 95 LCD.Write_String(Hour_String);
thomasmorris 31:4a88bf97b53e 96 }
thomasmorris 31:4a88bf97b53e 97 void LCD_Write_Minute()
thomasmorris 31:4a88bf97b53e 98 {
chills 33:3b5096f0126a 99 Time_Lock_Main.lock();
chills 33:3b5096f0126a 100 time_t Time = time(NULL);
chills 33:3b5096f0126a 101 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 102 int Minutes = Time_Pointer->tm_min;
chills 33:3b5096f0126a 103 Time_Lock_Main.unlock();
thomasmorris 31:4a88bf97b53e 104 stringstream ss;
thomasmorris 31:4a88bf97b53e 105 ss << Minutes;
thomasmorris 31:4a88bf97b53e 106 string Minute_String = ss.str();
thomasmorris 31:4a88bf97b53e 107 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 108 LCD.Write_String("Set Minute");
thomasmorris 31:4a88bf97b53e 109 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 110 LCD.Write_String(Minute_String);
thomasmorris 31:4a88bf97b53e 111 }
thomasmorris 31:4a88bf97b53e 112 void LCD_Write_Seconds()
thomasmorris 31:4a88bf97b53e 113 {
chills 33:3b5096f0126a 114 Time_Lock_Main.lock();
chills 33:3b5096f0126a 115 time_t Time = time(NULL);
chills 33:3b5096f0126a 116 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 117 int Seconds = Time_Pointer->tm_sec;
chills 33:3b5096f0126a 118 Time_Lock_Main.unlock();
thomasmorris 31:4a88bf97b53e 119 stringstream ss;
thomasmorris 31:4a88bf97b53e 120 ss << Seconds;
thomasmorris 31:4a88bf97b53e 121 string Second_String = ss.str();
thomasmorris 31:4a88bf97b53e 122 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 123 LCD.Write_String("Set Second");
thomasmorris 31:4a88bf97b53e 124 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 125 LCD.Write_String(Second_String);
thomasmorris 7:dfe19413fdc2 126 }
thomasmorris 30:4cde05cc7c4f 127
thomasmorris 30:4cde05cc7c4f 128 void LCD_Output()
thomasmorris 30:4cde05cc7c4f 129 {
thomasmorris 30:4cde05cc7c4f 130 while(1)
thomasmorris 29:64b1f95a807c 131 {
thomasmorris 30:4cde05cc7c4f 132 Thread::wait(10);//Dont Delete
chills 13:db857b3744c6 133 osEvent evt_lcd = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 31:4a88bf97b53e 134 DATA msg_lcd; //Create temporary instance of DATA class
thomasmorris 30:4cde05cc7c4f 135 if (evt_lcd.status == osEventMail)
thomasmorris 30:4cde05cc7c4f 136 {
chills 17:37d883f40c3d 137 DATA *Rec_Data_LCD = (DATA*)evt_lcd.value.p; //Create pointer to mailbox
thomasmorris 25:36699ed589ab 138
chills 17:37d883f40c3d 139 msg_lcd.set_time(Rec_Data_LCD->get_time()); //Copy time from mailbox to temporary instance
chills 17:37d883f40c3d 140 msg_lcd.set_temperature(Rec_Data_LCD->get_temperature()); //Copy temperature from mailbox to temporary instance
chills 17:37d883f40c3d 141 msg_lcd.set_pressure(Rec_Data_LCD->get_pressure()); //Copy pressure from mailbox to temporary instance
chills 17:37d883f40c3d 142 msg_lcd.set_light(Rec_Data_LCD->get_light()); //Copy light from mailbox to temporary instance
chills 17:37d883f40c3d 143 mail_box.free(Rec_Data_LCD); //Free space in the mailbox (delete earliest sample taken)
thomasmorris 30:4cde05cc7c4f 144 }
thomasmorris 30:4cde05cc7c4f 145 if(mode == 0)//Default mode
thomasmorris 30:4cde05cc7c4f 146 {
chills 33:3b5096f0126a 147 cout << "In mode 0 " << endl;
chills 33:3b5096f0126a 148
thomasmorris 30:4cde05cc7c4f 149 Thread::wait(3000);
chills 33:3b5096f0126a 150
chills 33:3b5096f0126a 151 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
chills 33:3b5096f0126a 152
thomasmorris 30:4cde05cc7c4f 153 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 154 LCD.Write_String("Temp Pres li");
thomasmorris 30:4cde05cc7c4f 155 LCD.DDRAM_Address(0x40);
thomasmorris 30:4cde05cc7c4f 156 LCD.Write_String(LCD_buffer);
thomasmorris 25:36699ed589ab 157
thomasmorris 30:4cde05cc7c4f 158 Thread::wait(3000);
chills 33:3b5096f0126a 159
chills 33:3b5096f0126a 160 Time_Lock_Main.lock();
chills 33:3b5096f0126a 161 time_t Time = time(NULL);
chills 33:3b5096f0126a 162 tm* Time_Pointer = localtime(&Time);
thomasmorris 30:4cde05cc7c4f 163 LCD.Display_Clear();
chills 33:3b5096f0126a 164 sprintf (LCD_buffer, "%d:%d %d,%d",Time_Pointer->tm_hour,Time_Pointer->tm_min,(Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));//Used for converting to a sting
chills 33:3b5096f0126a 165 Time_Lock_Main.unlock();
chills 33:3b5096f0126a 166
thomasmorris 30:4cde05cc7c4f 167 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 168 LCD.Write_String("Current Time:");
thomasmorris 30:4cde05cc7c4f 169 LCD.DDRAM_Address(0x40);
thomasmorris 30:4cde05cc7c4f 170 LCD.Write_String(LCD_buffer);
thomasmorris 31:4a88bf97b53e 171
thomasmorris 31:4a88bf97b53e 172 if(SW1.read() & SW2.read() == 1)
thomasmorris 31:4a88bf97b53e 173 {
thomasmorris 31:4a88bf97b53e 174 mode = 1;
thomasmorris 31:4a88bf97b53e 175 }
thomasmorris 30:4cde05cc7c4f 176 }
thomasmorris 30:4cde05cc7c4f 177 else if(mode == 1)//Choose either date setting or time setting
thomasmorris 30:4cde05cc7c4f 178 {
thomasmorris 30:4cde05cc7c4f 179 cout << "In Mode 1" << endl;
thomasmorris 30:4cde05cc7c4f 180 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 181 while(1)
thomasmorris 31:4a88bf97b53e 182 {
thomasmorris 31:4a88bf97b53e 183 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 184 LCD.Write_String("Date Time");
thomasmorris 31:4a88bf97b53e 185 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 186 LCD.Write_String("< >");
thomasmorris 31:4a88bf97b53e 187 Thread::wait(1000);
thomasmorris 31:4a88bf97b53e 188 if(SW1.read() == 1)
thomasmorris 31:4a88bf97b53e 189 {
thomasmorris 31:4a88bf97b53e 190 mode = 2;
thomasmorris 31:4a88bf97b53e 191 break;
thomasmorris 31:4a88bf97b53e 192 }
thomasmorris 31:4a88bf97b53e 193 if(SW2.read() == 1)
thomasmorris 31:4a88bf97b53e 194 {
thomasmorris 31:4a88bf97b53e 195 mode = 5;
thomasmorris 31:4a88bf97b53e 196 break;
thomasmorris 31:4a88bf97b53e 197 }
thomasmorris 31:4a88bf97b53e 198 }
thomasmorris 30:4cde05cc7c4f 199 }
thomasmorris 30:4cde05cc7c4f 200 else if(mode == 2)//Set the Year
thomasmorris 30:4cde05cc7c4f 201 {
thomasmorris 30:4cde05cc7c4f 202 cout << "In Mode 2" << endl;
thomasmorris 31:4a88bf97b53e 203 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 204 while(1)
thomasmorris 31:4a88bf97b53e 205 {
thomasmorris 32:8f2795716e97 206 LCD_Write_Year();
thomasmorris 31:4a88bf97b53e 207 Thread::wait(1000);
chills 33:3b5096f0126a 208 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 209 {
chills 33:3b5096f0126a 210 mode = 3;
chills 33:3b5096f0126a 211 break;
chills 33:3b5096f0126a 212 }
thomasmorris 31:4a88bf97b53e 213 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 214 {
thomasmorris 31:4a88bf97b53e 215 Add_Year();
thomasmorris 31:4a88bf97b53e 216 }
thomasmorris 31:4a88bf97b53e 217 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 218 {
thomasmorris 31:4a88bf97b53e 219 Subtract_Year();
thomasmorris 31:4a88bf97b53e 220 }
thomasmorris 31:4a88bf97b53e 221 LCD_Write_Year();
thomasmorris 31:4a88bf97b53e 222 }
thomasmorris 30:4cde05cc7c4f 223 }
thomasmorris 30:4cde05cc7c4f 224 else if(mode == 3)//Set the Month
thomasmorris 30:4cde05cc7c4f 225 {
thomasmorris 30:4cde05cc7c4f 226 cout << "In Mode 3" << endl;
thomasmorris 31:4a88bf97b53e 227 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 228 while(1)
thomasmorris 32:8f2795716e97 229 {
thomasmorris 32:8f2795716e97 230 LCD_Write_Month();
thomasmorris 31:4a88bf97b53e 231 Thread::wait(1000);
chills 33:3b5096f0126a 232 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 233 {
chills 33:3b5096f0126a 234 mode = 4;
chills 33:3b5096f0126a 235 break;
chills 33:3b5096f0126a 236 }
thomasmorris 31:4a88bf97b53e 237 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 238 {
thomasmorris 31:4a88bf97b53e 239 Add_Month();
thomasmorris 31:4a88bf97b53e 240 }
thomasmorris 31:4a88bf97b53e 241 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 242 {
thomasmorris 31:4a88bf97b53e 243 Subtract_Month();
thomasmorris 32:8f2795716e97 244 }
chills 33:3b5096f0126a 245
thomasmorris 31:4a88bf97b53e 246 }
thomasmorris 30:4cde05cc7c4f 247 }
thomasmorris 30:4cde05cc7c4f 248 else if(mode == 4)//Set the Day
thomasmorris 30:4cde05cc7c4f 249 {
thomasmorris 30:4cde05cc7c4f 250 cout << "In Mode 4" << endl;
thomasmorris 31:4a88bf97b53e 251 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 252 while(1)
thomasmorris 32:8f2795716e97 253 {
thomasmorris 32:8f2795716e97 254 LCD_Write_Day();
thomasmorris 31:4a88bf97b53e 255 Thread::wait(1000);
chills 33:3b5096f0126a 256 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 257 {
chills 33:3b5096f0126a 258 mode = 0;
chills 33:3b5096f0126a 259 break;
chills 33:3b5096f0126a 260 }
thomasmorris 31:4a88bf97b53e 261 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 262 {
thomasmorris 31:4a88bf97b53e 263 Add_Day();
thomasmorris 31:4a88bf97b53e 264 }
thomasmorris 31:4a88bf97b53e 265 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 266 {
thomasmorris 31:4a88bf97b53e 267 Subtract_Day();
thomasmorris 32:8f2795716e97 268 }
thomasmorris 31:4a88bf97b53e 269 }
thomasmorris 30:4cde05cc7c4f 270 }
thomasmorris 30:4cde05cc7c4f 271 else if(mode == 5)//Set the Hour
thomasmorris 30:4cde05cc7c4f 272 {
thomasmorris 30:4cde05cc7c4f 273 cout << "In Mode 5" << endl;
thomasmorris 31:4a88bf97b53e 274 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 275 while(1)
thomasmorris 31:4a88bf97b53e 276 {
thomasmorris 32:8f2795716e97 277 LCD_Write_Hour();
thomasmorris 32:8f2795716e97 278 Thread::wait(1000);
chills 33:3b5096f0126a 279 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 280 {
chills 33:3b5096f0126a 281 mode = 6;
chills 33:3b5096f0126a 282 break;
chills 33:3b5096f0126a 283 }
thomasmorris 31:4a88bf97b53e 284 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 285 {
thomasmorris 31:4a88bf97b53e 286 Add_Hour();
thomasmorris 31:4a88bf97b53e 287 }
thomasmorris 31:4a88bf97b53e 288 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 289 {
thomasmorris 31:4a88bf97b53e 290 Subtract_Hour();
thomasmorris 31:4a88bf97b53e 291 }
thomasmorris 31:4a88bf97b53e 292
chills 33:3b5096f0126a 293
thomasmorris 31:4a88bf97b53e 294 }
thomasmorris 30:4cde05cc7c4f 295 }
thomasmorris 30:4cde05cc7c4f 296 else if(mode == 6)//Set the Minute
thomasmorris 30:4cde05cc7c4f 297 {
thomasmorris 30:4cde05cc7c4f 298 cout << "In Mode 6" << endl;
thomasmorris 31:4a88bf97b53e 299 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 300 while(1)
thomasmorris 31:4a88bf97b53e 301 {
thomasmorris 32:8f2795716e97 302 LCD_Write_Minute();
thomasmorris 32:8f2795716e97 303 Thread::wait(1000);
chills 33:3b5096f0126a 304 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 305 {
chills 33:3b5096f0126a 306 mode = 7;
chills 33:3b5096f0126a 307 break;
chills 33:3b5096f0126a 308 }
thomasmorris 31:4a88bf97b53e 309 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 310 {
thomasmorris 31:4a88bf97b53e 311 Add_Minute();
thomasmorris 31:4a88bf97b53e 312 }
thomasmorris 31:4a88bf97b53e 313 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 314 {
thomasmorris 31:4a88bf97b53e 315 Subtract_Minute();
thomasmorris 32:8f2795716e97 316 }
thomasmorris 31:4a88bf97b53e 317 }
thomasmorris 30:4cde05cc7c4f 318 }
thomasmorris 30:4cde05cc7c4f 319 else if(mode == 7)//Set the Seconds
thomasmorris 30:4cde05cc7c4f 320 {
thomasmorris 30:4cde05cc7c4f 321 cout << "In Mode 7" << endl;
thomasmorris 31:4a88bf97b53e 322 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 323 while(1)
thomasmorris 31:4a88bf97b53e 324 {
thomasmorris 32:8f2795716e97 325 LCD_Write_Seconds();
thomasmorris 32:8f2795716e97 326 Thread::wait(1000);
chills 33:3b5096f0126a 327 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 328 {
chills 33:3b5096f0126a 329 mode = 0;
chills 33:3b5096f0126a 330 break;
chills 33:3b5096f0126a 331 }
thomasmorris 31:4a88bf97b53e 332 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 333 {
thomasmorris 31:4a88bf97b53e 334 Add_Second();
thomasmorris 31:4a88bf97b53e 335 }
thomasmorris 31:4a88bf97b53e 336 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 337 {
thomasmorris 31:4a88bf97b53e 338 Subtract_Second();
thomasmorris 32:8f2795716e97 339 }
thomasmorris 31:4a88bf97b53e 340 }
thomasmorris 30:4cde05cc7c4f 341 }
thomasmorris 30:4cde05cc7c4f 342 else
thomasmorris 30:4cde05cc7c4f 343 {
thomasmorris 30:4cde05cc7c4f 344 mode == 0;
thomasmorris 7:dfe19413fdc2 345 }
thomasmorris 7:dfe19413fdc2 346 }
thomasmorris 7:dfe19413fdc2 347 }
thomasmorris 29:64b1f95a807c 348 void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
thomasmorris 22:eb4cc12087b2 349 {
chills 24:7d2da96e05ad 350 string Serial_Input;
thomasmorris 25:36699ed589ab 351 while(1) {
thomasmorris 25:36699ed589ab 352 Serial_Input = "Blank";
chills 24:7d2da96e05ad 353 cout << "Please type in a command" << endl;
thomasmorris 25:36699ed589ab 354
thomasmorris 22:eb4cc12087b2 355 cin >> Serial_Input;
thomasmorris 25:36699ed589ab 356
thomasmorris 22:eb4cc12087b2 357 if (Serial_Input == "Test")
thomasmorris 22:eb4cc12087b2 358 {
chills 24:7d2da96e05ad 359 cout << "Test Confirmed" << endl;
thomasmorris 30:4cde05cc7c4f 360 cout << "This is the time " << get_current_time() << endl;
thomasmorris 25:36699ed589ab 361 }
thomasmorris 25:36699ed589ab 362 else if(Serial_Input == "READALL")
thomasmorris 22:eb4cc12087b2 363 {
chills 26:78f5e454e59f 364 cout << "Read All Confirmed " << Sample_Rate << endl;
chills 26:78f5e454e59f 365 Console_Output_Timer.attach(&Console_Output_ISR,Sample_Rate);
thomasmorris 25:36699ed589ab 366 }
chills 26:78f5e454e59f 367 else if(Serial_Input == "DELETEALL")
chills 26:78f5e454e59f 368 {
chills 27:807dd5660c4b 369 Sampling_Timer.detach();
chills 27:807dd5660c4b 370 Console_Output_Timer.detach();
thomasmorris 29:64b1f95a807c 371 for (int x = 0; x < mailsize; x++)
thomasmorris 29:64b1f95a807c 372 {
chills 27:807dd5660c4b 373 DATA *Delete_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
chills 27:807dd5660c4b 374 mail_box.free(Delete_Data); //Puts "Send_Data" into the mailbox
chills 27:807dd5660c4b 375 }
chills 27:807dd5660c4b 376 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 27:807dd5660c4b 377 cout << "Sampling Restarted" << endl;
thomasmorris 25:36699ed589ab 378 }
chills 27:807dd5660c4b 379 else if(Serial_Input == "READ")
thomasmorris 22:eb4cc12087b2 380 {
thomasmorris 29:64b1f95a807c 381 cout << "Please enter in the number of samples to read" <<endl;
thomasmorris 29:64b1f95a807c 382 int Loops; cin >> Loops;
thomasmorris 29:64b1f95a807c 383 for (int x = 0; x < Loops; x++)
thomasmorris 29:64b1f95a807c 384 {
thomasmorris 29:64b1f95a807c 385 Serial_Comms_Stealth_Mode();
thomasmorris 29:64b1f95a807c 386 }
thomasmorris 25:36699ed589ab 387 }
chills 26:78f5e454e59f 388 else if(Serial_Input == "DELETE")
thomasmorris 22:eb4cc12087b2 389 {
chills 26:78f5e454e59f 390 Sampling_Timer.detach();
chills 26:78f5e454e59f 391 Console_Output_Timer.detach();
chills 26:78f5e454e59f 392 cout << "Number of samples to delete:" << endl;
chills 26:78f5e454e59f 393 int Loops; cin >> Loops;
chills 26:78f5e454e59f 394 for (int x = 0; x < Loops; x++){
chills 26:78f5e454e59f 395 DATA *Delete_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
chills 26:78f5e454e59f 396 mail_box.free(Delete_Data); //Puts "Send_Data" into the mailbox
chills 26:78f5e454e59f 397 }
chills 26:78f5e454e59f 398 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 26:78f5e454e59f 399 cout << "Sampling Restarted" << endl;
thomasmorris 25:36699ed589ab 400 }
thomasmorris 25:36699ed589ab 401 else if(Serial_Input == "SETDATE")
chills 26:78f5e454e59f 402 {
thomasmorris 25:36699ed589ab 403 set_time(SETDATE());
chills 26:78f5e454e59f 404 }
thomasmorris 25:36699ed589ab 405 else if(Serial_Input == "SETT")
thomasmorris 22:eb4cc12087b2 406 {
chills 26:78f5e454e59f 407 float Temp_Sample_Rate = SETT();
chills 26:78f5e454e59f 408
chills 26:78f5e454e59f 409 if(Temp_Sample_Rate >= 1){Sample_Rate = Temp_Sample_Rate;}
chills 26:78f5e454e59f 410
chills 26:78f5e454e59f 411 Sampling_Timer.detach();
chills 26:78f5e454e59f 412 Console_Output_Timer.detach();
chills 26:78f5e454e59f 413 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
thomasmorris 25:36699ed589ab 414 }
chills 27:807dd5660c4b 415 else if(Serial_Input == "STATE")
chills 27:807dd5660c4b 416 {
chills 27:807dd5660c4b 417 Sampling_Timer.detach(); Console_Output_Timer.detach();
chills 27:807dd5660c4b 418 cout << "Turn Sampling On or Off:" << endl;
chills 27:807dd5660c4b 419 string State; cin >> State;
chills 27:807dd5660c4b 420
chills 27:807dd5660c4b 421 if(State == "On"){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}
chills 27:807dd5660c4b 422 if(State == "Off"){Sampling_Timer.detach();}
thomasmorris 25:36699ed589ab 423 }
thomasmorris 25:36699ed589ab 424 else if(Serial_Input == "LOGGING.<x>")
chills 26:78f5e454e59f 425 {
thomasmorris 25:36699ed589ab 426
thomasmorris 25:36699ed589ab 427 }
thomasmorris 25:36699ed589ab 428 else if(Serial_Input == "HELP")// Use this to display all of the availble commands
thomasmorris 25:36699ed589ab 429 {
thomasmorris 25:36699ed589ab 430 HELP();
thomasmorris 22:eb4cc12087b2 431 }
chills 26:78f5e454e59f 432 else if(Serial_Input == "STOP")
chills 26:78f5e454e59f 433 {
thomasmorris 28:09b5c46c8afd 434 Console_Output_Timer.detach(); //Stops Sampling
chills 26:78f5e454e59f 435 }
thomasmorris 30:4cde05cc7c4f 436 else if(Serial_Input == "Blank Blank")
thomasmorris 30:4cde05cc7c4f 437 {
thomasmorris 30:4cde05cc7c4f 438 cout << "Blank Blank complete" << endl;
thomasmorris 30:4cde05cc7c4f 439 }
thomasmorris 25:36699ed589ab 440 else
thomasmorris 22:eb4cc12087b2 441 {
chills 26:78f5e454e59f 442 cout << "Please enter an acceptable command" << endl;
thomasmorris 22:eb4cc12087b2 443 }
thomasmorris 22:eb4cc12087b2 444 }
thomasmorris 22:eb4cc12087b2 445 }
thomasmorris 29:64b1f95a807c 446 void Serial_Comms_Stealth_Mode()//Change this name
thomasmorris 29:64b1f95a807c 447 {
thomasmorris 29:64b1f95a807c 448 osEvent evt_serial = mail_box.get(); //Get the latest entry from "mail_box"
thomasmorris 29:64b1f95a807c 449
thomasmorris 29:64b1f95a807c 450 DATA *Rec_Data_Serial = (DATA*)evt_serial.value.p; //Create pointer to mailbox
thomasmorris 29:64b1f95a807c 451 DATA msg_serial; //Create temporary instance of DATA class
thomasmorris 29:64b1f95a807c 452
thomasmorris 29:64b1f95a807c 453 msg_serial.set_time(Rec_Data_Serial->get_time()); //Copy time from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 454 msg_serial.set_temperature(Rec_Data_Serial->get_temperature()); //Copy teperature from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 455 msg_serial.set_pressure(Rec_Data_Serial->get_pressure()); //Copy pressure from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 456 msg_serial.set_light(Rec_Data_Serial->get_light()); //Copy light from mailbox to temporary instance
thomasmorris 29:64b1f95a807c 457 mail_box.free(Rec_Data_Serial); //Free space in the mailbox (delete earliest sample taken)
thomasmorris 29:64b1f95a807c 458 time_t scom_time = msg_serial.get_time(); //Declare local variable for time
thomasmorris 29:64b1f95a807c 459 strftime(scom_time_buffer, 32, "%I:%M %p\t", localtime(&scom_time)); //Format time as a string
thomasmorris 29:64b1f95a807c 460 pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time
thomasmorris 29:64b1f95a807c 461 pc.printf("Temperature = %f\t", msg_serial.get_temperature()); //Print Temperature
thomasmorris 29:64b1f95a807c 462 pc.printf("Pressure = %f\t", msg_serial.get_pressure()); //Print Pressure
thomasmorris 29:64b1f95a807c 463 pc.printf("Light = %f\n\r", msg_serial.get_light()); //Print Light
thomasmorris 29:64b1f95a807c 464
thomasmorris 29:64b1f95a807c 465 Green_led.Toggle();
thomasmorris 29:64b1f95a807c 466 }
chills 24:7d2da96e05ad 467 void Serial_Comms()//Thread for Serial Communications
thomasmorris 5:2594b953f111 468 {
thomasmorris 25:36699ed589ab 469 while(1) {
thomasmorris 28:09b5c46c8afd 470 Thread::signal_wait(SerialCommsTime);//Stupid 0 Bug
thomasmorris 29:64b1f95a807c 471 Serial_Comms_Stealth_Mode();
thomasmorris 5:2594b953f111 472 }
thomasmorris 5:2594b953f111 473 }
thomasmorris 5:2594b953f111 474
thomasmorris 28:09b5c46c8afd 475 void Console_Output_ISR() {t2.signal_set(SerialCommsTime);}
chills 26:78f5e454e59f 476 void Sampling_ISR() {t1.signal_set(SamplingTime);}
chills 26:78f5e454e59f 477
chills 26:78f5e454e59f 478
thomasmorris 5:2594b953f111 479 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 480 {
thomasmorris 30:4cde05cc7c4f 481 while(1)
thomasmorris 30:4cde05cc7c4f 482 {
chills 10:46946784326d 483 Thread::signal_wait(SamplingTime); //Set the time between samples
thomasmorris 25:36699ed589ab 484
chills 10:46946784326d 485 temp = sensor.getTemperature(); //Read Temperature
chills 10:46946784326d 486 pressure = sensor.getPressure(); //Read Pressure
chills 10:46946784326d 487 lux = adcIn.read(); //Read Light
chills 33:3b5096f0126a 488
chills 33:3b5096f0126a 489 Time_Lock_Main.lock();
chills 16:067916791a25 490 time_t buffer_time = time(NULL); //Read Time
chills 33:3b5096f0126a 491 Time_Lock_Main.unlock();
thomasmorris 25:36699ed589ab 492
chills 16:067916791a25 493 DATA *Send_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
thomasmorris 25:36699ed589ab 494
chills 33:3b5096f0126a 495 if (Send_Data == NULL) {return;}
chills 33:3b5096f0126a 496
chills 10:46946784326d 497 Send_Data->set_time(buffer_time); //Pass in Time
chills 10:46946784326d 498 Send_Data->set_temperature(temp); //Pass in Temp
chills 10:46946784326d 499 Send_Data->set_pressure(pressure); //Pass in Pres
chills 10:46946784326d 500 Send_Data->set_light(lux); //Pass in Light
thomasmorris 25:36699ed589ab 501
chills 10:46946784326d 502 osStatus stat = mail_box.put(Send_Data); //Puts "Send_Data" into the mailbox
thomasmorris 25:36699ed589ab 503
thomasmorris 25:36699ed589ab 504 if (stat == osErrorResource) { //If mailbox overfills
chills 10:46946784326d 505 mail_box.free(Send_Data); //Free the mail box
chills 10:46946784326d 506 }
thomasmorris 5:2594b953f111 507 Red_led.Toggle();
thomasmorris 5:2594b953f111 508 t1.signal_set(NotSamplingTime);
thomasmorris 5:2594b953f111 509 }
thomasmorris 25:36699ed589ab 510 }
thomasmorris 25:36699ed589ab 511 int main()
thomasmorris 25:36699ed589ab 512 {
noutram 1:e1cf7663f5ff 513 //Greeting
chills 16:067916791a25 514 pc.printf("Test Start");
chills 16:067916791a25 515 pc.printf("\n\r");
thomasmorris 25:36699ed589ab 516
chills 10:46946784326d 517 set_time(1512940530); //Set RTC time to December 10 2017
thomasmorris 30:4cde05cc7c4f 518
chills 10:46946784326d 519 pc.baud(9600); //Sets the Serial Comms Baud Rate
thomasmorris 25:36699ed589ab 520
chills 20:cbb71f84cff9 521 LCD.Initialise();
thomasmorris 28:09b5c46c8afd 522 LCD.DDRAM_Address(0x00);
thomasmorris 29:64b1f95a807c 523
thomasmorris 25:36699ed589ab 524 post(); //Power on Self Test
thomasmorris 25:36699ed589ab 525
noutram 3:a88838ff33e7 526 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 527 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 528 printf("Init failed \n");
thomasmorris 28:09b5c46c8afd 529 LCD.Display_Clear();
thomasmorris 28:09b5c46c8afd 530 LCD.Write_String("CANNOT INIT SD"); //Change me
noutram 1:e1cf7663f5ff 531 errorCode(FATAL);
thomasmorris 25:36699ed589ab 532 }
noutram 1:e1cf7663f5ff 533 //Create a filing system for SD Card
thomasmorris 25:36699ed589ab 534 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 535
noutram 1:e1cf7663f5ff 536 //Open to WRITE
noutram 1:e1cf7663f5ff 537 FILE* fp = fopen("/sd/test.csv","a");
noutram 1:e1cf7663f5ff 538 if (fp == NULL) {
noutram 1:e1cf7663f5ff 539 error("Could not open file for write\n");
thomasmorris 28:09b5c46c8afd 540 LCD.Display_Clear();
thomasmorris 28:09b5c46c8afd 541 LCD.Write_String("CANNOT OPEN FILE\n\n");//Change me
noutram 1:e1cf7663f5ff 542 errorCode(FATAL);
noutram 1:e1cf7663f5ff 543 }
noutram 3:a88838ff33e7 544 //Last message before sampling begins
thomasmorris 28:09b5c46c8afd 545 LCD.Display_Clear();
chills 33:3b5096f0126a 546 LCD.Write_String("Ready Player");
thomasmorris 31:4a88bf97b53e 547 LCD.DDRAM_Address(0x40);
chills 33:3b5096f0126a 548 LCD.Write_String("One");
thomasmorris 31:4a88bf97b53e 549 LCD.DDRAM_Address(0x00);
thomasmorris 25:36699ed589ab 550
thomasmorris 25:36699ed589ab 551
thomasmorris 25:36699ed589ab 552 Sample_Rate = TimerInterval;
chills 24:7d2da96e05ad 553 //Run interrupt
chills 26:78f5e454e59f 554 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 24:7d2da96e05ad 555
thomasmorris 5:2594b953f111 556 t1.start(Sample);
chills 26:78f5e454e59f 557 t2.start(Serial_Comms);
thomasmorris 30:4cde05cc7c4f 558 t3.start(LCD_Output);
chills 24:7d2da96e05ad 559 //t4.start(Network);
thomasmorris 23:3c85d7f657a2 560 t5.start(Serial_Commands);
thomasmorris 25:36699ed589ab 561
thomasmorris 5:2594b953f111 562 //Main thread ID
thomasmorris 25:36699ed589ab 563
thomasmorris 5:2594b953f111 564 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 25:36699ed589ab 565
thomasmorris 5:2594b953f111 566 //Thread ID
thomasmorris 5:2594b953f111 567 id1 = t1.gettid();
thomasmorris 5:2594b953f111 568 id2 = t2.gettid();
thomasmorris 21:3c078c799caa 569 id3 = t3.gettid();
thomasmorris 21:3c078c799caa 570 id4 = t4.gettid();
chills 24:7d2da96e05ad 571 id5 = t5.gettid();
thomasmorris 25:36699ed589ab 572
thomasmorris 25:36699ed589ab 573 while (onBoardSwitch == 0) {
thomasmorris 25:36699ed589ab 574
noutram 1:e1cf7663f5ff 575 }
thomasmorris 25:36699ed589ab 576
noutram 1:e1cf7663f5ff 577 //Close File
chills 15:c1592fc1a501 578 /*
noutram 1:e1cf7663f5ff 579 fclose(fp);
thomasmorris 25:36699ed589ab 580
noutram 1:e1cf7663f5ff 581 //Close down
noutram 1:e1cf7663f5ff 582 sd.deinit();
noutram 1:e1cf7663f5ff 583 printf("Unmounted...\n");
noutram 1:e1cf7663f5ff 584 lcd.cls();
noutram 1:e1cf7663f5ff 585 lcd.printf("Unmounted...\n\n");
chills 15:c1592fc1a501 586 */
thomasmorris 25:36699ed589ab 587
noutram 1:e1cf7663f5ff 588 while(true) {
noutram 1:e1cf7663f5ff 589 greenLED = 1;
noutram 1:e1cf7663f5ff 590 wait(0.5);
noutram 1:e1cf7663f5ff 591 greenLED = 0;
thomasmorris 25:36699ed589ab 592 wait(0.1);
noutram 0:36e89e3ed7c4 593 }
noutram 0:36e89e3ed7c4 594 }