Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Mon Jan 08 14:20:30 2018 +0000
Revision:
41:859b5e1e3d9a
Parent:
39:5c499989d2b9
Child:
42:ae1640bca2e1
Working Looping SD card;

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 37:7c4d7f206039 4 Current Verision 18
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"
thomasmorris 38:8d86e0d8a816 15
thomasmorris 38:8d86e0d8a816 16 #define Do_Read_Data 1
thomasmorris 38:8d86e0d8a816 17 #define Dont_Read_Data 0
thomasmorris 38:8d86e0d8a816 18 #define Do_Delete_Data 1
thomasmorris 38:8d86e0d8a816 19 #define Dont_Delete_Data 0
chills 24:7d2da96e05ad 20
chills 33:3b5096f0126a 21 Mutex Time_Lock_Main;
thomasmorris 38:8d86e0d8a816 22 Mutex Data_Buffer_Lock;
thomasmorris 38:8d86e0d8a816 23
thomasmorris 41:859b5e1e3d9a 24 int Write_Data(DATA Data_Store, int Write_Pointer)//Writes data passed in to the array at address Write Pointer
thomasmorris 38:8d86e0d8a816 25 {
thomasmorris 41:859b5e1e3d9a 26 Data_Buffer_Lock.lock();//Appling lock for critial section
thomasmorris 38:8d86e0d8a816 27 Data_Buffer[Write_Pointer] = Data_Store;
thomasmorris 41:859b5e1e3d9a 28 if(Write_Pointer < mailsize - 1)//Checks if write pointer will be greater than mail size
thomasmorris 38:8d86e0d8a816 29 {
thomasmorris 41:859b5e1e3d9a 30 Write_Pointer = Write_Pointer + 1;//Increment write pointer
thomasmorris 38:8d86e0d8a816 31 }
thomasmorris 38:8d86e0d8a816 32 else
thomasmorris 38:8d86e0d8a816 33 {
thomasmorris 41:859b5e1e3d9a 34 Write_Pointer = 0;//Else set it to 0 thus a FIFO system
thomasmorris 38:8d86e0d8a816 35 }
thomasmorris 41:859b5e1e3d9a 36 Data_Buffer_Lock.unlock();//Releasing lock for critial section
thomasmorris 41:859b5e1e3d9a 37 return Write_Pointer;//New address of Write pointer
thomasmorris 38:8d86e0d8a816 38
thomasmorris 38:8d86e0d8a816 39 }
thomasmorris 38:8d86e0d8a816 40 DATA Read_Data(int Read_Pointer)
thomasmorris 38:8d86e0d8a816 41 {
thomasmorris 41:859b5e1e3d9a 42 Data_Buffer_Lock.lock();//Appling lock for critial section
thomasmorris 38:8d86e0d8a816 43 DATA Temp_Data = Data_Buffer[Read_Pointer];
thomasmorris 41:859b5e1e3d9a 44 Data_Buffer_Lock.unlock();//Releasing lock for critial section
thomasmorris 38:8d86e0d8a816 45
thomasmorris 38:8d86e0d8a816 46 return Temp_Data;
thomasmorris 38:8d86e0d8a816 47 }
thomasmorris 38:8d86e0d8a816 48 void Delete_Data(int Delete_Pointer)
thomasmorris 38:8d86e0d8a816 49 {
thomasmorris 38:8d86e0d8a816 50 Thread::signal_wait(Do_Delete_Data);
thomasmorris 41:859b5e1e3d9a 51 Data_Buffer_Lock.lock();//Appling lock for critial section
thomasmorris 38:8d86e0d8a816 52 Data_Buffer[Delete_Pointer].set_all_zero();
thomasmorris 41:859b5e1e3d9a 53 Data_Buffer_Lock.unlock();//Releasing lock for critial section
thomasmorris 41:859b5e1e3d9a 54 }
thomasmorris 41:859b5e1e3d9a 55
thomasmorris 41:859b5e1e3d9a 56 void SD_Card()//Writes data to the SD card
thomasmorris 41:859b5e1e3d9a 57 {
thomasmorris 41:859b5e1e3d9a 58 while(1)
thomasmorris 41:859b5e1e3d9a 59 {
thomasmorris 41:859b5e1e3d9a 60 Thread::signal_wait(SD_Data_Ready); //Waits till the sampler has data avaliable
thomasmorris 41:859b5e1e3d9a 61 if(Log_Value==1){cout << "In SD_Card Thread" << endl;}
thomasmorris 41:859b5e1e3d9a 62
thomasmorris 41:859b5e1e3d9a 63 //Open to WRITE
thomasmorris 41:859b5e1e3d9a 64 FILE* fp = fopen("/sd/test.csv","a");
thomasmorris 41:859b5e1e3d9a 65 time_t Time = Data_Active.get_time();
thomasmorris 41:859b5e1e3d9a 66 tm* Time_Pointer = localtime(&Time);
thomasmorris 41:859b5e1e3d9a 67 int temp_day = Time_Pointer->tm_mday;
thomasmorris 41:859b5e1e3d9a 68 int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
thomasmorris 41:859b5e1e3d9a 69 int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
thomasmorris 41:859b5e1e3d9a 70
thomasmorris 41:859b5e1e3d9a 71 int temp_hours = Time_Pointer->tm_hour;
thomasmorris 41:859b5e1e3d9a 72 int temp_minute = Time_Pointer->tm_min;
thomasmorris 41:859b5e1e3d9a 73
thomasmorris 41:859b5e1e3d9a 74 float temp_temperature = Data_Active.get_temperature();
thomasmorris 41:859b5e1e3d9a 75 float temp_pressure = Data_Active.get_pressure();
thomasmorris 41:859b5e1e3d9a 76 float temp_light = Data_Active.get_light();
thomasmorris 41:859b5e1e3d9a 77
thomasmorris 41:859b5e1e3d9a 78 fprintf(fp, "Date: %d/%d/%d,",temp_day,temp_month,temp_year);//Date
thomasmorris 41:859b5e1e3d9a 79 fprintf(fp, "Time: %d:%d,",temp_hours,temp_minute);//Time
thomasmorris 41:859b5e1e3d9a 80 fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature
thomasmorris 41:859b5e1e3d9a 81 fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure
thomasmorris 41:859b5e1e3d9a 82 fprintf(fp, "Light: %5.3f,",temp_light);//Light
thomasmorris 41:859b5e1e3d9a 83 fprintf(fp, "End of Data \n");//End of data
thomasmorris 41:859b5e1e3d9a 84
thomasmorris 41:859b5e1e3d9a 85 //Close File
thomasmorris 41:859b5e1e3d9a 86 fclose(fp);
thomasmorris 41:859b5e1e3d9a 87 //dump all Data_Buffer[mailsize];
thomasmorris 41:859b5e1e3d9a 88 }
thomasmorris 38:8d86e0d8a816 89 }
thomasmorris 35:26b0a9b55d82 90 void Network()
thomasmorris 8:0e4481b64353 91 {
thomasmorris 35:26b0a9b55d82 92 while(1)
thomasmorris 35:26b0a9b55d82 93 {
thomasmorris 41:859b5e1e3d9a 94 Thread::wait(NetworkWait);//Waits Network Wait amount of time
thomasmorris 37:7c4d7f206039 95 if(Log_Value==1){cout << "In Network Thread" << endl;} //Log this
thomasmorris 36:a0098306fc58 96
thomasmorris 36:a0098306fc58 97 time_t Time = Data_Active.get_time();
thomasmorris 36:a0098306fc58 98 tm* Time_Pointer = localtime(&Time);
thomasmorris 36:a0098306fc58 99 int temp_day = Time_Pointer->tm_mday;
thomasmorris 41:859b5e1e3d9a 100 int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
thomasmorris 41:859b5e1e3d9a 101 int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
thomasmorris 36:a0098306fc58 102
thomasmorris 36:a0098306fc58 103 int temp_hours = Time_Pointer->tm_hour;
thomasmorris 36:a0098306fc58 104 int temp_minute = Time_Pointer->tm_min;
thomasmorris 36:a0098306fc58 105
thomasmorris 36:a0098306fc58 106 float temp_temperature = Data_Active.get_temperature();
thomasmorris 36:a0098306fc58 107 float temp_pressure = Data_Active.get_pressure();
thomasmorris 36:a0098306fc58 108 float temp_light = Data_Active.get_light();
thomasmorris 36:a0098306fc58 109
thomasmorris 36:a0098306fc58 110
thomasmorris 41:859b5e1e3d9a 111 Networking(temp_day,temp_month,temp_year,temp_hours,temp_minute,temp_temperature,temp_pressure,temp_light);//Pass in networking data and time values
thomasmorris 35:26b0a9b55d82 112 }
chills 13:db857b3744c6 113 }
thomasmorris 31:4a88bf97b53e 114 void LCD_Write_Year()
thomasmorris 12:536eca338ae8 115 {
thomasmorris 41:859b5e1e3d9a 116 Time_Lock_Main.lock();//Appling lock for critial section
thomasmorris 37:7c4d7f206039 117 if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock taken"<<endl;}
chills 33:3b5096f0126a 118 time_t Time = time(NULL);
chills 33:3b5096f0126a 119 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 120 int Years = 1900 + Time_Pointer->tm_year;
thomasmorris 41:859b5e1e3d9a 121 Time_Lock_Main.unlock();//Releasing lock for critial section
thomasmorris 37:7c4d7f206039 122 if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock released"<<endl;}
thomasmorris 31:4a88bf97b53e 123 stringstream ss;
thomasmorris 31:4a88bf97b53e 124 ss << Years;
thomasmorris 31:4a88bf97b53e 125 string Year_String = ss.str();
thomasmorris 31:4a88bf97b53e 126 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 127 LCD.Write_String("Set Year");
thomasmorris 31:4a88bf97b53e 128 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 129 LCD.Write_String(Year_String);
thomasmorris 30:4cde05cc7c4f 130 }
thomasmorris 31:4a88bf97b53e 131 void LCD_Write_Month()
thomasmorris 31:4a88bf97b53e 132 {
thomasmorris 41:859b5e1e3d9a 133 Time_Lock_Main.lock();//Appling lock for critial section
thomasmorris 37:7c4d7f206039 134 if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock taken"<<endl;}
chills 33:3b5096f0126a 135 time_t Time = time(NULL);
chills 33:3b5096f0126a 136 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 137 int Months = 1 + Time_Pointer->tm_mon;
thomasmorris 41:859b5e1e3d9a 138 Time_Lock_Main.unlock();//Releasing lock for critial section
thomasmorris 37:7c4d7f206039 139 if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock released"<<endl;}
thomasmorris 31:4a88bf97b53e 140 stringstream ss;
thomasmorris 31:4a88bf97b53e 141 ss << Months;
thomasmorris 31:4a88bf97b53e 142 string Month_String = ss.str();
thomasmorris 31:4a88bf97b53e 143 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 144 LCD.Write_String("Set Month");
thomasmorris 31:4a88bf97b53e 145 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 146 LCD.Write_String(Month_String);
thomasmorris 31:4a88bf97b53e 147 }
thomasmorris 31:4a88bf97b53e 148 void LCD_Write_Day()
thomasmorris 31:4a88bf97b53e 149 {
thomasmorris 41:859b5e1e3d9a 150 Time_Lock_Main.lock();//Appling lock for critial section
thomasmorris 37:7c4d7f206039 151 if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock taken"<<endl;}
chills 33:3b5096f0126a 152 time_t Time = time(NULL);
chills 33:3b5096f0126a 153 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 154 int Days = Time_Pointer->tm_mday;
thomasmorris 41:859b5e1e3d9a 155 Time_Lock_Main.unlock();//Releasing lock for critial section
thomasmorris 37:7c4d7f206039 156 if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock released"<<endl;}
thomasmorris 31:4a88bf97b53e 157 stringstream ss;
thomasmorris 31:4a88bf97b53e 158 ss << Days;
thomasmorris 31:4a88bf97b53e 159 string Day_String = ss.str();
thomasmorris 31:4a88bf97b53e 160 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 161 LCD.Write_String("Set Day");
thomasmorris 31:4a88bf97b53e 162 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 163 LCD.Write_String(Day_String);
thomasmorris 31:4a88bf97b53e 164 }
thomasmorris 31:4a88bf97b53e 165 void LCD_Write_Hour()
thomasmorris 30:4cde05cc7c4f 166 {
thomasmorris 41:859b5e1e3d9a 167 Time_Lock_Main.lock();//Appling lock for critial section
thomasmorris 37:7c4d7f206039 168 if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock taken"<<endl;}
chills 33:3b5096f0126a 169 time_t Time = time(NULL);
chills 33:3b5096f0126a 170 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 171 int Hours = Time_Pointer->tm_hour;
thomasmorris 41:859b5e1e3d9a 172 Time_Lock_Main.unlock();//Releasing lock for critial section
thomasmorris 37:7c4d7f206039 173 if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock released"<<endl;}
thomasmorris 31:4a88bf97b53e 174 stringstream ss;
thomasmorris 31:4a88bf97b53e 175 ss << Hours;
thomasmorris 31:4a88bf97b53e 176 string Hour_String = ss.str();
thomasmorris 31:4a88bf97b53e 177 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 178 LCD.Write_String("Set Hour");
thomasmorris 31:4a88bf97b53e 179 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 180 LCD.Write_String(Hour_String);
thomasmorris 31:4a88bf97b53e 181 }
thomasmorris 31:4a88bf97b53e 182 void LCD_Write_Minute()
thomasmorris 31:4a88bf97b53e 183 {
thomasmorris 41:859b5e1e3d9a 184 Time_Lock_Main.lock();//Appling lock for critial section
thomasmorris 37:7c4d7f206039 185 if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock taken"<<endl;}
chills 33:3b5096f0126a 186 time_t Time = time(NULL);
chills 33:3b5096f0126a 187 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 188 int Minutes = Time_Pointer->tm_min;
thomasmorris 41:859b5e1e3d9a 189 Time_Lock_Main.unlock();//Releasing lock for critial section
thomasmorris 37:7c4d7f206039 190 if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock released"<<endl;}
thomasmorris 31:4a88bf97b53e 191 stringstream ss;
thomasmorris 31:4a88bf97b53e 192 ss << Minutes;
thomasmorris 31:4a88bf97b53e 193 string Minute_String = ss.str();
thomasmorris 31:4a88bf97b53e 194 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 195 LCD.Write_String("Set Minute");
thomasmorris 31:4a88bf97b53e 196 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 197 LCD.Write_String(Minute_String);
thomasmorris 31:4a88bf97b53e 198 }
thomasmorris 31:4a88bf97b53e 199 void LCD_Write_Seconds()
thomasmorris 31:4a88bf97b53e 200 {
thomasmorris 41:859b5e1e3d9a 201 Time_Lock_Main.lock();//Appling lock for critial section
thomasmorris 37:7c4d7f206039 202 if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock taken"<<endl;}
chills 33:3b5096f0126a 203 time_t Time = time(NULL);
chills 33:3b5096f0126a 204 tm* Time_Pointer = localtime(&Time);
chills 33:3b5096f0126a 205 int Seconds = Time_Pointer->tm_sec;
thomasmorris 41:859b5e1e3d9a 206 Time_Lock_Main.unlock();//Releasing lock for critial section
thomasmorris 37:7c4d7f206039 207 if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock released"<<endl;}
thomasmorris 31:4a88bf97b53e 208 stringstream ss;
thomasmorris 31:4a88bf97b53e 209 ss << Seconds;
thomasmorris 31:4a88bf97b53e 210 string Second_String = ss.str();
thomasmorris 31:4a88bf97b53e 211 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 212 LCD.Write_String("Set Second");
thomasmorris 31:4a88bf97b53e 213 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 214 LCD.Write_String(Second_String);
thomasmorris 7:dfe19413fdc2 215 }
thomasmorris 30:4cde05cc7c4f 216
thomasmorris 30:4cde05cc7c4f 217 void LCD_Output()
chills 34:c0b8705f183d 218 {
thomasmorris 30:4cde05cc7c4f 219 while(1)
thomasmorris 29:64b1f95a807c 220 {
thomasmorris 41:859b5e1e3d9a 221 Thread::wait(10);//Small wait
thomasmorris 30:4cde05cc7c4f 222 if(mode == 0)//Default mode
thomasmorris 30:4cde05cc7c4f 223 {
thomasmorris 37:7c4d7f206039 224 if(Log_Value == 1){cout << "In mode 0 " << endl;}//Log this
chills 33:3b5096f0126a 225
thomasmorris 41:859b5e1e3d9a 226 Thread::wait(Default_Mode_Toggle_Time);//Wait for this amount of time
thomasmorris 37:7c4d7f206039 227 if(Log_Value == 1){cout<<"Writing Data to LCD"<<endl;}
chills 34:c0b8705f183d 228 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 229
thomasmorris 30:4cde05cc7c4f 230 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 231 LCD.Write_String("Temp Pres li");
thomasmorris 30:4cde05cc7c4f 232 LCD.DDRAM_Address(0x40);
thomasmorris 41:859b5e1e3d9a 233 LCD.Write_String(LCD_buffer);//Print out current data values to the LCD
thomasmorris 25:36699ed589ab 234
thomasmorris 41:859b5e1e3d9a 235 Thread::wait(Default_Mode_Toggle_Time); //Wait for this amount of time
thomasmorris 37:7c4d7f206039 236 if(Log_Value == 1){cout<<"Writing Time and Date to LCD"<<endl;}
thomasmorris 41:859b5e1e3d9a 237 Time_Lock_Main.lock();//lock Time_lock for critial section
chills 34:c0b8705f183d 238 time_t Time = Data_Active.get_time();
chills 34:c0b8705f183d 239 tm* Time_Pointer = localtime(&Time);
thomasmorris 30:4cde05cc7c4f 240 LCD.Display_Clear();
chills 33:3b5096f0126a 241 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
thomasmorris 41:859b5e1e3d9a 242 Time_Lock_Main.unlock();//unlock Time_lock for critial section
chills 33:3b5096f0126a 243
thomasmorris 30:4cde05cc7c4f 244 LCD.DDRAM_Address(0x00);
thomasmorris 30:4cde05cc7c4f 245 LCD.Write_String("Current Time:");
thomasmorris 30:4cde05cc7c4f 246 LCD.DDRAM_Address(0x40);
thomasmorris 41:859b5e1e3d9a 247 LCD.Write_String(LCD_buffer);//Print the current time to the LCD
thomasmorris 31:4a88bf97b53e 248
thomasmorris 37:7c4d7f206039 249 if(Log_Value == 1){cout<<"Checking Switches for next mode"<<endl;}
thomasmorris 31:4a88bf97b53e 250 if(SW1.read() & SW2.read() == 1)
thomasmorris 31:4a88bf97b53e 251 {
thomasmorris 31:4a88bf97b53e 252 mode = 1;
thomasmorris 31:4a88bf97b53e 253 }
thomasmorris 30:4cde05cc7c4f 254 }
thomasmorris 30:4cde05cc7c4f 255 else if(mode == 1)//Choose either date setting or time setting
thomasmorris 30:4cde05cc7c4f 256 {
chills 34:c0b8705f183d 257 if(Log_Value == 1){cout << "In Mode 1" << endl;}
thomasmorris 30:4cde05cc7c4f 258 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 259 while(1)
thomasmorris 31:4a88bf97b53e 260 {
thomasmorris 31:4a88bf97b53e 261 LCD.DDRAM_Address(0x00);
thomasmorris 31:4a88bf97b53e 262 LCD.Write_String("Date Time");
thomasmorris 31:4a88bf97b53e 263 LCD.DDRAM_Address(0x40);
thomasmorris 31:4a88bf97b53e 264 LCD.Write_String("< >");
thomasmorris 31:4a88bf97b53e 265 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 266 if(Log_Value == 1){cout<<"Checking SW1 to go to Date setting"<<endl;}
thomasmorris 37:7c4d7f206039 267 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 268 {
thomasmorris 37:7c4d7f206039 269 mode = 2;//Date Setting
thomasmorris 31:4a88bf97b53e 270 break;
thomasmorris 31:4a88bf97b53e 271 }
thomasmorris 37:7c4d7f206039 272 if(Log_Value == 1){cout<<"Checking SW2 to go to Time setting"<<endl;}
thomasmorris 37:7c4d7f206039 273 if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 274 {
thomasmorris 37:7c4d7f206039 275 mode = 5;//Time Setting
thomasmorris 31:4a88bf97b53e 276 break;
thomasmorris 31:4a88bf97b53e 277 }
thomasmorris 31:4a88bf97b53e 278 }
thomasmorris 30:4cde05cc7c4f 279 }
thomasmorris 30:4cde05cc7c4f 280 else if(mode == 2)//Set the Year
thomasmorris 30:4cde05cc7c4f 281 {
chills 34:c0b8705f183d 282 if(Log_Value == 1){cout << "In Mode 2" << endl;}
thomasmorris 31:4a88bf97b53e 283 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 284 while(1)
thomasmorris 31:4a88bf97b53e 285 {
thomasmorris 32:8f2795716e97 286 LCD_Write_Year();
thomasmorris 31:4a88bf97b53e 287 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 288 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go to Month setting"<<endl;}
chills 33:3b5096f0126a 289 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 290 {
chills 33:3b5096f0126a 291 mode = 3;
chills 33:3b5096f0126a 292 break;
chills 33:3b5096f0126a 293 }
thomasmorris 37:7c4d7f206039 294 if(Log_Value == 1){cout<<"Checking SW1 to add Year"<<endl;}
thomasmorris 31:4a88bf97b53e 295 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 296 {
thomasmorris 31:4a88bf97b53e 297 Add_Year();
thomasmorris 31:4a88bf97b53e 298 }
thomasmorris 37:7c4d7f206039 299 if(Log_Value == 1){cout<<"Checking SW2 to subtract Year"<<endl;}
thomasmorris 31:4a88bf97b53e 300 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 301 {
thomasmorris 31:4a88bf97b53e 302 Subtract_Year();
thomasmorris 31:4a88bf97b53e 303 }
thomasmorris 31:4a88bf97b53e 304 LCD_Write_Year();
thomasmorris 31:4a88bf97b53e 305 }
thomasmorris 30:4cde05cc7c4f 306 }
thomasmorris 30:4cde05cc7c4f 307 else if(mode == 3)//Set the Month
thomasmorris 30:4cde05cc7c4f 308 {
chills 34:c0b8705f183d 309 if(Log_Value == 1){cout << "In Mode 3" << endl;}
thomasmorris 31:4a88bf97b53e 310 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 311 while(1)
thomasmorris 32:8f2795716e97 312 {
thomasmorris 32:8f2795716e97 313 LCD_Write_Month();
thomasmorris 31:4a88bf97b53e 314 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 315 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go to Day setting"<<endl;}
chills 33:3b5096f0126a 316 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 317 {
chills 33:3b5096f0126a 318 mode = 4;
chills 33:3b5096f0126a 319 break;
thomasmorris 37:7c4d7f206039 320 }
thomasmorris 37:7c4d7f206039 321 if(Log_Value == 1){cout<<"Checking SW1 to add Month"<<endl;}
thomasmorris 31:4a88bf97b53e 322 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 323 {
thomasmorris 31:4a88bf97b53e 324 Add_Month();
thomasmorris 31:4a88bf97b53e 325 }
thomasmorris 37:7c4d7f206039 326 if(Log_Value == 1){cout<<"Checking SW2 to subtract Month"<<endl;}
thomasmorris 31:4a88bf97b53e 327 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 328 {
thomasmorris 31:4a88bf97b53e 329 Subtract_Month();
thomasmorris 32:8f2795716e97 330 }
chills 33:3b5096f0126a 331
thomasmorris 31:4a88bf97b53e 332 }
thomasmorris 30:4cde05cc7c4f 333 }
thomasmorris 30:4cde05cc7c4f 334 else if(mode == 4)//Set the Day
thomasmorris 30:4cde05cc7c4f 335 {
chills 34:c0b8705f183d 336 if(Log_Value == 1){cout << "In Mode 4" << endl;}
thomasmorris 31:4a88bf97b53e 337 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 338 while(1)
thomasmorris 32:8f2795716e97 339 {
thomasmorris 32:8f2795716e97 340 LCD_Write_Day();
thomasmorris 31:4a88bf97b53e 341 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 342 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Default setting"<<endl;}
chills 33:3b5096f0126a 343 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 344 {
chills 33:3b5096f0126a 345 mode = 0;
chills 33:3b5096f0126a 346 break;
chills 33:3b5096f0126a 347 }
thomasmorris 37:7c4d7f206039 348 if(Log_Value == 1){cout<<"Checking SW1 to add Day"<<endl;}
thomasmorris 31:4a88bf97b53e 349 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 350 {
thomasmorris 31:4a88bf97b53e 351 Add_Day();
thomasmorris 31:4a88bf97b53e 352 }
thomasmorris 37:7c4d7f206039 353 if(Log_Value == 1){cout<<"Checking SW2 to subtract Day"<<endl;}
thomasmorris 31:4a88bf97b53e 354 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 355 {
thomasmorris 31:4a88bf97b53e 356 Subtract_Day();
thomasmorris 32:8f2795716e97 357 }
thomasmorris 31:4a88bf97b53e 358 }
thomasmorris 30:4cde05cc7c4f 359 }
thomasmorris 30:4cde05cc7c4f 360 else if(mode == 5)//Set the Hour
thomasmorris 30:4cde05cc7c4f 361 {
chills 34:c0b8705f183d 362 if(Log_Value == 1){cout << "In Mode 5" << endl;}
thomasmorris 31:4a88bf97b53e 363 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 364 while(1)
thomasmorris 31:4a88bf97b53e 365 {
thomasmorris 32:8f2795716e97 366 LCD_Write_Hour();
thomasmorris 32:8f2795716e97 367 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 368 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Minute setting"<<endl;}
chills 33:3b5096f0126a 369 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 370 {
chills 33:3b5096f0126a 371 mode = 6;
chills 33:3b5096f0126a 372 break;
thomasmorris 37:7c4d7f206039 373 }
thomasmorris 37:7c4d7f206039 374 if(Log_Value == 1){cout<<"Checking SW1 to add Hour"<<endl;}
thomasmorris 31:4a88bf97b53e 375 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 376 {
thomasmorris 31:4a88bf97b53e 377 Add_Hour();
thomasmorris 31:4a88bf97b53e 378 }
thomasmorris 37:7c4d7f206039 379 if(Log_Value == 1){cout<<"Checking SW2 to subtract Hour"<<endl;}
thomasmorris 31:4a88bf97b53e 380 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 381 {
thomasmorris 31:4a88bf97b53e 382 Subtract_Hour();
thomasmorris 31:4a88bf97b53e 383 }
thomasmorris 31:4a88bf97b53e 384 }
thomasmorris 30:4cde05cc7c4f 385 }
thomasmorris 30:4cde05cc7c4f 386 else if(mode == 6)//Set the Minute
thomasmorris 30:4cde05cc7c4f 387 {
chills 34:c0b8705f183d 388 if(Log_Value == 1){cout << "In Mode 6" << endl;}
thomasmorris 31:4a88bf97b53e 389 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 390 while(1)
thomasmorris 31:4a88bf97b53e 391 {
thomasmorris 32:8f2795716e97 392 LCD_Write_Minute();
thomasmorris 32:8f2795716e97 393 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 394 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Seconds setting"<<endl;}
chills 33:3b5096f0126a 395 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 396 {
chills 33:3b5096f0126a 397 mode = 7;
chills 33:3b5096f0126a 398 break;
chills 33:3b5096f0126a 399 }
thomasmorris 37:7c4d7f206039 400 if(Log_Value == 1){cout<<"Checking SW1 to add Minute"<<endl;}
thomasmorris 31:4a88bf97b53e 401 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 402 {
thomasmorris 31:4a88bf97b53e 403 Add_Minute();
thomasmorris 31:4a88bf97b53e 404 }
thomasmorris 37:7c4d7f206039 405 if(Log_Value == 1){cout<<"Checking SW2 to subtract Minute"<<endl;}
thomasmorris 31:4a88bf97b53e 406 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 407 {
thomasmorris 31:4a88bf97b53e 408 Subtract_Minute();
thomasmorris 32:8f2795716e97 409 }
thomasmorris 31:4a88bf97b53e 410 }
thomasmorris 30:4cde05cc7c4f 411 }
thomasmorris 30:4cde05cc7c4f 412 else if(mode == 7)//Set the Seconds
thomasmorris 30:4cde05cc7c4f 413 {
chills 34:c0b8705f183d 414 if(Log_Value == 1){cout << "In Mode 7" << endl;}
thomasmorris 31:4a88bf97b53e 415 LCD.Display_Clear();
thomasmorris 31:4a88bf97b53e 416 while(1)
thomasmorris 31:4a88bf97b53e 417 {
thomasmorris 32:8f2795716e97 418 LCD_Write_Seconds();
thomasmorris 32:8f2795716e97 419 Thread::wait(1000);
thomasmorris 37:7c4d7f206039 420 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Default setting"<<endl;}
chills 33:3b5096f0126a 421 if(SW1.read() & SW2.read() == 1)
chills 33:3b5096f0126a 422 {
chills 33:3b5096f0126a 423 mode = 0;
chills 33:3b5096f0126a 424 break;
thomasmorris 37:7c4d7f206039 425 }
thomasmorris 37:7c4d7f206039 426 if(Log_Value == 1){cout<<"Checking SW1 to add Second"<<endl;}
thomasmorris 31:4a88bf97b53e 427 if(SW1.read() == 1 & SW2.read() == 0)
thomasmorris 31:4a88bf97b53e 428 {
thomasmorris 31:4a88bf97b53e 429 Add_Second();
thomasmorris 31:4a88bf97b53e 430 }
thomasmorris 37:7c4d7f206039 431 if(Log_Value == 1){cout<<"Checking SW1 to subtract Second"<<endl;}
thomasmorris 31:4a88bf97b53e 432 else if(SW2.read() == 1 & SW1.read() == 0)
thomasmorris 31:4a88bf97b53e 433 {
thomasmorris 31:4a88bf97b53e 434 Subtract_Second();
thomasmorris 32:8f2795716e97 435 }
thomasmorris 31:4a88bf97b53e 436 }
thomasmorris 30:4cde05cc7c4f 437 }
thomasmorris 30:4cde05cc7c4f 438 else
thomasmorris 30:4cde05cc7c4f 439 {
thomasmorris 37:7c4d7f206039 440 if(Log_Value == 1){cout<<"Mode Error occured mode now set to 0"<<endl;}
thomasmorris 35:26b0a9b55d82 441 mode = 0;
thomasmorris 7:dfe19413fdc2 442 }
thomasmorris 7:dfe19413fdc2 443 }
thomasmorris 7:dfe19413fdc2 444 }
thomasmorris 29:64b1f95a807c 445 void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
thomasmorris 22:eb4cc12087b2 446 {
thomasmorris 38:8d86e0d8a816 447 char input[100];
thomasmorris 25:36699ed589ab 448 while(1) {
thomasmorris 38:8d86e0d8a816 449
thomasmorris 37:7c4d7f206039 450 if(Log_Value == 1){cout<<"In Serial_Commands"<<endl;}
thomasmorris 38:8d86e0d8a816 451 for (int x = 0; x < 100; x++){input[x] = ' ';};
thomasmorris 38:8d86e0d8a816 452
thomasmorris 37:7c4d7f206039 453 cout << "Please type in a command" << endl;//Log this
chills 34:c0b8705f183d 454
thomasmorris 38:8d86e0d8a816 455 cin.getline(input,sizeof(input),'\r');
thomasmorris 38:8d86e0d8a816 456
thomasmorris 38:8d86e0d8a816 457 //READ ALL FIX THIS
thomasmorris 39:5c499989d2b9 458 if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L')
thomasmorris 22:eb4cc12087b2 459 {
thomasmorris 38:8d86e0d8a816 460 if(Log_Value == 1){cout<<"READ ALL Confirmed"<<endl;}
thomasmorris 39:5c499989d2b9 461
thomasmorris 41:859b5e1e3d9a 462 Data_Buffer_Lock.lock();//Lock data buffer due to critical section
thomasmorris 39:5c499989d2b9 463 int Start_Address_Read_All = Write_Pointer;
thomasmorris 41:859b5e1e3d9a 464 Data_Buffer_Lock.unlock();//unLock data buffer due to critical section
thomasmorris 39:5c499989d2b9 465
thomasmorris 39:5c499989d2b9 466 for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All )
thomasmorris 39:5c499989d2b9 467 {
thomasmorris 39:5c499989d2b9 468 if(x_Read_All < 0){x_Read_All = x_Read_All + mailsize;}
thomasmorris 39:5c499989d2b9 469 else {x_Read_All = x_Read_All;}
thomasmorris 39:5c499989d2b9 470
thomasmorris 39:5c499989d2b9 471 DATA Serial_Read_Data = Read_Data(x_Read_All);
thomasmorris 39:5c499989d2b9 472
thomasmorris 39:5c499989d2b9 473 time_t Time = Serial_Read_Data.get_time();
thomasmorris 39:5c499989d2b9 474 tm* Time_Pointer = localtime(&Time);
thomasmorris 39:5c499989d2b9 475
thomasmorris 39:5c499989d2b9 476 strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer); //Format time as a string
thomasmorris 39:5c499989d2b9 477 pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
thomasmorris 39:5c499989d2b9 478 pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time
thomasmorris 39:5c499989d2b9 479 pc.printf("Temperature = %f\t", Serial_Read_Data.get_temperature()); //Print Temperature
thomasmorris 39:5c499989d2b9 480 pc.printf("Pressure = %f\t", Serial_Read_Data.get_pressure()); //Print Pressure
thomasmorris 39:5c499989d2b9 481 pc.printf("Light = %f\n\r", Serial_Read_Data.get_light()); //Print Light
thomasmorris 39:5c499989d2b9 482
thomasmorris 39:5c499989d2b9 483 if(x_Read_All == mailsize - 1){x_Read_All = 0;}
thomasmorris 39:5c499989d2b9 484 else {x_Read_All = x_Read_All + 1;}
thomasmorris 39:5c499989d2b9 485 }
thomasmorris 38:8d86e0d8a816 486 }
thomasmorris 38:8d86e0d8a816 487
thomasmorris 38:8d86e0d8a816 488 //DELETE ALL FIX THIS
thomasmorris 38:8d86e0d8a816 489 else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ' & input[7] == 'A' & input[8] == 'L' & input[9] == 'L' & input[10] == ' ')
chills 26:78f5e454e59f 490 {
thomasmorris 35:26b0a9b55d82 491 Sampling_Timer.detach();
thomasmorris 35:26b0a9b55d82 492 Console_Output_Timer.detach();
thomasmorris 38:8d86e0d8a816 493 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
thomasmorris 38:8d86e0d8a816 494 cout << "Sampling Restarted" << endl;
thomasmorris 38:8d86e0d8a816 495 if(Log_Value == 1){cout<<"DELETE ALL Confirmed"<<endl;}
thomasmorris 38:8d86e0d8a816 496 }
thomasmorris 38:8d86e0d8a816 497
thomasmorris 41:859b5e1e3d9a 498 //READ
thomasmorris 38:8d86e0d8a816 499 else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ')
thomasmorris 38:8d86e0d8a816 500 {
thomasmorris 38:8d86e0d8a816 501 int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead;
thomasmorris 38:8d86e0d8a816 502 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
thomasmorris 38:8d86e0d8a816 503 for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];}
thomasmorris 38:8d86e0d8a816 504 stringstream Number(ReadNumber);
thomasmorris 38:8d86e0d8a816 505 Number >> NumberToRead;
thomasmorris 38:8d86e0d8a816 506
thomasmorris 39:5c499989d2b9 507
thomasmorris 39:5c499989d2b9 508 if(Log_Value == 1){cout<<"Getting Data"<<endl;}
thomasmorris 39:5c499989d2b9 509
thomasmorris 41:859b5e1e3d9a 510 Data_Buffer_Lock.lock();//Lock data buffer due to critical section
thomasmorris 39:5c499989d2b9 511 int Start_Address = Write_Pointer;
thomasmorris 41:859b5e1e3d9a 512 Data_Buffer_Lock.unlock();//unLock data buffer due to critical section
thomasmorris 39:5c499989d2b9 513
thomasmorris 39:5c499989d2b9 514 for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x )
thomasmorris 39:5c499989d2b9 515 {
thomasmorris 39:5c499989d2b9 516 if(x < 0){x = x + mailsize;}
thomasmorris 39:5c499989d2b9 517 else {x = x;}
thomasmorris 39:5c499989d2b9 518
thomasmorris 39:5c499989d2b9 519 DATA Serial_Read_Data = Read_Data(x);
thomasmorris 39:5c499989d2b9 520
thomasmorris 39:5c499989d2b9 521 time_t Time = Serial_Read_Data.get_time();
thomasmorris 39:5c499989d2b9 522 tm* Time_Pointer = localtime(&Time);
thomasmorris 39:5c499989d2b9 523
thomasmorris 39:5c499989d2b9 524 strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer); //Format time as a string
thomasmorris 39:5c499989d2b9 525 pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
thomasmorris 39:5c499989d2b9 526 pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time
thomasmorris 39:5c499989d2b9 527 pc.printf("Temperature = %f\t", Serial_Read_Data.get_temperature()); //Print Temperature
thomasmorris 39:5c499989d2b9 528 pc.printf("Pressure = %f\t", Serial_Read_Data.get_pressure()); //Print Pressure
thomasmorris 39:5c499989d2b9 529 pc.printf("Light = %f\n\r", Serial_Read_Data.get_light()); //Print Light
thomasmorris 39:5c499989d2b9 530
thomasmorris 39:5c499989d2b9 531 if(x == mailsize - 1){x = 0;}
thomasmorris 39:5c499989d2b9 532 else {x = x + 1;}
thomasmorris 39:5c499989d2b9 533
thomasmorris 39:5c499989d2b9 534 Green_led.Toggle();
thomasmorris 39:5c499989d2b9 535 }
thomasmorris 39:5c499989d2b9 536 //Serial_Comms_Data_Get();
thomasmorris 38:8d86e0d8a816 537 if(Log_Value == 1){cout << "Read " << NumberToRead << " samples" << endl;}
thomasmorris 38:8d86e0d8a816 538
thomasmorris 25:36699ed589ab 539 }
thomasmorris 38:8d86e0d8a816 540
thomasmorris 38:8d86e0d8a816 541 //DELETE look into this
thomasmorris 38:8d86e0d8a816 542 else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ')
thomasmorris 22:eb4cc12087b2 543 {
thomasmorris 41:859b5e1e3d9a 544 Sampling_Timer.detach();//Stop the Sample timer
thomasmorris 41:859b5e1e3d9a 545 Console_Output_Timer.detach();//Stop Console Printing
thomasmorris 38:8d86e0d8a816 546
thomasmorris 38:8d86e0d8a816 547
thomasmorris 38:8d86e0d8a816 548 int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete;
thomasmorris 38:8d86e0d8a816 549 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
thomasmorris 38:8d86e0d8a816 550 for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];}
thomasmorris 38:8d86e0d8a816 551 stringstream Number(DeleteNumber);
thomasmorris 38:8d86e0d8a816 552 Number >> NumberToDelete;
thomasmorris 38:8d86e0d8a816 553
thomasmorris 38:8d86e0d8a816 554 if(Log_Value == 1){cout << "Deleted " << NumberToDelete << " samples" << endl;}
thomasmorris 38:8d86e0d8a816 555
thomasmorris 41:859b5e1e3d9a 556 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);//Start Sample timer
chills 26:78f5e454e59f 557 cout << "Sampling Restarted" << endl;
chills 34:c0b8705f183d 558 }
thomasmorris 38:8d86e0d8a816 559
thomasmorris 38:8d86e0d8a816 560 //STATE
thomasmorris 38:8d86e0d8a816 561 else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ')
chills 34:c0b8705f183d 562 {
thomasmorris 38:8d86e0d8a816 563 int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState;
thomasmorris 38:8d86e0d8a816 564 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
thomasmorris 38:8d86e0d8a816 565 for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];}
thomasmorris 38:8d86e0d8a816 566 stringstream Number(StateNumber);
thomasmorris 38:8d86e0d8a816 567 Number >> NumberToState;
thomasmorris 38:8d86e0d8a816 568
thomasmorris 41:859b5e1e3d9a 569 if(NumberToState==0){Sampling_Timer.detach(); Console_Output_Timer.detach();}//Stop Sampling
thomasmorris 41:859b5e1e3d9a 570 if(NumberToState==1){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}//Start Sampling
chills 26:78f5e454e59f 571 }
thomasmorris 38:8d86e0d8a816 572
thomasmorris 38:8d86e0d8a816 573 //SETDATE
thomasmorris 38:8d86e0d8a816 574 else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'D' & input[4] == 'A' & input[5] == 'T' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ')
thomasmorris 38:8d86e0d8a816 575 {
thomasmorris 41:859b5e1e3d9a 576 //int NumberOfChars = 0; int ArrayAddress = 0;
thomasmorris 38:8d86e0d8a816 577 string DayNumber, MonthNumber, YearNumber;
thomasmorris 38:8d86e0d8a816 578 int NumberToDay, NumberToMonth, NumberToYear;
thomasmorris 38:8d86e0d8a816 579
thomasmorris 38:8d86e0d8a816 580 for(int x=8; x < 10; x++){DayNumber += input[x];}
thomasmorris 38:8d86e0d8a816 581 stringstream Number_1(DayNumber);
thomasmorris 38:8d86e0d8a816 582 Number_1 >> NumberToDay;
thomasmorris 38:8d86e0d8a816 583
thomasmorris 38:8d86e0d8a816 584 for(int x=11; x < 13; x++){MonthNumber += input[x];}
thomasmorris 38:8d86e0d8a816 585 stringstream Number_2(MonthNumber);
thomasmorris 38:8d86e0d8a816 586 Number_2 >> NumberToMonth;
thomasmorris 38:8d86e0d8a816 587
thomasmorris 38:8d86e0d8a816 588 for(int x=14; x < 18; x++){YearNumber += input[x];}
thomasmorris 38:8d86e0d8a816 589 stringstream Number_3(YearNumber);
thomasmorris 38:8d86e0d8a816 590 Number_3 >> NumberToYear;
thomasmorris 38:8d86e0d8a816 591
thomasmorris 38:8d86e0d8a816 592 set_new_date(NumberToDay,NumberToMonth,NumberToYear);
thomasmorris 38:8d86e0d8a816 593 }
thomasmorris 38:8d86e0d8a816 594
thomasmorris 38:8d86e0d8a816 595 //SETTIME
thomasmorris 38:8d86e0d8a816 596 else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == 'I' & input[5] == 'M' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ')
thomasmorris 22:eb4cc12087b2 597 {
thomasmorris 41:859b5e1e3d9a 598 //int NumberOfChars = 0; int ArrayAddress = 0;
thomasmorris 38:8d86e0d8a816 599 string HourNumber, MinuteNumber, SecondNumber;
thomasmorris 38:8d86e0d8a816 600 int NumberToHour, NumberToMinute, NumberToSecond;
thomasmorris 38:8d86e0d8a816 601
thomasmorris 38:8d86e0d8a816 602 for(int x=8; x < 10; x++){HourNumber += input[x];}
thomasmorris 38:8d86e0d8a816 603 stringstream Number_1(HourNumber);
thomasmorris 38:8d86e0d8a816 604 Number_1 >> NumberToHour;
thomasmorris 38:8d86e0d8a816 605
thomasmorris 38:8d86e0d8a816 606 for(int x=11; x < 13; x++){MinuteNumber += input[x];}
thomasmorris 38:8d86e0d8a816 607 stringstream Number_2(MinuteNumber);
thomasmorris 38:8d86e0d8a816 608 Number_2 >> NumberToMinute;
thomasmorris 38:8d86e0d8a816 609
thomasmorris 38:8d86e0d8a816 610 for(int x=14; x < 16; x++){SecondNumber += input[x];}
thomasmorris 38:8d86e0d8a816 611 stringstream Number_3(SecondNumber);
thomasmorris 38:8d86e0d8a816 612 Number_3 >> NumberToSecond;
thomasmorris 38:8d86e0d8a816 613
thomasmorris 38:8d86e0d8a816 614 set_new_time(NumberToHour,NumberToMinute,NumberToSecond);
thomasmorris 38:8d86e0d8a816 615 }
thomasmorris 38:8d86e0d8a816 616
thomasmorris 38:8d86e0d8a816 617 //SETT
thomasmorris 38:8d86e0d8a816 618 else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ')
thomasmorris 38:8d86e0d8a816 619 {
thomasmorris 38:8d86e0d8a816 620
thomasmorris 38:8d86e0d8a816 621 int NumberOfChars = 0; int ArrayAddress = 0; string SettNumber; int NumberToSett;
thomasmorris 38:8d86e0d8a816 622 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
thomasmorris 38:8d86e0d8a816 623 for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];}
thomasmorris 38:8d86e0d8a816 624 stringstream Number(SettNumber);
thomasmorris 38:8d86e0d8a816 625 Number >> NumberToSett;
thomasmorris 38:8d86e0d8a816 626
thomasmorris 38:8d86e0d8a816 627 if(NumberToSett >= 1){Sample_Rate = NumberToSett;}
chills 26:78f5e454e59f 628
chills 26:78f5e454e59f 629 Sampling_Timer.detach();
chills 26:78f5e454e59f 630 Console_Output_Timer.detach();
chills 26:78f5e454e59f 631 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
thomasmorris 38:8d86e0d8a816 632 }
thomasmorris 38:8d86e0d8a816 633
thomasmorris 38:8d86e0d8a816 634 //LOGGING
thomasmorris 38:8d86e0d8a816 635 else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G' & input[7] == ' ')
thomasmorris 38:8d86e0d8a816 636 {
thomasmorris 38:8d86e0d8a816 637 int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging;
thomasmorris 38:8d86e0d8a816 638 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
thomasmorris 38:8d86e0d8a816 639 for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];}
thomasmorris 38:8d86e0d8a816 640 stringstream Number(LoggingNumber);
thomasmorris 38:8d86e0d8a816 641 Number >> NumberToLogging;
thomasmorris 38:8d86e0d8a816 642
thomasmorris 38:8d86e0d8a816 643 cout << "Successfully entered logging" << endl;
thomasmorris 38:8d86e0d8a816 644 cout << NumberToLogging << endl;
thomasmorris 38:8d86e0d8a816 645 Log_Value = NumberToLogging;
thomasmorris 25:36699ed589ab 646 }
thomasmorris 38:8d86e0d8a816 647
thomasmorris 38:8d86e0d8a816 648 //HELP
thomasmorris 38:8d86e0d8a816 649 else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')// Use this to display all of the availble commands
thomasmorris 25:36699ed589ab 650 {
thomasmorris 41:859b5e1e3d9a 651 HELP();//Run HELP Command
thomasmorris 22:eb4cc12087b2 652 }
thomasmorris 38:8d86e0d8a816 653
thomasmorris 38:8d86e0d8a816 654 //STOP
thomasmorris 38:8d86e0d8a816 655 else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'O' & input[3] == 'P')
chills 26:78f5e454e59f 656 {
thomasmorris 28:09b5c46c8afd 657 Console_Output_Timer.detach(); //Stops Sampling
chills 26:78f5e454e59f 658 }
thomasmorris 25:36699ed589ab 659 else
thomasmorris 22:eb4cc12087b2 660 {
chills 26:78f5e454e59f 661 cout << "Please enter an acceptable command" << endl;
thomasmorris 22:eb4cc12087b2 662 }
thomasmorris 22:eb4cc12087b2 663 }
thomasmorris 22:eb4cc12087b2 664 }
thomasmorris 38:8d86e0d8a816 665 void Serial_Comms_Data_Get()
thomasmorris 29:64b1f95a807c 666 {
thomasmorris 37:7c4d7f206039 667 if(Log_Value == 1){cout<<"Getting Data"<<endl;}
chills 34:c0b8705f183d 668 time_t Time = Data_Active.get_time();
chills 34:c0b8705f183d 669 tm* Time_Pointer = localtime(&Time);
chills 34:c0b8705f183d 670 strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer); //Format time as a string
chills 34:c0b8705f183d 671 pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
chills 34:c0b8705f183d 672 pc.printf("Time = %s", scom_time_buffer); //Print the string formatted time
chills 34:c0b8705f183d 673 pc.printf("Temperature = %f\t", Data_Active.get_temperature()); //Print Temperature
chills 34:c0b8705f183d 674 pc.printf("Pressure = %f\t", Data_Active.get_pressure()); //Print Pressure
chills 34:c0b8705f183d 675 pc.printf("Light = %f\n\r", Data_Active.get_light()); //Print Light
thomasmorris 29:64b1f95a807c 676
thomasmorris 29:64b1f95a807c 677 Green_led.Toggle();
chills 34:c0b8705f183d 678
thomasmorris 29:64b1f95a807c 679 }
thomasmorris 5:2594b953f111 680
thomasmorris 41:859b5e1e3d9a 681 void Sampling_ISR(){t1.signal_set(SamplingTime);}
chills 26:78f5e454e59f 682
chills 26:78f5e454e59f 683
thomasmorris 5:2594b953f111 684 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 685 {
thomasmorris 30:4cde05cc7c4f 686 while(1)
thomasmorris 30:4cde05cc7c4f 687 {
chills 10:46946784326d 688 Thread::signal_wait(SamplingTime); //Set the time between samples
thomasmorris 37:7c4d7f206039 689 if(Log_Value == 1){cout<<"Sample Time"<<endl;}
chills 34:c0b8705f183d 690 Data_Active.set_temperature(sensor.getTemperature()); //Read Temperature
chills 34:c0b8705f183d 691 Data_Active.set_pressure(sensor.getPressure()); //Read Pressure
chills 34:c0b8705f183d 692 Data_Active.set_light(adcIn.read()); //Read Light
chills 33:3b5096f0126a 693
chills 33:3b5096f0126a 694 Time_Lock_Main.lock();
chills 34:c0b8705f183d 695 time_t buffer_time = time(NULL);
chills 33:3b5096f0126a 696 Time_Lock_Main.unlock();
thomasmorris 25:36699ed589ab 697
chills 34:c0b8705f183d 698 Data_Active.set_time(buffer_time);
thomasmorris 25:36699ed589ab 699
thomasmorris 5:2594b953f111 700 Red_led.Toggle();
thomasmorris 39:5c499989d2b9 701 Write_Pointer = Write_Data(Data_Active,Write_Pointer);
thomasmorris 5:2594b953f111 702 t1.signal_set(NotSamplingTime);
thomasmorris 37:7c4d7f206039 703 if(Log_Value == 1){cout<<"New Sample avaliable"<<endl;}
thomasmorris 41:859b5e1e3d9a 704 t2.signal_set(SD_Data_Ready);
thomasmorris 5:2594b953f111 705 }
thomasmorris 25:36699ed589ab 706 }
thomasmorris 25:36699ed589ab 707 int main()
thomasmorris 25:36699ed589ab 708 {
thomasmorris 37:7c4d7f206039 709 set_time(1515352584); //Set RTC time to December 10 2017
thomasmorris 30:4cde05cc7c4f 710
chills 10:46946784326d 711 pc.baud(9600); //Sets the Serial Comms Baud Rate
thomasmorris 25:36699ed589ab 712
chills 20:cbb71f84cff9 713 LCD.Initialise();
thomasmorris 28:09b5c46c8afd 714 LCD.DDRAM_Address(0x00);
thomasmorris 29:64b1f95a807c 715
thomasmorris 25:36699ed589ab 716 post(); //Power on Self Test
thomasmorris 25:36699ed589ab 717
noutram 3:a88838ff33e7 718 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 719 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 720 printf("Init failed \n");
thomasmorris 28:09b5c46c8afd 721 LCD.Display_Clear();
thomasmorris 28:09b5c46c8afd 722 LCD.Write_String("CANNOT INIT SD"); //Change me
noutram 1:e1cf7663f5ff 723 errorCode(FATAL);
thomasmorris 25:36699ed589ab 724 }
noutram 1:e1cf7663f5ff 725 //Create a filing system for SD Card
thomasmorris 25:36699ed589ab 726 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 727
noutram 1:e1cf7663f5ff 728 //Open to WRITE
thomasmorris 41:859b5e1e3d9a 729 FILE* fp = fopen("/sd/test.csv","a");//test.csv is created in the SD Card
noutram 1:e1cf7663f5ff 730 if (fp == NULL) {
noutram 1:e1cf7663f5ff 731 error("Could not open file for write\n");
thomasmorris 28:09b5c46c8afd 732 LCD.Display_Clear();
thomasmorris 37:7c4d7f206039 733 LCD.Write_String("CANNOT OPEN FILE");
noutram 1:e1cf7663f5ff 734 errorCode(FATAL);
noutram 1:e1cf7663f5ff 735 }
thomasmorris 41:859b5e1e3d9a 736 //Close File
thomasmorris 41:859b5e1e3d9a 737 fclose(fp);
thomasmorris 37:7c4d7f206039 738 int network_temp;
thomasmorris 37:7c4d7f206039 739 network_temp = Network_Init();
thomasmorris 37:7c4d7f206039 740 if(network_temp == 1)//Sets up the network and checks if the network cable is not pluged in
thomasmorris 37:7c4d7f206039 741 {
thomasmorris 37:7c4d7f206039 742 error("Could not access network");
thomasmorris 37:7c4d7f206039 743 LCD.Display_Clear();
thomasmorris 37:7c4d7f206039 744 LCD.Write_String("Could not access network");
thomasmorris 41:859b5e1e3d9a 745 errorCode(NETWORK_FATAL);
thomasmorris 37:7c4d7f206039 746 }
noutram 3:a88838ff33e7 747 //Last message before sampling begins
thomasmorris 28:09b5c46c8afd 748 LCD.Display_Clear();
thomasmorris 35:26b0a9b55d82 749 LCD.Write_String("READY PLAYER");
thomasmorris 31:4a88bf97b53e 750 LCD.DDRAM_Address(0x40);
thomasmorris 35:26b0a9b55d82 751 LCD.Write_String(" One ");
thomasmorris 31:4a88bf97b53e 752 LCD.DDRAM_Address(0x00);
thomasmorris 25:36699ed589ab 753
thomasmorris 25:36699ed589ab 754 Sample_Rate = TimerInterval;
chills 24:7d2da96e05ad 755 //Run interrupt
chills 26:78f5e454e59f 756 Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
chills 24:7d2da96e05ad 757
thomasmorris 5:2594b953f111 758 t1.start(Sample);
thomasmorris 41:859b5e1e3d9a 759 t2.start(SD_Card);
thomasmorris 30:4cde05cc7c4f 760 t3.start(LCD_Output);
thomasmorris 35:26b0a9b55d82 761 t4.start(Network);
thomasmorris 23:3c85d7f657a2 762 t5.start(Serial_Commands);
thomasmorris 25:36699ed589ab 763
thomasmorris 5:2594b953f111 764 //Main thread ID
thomasmorris 25:36699ed589ab 765
thomasmorris 5:2594b953f111 766 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 25:36699ed589ab 767
thomasmorris 5:2594b953f111 768 //Thread ID
thomasmorris 5:2594b953f111 769 id1 = t1.gettid();
thomasmorris 5:2594b953f111 770 id2 = t2.gettid();
thomasmorris 21:3c078c799caa 771 id3 = t3.gettid();
thomasmorris 21:3c078c799caa 772 id4 = t4.gettid();
chills 24:7d2da96e05ad 773 id5 = t5.gettid();
thomasmorris 25:36699ed589ab 774
noutram 1:e1cf7663f5ff 775 while(true) {
thomasmorris 41:859b5e1e3d9a 776 if(onBoardSwitch == 1)
thomasmorris 41:859b5e1e3d9a 777 {
thomasmorris 41:859b5e1e3d9a 778 fclose(fp);//Close File
thomasmorris 41:859b5e1e3d9a 779 sd.deinit();//Close down
thomasmorris 41:859b5e1e3d9a 780 LCD.Display_Clear();
thomasmorris 41:859b5e1e3d9a 781 LCD.DDRAM_Address(0x00);
thomasmorris 41:859b5e1e3d9a 782 LCD.Write_String("SD Card");
thomasmorris 41:859b5e1e3d9a 783 LCD.DDRAM_Address(0x40);
thomasmorris 41:859b5e1e3d9a 784 LCD.Write_String("Unmounted");
thomasmorris 41:859b5e1e3d9a 785 LCD.DDRAM_Address(0x00);
thomasmorris 41:859b5e1e3d9a 786 cout << "SD Card Unmounted" << endl;
thomasmorris 41:859b5e1e3d9a 787 }
noutram 1:e1cf7663f5ff 788 greenLED = 1;
noutram 1:e1cf7663f5ff 789 wait(0.5);
noutram 1:e1cf7663f5ff 790 greenLED = 0;
thomasmorris 25:36699ed589ab 791 wait(0.1);
noutram 0:36e89e3ed7c4 792 }
thomasmorris 41:859b5e1e3d9a 793 }//End of Main