SEND
Fork of Final351CW_FINAL by
Embed:
(wiki syntax)
Show/hide line numbers
components.cpp
00001 /* ELEC351 COURSEWORK 2018 00002 DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL 00003 LIAM GRAZIER // DOUG TILLEY // ALEX BARON 00004 */ 00005 #include "mbed.h" 00006 #include "components.hpp" 00007 #include "lglcd.h" 00008 #include "time.h" 00009 #include "stdio.h" 00010 #define RED_DONE 1 00011 #define YELLOW_DONE 2 00012 //setup i/o 00013 DigitalIn onBoardSwitch(USER_BUTTON); 00014 DigitalOut onBoardLED(LED1); 00015 DigitalOut redLED(PE_15); 00016 DigitalOut yellowLED(PB_10); 00017 DigitalOut greenLED(PB_11); 00018 DigitalIn SW1(PE_12); 00019 DigitalIn SW2(PE_14); 00020 AnalogIn adcIn(PA_0); 00021 //sd block class init 00022 SDBlockDevice sd(PB_5, D12, D13, D10);// miso, sclk, cs 00023 //setup mutex locks 00024 Mutex Lock1; 00025 Mutex Lock2; 00026 Mutex Remove; 00027 //time setup start 00028 time_t rawtime; 00029 struct tm * timeinfo; 00030 int year, month ,day, hour, minute, second; 00031 char input = 0; 00032 char jtime[32]; 00033 char jdate[32]; 00034 char jdate1[32]; 00035 //time setup end 00036 //temp,pres sensor setup. 00037 #ifdef BME 00038 BME280 sensor(D14, D15); 00039 #else 00040 BMP280 sensor(D14, D15); 00041 #endif 00042 //lcd init from my class/ 00043 lglcd mylcd(D7,D6,D5,D4,D3,D2); 00044 void runanalysis(void) //task gets data, converts and prints to lcd 00045 { 00046 while(1) 00047 { 00048 Lock1.lock(); 00049 double temp = sensor.getTemperature(); //get temp 00050 double pressure = sensor.getPressure(); //get pressure 00051 double lightin = adcIn; //get light as a number 0-1 range 00052 //storage for the above three lines for later conversion 00053 char TEM[6]; 00054 char PRE[5]; 00055 char LIGHT[6]; 00056 //conversion from double floats to a Chars 00057 sprintf(TEM,"%.2f", temp); 00058 sprintf(PRE,"%.2f", pressure); 00059 sprintf(LIGHT,"%.2f", lightin); 00060 mylcd.setline(1,1); 00061 mylcd.write("L:"); 00062 //below if's for determining light levels 00063 if(lightin > 0.7 && lightin < 0.9) 00064 { 00065 mylcd.setline(1,4); 00066 mylcd.write("|||||||||MAX"); 00067 } 00068 else if(lightin > 0.55 && lightin < 0.69) 00069 { 00070 mylcd.setline(1,4); 00071 mylcd.write("||||||| "); 00072 } 00073 else if(lightin > 0.5 && lightin < 0.54) 00074 { 00075 mylcd.setline(1,4); 00076 mylcd.write("||||| "); 00077 } 00078 else if(lightin > 0.4 && lightin < 0.54) 00079 { 00080 mylcd.setline(1,4); 00081 mylcd.write("||| "); 00082 } 00083 else if(lightin > 0.3 && lightin < 0.39) 00084 { 00085 mylcd.setline(1,4); 00086 mylcd.write("|| "); 00087 } 00088 else if(lightin > 0.06 && lightin < 0.29) 00089 { 00090 mylcd.setline(1,4); 00091 mylcd.write("LOW LIGHT "); 00092 } 00093 else if(lightin < 0.05) 00094 { 00095 mylcd.setline(1,4); 00096 mylcd.write(" "); 00097 mylcd.setline(1,4); 00098 mylcd.write("disconnected"); //LDR most likely connected 00099 } 00100 mylcd.setline(2,1); 00101 mylcd.write("P:"); 00102 mylcd.write(PRE); 00103 mylcd.setline(2,10); 00104 mylcd.write("T:"); 00105 mylcd.write(TEM); 00106 wait(0.01); 00107 Lock1.unlock(); 00108 Thread::signal_wait(SIG_READY); //singal thread triggered by ticker 00109 } 00110 } 00111 void sdwipe(void)//sd delete function 00112 { 00113 while(1) 00114 { 00115 Lock2.lock(); 00116 time_t seconds = time(NULL); 00117 char prefix[4]; 00118 char jdate2[32]; 00119 strcpy(prefix,"/sd/"); 00120 strftime(jdate1, 32, "%F", localtime(&seconds)); 00121 strftime(jdate, 32, "%F\n\r", localtime(&seconds)); 00122 strftime(jtime, 32, "%X\n\r", localtime(&seconds)); 00123 sprintf(jdate2,"%s%s",prefix,jdate1); 00124 FATFileSystem fs("sd", &sd); 00125 char filename[32]; 00126 char suffix[4]; 00127 strcpy(suffix,".txt"); 00128 sprintf(filename,"%s%s",jdate2,suffix); //calculates filename to be used using date 00129 FILE* fp = fopen(filename,"w");//w overwrites file (deleteing) with a blank string 00130 if (fp == NULL) { 00131 errorCode(FATAL); 00132 printf("SD FAIL\n\r"); 00133 mylcd.clear(); 00134 mylcd.setline(1,0); 00135 mylcd.write("SD FAIL"); 00136 } 00137 if (fp != NULL){ 00138 printf("ALL RECORDS DELETED FROM SD\n\r"); 00139 fprintf(fp,"%s" "\n\r"); //blank string in for blank placehold 00140 wait(0.01); 00141 break; 00142 } 00143 break; 00144 fclose(fp); //close file 00145 } 00146 } 00147 void sdwrite(void) //write data to SD 00148 { 00149 while(1) 00150 { 00151 Lock2.lock(); 00152 time_t seconds = time(NULL); 00153 char prefix[4]; 00154 char jdate2[32]; 00155 //conversions for time / floats to strings 00156 strcpy(prefix,"/sd/"); 00157 strftime(jdate1, 32, "%F", localtime(&seconds)); 00158 strftime(jdate, 32, "%F\n\r", localtime(&seconds)); 00159 strftime(jtime, 32, "%X\n\r", localtime(&seconds)); 00160 sprintf(jdate2,"%s%s",prefix,jdate1); 00161 FATFileSystem fs("sd", &sd); 00162 Lock1.lock(); 00163 //get data 00164 double P = sensor.getPressure(); 00165 double L = adcIn; 00166 double t = sensor.getTemperature(); 00167 //create datastores 00168 char filename[32]; 00169 char suffix[4]; 00170 char tem[6]; 00171 char pre[5]; 00172 char light[6]; 00173 char com[1]; 00174 //convert to data store 00175 strcpy(com, ","); 00176 sprintf(pre,"%.2f", P); 00177 sprintf(tem,"%.2f",t); 00178 sprintf(light,"%.2f\n\r", L); 00179 strcpy(suffix,".txt"); 00180 Lock1.unlock(); 00181 Lock1.lock(); 00182 //calculate filename 00183 sprintf(filename,"%s%s",jdate2,suffix); 00184 Lock1.unlock(); 00185 FILE* fp = fopen(filename,"a"); //open file 00186 if (fp == NULL) //if sd file empty (NOT ABLE TO OPEN FILE) 00187 { 00188 errorCode(FATAL); 00189 printf("SD FAIL\n\r"); 00190 mylcd.clear(); 00191 mylcd.setline(1,0); 00192 mylcd.write("SD FAIL"); 00193 } 00194 if (fp != NULL) //if sd IS NOT empty (ABLE TO OPEN FILE) 00195 { 00196 char sdbuf[64];//buffer for write to SD 00197 sprintf(sdbuf,"%s%s%s%s%s%s%s%s%s\n\r",jdate,com,jtime,com,pre,com,tem,com,light); //conversion of all data for print to char 00198 fprintf(fp,"%s",sdbuf); //print the char converted above 00199 wait(0.01);//small wait 00200 } 00201 fclose(fp); //close file 00202 Lock2.unlock(); 00203 Thread::signal_wait(SIG_READY2); //thread signal from ticket in main 00204 } 00205 } 00206 void sdrun(void) //task for checking the SD card is In and init. 00207 { 00208 if ( sd.init() != 0) //if sd not initaliseed 00209 { 00210 printf("Init failed\n\r"); 00211 mylcd.clear(); 00212 mylcd.setline(1,1); 00213 mylcd.write("CANNOT INIT SD"); 00214 errorCode(FATAL); 00215 } 00216 if( sd.init() == 0) //if sd is initalised (BLOCKING IF NOT AS NO RETURN FUNCTION) 00217 { 00218 printf("Init Success \n\r"); 00219 mylcd.clear(); 00220 mylcd.setline(1,1); 00221 mylcd.write("SD GOOD MAN"); 00222 wait(0.5); //flash the SD error / good code! 00223 } 00224 } 00225 void sdcheck(void) //same as sdrun blocking but enables error codes also. 00226 { 00227 if(sd.init() != 0) 00228 { 00229 printf("Init failed \n\r"); 00230 mylcd.clear(); 00231 mylcd.setline(1,1); 00232 mylcd.write("CANNOT INIT SD\n\r"); 00233 errorCode(FATAL); 00234 } 00235 if( sd.init() == 0) 00236 { 00237 //printf("SD Good\n\r"); //used for debugging 00238 } 00239 } 00240 void sdremove(void) //sd card removal (triggered by interrupt) 00241 { 00242 while(1) 00243 { 00244 Thread::signal_wait(SIG_REMOVE); //singal triggered by ticker in main 00245 mylcd.clear(); 00246 Remove.lock(); 00247 sd.deinit(); //denit the sd (SAFE TO REMOVE) 00248 mylcd.clear(); 00249 Lock1.lock(); 00250 mylcd.setline(2,0); 00251 greenLED = 1; 00252 mylcd.write("R"); //write a R character to empty placehold on the LCD indicating safe to remove. 00253 Lock1.unlock(); 00254 printf("SD REMOVED\n\r"); 00255 errorCode(FATAL); //Fire LEDS to indicate 00256 Remove.unlock(); 00257 } 00258 } 00259 void lcdstart(void) //task for testing LCD on start of code. 00260 { 00261 mylcd.clear(); 00262 mylcd.setline(1,1); 00263 mylcd.write("INIT. SYSTEM"); 00264 mylcd.setline(2,1); 00265 mylcd.write("ELEC351"); 00266 } 00267 void errorCode(ELEC350_ERROR_CODE err) //nicks error code from sample 00268 { 00269 switch (err) 00270 { 00271 case OK: 00272 greenLED = 1; 00273 wait(1.0); 00274 greenLED = 0; 00275 return; 00276 case FATAL: 00277 while(1) 00278 { 00279 redLED = 1; 00280 wait(0.1); 00281 redLED = 0; 00282 wait(0.1); 00283 } 00284 } 00285 } 00286 void DispTime(void) //gets time from epoc and converts and prints. 00287 { 00288 time_t Count = time(NULL); //Read the RTC Time 00289 printf("Current Time - %s\n\r", ctime(&Count)); //Print the current time 00290 } 00291 void setuptime(void) //setupp and init time function 00292 { 00293 /*Initialising the time for our program to easy edit*/ 00294 time ( &rawtime ); 00295 timeinfo = localtime ( &rawtime ); 00296 time ( &rawtime ); 00297 timeinfo = localtime ( &rawtime ); 00298 } 00299 void runtime(void) //edit time function called in serial 00300 { 00301 while(1) //When added to the main code this will be changed to a while "SETDATE" 00302 { 00303 /*promts the user to input which edit they would like*/ 00304 printf ("What part do you want to edit? Time(T)/All(A).\n\r"); 00305 fflush(stdout); 00306 scanf ("%s",&input); 00307 /*Switch case input*/ 00308 switch(input) 00309 { 00310 case 'T': 00311 //Sequential Entering, Hour, Minute, Second respectively 00312 printf ("Enter hour:(00-23) \n\r"); 00313 fflush(stdout); 00314 scanf ("%d",&hour); 00315 printf ("Enter minute:(00-59) \n\r"); 00316 fflush(stdout); 00317 scanf ("%d",&minute); 00318 printf ("Enter second:(00-59) \n\r"); 00319 fflush(stdout); 00320 scanf ("%d",&second); 00321 break; 00322 /*Case A ----- All values Update sequence*/ 00323 case 'A': 00324 printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year); 00325 printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month); 00326 printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day); 00327 printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour); 00328 printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute); 00329 printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second); 00330 break; 00331 /*default to reset ----- Month Update sequence*/ 00332 default: 00333 printf ("Invalid\n\r"); 00334 set_time(1515513600); 00335 } 00336 /*Updating all the timings after the user has input all the data*/ 00337 /*Put here as once the user has finished editing it does a batch update*/ 00338 timeinfo->tm_year = year - 1900; 00339 timeinfo->tm_mon = month - 1; 00340 timeinfo->tm_mday = day; 00341 timeinfo->tm_hour = hour; 00342 timeinfo->tm_min = minute; 00343 timeinfo->tm_sec = second; 00344 time_t CurrTime = mktime(timeinfo); //Convert the to UNIX time 00345 set_time(CurrTime); //Sets time using the UNIX time 00346 DispTime(); 00347 return; //Display the new time 00348 } 00349 } 00350 void rundate(void)//setup date function 00351 { 00352 while(1) //When added to the main code this will be changed to a while "SETDATE" 00353 { 00354 /*promts the user to input which edit they would like*/ 00355 printf ("What part do you want to edit? Date(D)/All(A).\n\r"); 00356 fflush(stdout); 00357 scanf ("%s",&input); 00358 /*Switch case input*/ 00359 switch(input) 00360 { 00361 /*Case D ----- Date Update sequence*/ 00362 case 'D': 00363 //Sequential Entering, Day, Month, Year respectively 00364 printf ("Enter day:(01-31) \n\r"); 00365 fflush(stdout); 00366 scanf ("%d",&day); 00367 printf ("Enter month:(01-12) \n\r"); 00368 fflush(stdout); 00369 scanf ("%d",&month); 00370 printf ("Enter year:(1970-9999) \n\r"); 00371 fflush(stdout); 00372 scanf ("%d",&year); 00373 break; 00374 /*Case A ----- All values Update sequence*/ 00375 case 'A': 00376 printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year); 00377 printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month); 00378 printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day); 00379 printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour); 00380 printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute); 00381 printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second); 00382 break; 00383 /*default to reset ----- Month Update sequence*/ 00384 default: 00385 printf ("Invalid\n\r"); 00386 set_time(1515513600); 00387 } 00388 /*Updating all the timings after the user has input all the data*/ 00389 /*Put here as once the user has finished editing it does a batch update*/ 00390 timeinfo->tm_year = year - 1900; 00391 timeinfo->tm_mon = month - 1; 00392 timeinfo->tm_mday = day; 00393 timeinfo->tm_hour = hour; 00394 timeinfo->tm_min = minute; 00395 timeinfo->tm_sec = second; 00396 time_t CurrTime = mktime(timeinfo); //Convert the to UNIX time 00397 set_time(CurrTime); //Sets time using the UNIX time 00398 DispTime(); 00399 return; //Display the new time 00400 } 00401 }
Generated on Mon Jul 18 2022 05:40:54 by
1.7.2
