Version 8, working version with Alix, sams and ollies code. Displays time, date and sensor info onto terminal, LCD and networking, and saves onto SD card.

Dependencies:   BMP280 ELEC350-Practicals-FZ429 TextLCD BME280 ntp-client

Committer:
Alix955
Date:
Mon Dec 31 19:20:22 2018 +0000
Revision:
12:4c7eaac8ceef
Parent:
11:42b0c567cc8c
Version 8, integration of Alix & Sams work with older(?) version of ollies. Displays time, date and all sensor information onto LCD, Terminal and Networking, and saves sensor info to SD card.

Who changed what in which revision?

UserRevisionLine numberNew 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"
Alix955 9:f5eae5211225 6 #include "Network.hpp"
Alix955 10:eea19f8e6122 7 #include <stdio.h>
Alix955 12:4c7eaac8ceef 8 #include <string>
Alix955 9:f5eae5211225 9
Alix955 9:f5eae5211225 10
O_Thom 6:b7f6e0c0f646 11 class LCD_Data
O_Thom 5:f87129ac8bf3 12 {
Alix955 9:f5eae5211225 13 friend class Network;
Alix955 9:f5eae5211225 14
Alix955 9:f5eae5211225 15 private: //private variables can only be changed via functions in this class
O_Thom 6:b7f6e0c0f646 16 float temp; //current temperature of sensor, updated every 15 seconds
O_Thom 6:b7f6e0c0f646 17 float pressure; //current pressure of sensor, updated every 15 seconds
O_Thom 6:b7f6e0c0f646 18 float fLDR; //current light level from LDR, updated every 15 seconds
Alix955 12:4c7eaac8ceef 19
Alix955 12:4c7eaac8ceef 20 int flip; //integer used to flip the bottom line of LCD
O_Thom 6:b7f6e0c0f646 21
Alix955 12:4c7eaac8ceef 22 string day; //string containing the current day when pulled from NTP server
Alix955 12:4c7eaac8ceef 23 string month; //string containing the current month when pulled from NTP server
Alix955 12:4c7eaac8ceef 24 string date; //string containing the current date when pulled from NTP server
Alix955 12:4c7eaac8ceef 25 string time; //string containing the current time when pulled from NTP server
Alix955 12:4c7eaac8ceef 26 string year; //string containing the current year when pulled from NTP server
Alix955 12:4c7eaac8ceef 27
Alix955 9:f5eae5211225 28
Alix955 9:f5eae5211225 29 private:
Alix955 9:f5eae5211225 30 struct tm Time_Date; //decale instance Time_Date of structure tm which is defined by mbed / C
Alix955 9:f5eae5211225 31
Alix955 9:f5eae5211225 32
Alix955 12:4c7eaac8ceef 33 void update_temp(double t) //used to update the current temperature value with an input
O_Thom 6:b7f6e0c0f646 34 {
Alix955 12:4c7eaac8ceef 35 temp = t; //sets private variable temp
O_Thom 6:b7f6e0c0f646 36 }
Alix955 12:4c7eaac8ceef 37 void update_pressure(double p) //used to update the current pressure value with an input
O_Thom 6:b7f6e0c0f646 38 {
Alix955 12:4c7eaac8ceef 39 pressure = p;
O_Thom 6:b7f6e0c0f646 40 }
Alix955 12:4c7eaac8ceef 41 void update_LDR(double L) //used to update the LDR value with an input
O_Thom 6:b7f6e0c0f646 42 {
O_Thom 6:b7f6e0c0f646 43 fLDR = L;
O_Thom 6:b7f6e0c0f646 44 }
O_Thom 6:b7f6e0c0f646 45
Alix955 9:f5eae5211225 46
Alix955 9:f5eae5211225 47
O_Thom 5:f87129ac8bf3 48 public:
Alix955 9:f5eae5211225 49
Alix955 12:4c7eaac8ceef 50 EventQueue LCD_Queue; //create an event queue for main to run
Alix955 9:f5eae5211225 51 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(&timestamp);
Alix955 9:f5eae5211225 52
Alix955 9:f5eae5211225 53
Alix955 9:f5eae5211225 54
O_Thom 5:f87129ac8bf3 55 LCD_Data(){ //constructor, initializes the FLIP variable for use in toggling the bottom line of the LCD
Alix955 12:4c7eaac8ceef 56 flip = 1; //initalize what state the bottom line starts on
Alix955 12:4c7eaac8ceef 57 temp = 0; //set temperature to start at 0
Alix955 12:4c7eaac8ceef 58 pressure = 0; //set pressure to start at 0
Alix955 12:4c7eaac8ceef 59 fLDR = 0; //set LDR value to start at 0
Alix955 9:f5eae5211225 60
Alix955 9:f5eae5211225 61
O_Thom 5:f87129ac8bf3 62 }
Alix955 9:f5eae5211225 63
Alix955 9:f5eae5211225 64
Alix955 9:f5eae5211225 65
Alix955 12:4c7eaac8ceef 66 void update_sensor_info(sample_message msg) //updates all current sensor information. recieves sensor info in structure type sample_message and takes it apart
O_Thom 5:f87129ac8bf3 67 {
Alix955 12:4c7eaac8ceef 68 update_temp(msg.temp); // takes temperature from sample_message and feeds to update temp function
Alix955 12:4c7eaac8ceef 69 update_pressure(msg.pressure); //takes pressure from sample_message and feeds to update pressure function
Alix955 12:4c7eaac8ceef 70 update_LDR(msg.ldr); //takes the LDR value from sample_message and feeds to update LDR function
O_Thom 5:f87129ac8bf3 71 }
Alix955 9:f5eae5211225 72
Alix955 10:eea19f8e6122 73
Alix955 9:f5eae5211225 74
Alix955 9:f5eae5211225 75
Alix955 12:4c7eaac8ceef 76 void display_LCD() //updates the current LCD display with the new sensor information, and flips bottom line of LCD
O_Thom 5:f87129ac8bf3 77 {
O_Thom 5:f87129ac8bf3 78 lcd.cls(); //clear current LCD display
Alix955 9:f5eae5211225 79
O_Thom 5:f87129ac8bf3 80 lcd.printf("%4.2fC", temp); //print temperature to the top line of LCD, 2dp celcius
Alix955 9:f5eae5211225 81
Alix955 9:f5eae5211225 82
O_Thom 5:f87129ac8bf3 83 switch(flip){
O_Thom 5:f87129ac8bf3 84 case 1:
O_Thom 5:f87129ac8bf3 85 lcd.printf("\n%4.2f mbar", pressure); //print pressure to bottom line of LCD, 2dp mbar
Alix955 12:4c7eaac8ceef 86 flip = 2; //swaps to case 2 next time function is run so LDR is printed instead
O_Thom 6:b7f6e0c0f646 87 break;
O_Thom 6:b7f6e0c0f646 88 case 2:
Alix955 12:4c7eaac8ceef 89 lcd.printf("\n%4.2f Lux", fLDR); //print LDR value to bottom line of LCD, 2dp
Alix955 12:4c7eaac8ceef 90 flip = 1; //swaps to case 1 next time function is run so pressure is printed instead
Alix955 9:f5eae5211225 91 break;
Alix955 12:4c7eaac8ceef 92 case 3: //only ever used when the interupt button is pressed to update time
Alix955 12:4c7eaac8ceef 93 lcd.printf("\nTime updated"); //informs the user the current time has been set by printing onto bottom line
Alix955 9:f5eae5211225 94 printf("Current time is %s\r\n", ctime(&timestamp)); //prints current time and date in human readable time to terminal
Alix955 12:4c7eaac8ceef 95 flip = 1; //swaps back to case 1 next time function is run
Alix955 9:f5eae5211225 96 break;
O_Thom 6:b7f6e0c0f646 97 default:
Alix955 12:4c7eaac8ceef 98 printf("Error in LCD flip function"); //only reached if the case is set incorrectly
O_Thom 5:f87129ac8bf3 99 break;
O_Thom 5:f87129ac8bf3 100 }
O_Thom 5:f87129ac8bf3 101 }
Alix955 9:f5eae5211225 102
Alix955 9:f5eae5211225 103
Alix955 9:f5eae5211225 104
Alix955 12:4c7eaac8ceef 105 void update_time_date (){ //used to update time and date, called only when button interrupt is pressed
Alix955 10:eea19f8e6122 106
Alix955 10:eea19f8e6122 107 timestamp = ntp.get_timestamp(); //reads the current time from a local NTP server in UNIX format
Alix955 12:4c7eaac8ceef 108
Alix955 10:eea19f8e6122 109 string current_time = ctime(&timestamp); //converts time to human readable format string
Alix955 12:4c7eaac8ceef 110
Alix955 12:4c7eaac8ceef 111 printf("%s", current_time); //prints current time and date onto terminal
Alix955 10:eea19f8e6122 112
Alix955 10:eea19f8e6122 113
Alix955 10:eea19f8e6122 114 day.assign(current_time, 0, 3); //extract only day from current_time string
Alix955 12:4c7eaac8ceef 115 month.assign(current_time, 4, 3); //extract only month from current_time string
Alix955 12:4c7eaac8ceef 116 date.assign(current_time, 9, 1); //extract only date from current_time string
Alix955 12:4c7eaac8ceef 117 time.assign(current_time, 11, 8); //extract only time from current_time string
Alix955 12:4c7eaac8ceef 118 year.assign(current_time, 20, 4); //extract only year from current_time string
Alix955 10:eea19f8e6122 119
Alix955 10:eea19f8e6122 120
Alix955 10:eea19f8e6122 121 //printf("current day is: %s\n", day);
Alix955 10:eea19f8e6122 122 // printf("current year is: %s\n", year);
Alix955 10:eea19f8e6122 123 // printf("current time is: %s\n", time);
Alix955 10:eea19f8e6122 124 // printf("current date is: %s\n", date);
Alix955 10:eea19f8e6122 125 // printf("current month is: %s\n", month);
Alix955 10:eea19f8e6122 126
Alix955 10:eea19f8e6122 127
Alix955 10:eea19f8e6122 128
Alix955 12:4c7eaac8ceef 129 m_oNet.Network_Queue.call(&m_oNet, &Network::update_Time, current_time); //sends the whole time and date string to networking to be displayed
Alix955 9:f5eae5211225 130
Alix955 12:4c7eaac8ceef 131 time_and_date Timemsg; // Define instance of message structure, used by serial communications
Alix955 12:4c7eaac8ceef 132 Timemsg.day = day;
Alix955 10:eea19f8e6122 133 Timemsg.month = month;
Alix955 10:eea19f8e6122 134 Timemsg.date = date;
Alix955 10:eea19f8e6122 135 Timemsg.time = time;
Alix955 10:eea19f8e6122 136 Timemsg.year = year;
Alix955 10:eea19f8e6122 137 Timemsg.current_time = current_time;
Alix955 10:eea19f8e6122 138
Alix955 10:eea19f8e6122 139 flip = 3; //will tell the user that the time has been updated on next FLIP instance with the LCD
Alix955 9:f5eae5211225 140
Alix955 12:4c7eaac8ceef 141 // return Timemsg; //swap this for a function that sends the structure to ollie?
Alix955 11:42b0c567cc8c 142
Alix955 9:f5eae5211225 143 }
Alix955 9:f5eae5211225 144
Alix955 9:f5eae5211225 145
Alix955 9:f5eae5211225 146 }; //end of LCD class
Alix955 9:f5eae5211225 147
Alix955 9:f5eae5211225 148
Alix955 9:f5eae5211225 149
O_Thom 6:b7f6e0c0f646 150 // Define the member object for the LCD
O_Thom 5:f87129ac8bf3 151 LCD_Data m_oDisplay;
O_Thom 6:b7f6e0c0f646 152 #endif