PROJ515 / Mbed OS ELEC-351-GROUP-E-CW

Dependencies:   BMP280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD.cpp Source File

LCD.cpp

00001 #include "mbed.h"
00002 #include "LCD.h"
00003 #include "DriverLCD.h"
00004 #include "rtos.h"
00005 
00006 using namespace std;
00007 //Initalise TextLCD and button1 before constructor.
00008 ENVDISPLAY::ENVDISPLAY(PinName rsPin, PinName rePin, PinName d4Pin, PinName d5Pin, PinName d6Pin, PinName d7Pin, PinName Button1Pin, PinName Button2Pin) : DriverLCD(rsPin, rePin, d4Pin, d5Pin, d6Pin, d7Pin), Button1(Button1Pin), Button2(Button2Pin)
00009 {
00010     //constructor. Initalise variables
00011     _latestTemp = 0.0;
00012     _latestPres = 0.0;
00013     _latestLDR = 0.0;
00014     _latestTime = 0;
00015     _currentState = SCROLLREADINGS;
00016 }
00017 
00018 
00019 void ENVDISPLAY::SendMessage(char sentText[], bool returnToReadings)
00020 {
00021     if (strlen(sentText)>32) {
00022         //overflow error
00023         LogEvent(Log_LCDOverflow);
00024     } else {
00025         std::copy( sentText, sentText+strlen(sentText), _message ); //store inputted message to _message
00026         _AutoQuit = returnToReadings;   //Set AutoQuitState
00027         _currentState = MESSAGE;    //Change LCD state to show message
00028     }
00029 }
00030 
00031 
00032 void ENVDISPLAY::UpdateData(float temp, float pres, float LDR, time_t sampleTime)
00033 {
00034     _latestTemp = temp;
00035     _latestPres = pres;
00036     _latestLDR = LDR;
00037     _latestTime = sampleTime;
00038 }
00039 
00040 
00041 void ENVDISPLAY::StateMachine(void)
00042 {
00043     while (true) {
00044         cls();  //clear LCD
00045         switch (_currentState) {
00046             case SCROLLREADINGS:
00047                 LogEvent(Log_LCDScroll);   //Log position
00048 
00049                 tm T = ReturnDateTimeStruct(_latestTime);   //Get a time structure
00050                 printf("Temp: %5.1fC\n",_latestTemp);   //Print temperature
00051                 printf("Pres: %5.1fmBar\n",_latestPres);//Print Pressure
00052                 Thread::wait(SCREENDELAY_MS);   //Hold screen for specified time
00053                 cls();  //Clear screen
00054                 printf("LDR: %4.3f\n",_latestLDR);  //Print Light Level
00055                 printf("%2d/%2d %2d:%2d:%2d\n",T.tm_mday,T.tm_mon,T.tm_hour,T.tm_min,T.tm_sec); //Print time DD/MM HH:mm:ss
00056                 Thread::wait(SCREENDELAY_MS);   //Hold screen for specified time
00057                 break;
00058 
00059             case MESSAGE:
00060                 LogEvent(Log_LCDMessage);   //Log position
00061                 printf("%s\n",_message);    //Print custom message to display
00062                 Thread::wait(SCREENDELAY_MS);//Hold screen for specified time
00063                 if (_AutoQuit) {
00064                     //If it has been specified to return to scroll readings:
00065                     _currentState = SCROLLREADINGS; //Change state
00066                     memset(_message, 0, 32*sizeof(char));    //Wipe message
00067                 }
00068                 //Else stay in message state.
00069                 break;
00070 
00071             case ADJUSTTIME:
00072                 _EditTime = ReturnDateTimeStruct(time(0));          //Get system time as structure
00073                 Thread::signal_clr(1);      //Clear any flags that have already been set
00074                 strcpy(_editingInTime, "Day");                      //Say what we will be adjusting
00075                 _EditTime.tm_mday = TimeEditor(_EditTime.tm_mday, 31,1); //Enter time set loop then update value in structure
00076                 strcpy(_editingInTime, "MM");                    //Say what we will be adjusting
00077                 _EditTime.tm_mon = TimeEditor(_EditTime.tm_mon, 12,1); //Enter time set loop then update value in structure
00078                 strcpy(_editingInTime, "Year");                    //Say what we will be adjusting
00079                 _EditTime.tm_year = TimeEditor(_EditTime.tm_year, 2020,1996); //Enter time set loop then update value in structure
00080                 strcpy(_editingInTime, "Hour");                    //Say what we will be adjusting
00081                 _EditTime.tm_hour = TimeEditor(_EditTime.tm_hour, 23,0); //Enter time set loop then update value in structure
00082                 strcpy(_editingInTime, "Mins");                    //Say what we will be adjusting
00083                 _EditTime.tm_min = TimeEditor(_EditTime.tm_min, 59,0); //Enter time set loop then update value in structure
00084 
00085                 SetDate(_EditTime.tm_mday, _EditTime.tm_mon, _EditTime.tm_year);    //Set system date
00086                 SetTime(_EditTime.tm_hour, _EditTime.tm_min, 0);
00087                 SendMessage("Time and date   set",true);
00088                 break;
00089         }
00090     }
00091 }
00092 
00093 void ENVDISPLAY::Start(void)
00094 {
00095     LCDThread.start(this, &ENVDISPLAY::StateMachine);  //Start thread
00096     this->Button1.rise(this, &ENVDISPLAY::Button1ISR); //Initalise button interrupt
00097     this->Button2.rise(this, &ENVDISPLAY::Button2ISR); //Initalise button interrupt
00098     SendMessage("Starting...",true);                   //WRite message
00099 }
00100 
00101 void ENVDISPLAY::Button1ISR(void)
00102 {
00103 
00104     But1Debouncer.attach(this, &ENVDISPLAY::But1DebouncerISR,0.2); //Start debouncer
00105 }
00106 
00107 void ENVDISPLAY::Button2ISR(void)
00108 {
00109     But2Debouncer.attach(this, &ENVDISPLAY::But2DebouncerISR,0.2); //Start debouncer
00110 }
00111 
00112 void ENVDISPLAY::But1DebouncerISR(void)
00113 {
00114     
00115     _but1Pressed = true;    //Signal Button 1 as pressed
00116     _currentState = ADJUSTTIME;//Change State machine
00117     LCDThread.signal_set(1);//Signal to thread to continue
00118 }
00119 
00120 void ENVDISPLAY::But2DebouncerISR(void)
00121 {
00122     _but2Pressed = true;
00123     LCDThread.signal_set(1);
00124 }
00125 
00126 int ENVDISPLAY::TimeEditor(int changingVal, int upperLimit, int lowerLimit)
00127 {
00128     _but1Pressed = false;       //Set switches to 'not been pushed'
00129     _but2Pressed = false;
00130     while (!_but2Pressed) {
00131         cls();          //Clear the screen
00132         printf("Adjust The %s\n",_editingInTime);   //print value to screen
00133         printf("%2d\n",changingVal);
00134         _but1Pressed = false;       //Prevent against the buttons having been pushed early
00135         _but2Pressed = false;
00136         Thread::signal_wait(1); //Wait for a button to be pushed
00137 
00138         if (_but1Pressed) {
00139             //Then user wants to increment
00140             if ((changingVal+1) > upperLimit) {
00141                 changingVal = lowerLimit;   //overflow so loop
00142             } else {
00143                 changingVal += 1;  //incrment
00144             }
00145             _but2Pressed = false;   //Button 1 was pressed so 2 must not have been
00146         } else {
00147             //Button 2 has been pressed so user wants to step forward.
00148             //This will exit while loop and move to next step
00149         }
00150     }
00151     memset(_editingInTime, 0, 10*sizeof(char));    //Clear the variable name
00152     return changingVal;     //Return the new number.
00153 }
00154 
00155 void ENVDISPLAY::POST(void)
00156 {
00157     cls();  //Clear screen
00158     printf("B1: %d\n",Button1.read());  //Display button 1 state
00159     printf("B2: %d\n",Button2.read());  //Display button 2 state
00160 }