From 1d775584db088eaa324894a1585f284aa1f20ab7 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 3 Oct 2023 02:01:55 +0700 Subject: [PATCH] cud lcd implementation --- cud_display/cud_display.HMI | Bin 11984925 -> 11999129 bytes .../NexButton.cpp | 5 - src/espmega_iot_core.cpp | 2 +- src/user_code.cpp | 157 +++++++++++------- src/user_code.hpp | 7 +- 5 files changed, 101 insertions(+), 70 deletions(-) diff --git a/cud_display/cud_display.HMI b/cud_display/cud_display.HMI index 0e2033a87fe2516daba328d9fd9b89f5dd55b83d..12353fe4ea65cbbe69fe3ab2834c500884396a63 100644 GIT binary patch delta 2547 zcmeH|U2GIp6vywK{TMB@5zCe?38fe$iVaq)QBkqGEy#yrQL9$8Wfc@eU|I@RXz6PI z*iymL7P+!exAhAOt+nmtjvx(-^u>hu@u1OAqdxc`4=RZdBx>T>-I-3+Zqye;jGNre zne%bax&Qg)PDkvlXJ6llhZpzr0=?L{i7|G%kFg(TdGo}tTH={p?;TQXJ;nGi#pW0x za*h73{#vnk9;do9r*gk-2ozv22xoEj*d1!gfkghjf`1g86FEki>qd~(MG>k#R z7&MGQ!x%J-q3gRZiiAN!_g{&daSP_+R@{c$F%Ng35CH^HgksFc0^Es(ScJt`g1c}x zmf{|iU>WYkeYhW`cmNOLA(Y`^EXN~w6y>PE3WV?&9>+?o!V`EBtFZ=8VJ)7gO{;)E&E^^W5+E!l)>0Z%g(*rvA^;$7PI_OzvC~T#@GqV zKXryT&x+pZezmBSY2F?P%Z8VqBZna)~Jd& z7l9#JYK|O}X)et*MTCZBrug}UoNq<=_u@K9$vs0dsIBIr^@yBoPTi>vXxuHjVzS)I z6CZx6vP5CK3YxvAui%R)lqKdwyILDP0SLNU22LrSFZ}~EyH1z!^LGgRH^uKPzJ5cbW5vf z!{`CUT#yFg0T~pvN@khU`=p19-tD9b?LM1}^272PEh9TIf?mC9ol#L;BlwW?iIY7l z%)<$(xgi4W%Ezn3GDVT>%_>Vf&l1A(msSwsvAS^m#^u|&EoZxgkfD^F(0pJPa1AGJj3x3o8HoqDFl;113rzP+~ U_`iJPSycRgvBf_E7tO`?8`>w8C;$Ke delta 1488 zcmd^7TS!z<6g}tO882xGl}ZtbL4qJXz>mtxd}Z&_N-KMeW?7SFj%n{4Czok8>9yBM zGfR7ET9bJ@C&kLhi0Y%4&_{g>ef3d+9~GT(t{LUJ# zN)y^>lony1MhJN_nv%)+nG*f{j3!RJIr_*$cBH(?D0OM&E{)1iWT-MMGORNEgiF($ z{U>!T{BQRx)wOb6n(ES2m!`Tj)upK}O?6Gn@BbGB?Yj0Q9Mdra(U^%@n2i|BK`i1B zj|3!QE|M@0^RWO6u?UN?1WU0D$wz(Ewli4wSQ2&DkZa2VyNz!4n9 zF}QIYm8gP_YMj7HoWg0G0mE6;pcdy)hx4e%1zbb}8gU7iaRpa#4NYjqb+n)rH*gak z+(H{}qa6nB7<`e#>Q{F3shuS|c!IM~_5DR3{0P}+;w*nc?wWX9AR!$l?j1=;r->_r zhcroJhj=ockO!vbLDo=8Zy$;U+e@h|w zg;UwXZX<|2Z{;OQ5n*lhJcw>%6WlzQI$6q99;tjHth&VrqMgDKEb1r=>*W^lqS9Uioz3)vss(6m*{>VQo%+_Pr1`9G25yB)LFLECn?Ft?5oqr7Tl=GxPNetsY33I;o z`nkB3in@NzC=Fl@-dDbCGb}9c*g%P#JWT|+`7o6ZXRl81F>F#5PmqJi0c=S81S6SNY1T^F3C2zE!M=iG3aa*YT4>f4u;Uy=SDcKiL03f$(>8xz%5`@1s4> RytMBCKIXUe`{;q?{{U~#3~~Sf diff --git a/lib/ITEADLIB_Arduino_Nextion-master/NexButton.cpp b/lib/ITEADLIB_Arduino_Nextion-master/NexButton.cpp index 643f96e..f9b9e66 100644 --- a/lib/ITEADLIB_Arduino_Nextion-master/NexButton.cpp +++ b/lib/ITEADLIB_Arduino_Nextion-master/NexButton.cpp @@ -314,11 +314,6 @@ bool NexButton::Set_background_image_pic(uint32_t number) cmd += ".pic="; cmd += buf; sendCommand(cmd.c_str()); - - cmd = ""; - cmd += "ref "; - cmd += getObjName(); - sendCommand(cmd.c_str()); return recvRetCommandFinished(); } diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index e651816..1820fff 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -49,7 +49,7 @@ char PWM_SET_VALUE_TOPIC[70]; // Infrared Transciever #define IR_RECIEVE_PIN 35 -#define IR_SEND_PIN 17 +#define IR_SEND_PIN 12 #define MARK_EXCESS_MICROS 20 #define RAW_BUFFER_LENGTH 750 diff --git a/src/user_code.cpp b/src/user_code.cpp index 9fda500..c719717 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -2,24 +2,24 @@ // AC Control Locking char AC_LOCK_TOPIC[75]; +char AC_LOCK_REPORT_TOPIC[75]; #define AC_LOCK_ADDRESS 15002 bool ac_lock = false; // Display Componets -NexText clock_txt = NexText(1, 2, "clock"); -NexDSButton lt_bt = NexDSButton(1, 3, "lt_bt"); -NexDSButton fan_bt = NexDSButton(1, 4, "fan_bt"); -NexDSButton puri_bt = NexDSButton(1, 5, "puri_bt"); -NexButton up_bt = NexButton(1, 6, "up_bt"); -NexButton down_bt = NexButton(1, 7, "down_bt"); -NexButton mode_off_btn = NexButton(1, 8, "mode_off_btn"); -NexButton mode_fan_btn = NexButton(1, 9, "mode_fan_btn"); -NexButton mode_cool_btn = NexButton(1, 10, "mode_cool_btn"); -NexButton fan_auto_btn = NexButton(1, 11, "fan_auto_btn"); -NexButton fan_1_btn = NexButton(1, 12, "fan_1_btn"); -NexButton fan_2_btn = NexButton(1, 13, "fan_2_btn"); -NexButton fan_3_btn = NexButton(1, 14, "fan_3_btn"); -NexText temp_txt = NexText(1, 15, "temp_txt"); +NexButton up_bt = NexButton(1, 2, "up_bt"); +NexButton down_bt = NexButton(1, 3, "down_bt"); +NexButton mode_off_btn = NexButton(1, 4, "mode_off_btn"); +NexButton mode_fan_btn = NexButton(1, 5, "mode_fan_btn"); +NexButton mode_cool_btn = NexButton(1, 6, "mode_cool_btn"); +NexButton fan_auto_btn = NexButton(1, 7, "fan_auto_btn"); +NexButton fan_1_btn = NexButton(1, 8, "fan_1_btn"); +NexButton fan_2_btn = NexButton(1, 9, "fan_2_btn"); +NexButton fan_3_btn = NexButton(1, 10, "fan_3_btn"); +NexButton lt_bt = NexButton(1, 11, "lt_bt"); +NexButton fan_bt = NexButton(1, 12, "fan_bt"); +NexButton puri_bt = NexButton(1, 13, "puri_bt"); + // List of Component ID Message to listen to NexTouch *nex_listen_list[] = @@ -52,19 +52,17 @@ This code will run after every component is initialized */ void user_init() { - ESPMega_EXTLCD.print("page dashboard"); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); + elcd.print("page dashboard"); + elcd_sendstop(); memcpy(AC_LOCK_TOPIC, MQTT_BASE_TOPIC, 20); - strcat(AC_LOCK_TOPIC, "/ac/lock"); + strcat(AC_LOCK_TOPIC, "/ac/lock/set"); + memcpy(AC_LOCK_REPORT_TOPIC, MQTT_BASE_TOPIC, 20); + strcat(AC_LOCK_REPORT_TOPIC, "/ac/lock"); ac_lock = ESPMega_FRAM.read8(AC_LOCK_ADDRESS); ac_lock = ac_lock > 1 ? 0 : ac_lock; - ESPMega_EXTLCD.print("dashboard.pic="); - ESPMega_EXTLCD.print(ac_lock ? "2" : "1"); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); + elcd.print("dashboard.pic="); + elcd.print(ac_lock ? "2" : "1"); + elcd_sendstop(); mqtt.subscribe(AC_LOCK_TOPIC, ac_lock_callback); lt_bt.attachPop(lt_btn_cb, <_bt); fan_bt.attachPop(fan_btn_cb, &fan_bt); @@ -78,6 +76,9 @@ void user_init() fan_1_btn.attachPop(fan_1_btn_cb, &fan_1_btn); fan_2_btn.attachPop(fan_2_btn_cb, &fan_2_btn); fan_3_btn.attachPop(fan_3_btn_cb, &fan_3_btn); + ac_update_lcd(); + pwm_update_lcd(); + timer_tick_callback(); } /* @@ -101,18 +102,7 @@ void virtual_interrupt_user_callback(int pin, int state) void pwm_changed_user_callback(int pin) { - if ((pin == LIGHT1_PIN) || (pin == LIGHT2_PIN) || (pin == LIGHT3_PIN) || (pin = LIGHT4_PIN)) - { - lt_bt.setValue(cud_light_group_state()); - } - else if (pin == FAN1_PIN || pin == FAN2_PIN || pin == FAN3_PIN) - { - fan_bt.setValue(cud_fan_group_state()); - } - else if (pin == AIR_PURIFIER_PIN) - { - puri_bt.setValue(pwm_get_state(pin)); - } + pwm_update_lcd(); } /* @@ -120,10 +110,9 @@ This code will run every 15 seconds */ void timer_tick_callback() { - char time_buffer[15]; rtctime_t time = ESPMega_getTime(); - sprintf(time_buffer, "%02d:%02d", time.hours, time.minutes); - clock_txt.setText(time_buffer); + elcd.printf("clock.txt=\"%02d:%02d\"", time.hours, time.minutes); + elcd_sendstop(); } void lt_btn_cb(void *comp) @@ -140,19 +129,21 @@ void puri_btn_cb(void *comp) } void temp_up_btn_cb(void *comp) { + Serial.println("UPCB"); if (!ac_lock) { - uint8_t new_temp = ac_get_temperature(); ac_set_state(ac_get_mode(), ac_get_temperature() + 1, ac_get_fan_speed()); } + Serial.println("UPCBEND"); } void temp_down_btn_cb(void *comp) { + Serial.println("DOWNCB"); if (!ac_lock) { - uint8_t new_temp = ac_get_temperature(); ac_set_state(ac_get_mode(), ac_get_temperature() - 1, ac_get_fan_speed()); } + Serial.println("DOWNCBEND"); } void mode_cool_btn_cb(void *comp) { @@ -223,18 +214,41 @@ bool cud_fan_group_state() return pwm_get_state(FAN1_PIN) || pwm_get_state(FAN2_PIN) || pwm_get_state(FAN3_PIN); } -void ac_changed_user_callback(int mode, int temperature, int fan_speed) +void ac_changed_user_callback(uint8_t mode, uint8_t temperature, uint8_t fan_speed) { - char temp_text[3]; - sprintf(temp_text, "%dC", temperature); - temp_txt.setText(temp_text); - mode_off_btn.Set_background_image_pic(mode == 0 ? 14 : 13); - mode_fan_btn.Set_background_image_pic(mode == 2 ? 16 : 15); - mode_cool_btn.Set_background_image_pic(mode == 1 ? 18 : 17); - fan_auto_btn.Set_background_image_pic(fan_speed == 0 ? 20 : 19); - fan_1_btn.Set_background_image_pic(fan_speed == 3 ? 22 : 21); - fan_2_btn.Set_background_image_pic(fan_speed == 2 ? 24 : 23); - fan_3_btn.Set_background_image_pic(fan_speed == 1 ? 26 : 25); + ac_update_lcd(); +} + +void ac_update_lcd() { + uint8_t temperature = ac_get_temperature(); + uint8_t mode = ac_get_mode(); + uint8_t fan_speed = ac_get_fan_speed(); + elcd_sendstop(); + elcd.print("temp_txt.txt=\""); + elcd.print(temperature); + elcd.print("C\""); + elcd_sendstop(); + elcd.print("mode_off_btn.pic="); + elcd.print(mode == 0 ? 14 : 13); + elcd_sendstop(); + elcd.print("mode_fan_btn.pic="); + elcd.print(mode == 2 ? 16 : 15); + elcd_sendstop(); + elcd.print("mode_cool_btn.pic="); + elcd.print(mode == 1 ? 18 : 17); + elcd_sendstop(); + elcd.print("fan_auto_btn.pic="); + elcd.print(fan_speed == 0 ? 20 : 19); + elcd_sendstop(); + elcd.print("fan_1_btn.pic="); + elcd.print(fan_speed == 3 ? 22 : 21); + elcd_sendstop(); + elcd.print("fan_2_btn.pic="); + elcd.print(fan_speed == 2 ? 24 : 23); + elcd_sendstop(); + elcd.print("fan_3_btn.pic="); + elcd.print(fan_speed == 1 ? 26 : 25); + elcd_sendstop(); } void ac_lock_callback(String topic, String payload) @@ -243,20 +257,39 @@ void ac_lock_callback(String topic, String payload) { ac_lock = true; ESPMega_FRAM.write8(AC_LOCK_ADDRESS, ac_lock); - ESPMega_EXTLCD.print("dashboard.pic="); - ESPMega_EXTLCD.print(ac_lock ? "2" : "1"); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); + elcd.print("dashboard.pic="); + elcd.print(ac_lock ? "2" : "1"); + elcd_sendstop(); } else if (payload.equals("unlock")) { ac_lock = false; ESPMega_FRAM.write8(AC_LOCK_ADDRESS, ac_lock); - ESPMega_EXTLCD.print("dashboard.pic="); - ESPMega_EXTLCD.print(ac_lock ? "2" : "1"); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); - ESPMega_EXTLCD.write(0xFF); + elcd.print("dashboard.pic="); + elcd.print(ac_lock ? "2" : "1"); + elcd_sendstop(); } + ac_lock_report(); +} + +void ac_lock_report() { + mqtt_client.publish(AC_LOCK_REPORT_TOPIC,ac_lock?"lock":"unlock"); +} + +void elcd_sendstop() { + ESPMega_EXTLCD.write(0xFF); + ESPMega_EXTLCD.write(0xFF); + ESPMega_EXTLCD.write(0xFF); +} + +void pwm_update_lcd() { + elcd.print("lt_bt.pic="); + elcd.print(cud_light_group_state()?4:3); + elcd_sendstop(); + elcd.print("fan_bt.pic="); + elcd.print(cud_fan_group_state()?6:5); + elcd_sendstop(); + elcd.print("puri_bt.pic="); + elcd.print(pwm_get_state(AIR_PURIFIER_PIN)?8:7); + elcd_sendstop(); } \ No newline at end of file diff --git a/src/user_code.hpp b/src/user_code.hpp index 1647c26..db96e76 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -39,6 +39,7 @@ I0-I2: Fan Switch #ifdef ENABLE_EXTERNAL_LCD #define ESPMega_EXTLCD Serial2 +#define elcd ESPMega_EXTLCD #endif // User Defined Functions @@ -47,7 +48,7 @@ void user_init(); void user_loop(); void virtual_interrupt_user_callback(int pin, int state); void pwm_changed_user_callback(int pin); -void ac_changed_user_callback(int mode, int temperature, int fan_speed); +void ac_changed_user_callback(uint8_t mode, uint8_t temperature, uint8_t fan_speed); void timer_tick_callback(); void lt_btn_cb(void *comp); void fan_btn_cb(void *comp); @@ -66,7 +67,9 @@ bool cud_light_group_state(); void cud_fan_toggle(); bool cud_fan_group_state(); void ac_lock_callback(String topic, String payload); - +void elcd_sendstop(); +void ac_update_lcd(); +void pwm_update_lcd(); // ESPMega IoT Core Build-in Functions extern void pwm_set_state(int id, int state);