Liam Grazier / Mbed OS Final351CWfolderonly

Fork of Final351CW_FINAL by Liam Grazier

Revision:
10:098c2fa0a1a6
Parent:
9:e27b3f34de24
--- a/Components/components.cpp	Tue Jan 09 06:14:41 2018 +0000
+++ b/Components/components.cpp	Tue Jan 09 11:33:14 2018 +0000
@@ -1,3 +1,7 @@
+/*   ELEC351 COURSEWORK 2018 
+DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL
+LIAM GRAZIER // DOUG TILLEY // ALEX BARON 
+ */
 #include "mbed.h"
 #include "components.hpp"
 #include "lglcd.h"
@@ -5,17 +9,22 @@
 #include "stdio.h"
 #define RED_DONE 1
 #define YELLOW_DONE 2
-//Digital outputs
+//setup i/o
 DigitalIn onBoardSwitch(USER_BUTTON);
 DigitalOut onBoardLED(LED1);
 DigitalOut redLED(PE_15);
 DigitalOut yellowLED(PB_10);
 DigitalOut greenLED(PB_11);
+DigitalIn  SW1(PE_12);
+DigitalIn  SW2(PE_14);
+AnalogIn adcIn(PA_0);
+//sd block class init
 SDBlockDevice sd(PB_5, D12, D13, D10);// miso, sclk, cs 
+//setup mutex locks
 Mutex Lock1;
 Mutex Lock2;
 Mutex Remove;
-//Inputs
+//time setup start
 time_t rawtime;
 struct tm * timeinfo;
 int year, month ,day, hour, minute, second;
@@ -23,115 +32,101 @@
 char jtime[32];
 char jdate[32];
 char jdate1[32];
-DigitalIn  SW1(PE_12);
-DigitalIn  SW2(PE_14);
-//Serial pc(USBTX, USBRX);
-AnalogIn adcIn(PA_0);
-//Environmental Sensor driver
+//time setup end 
+//temp,pres sensor setup.
 #ifdef BME
 BME280 sensor(D14, D15);
 #else
 BMP280 sensor(D14, D15);
 #endif
+//lcd init from my class/
 lglcd mylcd(D7,D6,D5,D4,D3,D2);
