Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Barometer.cpp Source File

Barometer.cpp

Go to the documentation of this file.
00001 /**
00002 @file Barometer.cpp
00003 
00004 @brief Member functions implementations
00005 
00006 */
00007 #include "mbed.h"
00008 #include "Barometer.h"
00009 
00010 
00011 ///
00012 void button1Pressed()  //ISR to subtract 1 from the value of choice when the first button is pressed
00013 {
00014     if (debounce.read_ms()>200) // only allow if debounce timer
00015         button1Flag =1; // has passed 200 ms
00016     debounce.reset(); // restart timer when the toggle is performed
00017 }
00018 
00019 
00020 ///
00021 void button2Pressed() //ISR to add 1 to the value of the choice when the second button is pressed
00022 {
00023     if (debounce.read_ms()>200) // only allow if debounce timer
00024         button2Flag =1; // has passed 200 ms
00025     debounce.reset(); // restart timer when the toggle is performed
00026 }
00027 
00028 
00029 ///
00030 void button3Pressed()
00031 {
00032     if (debounce.read_ms()>200) // only allow if debounce timer
00033         button3Flag =1; // has passed 200 ms
00034     debounce.reset(); // restart timer when the toggle is performed
00035 }
00036 
00037 
00038 ///
00039 void button4Pressed()
00040 {
00041     if (debounce.read_ms()>200) // only allow if debounce timer
00042         button4Flag =1; // has passed 200 ms
00043     debounce.reset(); // restart timer when the toggle is performed
00044 }
00045 
00046 
00047 
00048 /// ISR called by the timer to set the timer flag to 1 (flag is used to update the values of pressure and temperature every second for the live data function)
00049 void timerExpired()
00050 {
00051     yellowLED = 0;
00052     timerFlag =1;
00053 
00054 }
00055 
00056 
00057 ///An ISR called by data logger timer to set the dataLoggerFlag to 1
00058 void dataLoggerTimerExpired ()
00059 {
00060     yellowLED = 0;
00061     dataLoggerFlag  = 1 ;
00062 
00063 }
00064 
00065 
00066 /// open a local file and print the recieved char arrays in the stream
00067 void saveToFile(char *data,char *data1,char *data2,char *data3)
00068 {
00069     FILE* pFile = fopen("/local/textfile.csv","w"); // open file access
00070     fprintf (pFile, "%s\n%s\n%s\n%s", data,data1,data2,data3);
00071     fclose (pFile);
00072 }
00073 
00074 
00075 /// Gets the current time and stores them in bufferTime and bufferDate
00076 void updateTime()
00077 {
00078     time_t seconds = time(NULL); // get current time
00079 // format time into a string (time and date)
00080     currentTime  = seconds ; // sets the current time to an integer for alarm clock check
00081     //serial.printf("current time :%i \n",currentTime);
00082     //serial.printf("Unix date :%i \n",UNIXdate);
00083     strftime(bufferTime , 14 , "%H:%M", localtime(&seconds));
00084     strftime(bufferDate , 14 , "%d/%m/%y", localtime(&seconds));
00085 }
00086 
00087 
00088 /// Displays the screen buffers on the lcd
00089 void display()
00090 {
00091     lcd.clear();
00092     lcd.printString(buffer0 ,0,0);
00093     lcd.printString(buffer1 ,0,1);
00094     lcd.printString(buffer2 ,0,2);
00095     lcd.printString(buffer3 ,0,3);
00096     lcd.printString(buffer4 ,0,4);
00097     lcd.printString(buffer5 ,0,5);
00098     wait(0.01);//
00099 
00100 }
00101 
00102 
00103 
00104 ///Imports the data and saves them to the bufferss
00105 void readData()
00106 {
00107 
00108     Measurement measurement;  // measurement structure declared in BMP180 class
00109 
00110     measurement = bmp180.readValues();
00111     // serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);
00112 
00113 
00114     if (unitFlag ==1 || unitFlag ==2 ) { //c
00115 
00116         temperature  = measurement.temperature;
00117         int length = sprintf(bufferT ,"%0.1f",temperature ); // print formatted data to buffer
00118         // it is important the format specifier ensures the length will fit in the buffer
00119     }
00120     if (unitFlag ==3 || unitFlag ==4 ) {//f
00121         temperature  =( measurement.temperature*(9/5))+32;
00122         int length = sprintf(bufferT ,"%0.1f",temperature ); // print formatted data to buffer
00123         // it is important the format specifier ensures the length will fit in the buffer
00124     }
00125 
00126 
00127 
00128 
00129     if (unitFlag ==1 || unitFlag ==3 ) {//mb
00130         pressure  = measurement.pressure;  // same idea with float
00131         int  length = sprintf(bufferP ,"%.2f",pressure ); // print formatted data to buffer
00132         // it is important the format specifier ensures the length will fit in the buffer
00133     }
00134     if (unitFlag ==2 || unitFlag ==4 ) {//atm
00135         pressure  = measurement.pressure*0.0009869;  // same idea with floats
00136         int length = sprintf(bufferP ,"%.2f",pressure ); // print formatted data to buffer
00137         // it is important the format specifier ensures the length will fit in the buffer
00138     }
00139 
00140 }
00141 
00142 
00143 
00144 
00145 /// Called by the powerSaverTimeout to turn off the lcd and put the mbed in sleep mode
00146 void powerSaverExpired()
00147 {
00148     powerSaverFlag  = 1;
00149 }
00150 
00151 
00152 
00153 
00154 
00155 
00156 /// Provides the power saving features
00157 void powerSaverCheck()   // checks if the powersaverFlag is set
00158 {
00159     if (powerSaverFlag  == 1) {
00160         while (1) {
00161             thresholdCheck();
00162             alarmClockCheck();
00163             updateTime();
00164             loggerData();
00165 
00166             if (button1Flag  || button2Flag  || button3Flag  || button4Flag ) { // if any of the buttons are pressed
00167                 powerSaverTimeout.attach(&powerSaverExpired, powerSaverTime ); // setup a timeiut to call the savePower function
00168                 powerSaverFlag  = 0;
00169                 lcd.init();
00170                 button1Flag  = 0 ;
00171                 button2Flag  = 0 ;
00172                 button3Flag  = 0 ;
00173                 button4Flag  = 0 ;
00174                 break;
00175             }
00176             lcd.turnOff();
00177             Sleep();
00178         }
00179     }
00180 }
00181 
00182 
00183 
00184 /// Displays the live values of temperature and pressure, refreshes every 1 second
00185 void liveData()
00186 {
00187     timerFlag =1;
00188     timer.attach(&timerExpired,1.0);
00189     while (1) {
00190         alarmClockCheck();
00191         powerSaverCheck();
00192         updateTime();
00193         loggerData();
00194         if (timerFlag ) {
00195 
00196             yellowLED = 1;
00197             timerFlag =0;
00198             readData();
00199             thresholdCheck();
00200             strncpy(buffer1 , bufferTime , 14);
00201             strncpy(buffer2 , bufferDate , 14);
00202             sprintf (buffer3 , "T: %s", bufferT );
00203            sprintf (buffer4 , "P: %s", bufferP );
00204             //strncpy(buffer3, bufferT, 14);
00205 //            strncpy(buffer4, bufferP, 14);
00206             int dispaly = sprintf (buffer5 , "");//convert integer to buffer str
00207             int dispaly1 = sprintf (buffer0 , "  Live Data");//convert integer to buffer str
00208             display();
00209         }
00210         yellowLED = 0;
00211         if (button3Flag ) {
00212             button3Flag  = 0;
00213             timer.detach();
00214             break;
00215         }
00216     }
00217 }
00218 
00219 
00220 
00221 ///  gets the temperature and pressure data and stores to suitable arrays, saves the arrays to the local file
00222 void loggerData()
00223 {
00224 
00225 
00226     if(dataLoggerFlag  ==1) {
00227         int k ;
00228         int sumTemperature = 0;
00229         int sumPressure = 0;
00230         greenLED = 1;
00231         readData();
00232         thresholdCheck();
00233         dataLoggerFlag =0;
00234 //write the data to the arrays (arrays used to plot graphs)
00235         //read the data from sensor
00236 
00237 
00238 //reads the saved data back from the local file
00239 
00240         FILE* pFilea = fopen("/local/textfile.csv","r"); // open file access
00241 
00242         if (pFilea) {//checks if any data is available from the previous readings
00243             fscanf (pFilea, "%s\n %s\n %s\n %s\n", temperatureRecieved ,pressureRecieved ,timeRecieved ,dateRecieved );
00244             fclose (pFilea);
00245 
00246 
00247             //initialises the arrays with the saved data from the past
00248             strcpy (temperatureSent ,temperatureRecieved );
00249             strcpy (pressureSent ,pressureRecieved );
00250             strcpy (timeSent ,timeRecieved );
00251             strcpy (dateSent ,dateRecieved );
00252         }
00253 
00254 
00255 //inserts the int values to char arrays to save them to the local file
00256         strcat(temperatureSent ,bufferT );
00257         strcat(temperatureSent ,",");
00258         strcat(pressureSent ,bufferP );
00259         strcat(pressureSent ,",");
00260 
00261 
00262 
00263         //apends the new time and current stamp to the array with , between them
00264         strcat(timeSent ,bufferTime );
00265         strcat(timeSent ,",");
00266         strcat (dateSent ,bufferDate );
00267         strcat (dateSent ,",");
00268 
00269         //send the arrays above to the local file
00270         saveToFile(temperatureSent ,pressureSent ,timeSent ,dateSent );
00271         serial.printf ("sent temperatures %s\nsent pressures %s\nsent time %s\nsent date %s\n",temperatureSent ,pressureSent ,timeSent ,dateSent );
00272 
00273 //reads the saved data back from the local file
00274 
00275         FILE* pFileb = fopen("/local/textfile.csv","r"); // open file access
00276         fscanf (pFileb, "%s\n %s\n %s\n %s\n", temperatureRecieved ,pressureRecieved ,timeRecieved ,dateRecieved );
00277         fclose (pFileb);
00278 
00279         serial.printf ("recieved temperatures %s\nrecieved pressure %s\nrecieved time %s\nrecieved date %s\n",temperatureRecieved ,pressureRecieved ,timeRecieved ,dateRecieved );
00280 
00281 
00282         //seperates the array into tokens(seperated after the delimiter , )
00283         pch  = strtok (timeRecieved ,",");//pch is the arguement of the for str
00284         while (pch  != NULL) {
00285             //serial.printf (" splitted time = %s\n",pch);
00286             pch  = strtok (NULL, ",");
00287         }
00288         pch1  = strtok (dateRecieved ,",");
00289         while (pch1  != NULL) {
00290             //serial.printf (" splitted date= %s\n",pch1);
00291             pch1  = strtok (NULL, ",");
00292         }
00293         pch2  = strtok (temperatureRecieved ,",");
00294         int p=0;
00295         while (pch2  != NULL) {
00296             //serial.printf (" splitted temperature = %s\n",pch2);
00297             arrayT [p]=atof(pch2 );
00298             pch2  = strtok (NULL, ",");
00299             p++;
00300         }
00301 
00302         p=0;
00303         pch3  = strtok (pressureRecieved ,",");
00304         while (pch3  != NULL) {
00305             // serial.printf (" splitted pressure= %s\n",pch3);
00306             arrayP [p]=atof(pch3 );
00307             pch3  = strtok (NULL, ",");
00308             p++;
00309         }
00310 
00311 // calculate the average value of the arrays and save them to an integer
00312 
00313 
00314 
00315         minTemperature  = arrayT [0]; // sets the min to the first value in the array
00316         minPressure  = arrayP [0];
00317         maxTemperature  = arrayT [0];
00318         maxPressure  = arrayP [0];
00319 
00320 
00321         for (k = 0 ; k<p ; k++) { // loops through the arrays
00322              printf("seperated temperature array = %lf\n",arrayT [k]);
00323              printf("seperated pressure array = %lf\n",arrayP [k]);
00324 
00325 
00326             if (unitFlag ==1 || unitFlag ==2 ) { //c
00327                 arrayTG [k]=(arrayT [k]/60);
00328             }
00329             if (unitFlag ==3 || unitFlag ==4 ) {//f
00330                 arrayTG [k]=(arrayT [k]/140);
00331             }
00332 
00333 
00334 
00335 
00336             if (unitFlag ==1 || unitFlag ==3 ) {//mb
00337                 arrayPG [k]=(arrayP [k]/1500);
00338             }
00339             if (unitFlag ==2 || unitFlag ==4 ) {//atm
00340                 arrayPG [k]=(arrayP [k]/2);
00341             }
00342 
00343 
00344 
00345             sumTemperature += arrayT [k]; // calculates the sum of the stored values
00346             averageTemperature  = sumTemperature/(k+1) ; //calculates the average value
00347 
00348             sumPressure += arrayP [k]; // calculates the sum of the stored values
00349             averagePressure  =(sumPressure/(k+1)) ; //calculates the average value
00350 
00351             if(arrayT [k]>maxTemperature ) { // checks if any of the values in the array is bigger than the fist value
00352                 maxTemperature =arrayT [k];  // if any greater values, sets the max value to that
00353             }
00354             if(arrayT [k]<minTemperature ) { // finds the smallest value of the array
00355                 minTemperature =arrayT [k];
00356             }
00357             if(arrayP [k]>maxPressure ) { // checks if any of the values in the array is bigger than the fist value
00358                 maxPressure =(arrayP [k]);  // if any greater values, sets the max value to that
00359             }
00360             if(arrayP [k]<maxPressure ) {
00361                 minPressure =(arrayP [k]);
00362             }
00363 
00364         }
00365 
00366 
00367 
00368         if (p>83) { // if the number of reading taken is more than 84 clear the arrays and the local file
00369             strcpy (temperatureSent ,"");
00370             strcpy (pressureSent ,"");
00371             strcpy (timeSent ,"");
00372             strcpy (dateSent ,"");
00373         }
00374     }
00375     greenLED = 0;
00376 }
00377 
00378 
00379 
00380 
00381 /// Show a summery of minimum maximum and average values for temperature and pressure as well as the temperature-time and pressure_time graphs
00382 void dataLogger()
00383 {
00384     int swipe = 1 ;
00385     while (1) {
00386         alarmClockCheck();
00387         powerSaverCheck();
00388         updateTime();
00389         if (runLoggerFlag ) {
00390 
00391 
00392             loggerData();
00393 
00394 
00395             if (button4Flag ) {
00396                 button4Flag  = 0 ;
00397                 swipe ++;
00398             }
00399             if (swipe > 4) {
00400                 swipe = 1;
00401             }
00402 
00403             strncpy(buffer0 , bufferTime , 14);
00404             int display5 = sprintf (buffer5 , " Back    Next");//convert integer to buffer str
00405 
00406             switch (swipe) {
00407                 case 1: {
00408                     int display1 = sprintf (buffer1 , "Temperature");
00409                     int display2 = sprintf (buffer2 , "Min = %0.2f", minTemperature );
00410                     int display3 = sprintf (buffer3 , "Max = %0.2f", maxTemperature );
00411                     int display4 = sprintf (buffer4 , "Avg = %0.2f", averageTemperature );
00412                     display();
00413                     break;
00414                 }
00415                 case 2 : {
00416                     lcd.clear();
00417                     lcd.printString("  Temperature",0,0);
00418                     lcd.plotArray(arrayTG );
00419                     break;
00420                 }
00421                 case 3 : {
00422                     int display11 = sprintf (buffer1 , "Pressure");
00423                     int display22 = sprintf (buffer2 , "Min = %0.2f", minPressure );
00424                     int display33 = sprintf (buffer3 , "Max = %0.2f", maxPressure );
00425                     int display44 = sprintf (buffer4 , "Avg = %0.2f", averagePressure );
00426                     display();
00427                     break;
00428                 }
00429                 case 4 : {
00430                     lcd.clear();
00431                     lcd.printString("   Pressure",0,0);
00432                     lcd.plotArray(arrayPG );
00433                     break;
00434                 }
00435                 default :
00436                     break;
00437             }
00438         } else {
00439             lcd.clear();
00440             lcd.printString("  To Turn On",0,1);
00441             lcd.printString("    Go To",0,2);
00442             lcd.printString("   Settings",0,3);
00443             lcd.printString ("Back",0,5);
00444 
00445         }
00446 
00447         if (button3Flag ) {
00448             button3Flag =0;
00449             break;
00450         }
00451         wait(0.1);
00452     }
00453 }
00454 
00455 
00456 
00457 /// Checks if the current temperture and pressure is equal to one of the thresholds set by the user, if yes it triggers the alarm and sets the buzzer
00458 void thresholdCheck()
00459 {
00460     if (thresholdAlarmFlag ) {
00461         thresholdAlarmFlag  = 0;
00462         if (int(temperature ) <= fsmH[0].output) { //if the current temp is less than the set threshold
00463             while (1) {
00464                 redLED = 0 ;
00465                 strncpy(buffer0 , bufferTime , 14);
00466                 int display2 = sprintf (buffer1 , "");
00467                 int display0 = sprintf (buffer2 , "Threshold");
00468                 int display1 = sprintf (buffer3 , "Reached!");
00469                 int display4 = sprintf (buffer4 , "%0.1f!",temperature );
00470                 int display5 = sprintf (buffer5 , "Stop");
00471                 display();
00472                 for (int i=0; i<=11; i++) {
00473                     buzzer.period(1/(frequency [i])); // set PWM period
00474                     buzzer=0.5; // set duty cycle
00475                     wait(0.5); // hold for beat period
00476                     redLED = 1 ;
00477                     if (button3Flag ) {
00478                         buzzer = 0 ;
00479                         lcd.setBrightness(0.5);
00480                         button3Flag =0;
00481                         goto exit ;
00482                     }
00483                 }
00484             }
00485         }
00486         if (int(temperature ) >= fsmH[1].output) { //if the current temp is greater than the set threshold
00487             while (1) {
00488                 redLED = 0 ;
00489                 strncpy(buffer0 , bufferTime , 14);
00490                 int display2 = sprintf (buffer1 , "");
00491                 int display0 = sprintf (buffer2 , "Threshold");
00492                 int display1 = sprintf (buffer3 , "Reached!");
00493                 int display4 = sprintf (buffer4 , "%0.1f!",temperature  );
00494                 int display5 = sprintf (buffer5 , "Stop");
00495                 display();
00496                 for (int i=0; i<=11; i++) {
00497                     buzzer.period(1/(frequency [i])); // set PWM period
00498                     buzzer=0.5; // set duty cycle
00499                     wait(0.5); // hold for beat period
00500                     redLED = 1 ;
00501                     if (button3Flag ) {
00502                         buzzer = 0 ;
00503                         lcd.setBrightness(0.5);
00504                         button3Flag =0;
00505                         goto exit ;
00506                     }
00507                 }
00508             }
00509         }
00510         if (int(pressure ) <= fsmH[2].output) {  //if the current pressure is less than the set threshold
00511             while (1) {
00512                 redLED = 0 ;
00513                 strncpy(buffer0 , bufferTime , 14);
00514                 int display2 = sprintf (buffer1 , "");
00515                 int display0 = sprintf (buffer2 , "Threshold");
00516                 int display1 = sprintf (buffer3 , "Reached!");
00517                 int display4 = sprintf (buffer4 , "%0.1f!",pressure );
00518                 int display5 = sprintf (buffer5 , "Stop");
00519                 display();
00520                 for (int i=0; i<=11; i++) {
00521                     buzzer.period(1/(frequency [i])); // set PWM period
00522                     buzzer=0.5; // set duty cycle
00523                     redLED = 1 ;
00524                     if (button3Flag ) {
00525                         buzzer = 0 ;
00526                         button3Flag =0;
00527                         goto exit ;
00528                     }
00529                 }
00530             }
00531         }
00532         if (int(pressure ) >= fsmH[3].output) {  //if the current pressure is greater than the set threshold
00533 
00534             while (1) {
00535                 redLED = 0 ;
00536                 strncpy(buffer0 , bufferTime , 14);
00537                 int display2 = sprintf (buffer1 , "");
00538                 int display0 = sprintf (buffer2 , "Threshold");
00539                 int display1 = sprintf (buffer3 , "Reached!");
00540                 int display4 = sprintf (buffer4 , "%0.1f!",pressure );
00541                 int display5 = sprintf (buffer5 , "Stop");
00542                 display();
00543                 for (int i=0; i<=11; i++) {
00544                     buzzer.period(1/(frequency [i])); // set PWM period
00545                     buzzer=1; // set duty cycle
00546                     redLED = 1 ;
00547                     if (button3Flag ) {
00548                         buzzer = 0 ;
00549                         button3Flag =0;
00550                         goto exit ;
00551                     }
00552                 }
00553             }
00554         }
00555 exit:
00556         redLED = 0 ;
00557     }
00558 
00559 }
00560 
00561 ///Allows the user to set the minimum and maximum values of temperature and pressure for the thresholds
00562 void threshold()
00563 {
00564     state  = 0;
00565     //sets the initial threshhold values to the values recieved from the sensor
00566     readData();
00567     fsmH[0].output = temperature -20;
00568     fsmH[1].output = temperature ;
00569     fsmH[2].output = pressure -100;
00570     fsmH[3].output = pressure ;
00571 
00572     while (1) {
00573         alarmClockCheck();
00574         powerSaverCheck();
00575         updateTime();
00576         loggerData();
00577 
00578 
00579 
00580         int display0 = sprintf (buffer0 , "Set thresholds");//convert integer to buffer str
00581         int display1 = sprintf (buffer1 , "");//convert integer to buffer str
00582         int display2 = sprintf (buffer2 , "");//convert integer to buffer str
00583         int display3 = sprintf (buffer3 , ">>%s %d", fsmH[state ].title,fsmH[state ].output);//convert integer to buffer str
00584         int display4 = sprintf (buffer4 , "");//convert integer to buffer str
00585         int display5 = sprintf (buffer5 , " Back    Save");//convert integer to buffer str
00586 
00587 
00588         display();
00589 
00590 
00591 
00592         if (button4Flag ) {
00593             button4Flag = 0;
00594             fsmH[state ].output ++;
00595         }
00596 
00597 
00598 
00599 
00600         //Navigates to the next5 state of the fsm when one of the buttons is pressed
00601         if (button1Flag  == 1) {
00602             button1Flag =0;
00603             state =fsmH[state ].nextState[1];
00604         }
00605         if (button2Flag  == 1) {
00606             button2Flag =0;
00607             state =fsmH[state ].nextState[0];
00608         }
00609 
00610 
00611         //checks the limits of the values
00612         if (fsmH[0].output >(temperature *1.5)) { //min temperature
00613             fsmH[0].output = -(temperature *1.5) ;
00614         }
00615         if (fsmH[1].output>(temperature *1.5)) { // max temperature
00616             fsmH[1].output = -(temperature *1.5) ;
00617         }
00618         if (fsmH[2].output>(pressure *1.10)) { //min pressure
00619             fsmH[2].output = -(pressure *1.10) ;
00620         }
00621         if (fsmH[3].output>(pressure *1.10)) { //max pressure
00622             fsmH[3].output = -(pressure *1.10) ;
00623         }
00624 
00625 
00626         if (button3Flag ) {//if wants to go back
00627             button3Flag =0;
00628             state  = 0;
00629             while (1) { //runs a while loop
00630                 int display0 = sprintf (buffer0 , "");
00631                 int display1 = sprintf (buffer1 , "");
00632                 int display2 = sprintf (buffer2 , "Set Thresholds?"); //asks to set the threshold
00633                 int display3 = sprintf (buffer3 , ">>%s", fsmG[state ].title); // yes or no (1 or 0 output)
00634                 int display4 = sprintf (buffer4 , "");
00635                 int display5 = sprintf (buffer5 , "");
00636                 display();
00637                 thresholdAlarmFlag  = fsmG[state ].output ; //yes or no (1 or 0)
00638 
00639 
00640                 //Navigates to the next state of the fsm when one of the buttons is pressed
00641                 if (button1Flag  == 1) {
00642                     button1Flag =0;
00643                     state =fsmG[state ].nextState[1];
00644                 }
00645                 if (button2Flag  == 1) {
00646                     button2Flag =0;
00647                     state =fsmG[state ].nextState[0];
00648                 }
00649 
00650 
00651 
00652                 if (button3Flag ) {
00653                     //serial.printf("thresholds flag :%i \n",fsmG[state].output);
00654                     //serial.printf("thresholds: mint%i maxt%i minP%i maxP %i \n",fsmH[0].output,fsmH[1].output,fsmH[2].output,fsmH[3].output);
00655                     break;
00656                 }
00657             }
00658         }
00659         if (button3Flag ) {
00660             button3Flag =0;
00661             break ;
00662         }
00663     }
00664 }
00665 
00666 
00667 
00668 
00669 
00670 /// Compares the time set by the user in the alarm clock section with the current time, turn on the buzzer if they are equal
00671 void alarmClockCheck()
00672 {
00673     if (alarmClockFlag ) {
00674         if (currentTime  == UNIXdate ) { //if the current time is equal to the time set by user
00675             //serial.printf("Alarm clock flag is set\n");
00676             alarmClockFlag  =0 ;
00677             while (1) {
00678                 int display0 = sprintf (buffer0 , "");
00679                 int display1 = sprintf (buffer1 , "Alarm !");
00680                 strncpy(buffer2 , bufferTime , 14);
00681                 int display2 = sprintf (buffer3 , "");
00682                 int display4 = sprintf (buffer4 , "");
00683                 int display5 = sprintf (buffer5 , "Stop");
00684                 display();
00685                 for (int i=0; i<=11; i++) {
00686                     buzzer.period(1/(frequency [i])); // set PWM period
00687                     buzzer=0.5; // set duty cycle
00688                     wait(0.5); // hold for beat period
00689                     if (button3Flag ) {
00690                         buzzer = 0 ;
00691                         lcd.setBrightness(0.5);
00692                         break;
00693                     }
00694                 }
00695                 if (button3Flag ) {
00696                     button3Flag =0;
00697                     break;
00698                 }
00699 
00700             }
00701         }
00702     }
00703 }
00704 
00705 
00706 
00707 
00708 
00709 
00710 /// Allows the user to set the time for the alarm clock
00711 void alarmClock ()
00712 {
00713     state  = 0;
00714     while (1) {
00715         alarmClockCheck();
00716         powerSaverCheck();
00717         updateTime();
00718         loggerData();
00719 
00720 
00721         int display0 = sprintf (buffer0 , "Set Alarm");//convert integer to buffer str
00722         int display1 = sprintf (buffer1 , "");//convert integer to buffer str
00723         int display2 = sprintf (buffer2 , "" );//convert integer to buffer str
00724         int display3 = sprintf (buffer3 , ">>%s %d", fsmC[state ].title,fsmC[state ].output);//convert integer to buffer str
00725         int display4 = sprintf (buffer4 , "" );//convert integer to buffer str
00726         int display5 = sprintf (buffer5 , " Back    Save" );//convert integer to buffer str
00727 
00728 
00729 
00730         display();
00731 
00732 
00733 
00734         if (button4Flag ) {
00735             button4Flag = 0;
00736             fsmC[state ].output ++;
00737             calculateUNIXTime();
00738         }
00739 
00740 
00741 
00742 //Navigates to the next5 state of the fsm when one of the buttons is pressed
00743         if (button1Flag  == 1) {
00744             button1Flag =0;
00745             state =fsmC[state ].nextState[1];
00746         }
00747         if (button2Flag  == 1) {
00748             button2Flag =0;
00749             state =fsmC[state ].nextState[0];
00750         }
00751 
00752 
00753 
00754         //checks the limits of the values
00755         if (fsmC[0].output >23) { //hour
00756             fsmC[0].output = 0 ;
00757         }
00758         if (fsmC[1].output>59) { // minute
00759             fsmC[1].output = 0 ;
00760         }
00761         if (fsmC[3].output>11) { //month
00762             fsmC[3].output = 0 ;
00763         }
00764         if (fsmC[2].output>31) { //day
00765             fsmC[2].output = 1 ;
00766         }
00767         if (fsmC[4].output>2030) { //year
00768             fsmC[4].output = 2015 ;
00769         }
00770 
00771 
00772 
00773         if (button3Flag ) {
00774             button3Flag =0;
00775             while (1) {
00776                 int display0 = sprintf (buffer0 , "");
00777                 int display1 = sprintf (buffer1 , "");
00778                 int display2 = sprintf (buffer2 , "Set Alarm?");
00779                 int display3 = sprintf (buffer3 , ">>%s", fsmG[state ].title);
00780                 int display4 = sprintf (buffer4 , "");
00781                 int display5 = sprintf (buffer5 , "");
00782                 display();
00783                 alarmClockFlag  = fsmG[state ].output ;
00784                 if (state >1) {
00785                     state  = 0;
00786                 }
00787                 if (state <0) {
00788                     state  = 1;
00789                 }
00790                 if (button3Flag ) {
00791                     break ;
00792                 }
00793 
00794 
00795                 //Navigates to the next5 state of the fsm when one of the buttons is pressed
00796                 if (button1Flag  == 1) {
00797                     button1Flag =0;
00798                     state =fsmG[state ].nextState[1];
00799                 }
00800                 if (button2Flag  == 1) {
00801                     button2Flag =0;
00802                     state =fsmG[state ].nextState[0];
00803                 }
00804             }
00805         }
00806         if (button3Flag ) {
00807             button3Flag =0;
00808             break ;
00809         }
00810     }
00811 }
00812 
00813 
00814 
00815 
00816 ///A menu to indicate the alarm options (Alarm clock and thresholds alarm), also allows the user to navigate through the given options
00817 void alarmsMenu ()
00818 {
00819     state  = 0 ;
00820     while (1) {
00821         alarmClockCheck();
00822         powerSaverCheck();
00823         updateTime();
00824         loggerData();
00825         int display0 = sprintf (buffer0 , "    Alarms");
00826         int display1 = sprintf (buffer1 , "");
00827         int display2 = sprintf (buffer2 , "");
00828         int display3 = sprintf (buffer3 , ">>%s", fsmF[state ].title);
00829         int display4 = sprintf (buffer4 , "");
00830         int display5 = sprintf (buffer5 , " Back    Next");
00831 
00832         display();
00833 
00834 
00835 
00836         if (state  > 1) {
00837             state  = 0 ;
00838         }
00839         if (state  < 0) {
00840             state  = 1 ;
00841         }
00842         if (button3Flag ) {
00843             button3Flag =0;
00844             state  = 0;
00845             break;
00846         }
00847 
00848         switch (state ) {
00849             case 0: //thresholds
00850 
00851                 if (button4Flag ) {
00852                     button4Flag =0;
00853                     threshold();
00854                 }
00855                 break ;
00856 
00857             case 1 :
00858                 if (button4Flag ) {
00859                     button4Flag =0;
00860                     alarmClock();
00861                 }
00862                 break ;
00863                 default:
00864                 break;
00865         }
00866 
00867         //Navigates to the next state of the fsm when one of the buttons is pressed
00868         if (button1Flag  == 1) {
00869             button1Flag =0;
00870             state =fsmF[state ].nextState[1];
00871         }
00872         if (button2Flag  == 1) {
00873             button2Flag =0;
00874             state =fsmF[state ].nextState[0];
00875         }
00876     }
00877 }
00878 
00879 
00880 
00881 /// Allows the user to turn on the data logger and set the time interval between the readings
00882 void dataLoggerSetting()
00883 {
00884     state  = runLoggerFlag  ; //inits the state to the saved flag by the user in the past
00885     while (1) {
00886         alarmClockCheck();
00887         powerSaverCheck();
00888         updateTime();
00889         loggerData();
00890         sprintf (buffer0 , "");
00891         sprintf (buffer1 , "");
00892         sprintf (buffer2 , "Data Logger");
00893         sprintf (buffer3 , ">>%s", fsmE[state ].title);
00894         if (state  == 1) {
00895             sprintf (buffer4 , "%i mins", fsmE[state ].output/60);
00896         } else { // if the power saver is off it doesnt show the time
00897             sprintf (buffer4 , "");
00898         }
00899         sprintf (buffer5 , " Back    Save");
00900 
00901 
00902         display();
00903 
00904 
00905 
00906         runLoggerFlag  = state  ; //sets the flag to the value of state (0 is off, 1 is on)
00907         dataLoggerTime  = fsmE[state ].output; //sets the timer to to value of output from the fsm
00908         // serial.printf("data logger Time = %i \n dataLoggerFlag = %i \n",dataLoggerTime , runLoggerFlag);
00909 
00910         if (button4Flag ) {
00911             button4Flag =0;
00912             fsmE[state ].output += 60 ; //adds a minute to the timer when button 4 is pressed
00913         }
00914         if (fsmE[state ].output > 1800) {
00915             fsmE[state ].output = 60 ;
00916         }
00917 
00918         if (state  > 1) {
00919             state  = 0 ;
00920         }
00921 
00922         if (button3Flag ) {
00923             if (state  == 1) { // if the data logger is swiched on
00924                 dataLoggerTimer.attach(&dataLoggerTimerExpired, dataLoggerTime ); // set the timer
00925             }
00926             if (state  == 0) { // if the data logger is swiched off
00927                 dataLoggerTimer.detach(); // detach the timer
00928             }
00929             dataLoggerFlag  = 1;
00930             button3Flag =0;
00931             state  = 0;
00932             break;
00933         }
00934         //Navigates to the next5 state of the fsm when one of the buttons is pressed
00935         if (button1Flag  == 1) {
00936             button1Flag =0;
00937             state =fsmE[state ].nextState[1];
00938         }
00939         if (button2Flag  == 1) {
00940             button2Flag =0;
00941             state =fsmE[state ].nextState[0];
00942         }
00943     }
00944 
00945 }
00946 
00947 
00948 
00949 
00950 
00951 
00952 
00953 
00954 
00955 
00956 
00957 /// Allows the user to set the power saving option and set the timeout
00958 void powerSaverSetting()
00959 {
00960 
00961     state  = powerSaverFlag  ; //inits the state to the saved flag by the user in the past
00962     while (1) {
00963         alarmClockCheck();
00964         powerSaverCheck();
00965         updateTime();
00966         loggerData();
00967         sprintf (buffer0 , "");
00968         sprintf (buffer1 , "");
00969         sprintf (buffer2 , "Power Saver");
00970         sprintf (buffer3 , ">>%s", fsmE[state ].title);
00971         if (state  == 1) {
00972             sprintf (buffer4 , "%i mins", fsmE[state ].output/60);
00973         } else { // if the power saver is off it dowsnt show the time
00974             sprintf (buffer4 , "");
00975         }
00976         sprintf (buffer5 , "Back    Save");
00977 
00978 
00979         display();
00980 
00981         powerSaverTime  = fsmE[state ].output; //sets the timer to to value of output from the fsm
00982         //serial.printf("Power Saver Time = %i \n powerSaverFlag = %i \n",powerSaverTime , powerSaverFlag);
00983 
00984         if (button4Flag ) {
00985             button4Flag =0;
00986             fsmE[state ].output += 60 ; //adds a minute to the timer when button 4 is pressed
00987         }
00988         if (fsmE[state ].output > 600) { // maximum time is 10 minutes
00989             fsmE[state ].output = 60 ;
00990         }
00991 
00992         if (state  > 1) {
00993             state  = 0 ;
00994         }
00995 
00996         if (button3Flag ) {
00997             button3Flag =0;
00998 
00999             if (state == 1) {
01000                 powerSaverTimeout.attach(&powerSaverExpired, powerSaverTime ); // setup a timeiut to call the savePower function
01001             }
01002             break;
01003         }
01004 
01005 
01006         //Navigates to the next5 state of the fsm when one of the buttons is pressed
01007         if (button1Flag  == 1) {
01008             button1Flag =0;
01009             state =fsmF[state ].nextState[1];
01010         }
01011         if (button2Flag  == 1) {
01012             button2Flag =0;
01013             state =fsmF[state ].nextState[0];
01014         }
01015     }
01016 }
01017 
01018 
01019 
01020 
01021 
01022 
01023 
01024 /// Converts the entered raw date and time values by the user to create the unix time stamp
01025 void calculateUNIXTime()
01026 {
01027 
01028     time_t rawtime;
01029     struct tm * timeinfo;
01030 
01031     // get current timeinfo
01032     time ( &rawtime );
01033     // convert to struct
01034     timeinfo = localtime ( &rawtime );
01035     // now modify the timeinfo to the given date
01036     timeinfo->tm_year   = (fsmC[4].output) - 1900;
01037     timeinfo->tm_mon    = (fsmC[3].output) - 1;    //months since January - [0,11]
01038     timeinfo->tm_mday   = (fsmC[2].output) ;        //day of the month - [1,31]
01039     timeinfo->tm_hour   = (fsmC[0].output) ;        //hours since midnight - [0,23]
01040     timeinfo->tm_min    = (fsmC[1].output) ;         //minutes after the hour - [0,59]
01041     timeinfo->tm_sec    = 0 ;          //seconds after the minute - [0,59]
01042 
01043     //call mktime to create unix time stamp from timeinfo struct
01044     UNIXdate  = mktime ( timeinfo );
01045 
01046 }
01047 
01048 
01049 
01050 
01051 /// Allows the user to change the units for temperature and pressure
01052 void unitsSetting()
01053 {
01054     state  = 0;
01055     while (1) {
01056         powerSaverCheck();
01057         alarmClockCheck();
01058         updateTime();
01059         loggerData();
01060 
01061         sprintf (buffer0 , "Set Units");//convert integer to buffer str
01062         sprintf (buffer1 , "");//convert integer to buffer str
01063         sprintf (buffer2 , "");//convert integer to buffer str
01064         sprintf (buffer3 , ">>%s", fsmD[state ].title);//convert integer to buffer str
01065         sprintf (buffer4 , "");//convert integer to buffer str
01066         sprintf (buffer5 , " Back    Save");//convert integer to buffer str
01067 
01068 
01069         unitFlag  = fsmD[state ].output;
01070 
01071         display();
01072 
01073         if (button3Flag ) {
01074             button3Flag =0;
01075             state  = 0;
01076             break;
01077         }
01078 
01079         //Navigates to the next5 state of the fsm when one of the buttons is pressed
01080         if (button1Flag  == 1) {
01081             button1Flag =0;
01082             state =fsmD[state ].nextState[1];
01083         }
01084         if (button2Flag  == 1) {
01085             button2Flag =0;
01086             state =fsmD[state ].nextState[0];
01087         }
01088     }
01089 }
01090 
01091 
01092 
01093 
01094 
01095 
01096 /// Used by the user to set the current date and time
01097 void timeDateSetting()
01098 {
01099 
01100     state  = 0;
01101     while (1) {
01102         alarmClockCheck();
01103         powerSaverCheck();
01104         updateTime();
01105         loggerData();
01106 
01107         sprintf (buffer0 , "Set Time/Date");//convert integer to buffer str
01108         sprintf (buffer1 , "");
01109         sprintf (buffer2 , "");
01110         sprintf (buffer3 , ">>%s %d", fsmC[state ].title,fsmC[state ].output);//convert integer to buffer str
01111         sprintf (buffer4 , "");
01112         sprintf (buffer5 , "Back    Save");
01113 
01114 
01115         display();
01116 
01117 
01118 
01119         if (button4Flag ) {
01120             button4Flag = 0;
01121             fsmC[state ].output ++;
01122             calculateUNIXTime();
01123             set_time(UNIXdate ); // initialise time from the calculated UNIX time entered by the user
01124         }
01125 
01126 
01127         //checks the limits of the values
01128         if (fsmC[0].output >23) { //hour
01129             fsmC[0].output = 0 ;
01130         }
01131         if (fsmC[1].output>59) { // minute
01132             fsmC[1].output = 0 ;
01133         }
01134         if (fsmC[3].output>11) { //month
01135             fsmC[3].output = 0 ;
01136         }
01137         if (fsmC[2].output>31) { //day
01138             fsmC[2].output = 1 ;
01139         }
01140         if (fsmC[4].output>2030) { //year
01141             fsmC[4].output = 2015 ;
01142         }
01143 
01144 
01145 
01146         if (button3Flag ) {
01147             button3Flag =0;
01148             state  = 0;
01149             break;
01150         }
01151         //Navigates to the next5 state of the fsm when one of the buttons is pressed
01152         if (button1Flag  == 1) {
01153             button1Flag =0;
01154             state =fsmC[state ].nextState[1];
01155         }
01156         if (button2Flag  == 1) {
01157             button2Flag =0;
01158             state =fsmC[state ].nextState[0];
01159         }
01160     }
01161 }
01162 
01163 
01164 /// The settings menu allows user to change the settings
01165 void settingsMenu()
01166 {
01167     state  = 0;
01168     while(button3Flag ==0) {
01169         alarmClockCheck();
01170         powerSaverCheck();
01171         updateTime();
01172         loggerData();
01173         strncpy(buffer0 , bufferTime , 14);
01174         int dispaly = sprintf (buffer1 , "Settings");
01175         int dispaly1 = sprintf (buffer2 , "");
01176         int dispaly2 = sprintf (buffer3 , ">>%s", fsmB[state ].title);//convert integer to buffer str
01177         int dispaly3 = sprintf (buffer4 , "");
01178         int dispaly4 = sprintf (buffer5 , "");
01179 
01180         display();
01181 
01182 
01183         switch (state ) {
01184             case 0:
01185                 if (button4Flag ) {
01186                     button4Flag =0;
01187                     timeDateSetting();
01188                 }
01189 
01190 
01191                 break;
01192             case 1:
01193 
01194 
01195                 if (button4Flag ) {
01196                     button4Flag =0;
01197                     unitsSetting();
01198                 }
01199 
01200                 break;
01201             case 2:
01202 
01203                 if (button4Flag ) {
01204                     button4Flag  = 0;
01205                     powerSaverSetting();
01206                 }
01207                 break;
01208             case 3:
01209                 if (button4Flag ) {
01210                     button4Flag  = 0;
01211                     dataLoggerSetting();
01212                 }
01213                 break;
01214             default:
01215                 break;
01216         }
01217         // if (button3Flag) {
01218 //            button3Flag=0;
01219 //            state = 0;
01220 //            break;
01221 //        }
01222         //Navigates to the next5 state of the fsm when one of the buttons is pressed
01223         if (button1Flag  == 1) {
01224             button1Flag =0;
01225             state =fsmB[state ].nextState[1];
01226         }
01227         if (button2Flag  == 1) {
01228             button2Flag =0;
01229             state =fsmB[state ].nextState[0];
01230         }
01231     }
01232 }
01233 
01234 /// The first main menu
01235 void startMenu()
01236 {
01237     while(1) {
01238         updateTime();
01239         alarmClockCheck();
01240         powerSaverCheck();
01241         loggerData();
01242         button3Flag =0;
01243         strncpy(buffer0 , bufferTime , 14);
01244         sprintf (buffer1 , "Main Menu");
01245         sprintf (buffer2 , "");
01246         sprintf (buffer3 , ">>%s", fsmA[state ].title);
01247         sprintf (buffer4 , "");
01248         sprintf (buffer5 , "");
01249         display();
01250 
01251 
01252         switch (state ) {
01253             case 0:
01254                 if (button4Flag ) {
01255                     button4Flag =0;
01256                     liveData();
01257                 }
01258                 break;
01259             case 1:
01260                 if (button4Flag ) {
01261                     button4Flag =0;
01262                     dataLogger();
01263                 }
01264                 break;
01265             case 2:
01266                 if (button4Flag ) {
01267                     button4Flag =0;
01268                     alarmsMenu();
01269                 }
01270                 break;
01271             case 3:
01272                 if (button4Flag ) {
01273                     button4Flag =0;
01274                     settingsMenu();
01275                 }
01276                 break;
01277             default:
01278                 break;
01279         }
01280 
01281 
01282         //Navigates to the next5 state of the fsm when one of the buttons is pressed
01283         if (button1Flag  == 1) {
01284             button1Flag =0;
01285             state =fsmA[state ].nextState[1];
01286         }
01287         if (button2Flag  == 1) {
01288             button2Flag =0;
01289             state =fsmA[state ].nextState[0];
01290         }
01291 
01292     }
01293 }
01294 
01295 
01296 
01297 
01298 int main ()
01299 {
01300 
01301     /// Initialise barometer
01302     bmp180.init();
01303     /// Initialise the lcd
01304     lcd.init();
01305     /// call the appropriate ISR on rising edge (when any of the buttons are pressed)
01306     button1.rise(&button1Pressed);
01307     button2.rise(&button2Pressed);
01308     button3.rise(&button3Pressed);
01309     button4.rise(&button4Pressed);
01310 
01311     debounce.start();
01312     /// Set frequency at 40kHz for the leds
01313     redLED.period_us(25);
01314     yellowLED.period_us(25);
01315     greenLED.period_us(25);
01316     state  = 0;
01317     startMenu();
01318 }
01319 
01320 
01321