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

Dependencies:   BMP280

Committer:
mwthewsey
Date:
Tue Jan 09 20:51:19 2018 +0000
Revision:
21:6e733076f49c
Parent:
20:25939e03b803
Child:
22:617bf92b481f
Everything working. Comments checked.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mwthewsey 11:b538e73841ae 1 #include "mbed.h"
mwthewsey 11:b538e73841ae 2 #include "LCD.h"
mwthewsey 11:b538e73841ae 3 #include "TextLCD.h"
mwthewsey 11:b538e73841ae 4 #include "rtos.h"
mwthewsey 11:b538e73841ae 5
mwthewsey 11:b538e73841ae 6 using namespace std;
mwthewsey 21:6e733076f49c 7 //Initalise TextLCD and button1 before constructor.
mwthewsey 11:b538e73841ae 8 ENVDISPLAY::ENVDISPLAY(PinName rsPin, PinName rePin, PinName d4Pin, PinName d5Pin, PinName d6Pin, PinName d7Pin, PinName Button1Pin, PinName Button2Pin) : TextLCD(rsPin, rePin, d4Pin, d5Pin, d6Pin, d7Pin), Button1(Button1Pin), Button2(Button2Pin)
mwthewsey 11:b538e73841ae 9 {
mwthewsey 21:6e733076f49c 10 //constructor. Initalise variables
mwthewsey 11:b538e73841ae 11 _latestTemp = 0.0;
mwthewsey 11:b538e73841ae 12 _latestPres = 0.0;
mwthewsey 11:b538e73841ae 13 _latestLDR = 0.0;
mwthewsey 11:b538e73841ae 14 _latestTime = 0;
mwthewsey 11:b538e73841ae 15 _currentState = SCROLLREADINGS;
mwthewsey 11:b538e73841ae 16 }
mwthewsey 21:6e733076f49c 17
mwthewsey 21:6e733076f49c 18
mwthewsey 11:b538e73841ae 19 void ENVDISPLAY::SendMessage(char sentText[], bool returnToReadings)
mwthewsey 11:b538e73841ae 20 {
mwthewsey 11:b538e73841ae 21 if (strlen(sentText)>32)
mwthewsey 11:b538e73841ae 22 {
mwthewsey 11:b538e73841ae 23 //overflow error
mwthewsey 21:6e733076f49c 24 LogEvent(Log_LCDOverflow);
mwthewsey 11:b538e73841ae 25 } else {
mwthewsey 21:6e733076f49c 26 std::copy( sentText, sentText+strlen(sentText), _message ); //store inputted message to _message
mwthewsey 11:b538e73841ae 27 _AutoQuit = returnToReadings; //Set AutoQuitState
mwthewsey 11:b538e73841ae 28 _currentState = MESSAGE; //Change LCD state to show message
mwthewsey 11:b538e73841ae 29 }
mwthewsey 11:b538e73841ae 30 }
mwthewsey 11:b538e73841ae 31
mwthewsey 21:6e733076f49c 32
mwthewsey 11:b538e73841ae 33 void ENVDISPLAY::UpdateData(float temp, float pres, float LDR, time_t sampleTime)
mwthewsey 11:b538e73841ae 34 {
mwthewsey 11:b538e73841ae 35 _latestTemp = temp;
mwthewsey 11:b538e73841ae 36 _latestPres = pres;
mwthewsey 11:b538e73841ae 37 _latestLDR = LDR;
mwthewsey 11:b538e73841ae 38 _latestTime = sampleTime;
mwthewsey 11:b538e73841ae 39 }
mwthewsey 11:b538e73841ae 40
mwthewsey 21:6e733076f49c 41
mwthewsey 11:b538e73841ae 42 void ENVDISPLAY::StateMachine(void)
mwthewsey 11:b538e73841ae 43 {
mwthewsey 11:b538e73841ae 44 while (true) {
mwthewsey 21:6e733076f49c 45 cls(); //clear LCD
mwthewsey 11:b538e73841ae 46 switch (_currentState) {
mwthewsey 11:b538e73841ae 47 case SCROLLREADINGS:
Swaggie 20:25939e03b803 48 LogEvent(Log_LCDScroll); //Log position
Swaggie 20:25939e03b803 49
mwthewsey 11:b538e73841ae 50 tm T = ReturnDateTimeStruct(_latestTime); //Get a time structure
mwthewsey 11:b538e73841ae 51 printf("Temp: %5.1fC\n",_latestTemp); //Print temperature
mwthewsey 11:b538e73841ae 52 printf("Pres: %5.1fmBar\n",_latestPres);//Print Pressure
mwthewsey 11:b538e73841ae 53 Thread::wait(SCREENDELAY_MS); //Hold screen for specified time
mwthewsey 11:b538e73841ae 54 cls(); //Clear screen
mwthewsey 11:b538e73841ae 55 printf("LDR: %4.3f\n",_latestLDR); //Print Light Level
mwthewsey 11:b538e73841ae 56 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
mwthewsey 11:b538e73841ae 57 Thread::wait(SCREENDELAY_MS); //Hold screen for specified time
mwthewsey 11:b538e73841ae 58 break;
mwthewsey 11:b538e73841ae 59
mwthewsey 11:b538e73841ae 60 case MESSAGE:
Swaggie 20:25939e03b803 61 LogEvent(Log_LCDMessage); //Log position
mwthewsey 11:b538e73841ae 62 printf("%s\n",_message); //Print custom message to display
mwthewsey 11:b538e73841ae 63 Thread::wait(SCREENDELAY_MS);//Hold screen for specified time
mwthewsey 11:b538e73841ae 64 if (_AutoQuit) {
mwthewsey 11:b538e73841ae 65 //If it has been specified to return to scroll readings:
mwthewsey 11:b538e73841ae 66 _currentState = SCROLLREADINGS; //Change state
mwthewsey 11:b538e73841ae 67 memset(_message, 0, 32*sizeof(char)); //Wipe message
mwthewsey 11:b538e73841ae 68 }
mwthewsey 11:b538e73841ae 69 //Else stay in message state.
mwthewsey 11:b538e73841ae 70 break;
mwthewsey 11:b538e73841ae 71
mwthewsey 11:b538e73841ae 72 case ADJUSTTIME:
mwthewsey 11:b538e73841ae 73 printf("Adjust the time\n");
mwthewsey 11:b538e73841ae 74 while (Button2 == 0) {}; //Spin
mwthewsey 11:b538e73841ae 75 SendMessage("Time Set",true);
mwthewsey 11:b538e73841ae 76 Button1.rise(this, &ENVDISPLAY::Button1ISR); //Reattach interrupt
mwthewsey 11:b538e73841ae 77 break;
mwthewsey 11:b538e73841ae 78 }
mwthewsey 11:b538e73841ae 79 }
mwthewsey 11:b538e73841ae 80 }
mwthewsey 11:b538e73841ae 81
mwthewsey 11:b538e73841ae 82 void ENVDISPLAY::Start(void)
mwthewsey 11:b538e73841ae 83 {
mwthewsey 21:6e733076f49c 84 LCDThread.start(this, &ENVDISPLAY::StateMachine); //Start thread
mwthewsey 21:6e733076f49c 85 this->Button1.rise(this, &ENVDISPLAY::Button1ISR); //Initalise button interrupt
mwthewsey 21:6e733076f49c 86 SendMessage("Starting...",true); //WRite message
mwthewsey 11:b538e73841ae 87 }
mwthewsey 11:b538e73841ae 88
mwthewsey 11:b538e73841ae 89 void ENVDISPLAY::Button1ISR(void)
mwthewsey 11:b538e73841ae 90 {
mwthewsey 11:b538e73841ae 91 _currentState = ADJUSTTIME;
mwthewsey 11:b538e73841ae 92 Button1.rise(NULL); //Detach IR
mwthewsey 11:b538e73841ae 93 }