-
-//POWER ON SELF TEST
-void post() 
+void runanalysis(void) //task gets data, converts and prints to lcd
 {
-    //posttest 
-}
-void runanalysis(void){
-    while(1){     
-Lock1.lock();
-double temp = sensor.getTemperature();
-double pressure = sensor.getPressure();
-double lightin = adcIn;
-char TEM[6];
-char PRE[5];  
-char LIGHT[6];
-sprintf(TEM,"%.2f", temp);
-sprintf(PRE,"%.2f", pressure);
-sprintf(LIGHT,"%.2f", lightin);
-mylcd.setline(1,1);
-mylcd.write("L:");
-if(lightin > 0.7 && lightin < 0.9)
-{
-mylcd.setline(1,4);
- mylcd.write("|||||||||MAX");    
-}
-else if(lightin > 0.55 && lightin < 0.69)
-{
-mylcd.setline(1,4);
- mylcd.write("|||||||     ");
-}
-else if(lightin > 0.5 && lightin < 0.54)
-{
-mylcd.setline(1,4);
- mylcd.write("|||||       ");
+    while(1)
+    {     
+        Lock1.lock();
+        double temp = sensor.getTemperature(); //get temp
+        double pressure = sensor.getPressure(); //get pressure
+        double lightin = adcIn; //get light as a number 0-1 range 
+        //storage for the above three lines for later conversion
+        char TEM[6];
+        char PRE[5];  
+        char LIGHT[6];
+        //conversion from double floats to a Chars 
+        sprintf(TEM,"%.2f", temp);
+        sprintf(PRE,"%.2f", pressure);
+        sprintf(LIGHT,"%.2f", lightin);
+        mylcd.setline(1,1);
+        mylcd.write("L:");
+        //below if's for determining light levels 
+        if(lightin > 0.7 && lightin < 0.9) 
+        {
+            mylcd.setline(1,4);
+            mylcd.write("|||||||||MAX");    
+        }
+        else if(lightin > 0.55 && lightin < 0.69)
+        {
+            mylcd.setline(1,4);
+            mylcd.write("|||||||     ");
+        }
+        else if(lightin > 0.5 && lightin < 0.54)
+        {
+            mylcd.setline(1,4);
+            mylcd.write("|||||       ");
+        }
+        else if(lightin > 0.4 && lightin < 0.54)
+        {
+            mylcd.setline(1,4);
+            mylcd.write("|||         ");
+        }
+        else if(lightin > 0.3 && lightin < 0.39)
+        {
+            mylcd.setline(1,4);
+            mylcd.write("||          ");
+        } 
+        else if(lightin > 0.06 && lightin < 0.29)
+        {
+            mylcd.setline(1,4);
+            mylcd.write("LOW LIGHT    ");
+        } 
+        else if(lightin < 0.05)
+        {
+            mylcd.setline(1,4);
+            mylcd.write("             ");
+            mylcd.setline(1,4);
+            mylcd.write("disconnected"); //LDR most likely connected 
+        }
+        mylcd.setline(2,1);
+        mylcd.write("P:");
+        mylcd.write(PRE);
+        mylcd.setline(2,10);
+        mylcd.write("T:");
+        mylcd.write(TEM);
+        wait(0.01);
+        Lock1.unlock();
+        Thread::signal_wait(SIG_READY); //singal thread triggered by ticker 
+    }
 }
-else if(lightin > 0.4 && lightin < 0.54)
-{
-mylcd.setline(1,4);
- mylcd.write("|||         ");
-}
-else if(lightin > 0.3 && lightin < 0.39)
-{
-mylcd.setline(1,4);
- mylcd.write("||          ");
-} 
-else if(lightin > 0.06 && lightin < 0.29)
-{
-mylcd.setline(1,4);
- mylcd.write("LOW LIGHT    ");
-} 
-else if(lightin < 0.05)
-{
-mylcd.setline(1,4);
-mylcd.write("             ");
-mylcd.setline(1,4);
-mylcd.write("disconnected");
-}
-mylcd.setline(2,1);
-mylcd.write("P:");
-mylcd.write(PRE);
-mylcd.setline(2,10);
-mylcd.write("T:");
-mylcd.write(TEM);
-wait(0.01);
-Lock1.unlock();
-Thread::signal_wait(SIG_READY);
-}
-}
-void sdwrite(void)
-{
+void sdwipe(void)//sd delete function
+{ 
     while(1)
     {
         Lock2.lock();
 time_t seconds = time(NULL);
+char prefix[4];
+char jdate2[32];
+strcpy(prefix,"/sd/");
 strftime(jdate1, 32, "%F", localtime(&seconds));
 strftime(jdate, 32, "%F\n\r", localtime(&seconds));
 strftime(jtime, 32, "%X\n\r", localtime(&seconds));
+sprintf(jdate2,"%s%s",prefix,jdate1);
 FATFileSystem fs("sd", &sd);
 char filename[32];
 char suffix[4];
-char prefix[4];
-strcpy(prefix,"/sd/");
 strcpy(suffix,".txt");
-Lock1.lock();
-double P = sensor.getPressure();
-double L = adcIn;
-double t = sensor.getTemperature();
-char tem[6];
-char pre[5];  
-char light[6];
-char com[1];
-strcpy(com, ",");
-sprintf(pre,"%.2f", P);
-sprintf(tem,"%.2f",t);
-sprintf(light,"%.2f\n\r", L);
-sprintf(filename,"%s%s%s",prefix,jdate1,suffix);
-Lock1.unlock();
-FILE* fp = fopen(filename,"a");
+sprintf(filename,"%s%s",jdate2,suffix); //calculates filename to be used using date
+FILE* fp = fopen(filename,"w");//w overwrites file (deleteing) with a blank string 
     if (fp == NULL) {
         errorCode(FATAL);
         printf("SD FAIL\n\r");
@@ -140,89 +135,147 @@
         mylcd.write("SD FAIL");
     }
     if (fp != NULL){
-    //printf("SD Success\n\r");
-  //  fprintf(fp,"%s\n\r", "*C,  mbar,   light level 0-1 scale");
-    fprintf(fp,"%s",jdate);//date
-    fprintf(fp,"%s",com);
-    fprintf(fp,"%s",jtime);//time
-    fprintf(fp,"%s",com);
-    fprintf(fp,"%s",pre);
-    fprintf(fp,"%s",com);
-    fprintf(fp,"%s",tem);
-    fprintf(fp,"%s",com);
-    fprintf(fp,"%s\n\r",light);
+    printf("ALL RECORDS DELETED FROM SD\n\r");
+    fprintf(fp,"%s" "\n\r"); //blank string in for blank placehold 
     wait(0.01);
+    break;
     }
-      fclose(fp);      
-      Lock2.unlock(); 
-      Thread::signal_wait(SIG_READY2);   
-
+    break;
+    fclose(fp);      //close file 
 }
 }
-void sdrun(void)
+void sdwrite(void) //write data to SD 
 {
-if ( sd.init() != 0) {
-printf("Init failed\n\r");
-mylcd.clear();
-mylcd.setline(1,1);
-mylcd.write("CANNOT INIT SD");        
-errorCode(FATAL);
-}
-if( sd.init() == 0){
-printf("Init Success \n\r");
-mylcd.clear();
-mylcd.setline(1,1);
-mylcd.write("SD GOOD MAN");
-wait(0.5); //flash the SD error / good code! 
-}
+    while(1)
+    {
+        Lock2.lock();
+        time_t seconds = time(NULL);
+        char prefix[4];
+        char jdate2[32];
+        //conversions for time / floats to strings 
+        strcpy(prefix,"/sd/");
+        strftime(jdate1, 32, "%F", localtime(&seconds));
+        strftime(jdate, 32, "%F\n\r", localtime(&seconds));
+        strftime(jtime, 32, "%X\n\r", localtime(&seconds));
+        sprintf(jdate2,"%s%s",prefix,jdate1);
+        FATFileSystem fs("sd", &sd);
+        Lock1.lock();
+        //get data 
+        double P = sensor.getPressure();
+        double L = adcIn;
+        double t = sensor.getTemperature();
+        //create datastores
+        char filename[32];
+        char suffix[4];
+        char tem[6];
+        char pre[5];  
+        char light[6];
+        char com[1];
+        //convert to data store
+        strcpy(com, ",");
+        sprintf(pre,"%.2f", P);
+        sprintf(tem,"%.2f",t);
+        sprintf(light,"%.2f\n\r", L);
+        strcpy(suffix,".txt");
+        Lock1.unlock();
+        Lock1.lock();
+        //calculate filename
+        sprintf(filename,"%s%s",jdate2,suffix);
+        Lock1.unlock();
+        FILE* fp = fopen(filename,"a"); //open file
+        if (fp == NULL) //if sd file empty (NOT ABLE TO OPEN FILE)
+        {
+            errorCode(FATAL);
+            printf("SD FAIL\n\r");
+            mylcd.clear();
+            mylcd.setline(1,0);
+            mylcd.write("SD FAIL");
+        }
+        if (fp != NULL) //if sd IS NOT empty (ABLE TO OPEN FILE)
+        {
+            char sdbuf[64];//buffer for write to SD 
+            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 
+            fprintf(fp,"%s",sdbuf); //print the char converted above
+            wait(0.01);//small wait
+        }
+        fclose(fp);      //close file
+        Lock2.unlock(); 
+        Thread::signal_wait(SIG_READY2); //thread signal from ticket in main    
+    }
 }
-void sdcheck(void){
-if ( sd.init() != 0) {
-printf("Init failed \n\r");
-mylcd.clear();
-mylcd.setline(1,1);
-mylcd.write("CANNOT INIT SD\n\r");        
-errorCode(FATAL);
+void sdrun(void) //task for checking the SD card is In and init. 
+{
+    if ( sd.init() != 0)  //if sd not initaliseed 
+    {
+        printf("Init failed\n\r");
+        mylcd.clear();
+        mylcd.setline(1,1);
+        mylcd.write("CANNOT INIT SD");        
+        errorCode(FATAL);
+    }
+    if( sd.init() == 0) //if sd is initalised (BLOCKING IF NOT AS NO RETURN FUNCTION)
+    {
+        printf("Init Success \n\r");
+        mylcd.clear();
+        mylcd.setline(1,1);
+        mylcd.write("SD GOOD MAN");
+        wait(0.5); //flash the SD error / good code! 
+    }
 }
-if( sd.init() == 0){
-//printf("SD Good\n\r");
-}
-}
-void sdremove(void)
+void sdcheck(void) //same as sdrun blocking but enables error codes also. 
 {
-    while(1){
-    Thread::signal_wait(SIG_REMOVE); 
-    mylcd.clear();  
-    Remove.lock();
-                sd.deinit();
-                mylcd.clear();
-                Lock1.lock();
-                mylcd.setline(2,0);
-                greenLED = 1;
-                mylcd.write("R");
-                Lock1.unlock();
-                printf("SD REMOVED\n\r");
-                errorCode(FATAL);
-    Remove.unlock();
+    if(sd.init() != 0)
+    {
+        printf("Init failed \n\r");
+        mylcd.clear();
+        mylcd.setline(1,1);
+        mylcd.write("CANNOT INIT SD\n\r");        
+        errorCode(FATAL);
+    }
+    if( sd.init() == 0)
+    {
+    //printf("SD Good\n\r"); //used for debugging
+    }
 }
+void sdremove(void) //sd card removal (triggered by interrupt)
+{
+    while(1)
+    {
+        Thread::signal_wait(SIG_REMOVE); //singal triggered by ticker in main 
+        mylcd.clear();  
+        Remove.lock();
+        sd.deinit(); //denit the sd (SAFE TO REMOVE)
+        mylcd.clear();
+        Lock1.lock();
+        mylcd.setline(2,0);
+        greenLED = 1;
+        mylcd.write("R"); //write a R character to empty placehold on the LCD indicating safe to remove. 
+        Lock1.unlock();
+        printf("SD REMOVED\n\r");
+        errorCode(FATAL); //Fire LEDS to indicate 
+        Remove.unlock();
+    }
 }
-void lcdstart(void){
+void lcdstart(void) //task for testing LCD on start of code. 
+{
     mylcd.clear();
     mylcd.setline(1,1);
     mylcd.write("INIT. SYSTEM");
     mylcd.setline(2,1);
     mylcd.write("ELEC351");
-    }
-void errorCode(ELEC350_ERROR_CODE err)
+}
+void errorCode(ELEC350_ERROR_CODE err) //nicks error code from sample 
 {
-    switch (err) {
-      case OK:
+    switch (err)
+    {
+        case OK:
         greenLED = 1;
         wait(1.0);
         greenLED = 0;
         return;                
-      case FATAL:
-        while(1) {
+        case FATAL:
+        while(1) 
+        {
             redLED = 1;
             wait(0.1);
             redLED = 0;
@@ -230,78 +283,110 @@
         }
     }
 }
-
-///dougs code
-
-void DispTime(void)
+void DispTime(void) //gets time from epoc and converts and prints. 
 {
     time_t Count = time(NULL);                      //Read the RTC Time
     printf("Current Time - %s\n\r", ctime(&Count)); //Print the current time
 }
-
-void setuptime(void)
+void setuptime(void) //setupp and init time function 
 {
-
     /*Initialising the time for our program to easy edit*/
     time ( &rawtime );
     timeinfo = localtime ( &rawtime );
-    
-    /*Setting the time to the deadline time*/
-    /*Same as button Code*/
-    //Set the initialisation time to: Tuesday 9th January 2018, 16:00:00.
-    //set_time(1515513600);
-    //Initialisation of the times.
     time ( &rawtime );
-    timeinfo = localtime ( &rawtime ); 
-    //Displays the initialisation time
-    DispTime();
-    /*End the initialisation */
-   
+    timeinfo = localtime ( &rawtime );  
 }
-
-    void runtime(void){
+void runtime(void) //edit time function called in serial 
+{
     while(1) //When added to the main code this will be changed to a while "SETDATE"
     {  
+        /*promts the user to input which edit they would like*/
+        printf ("What part do you want to edit? Time(T)/All(A).\n\r"); 
+        fflush(stdout); 
+        scanf ("%s",&input);
+        /*Switch case input*/
+        switch(input)
+        {
+        case 'T': 
+            //Sequential Entering, Hour, Minute, Second respectively
+            printf ("Enter hour:(00-23) \n\r"); 
+            fflush(stdout); 
+            scanf ("%d",&hour);
+            printf ("Enter minute:(00-59) \n\r"); 
+            fflush(stdout); 
+            scanf ("%d",&minute);
+            printf ("Enter second:(00-59) \n\r"); 
+            fflush(stdout); 
+            scanf ("%d",&second);
+        break;
+        /*Case A ----- All values Update sequence*/
+        case 'A':
+            printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year);
+            printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month);      
+            printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day);
+            printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour);
+            printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute);
+            printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second);
+        break;
+        /*default to reset ----- Month Update sequence*/
+        default:
+            printf  ("Invalid\n\r");
+            set_time(1515513600);
+        }  
+        /*Updating all the timings after the user has input all the data*/
+        /*Put here as once the user has finished editing it does a batch update*/
+        timeinfo->tm_year = year - 1900;
+        timeinfo->tm_mon = month - 1;
+        timeinfo->tm_mday = day;
+        timeinfo->tm_hour = hour;
+        timeinfo->tm_min = minute;
+        timeinfo->tm_sec = second;
+        time_t CurrTime = mktime(timeinfo); //Convert the to UNIX time
+        set_time(CurrTime);                 //Sets time using the UNIX time
+        DispTime();  
+        return;                       //Display the new time
+    }
+}
+void rundate(void)//setup date function 
+{
+    while(1) //When added to the main code this will be changed to a while "SETDATE"
+        {
             /*promts the user to input which edit they would like*/
-            printf ("What part do you want to edit? Time(T)/All(A).\n\r"); 
+            printf ("What part do you want to edit? Date(D)/All(A).\n\r"); 
             fflush(stdout); 
             scanf ("%s",&input);
             /*Switch case input*/
             switch(input)
             {
-            case 'T':
-                //Sequential Entering, Hour, Minute, Second respectively
-                printf ("Enter hour:(00-23) \n\r"); 
-                fflush(stdout); 
-                scanf ("%d",&hour);
-
-                printf ("Enter minute:(00-59) \n\r"); 
-                fflush(stdout); 
-                scanf ("%d",&minute);
-
-                printf ("Enter second:(00-59) \n\r"); 
-                fflush(stdout); 
-                scanf ("%d",&second);
-                
-            break;
-        
-            /*Case A ----- All values Update sequence*/
-            case 'A':
-                printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year);
-                printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month);      
-                printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day);
-                printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour);
-                printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute);
-                printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second);
-            break;
-        
-            /*default to reset ----- Month Update sequence*/
-            default:
-                printf  ("Invalid\n\r");
-                set_time(1515513600);
-        }  
-        /*Updating all the timings after the user has input all the data*/
-        /*Put here as once the user has finished editing it does a batch update*/
+                /*Case D ----- Date Update sequence*/  
+                case 'D': 
+                    //Sequential Entering, Day, Month, Year respectively 
+                    printf ("Enter day:(01-31) \n\r"); 
+                    fflush(stdout); 
+                    scanf ("%d",&day); 
+                    printf ("Enter month:(01-12) \n\r"); 
+                    fflush(stdout); 
+                    scanf ("%d",&month);
+                    printf ("Enter year:(1970-9999) \n\r"); 
+                    fflush(stdout); 
+                    scanf ("%d",&year);                       
+                break;    
+                /*Case A ----- All values Update sequence*/
+                case 'A':
+                    printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year);
+                    printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month);      
+                    printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day);
+                    printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour);
+                    printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute);
+                    printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second);
+                break;
+                /*default to reset ----- Month Update sequence*/
+                default:
+                    printf  ("Invalid\n\r");
+                    set_time(1515513600);
+            }  
+            /*Updating all the timings after the user has input all the data*/
+           /*Put here as once the user has finished editing it does a batch update*/
             timeinfo->tm_year = year - 1900;
             timeinfo->tm_mon = month - 1;
             timeinfo->tm_mday = day;
