![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Repo. for the ELEC351 Coursework - Oliver Thompson
Dependencies: BMP280 ELEC350-Practicals-FZ429- TextLCD watchdog_RTOS BME280 ntp-client
LCD.hpp@20:2ce28a5032d5, 2019-01-02 (annotated)
- Committer:
- O_Thom
- Date:
- Wed Jan 02 12:12:30 2019 +0000
- Revision:
- 20:2ce28a5032d5
- Parent:
- 19:c3b396b65f2a
Serial Messaging Complete - SD Class need to handle commands. LCD and Networking Time and Date Update Needs Testing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
O_Thom | 6:b7f6e0c0f646 | 1 | #ifndef _LCD_ |
O_Thom | 6:b7f6e0c0f646 | 2 | #define _LCD_ |
O_Thom | 6:b7f6e0c0f646 | 3 | #include "mbed.h" |
O_Thom | 6:b7f6e0c0f646 | 4 | #include "messageStruct.hpp" |
O_Thom | 6:b7f6e0c0f646 | 5 | #include "sample_hardware.hpp" |
O_Thom | 19:c3b396b65f2a | 6 | #include "Network.hpp" |
O_Thom | 19:c3b396b65f2a | 7 | #include <stdio.h> |
O_Thom | 19:c3b396b65f2a | 8 | #include <string.h> |
O_Thom | 19:c3b396b65f2a | 9 | |
O_Thom | 20:2ce28a5032d5 | 10 | Mail<time_and_date,5> Serial2LCD; // Serial -> LCD Mailbox 5 Elements Wide Time&Date |
O_Thom | 9:654e14de9d74 | 11 | |
O_Thom | 6:b7f6e0c0f646 | 12 | class LCD_Data |
O_Thom | 5:f87129ac8bf3 | 13 | { |
O_Thom | 19:c3b396b65f2a | 14 | friend class Network; |
O_Thom | 19:c3b396b65f2a | 15 | |
O_Thom | 19:c3b396b65f2a | 16 | private: //private variables can only be changed via functions in this class |
O_Thom | 6:b7f6e0c0f646 | 17 | float temp; //current temperature of sensor, updated every 15 seconds |
O_Thom | 6:b7f6e0c0f646 | 18 | float pressure; //current pressure of sensor, updated every 15 seconds |
O_Thom | 6:b7f6e0c0f646 | 19 | float fLDR; //current light level from LDR, updated every 15 seconds |
O_Thom | 6:b7f6e0c0f646 | 20 | int flip; |
O_Thom | 19:c3b396b65f2a | 21 | string day; |
O_Thom | 19:c3b396b65f2a | 22 | string month; |
O_Thom | 19:c3b396b65f2a | 23 | string date; |
O_Thom | 19:c3b396b65f2a | 24 | string time; |
O_Thom | 19:c3b396b65f2a | 25 | string year; |
O_Thom | 19:c3b396b65f2a | 26 | |
O_Thom | 19:c3b396b65f2a | 27 | |
O_Thom | 19:c3b396b65f2a | 28 | private: |
O_Thom | 19:c3b396b65f2a | 29 | struct tm Time_Date; //decale instance Time_Date of structure tm which is defined by mbed / C |
O_Thom | 19:c3b396b65f2a | 30 | |
O_Thom | 6:b7f6e0c0f646 | 31 | |
O_Thom | 6:b7f6e0c0f646 | 32 | void update_temp(double t) //use this function to update the current temperature value |
O_Thom | 6:b7f6e0c0f646 | 33 | { |
O_Thom | 6:b7f6e0c0f646 | 34 | temp = t; |
O_Thom | 6:b7f6e0c0f646 | 35 | } |
O_Thom | 6:b7f6e0c0f646 | 36 | void update_pressure(double p) //use this function to update the current pressure value |
O_Thom | 6:b7f6e0c0f646 | 37 | { |
O_Thom | 6:b7f6e0c0f646 | 38 | pressure = p; |
O_Thom | 6:b7f6e0c0f646 | 39 | } |
O_Thom | 6:b7f6e0c0f646 | 40 | void update_LDR(double L) |
O_Thom | 6:b7f6e0c0f646 | 41 | { |
O_Thom | 6:b7f6e0c0f646 | 42 | fLDR = L; |
O_Thom | 6:b7f6e0c0f646 | 43 | } |
O_Thom | 6:b7f6e0c0f646 | 44 | |
O_Thom | 19:c3b396b65f2a | 45 | |
O_Thom | 19:c3b396b65f2a | 46 | |
O_Thom | 5:f87129ac8bf3 | 47 | public: |
O_Thom | 19:c3b396b65f2a | 48 | |
O_Thom | 5:f87129ac8bf3 | 49 | EventQueue LCD_Queue; //create an event queue for main |
O_Thom | 19:c3b396b65f2a | 50 | time_t timestamp; //current time in format of unix time, can be converted to DAY_OF_WEEK MONTH DAY HOUR:MINUTE:SECOND YEAR using ctime(×tamp); |
O_Thom | 19:c3b396b65f2a | 51 | |
O_Thom | 20:2ce28a5032d5 | 52 | |
O_Thom | 20:2ce28a5032d5 | 53 | void getSerial2LCD() // Queue Consumer |
O_Thom | 20:2ce28a5032d5 | 54 | { |
O_Thom | 20:2ce28a5032d5 | 55 | if (!Serial2LCD.empty()) |
O_Thom | 20:2ce28a5032d5 | 56 | { |
O_Thom | 20:2ce28a5032d5 | 57 | osEvent evt = Serial2LCD.get(); |
O_Thom | 20:2ce28a5032d5 | 58 | switch (evt.status) |
O_Thom | 20:2ce28a5032d5 | 59 | { |
O_Thom | 20:2ce28a5032d5 | 60 | case osEventMail: |
O_Thom | 20:2ce28a5032d5 | 61 | //Normal status |
O_Thom | 20:2ce28a5032d5 | 62 | time_and_date *time_copy = (time_and_date*)evt.value.p; |
O_Thom | 20:2ce28a5032d5 | 63 | time = time_copy->current_time; // Update the internal clock |
O_Thom | 20:2ce28a5032d5 | 64 | Serial2Net.free(time_copy); // Free up the space in the memory Pool |
O_Thom | 20:2ce28a5032d5 | 65 | default: |
O_Thom | 20:2ce28a5032d5 | 66 | //All other errors (see cmsis_os.h for meaning of error code) |
O_Thom | 20:2ce28a5032d5 | 67 | //printf("Serial2Net->get() returned %02x status\n\r", evt.status); |
O_Thom | 20:2ce28a5032d5 | 68 | } |
O_Thom | 20:2ce28a5032d5 | 69 | } |
O_Thom | 20:2ce28a5032d5 | 70 | } |
O_Thom | 20:2ce28a5032d5 | 71 | |
O_Thom | 11:b8e8630c7e3b | 72 | |
O_Thom | 5:f87129ac8bf3 | 73 | LCD_Data(){ //constructor, initializes the FLIP variable for use in toggling the bottom line of the LCD |
O_Thom | 5:f87129ac8bf3 | 74 | flip = 1; |
O_Thom | 6:b7f6e0c0f646 | 75 | temp = 0; |
O_Thom | 6:b7f6e0c0f646 | 76 | pressure = 0; |
O_Thom | 6:b7f6e0c0f646 | 77 | fLDR = 0; |
O_Thom | 19:c3b396b65f2a | 78 | |
O_Thom | 19:c3b396b65f2a | 79 | |
O_Thom | 5:f87129ac8bf3 | 80 | } |
O_Thom | 10:08c366434f2b | 81 | |
O_Thom | 19:c3b396b65f2a | 82 | |
O_Thom | 19:c3b396b65f2a | 83 | |
O_Thom | 6:b7f6e0c0f646 | 84 | void update_sensor_info(sample_message msg) //updates all current sensor information, this is called by a ticker every 5 seconds to read from the mailbox |
O_Thom | 5:f87129ac8bf3 | 85 | { |
O_Thom | 6:b7f6e0c0f646 | 86 | update_temp(msg.temp); // Include message class passing of data |
O_Thom | 6:b7f6e0c0f646 | 87 | update_pressure(msg.pressure); |
O_Thom | 6:b7f6e0c0f646 | 88 | update_LDR(msg.ldr); |
O_Thom | 5:f87129ac8bf3 | 89 | } |
O_Thom | 19:c3b396b65f2a | 90 | |
O_Thom | 19:c3b396b65f2a | 91 | |
O_Thom | 19:c3b396b65f2a | 92 | |
O_Thom | 19:c3b396b65f2a | 93 | |
O_Thom | 5:f87129ac8bf3 | 94 | void display_LCD() //updates the current LCD display with the new sensor information |
O_Thom | 5:f87129ac8bf3 | 95 | { |
O_Thom | 5:f87129ac8bf3 | 96 | lcd.cls(); //clear current LCD display |
O_Thom | 19:c3b396b65f2a | 97 | |
O_Thom | 5:f87129ac8bf3 | 98 | lcd.printf("%4.2fC", temp); //print temperature to the top line of LCD, 2dp celcius |
O_Thom | 19:c3b396b65f2a | 99 | |
O_Thom | 19:c3b396b65f2a | 100 | |
O_Thom | 5:f87129ac8bf3 | 101 | switch(flip){ |
O_Thom | 5:f87129ac8bf3 | 102 | case 1: |
O_Thom | 5:f87129ac8bf3 | 103 | lcd.printf("\n%4.2f mbar", pressure); //print pressure to bottom line of LCD, 2dp mbar |
O_Thom | 6:b7f6e0c0f646 | 104 | flip = 2; |
O_Thom | 6:b7f6e0c0f646 | 105 | break; |
O_Thom | 6:b7f6e0c0f646 | 106 | case 2: |
O_Thom | 6:b7f6e0c0f646 | 107 | lcd.printf("\n%4.2f Lux", fLDR); //print pressure to bottom line of LCD, 2dp mbar |
O_Thom | 6:b7f6e0c0f646 | 108 | flip = 1; |
O_Thom | 19:c3b396b65f2a | 109 | break; |
O_Thom | 19:c3b396b65f2a | 110 | case 3: //only ever called when the interupt button is pressed to update time |
O_Thom | 19:c3b396b65f2a | 111 | lcd.printf("\nTime updated"); //informs the user the current time has been set |
O_Thom | 19:c3b396b65f2a | 112 | printf("Current time is %s\r\n", ctime(×tamp)); //prints current time and date in human readable time to terminal |
O_Thom | 19:c3b396b65f2a | 113 | flip = 1; |
O_Thom | 19:c3b396b65f2a | 114 | break; |
O_Thom | 6:b7f6e0c0f646 | 115 | default: |
O_Thom | 5:f87129ac8bf3 | 116 | printf("Error in LCD flip function"); |
O_Thom | 5:f87129ac8bf3 | 117 | break; |
O_Thom | 5:f87129ac8bf3 | 118 | } |
O_Thom | 19:c3b396b65f2a | 119 | } |
O_Thom | 19:c3b396b65f2a | 120 | |
O_Thom | 19:c3b396b65f2a | 121 | |
O_Thom | 19:c3b396b65f2a | 122 | |
O_Thom | 19:c3b396b65f2a | 123 | time_and_date update_time_date (){ |
O_Thom | 19:c3b396b65f2a | 124 | |
O_Thom | 19:c3b396b65f2a | 125 | timestamp = ntp.get_timestamp(); //reads the current time from a local NTP server in UNIX format |
O_Thom | 19:c3b396b65f2a | 126 | |
O_Thom | 19:c3b396b65f2a | 127 | string current_time = ctime(×tamp); //converts time to human readable format string |
O_Thom | 19:c3b396b65f2a | 128 | |
O_Thom | 10:08c366434f2b | 129 | |
O_Thom | 19:c3b396b65f2a | 130 | |
O_Thom | 19:c3b396b65f2a | 131 | day.assign(current_time, 0, 3); //extract only day from current_time string |
O_Thom | 19:c3b396b65f2a | 132 | month.assign(current_time, 4, 3); //extract only month from "" |
O_Thom | 19:c3b396b65f2a | 133 | date.assign(current_time, 9, 1); //extract only date from "" |
O_Thom | 19:c3b396b65f2a | 134 | time.assign(current_time, 11, 8); //extract only time from "" |
O_Thom | 19:c3b396b65f2a | 135 | year.assign(current_time, 20, 4); //extract only year from "" |
O_Thom | 19:c3b396b65f2a | 136 | |
O_Thom | 19:c3b396b65f2a | 137 | |
O_Thom | 19:c3b396b65f2a | 138 | //printf("current day is: %s\n", day); |
O_Thom | 20:2ce28a5032d5 | 139 | // printf("current year is: %s\n", year); |
O_Thom | 20:2ce28a5032d5 | 140 | // printf("current time is: %s\n", time); |
O_Thom | 20:2ce28a5032d5 | 141 | // printf("current date is: %s\n", date); |
O_Thom | 19:c3b396b65f2a | 142 | // printf("current month is: %s\n", month); |
O_Thom | 19:c3b396b65f2a | 143 | |
O_Thom | 19:c3b396b65f2a | 144 | |
O_Thom | 19:c3b396b65f2a | 145 | |
O_Thom | 19:c3b396b65f2a | 146 | m_oNet.Network_Queue.call(&m_oNet, &Network::update_Time, current_time); |
O_Thom | 19:c3b396b65f2a | 147 | |
O_Thom | 19:c3b396b65f2a | 148 | time_and_date Timemsg; // Define instance of message structure |
O_Thom | 20:2ce28a5032d5 | 149 | Timemsg.day = day; |
O_Thom | 20:2ce28a5032d5 | 150 | Timemsg.month = month; |
O_Thom | 20:2ce28a5032d5 | 151 | Timemsg.date = date; |
O_Thom | 20:2ce28a5032d5 | 152 | Timemsg.time = time; |
O_Thom | 20:2ce28a5032d5 | 153 | Timemsg.year = year; |
O_Thom | 20:2ce28a5032d5 | 154 | Timemsg.current_time = current_time; |
O_Thom | 19:c3b396b65f2a | 155 | |
O_Thom | 19:c3b396b65f2a | 156 | flip = 3; //will tell the user that the time has been updated on next FLIP instance with the LCD |
O_Thom | 19:c3b396b65f2a | 157 | |
O_Thom | 19:c3b396b65f2a | 158 | return Timemsg; //swap this for a function that sends the structure to ollie? |
O_Thom | 19:c3b396b65f2a | 159 | |
O_Thom | 19:c3b396b65f2a | 160 | } |
O_Thom | 19:c3b396b65f2a | 161 | |
O_Thom | 19:c3b396b65f2a | 162 | |
O_Thom | 19:c3b396b65f2a | 163 | }; //end of LCD class |
O_Thom | 19:c3b396b65f2a | 164 | |
O_Thom | 19:c3b396b65f2a | 165 | // Define the member object for the LCD |
O_Thom | 19:c3b396b65f2a | 166 | LCD_Data m_oDisplay; |
O_Thom | 20:2ce28a5032d5 | 167 | #endif |
O_Thom | 20:2ce28a5032d5 | 168 | |
O_Thom | 20:2ce28a5032d5 | 169 |