FINAL PROJECT isn't it

Fork of ELEC351 by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Sun Jan 07 16:17:24 2018 +0000
Revision:
36:a0098306fc58
Parent:
35:26b0a9b55d82
Child:
37:7c4d7f206039
Working Network

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