@@ -313,57 +398,4 @@
             DispTime();  
             return;                       //Display the new time
         }
-    }
-
-void rundate(void){
-    while(1) //When added to the main code this will be changed to a while "SETDATE"
-    {
-    
-            /*promts the user to input which edit they would like*/
-            printf ("What part do you want to edit? Date(D)/All(A).\n\r"); 
-            fflush(stdout); 
-            scanf ("%s",&input);
-            /*Switch case input*/
-            switch(input)
-            {
-            /*Case D ----- Date Update sequence*/  
-            case 'D': 
-                //Sequential Entering, Day, Month, Year respectively 
-                printf ("Enter day:(01-31) \n\r"); 
-                fflush(stdout); 
-                scanf ("%d",&day); 
-                printf ("Enter month:(01-12) \n\r"); 
-                fflush(stdout); 
-                scanf ("%d",&month);
-                printf ("Enter year:(1970-9999) \n\r"); 
-                fflush(stdout); 
-                scanf ("%d",&year);                       
-            break;    
-            /*Case A ----- All values Update sequence*/
-            case 'A':
-                printf ("Enter year:(0-9999) \n\r"); fflush(stdout); scanf ("%d",&year);
-                printf ("Enter month:(01-12) \n\r"); fflush(stdout); scanf ("%d",&month);      
-                printf ("Enter day:(01-31) \n\r"); fflush(stdout); scanf ("%d",&day);
-                printf ("Enter hour:(00-23)\n\r"); fflush(stdout); scanf ("%d",&hour);
-                printf ("Enter minute:(00-59) \n\r"); fflush(stdout); scanf ("%d",&minute);
-                printf ("Enter second:(00-59) \n\r"); fflush(stdout); scanf ("%d",&second);
-            break;
-            /*default to reset ----- Month Update sequence*/
-            default:
-                printf  ("Invalid\n\r");
-                set_time(1515513600);
-        }  
-        /*Updating all the timings after the user has input all the data*/
-        /*Put here as once the user has finished editing it does a batch update*/
-            timeinfo->tm_year = year - 1900;
-            timeinfo->tm_mon = month - 1;
-            timeinfo->tm_mday = day;
-            timeinfo->tm_hour = hour;
-            timeinfo->tm_min = minute;
-            timeinfo->tm_sec = second;
-            time_t CurrTime = mktime(timeinfo); //Convert the to UNIX time
-            set_time(CurrTime);                 //Sets time using the UNIX time
-            DispTime();  
-            return;                       //Display the new time
-        }
-    }
\ No newline at end of file
+}
\ No newline at end of file