Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 00004 # define TEMPERATURE_TIMEOUT 10*60+5 //s 00005 00006 // https://github.com/andresv/OOThermometer 00007 // mbed shows data from serial on LCD screen 00008 00009 TextLCD lcd(p26, p25, p24, p23, p22, p21, TextLCD::LCD16x2B); // rs, e, d4-d7 00010 DigitalOut led1(LED1); 00011 00012 Serial pc(USBTX, USBRX); 00013 Serial base(p9, p10); 00014 Timeout syncronizator; 00015 Timeout timeouter; 00016 00017 Timer in_timer; 00018 Timer out_timer; 00019 00020 enum { 00021 SERIAL_ADDR_NODE = 0x01, 00022 SERIAL_ADDR_BASE = 0x00, 00023 00024 MSG_THERMOMETER = 0x01, 00025 MSG_REQ_THERMOMETER = 0x10 00026 }; 00027 00028 typedef struct { 00029 unsigned short battery; 00030 int light; 00031 int temp; 00032 char addr; 00033 char header; 00034 } __attribute__((packed)) thermometer_msg_t; 00035 00036 typedef struct { 00037 char header; 00038 } __attribute__((packed)) req_thermometer_msg_t; 00039 00040 char rx_buffer[50]; 00041 char* inserter = rx_buffer + sizeof(rx_buffer); 00042 unsigned int bytes_received = 0; 00043 00044 float in_temp = 0.0; 00045 float out_temp = 0.0; 00046 00047 void packet_received(); 00048 void check_timeout(); 00049 void update_out_temp(); 00050 void update_in_temp(); 00051 void request_temp(); 00052 00053 //---------------------------------------------------------------------------- 00054 // Byte received from UART 00055 //---------------------------------------------------------------------------- 00056 void receive() { 00057 led1 = !led1; 00058 if (base.readable()) { 00059 // if we have not got any data for 50 ms display data 00060 // because it was probably packet end 00061 // here it is like watchdog kick: 00062 syncronizator.attach(&packet_received, 0.05); 00063 00064 *inserter = base.getc(); 00065 inserter--; 00066 bytes_received++; 00067 00068 if (inserter == rx_buffer) 00069 inserter = rx_buffer + sizeof(rx_buffer); 00070 } 00071 } 00072 00073 //---------------------------------------------------------------------------- 00074 // Probabaly packet received from UART 00075 //---------------------------------------------------------------------------- 00076 void packet_received() { 00077 thermometer_msg_t* msg = (thermometer_msg_t*)(inserter+1); 00078 unsigned int i = 0; 00079 00080 for (i=0; i<bytes_received; i++) 00081 pc.printf("%02X\n", ((char*)msg)[i]); 00082 pc.printf("\n"); 00083 00084 if (msg->header == MSG_THERMOMETER && bytes_received == sizeof(thermometer_msg_t)) { 00085 if (msg->addr == SERIAL_ADDR_BASE) { 00086 in_temp = ((float)msg->temp)/10; 00087 in_timer.reset(); 00088 update_in_temp(); 00089 } 00090 else if (msg->addr == SERIAL_ADDR_NODE) { 00091 out_temp = ((float)msg->temp)/10; 00092 out_timer.reset(); 00093 update_out_temp(); 00094 } 00095 } 00096 00097 bytes_received = 0; 00098 inserter = rx_buffer + sizeof(rx_buffer); 00099 } 00100 00101 //---------------------------------------------------------------------------- 00102 // Check whether there is timeout condition 00103 //---------------------------------------------------------------------------- 00104 void check_timeout() { 00105 pc.printf("timeout!\n"); 00106 timeouter.attach(&check_timeout, TEMPERATURE_TIMEOUT+1); 00107 update_in_temp(); 00108 update_out_temp(); 00109 } 00110 00111 void update_in_temp() { 00112 lcd.locate(2, 1); 00113 pc.printf("in timer %.1f\n", in_timer.read()); 00114 if (in_timer.read() < TEMPERATURE_TIMEOUT) { 00115 lcd.printf("%.1f\n", in_temp); 00116 } 00117 else { 00118 lcd.printf("XX.X\n"); 00119 } 00120 } 00121 00122 void update_out_temp() { 00123 lcd.locate(10, 1); 00124 pc.printf("out timer %.1f\n", out_timer.read()); 00125 if (out_timer.read() < TEMPERATURE_TIMEOUT) { 00126 lcd.printf("%.1f\n", out_temp); 00127 } 00128 else { 00129 lcd.printf("XX.X\n"); 00130 } 00131 } 00132 00133 //---------------------------------------------------------------------------- 00134 // Request temperature from base and node 00135 //---------------------------------------------------------------------------- 00136 void request_temp() { 00137 req_thermometer_msg_t msg; 00138 unsigned int i; 00139 00140 msg.header = MSG_REQ_THERMOMETER; 00141 00142 for (i=0; i<sizeof(req_thermometer_msg_t); i++) 00143 base.putc(((char*)&msg)[i]); 00144 } 00145 00146 //---------------------------------------------------------------------------- 00147 // Main 00148 //---------------------------------------------------------------------------- 00149 int main() { 00150 base.baud(115200); 00151 pc.baud(115200); 00152 00153 base.attach(&receive, Serial::RxIrq); 00154 lcd.printf(" IN OUT\n"); 00155 00156 in_timer.start(); 00157 out_timer.start(); 00158 timeouter.attach(&check_timeout, TEMPERATURE_TIMEOUT+1); 00159 00160 request_temp(); 00161 while(1) { 00162 } 00163 }
Generated on Mon Jul 18 2022 14:01:15 by
1.7.2