cud_iot_cpp

This commit is contained in:
Siwat Sirichai 2023-10-02 18:45:59 +07:00
parent 39a08fadce
commit 1ea278834a
2 changed files with 88 additions and 1 deletions

View File

@ -1,5 +1,10 @@
#include <user_code.hpp>
// AC Control Locking
char AC_LOCK_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");
@ -14,6 +19,8 @@ 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 temperature_txt = NexText(1, 15, "temperature");
NexPage dashboard_page = NexPage(1, 0, "dashboard");
// List of Component ID Message to listen to
NexTouch *nex_listen_list[] =
@ -50,6 +57,16 @@ void user_init()
ESPMega_EXTLCD.write(0xFF);
ESPMega_EXTLCD.write(0xFF);
ESPMega_EXTLCD.write(0xFF);
memcpy(AC_LOCK_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_LOCK_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);
mqtt.subscribe(AC_LOCK_TOPIC, ac_lock_callback);
lt_bt.attachPop(lt_btn_cb, &lt_bt);
fan_bt.attachPop(fan_btn_cb, &fan_bt);
puri_bt.attachPop(puri_btn_cb, &puri_bt);
@ -77,6 +94,9 @@ This code will run when an input pin changed state
*/
void virtual_interrupt_user_callback(int pin, int state)
{
if (pin>=0&pin<=7){
pwm_toggle(pin);
}
}
void pwm_changed_user_callback(int pin)
@ -120,12 +140,40 @@ void puri_btn_cb(void *comp)
}
void ac_temp_adj_btn_cb(void *comp)
{
if (!ac_lock)
{
uint8_t new_temp = ac_get_temperature();
if (comp == &up_bt)
ac_set_state(ac_get_mode(), ac_get_temperature() + 1, ac_get_fan_speed());
else if (comp == &down_bt)
ac_set_state(ac_get_mode(), ac_get_temperature() - 1, ac_get_fan_speed());
}
}
void ac_mode_adj_btn_cb(void *comp)
{
if (!ac_lock)
{
if (comp == &mode_off_btn)
ac_set_state(0, ac_get_temperature(), ac_get_fan_speed());
else if (comp == &mode_fan_btn)
ac_set_state(1, ac_get_temperature(), ac_get_fan_speed());
else if (comp == &mode_cool_btn)
ac_set_state(2, ac_get_temperature(), ac_get_fan_speed());
}
}
void ac_fan_adj_btn_cb(void *comp)
{
if (!ac_lock)
{
if (comp == &fan_auto_btn)
ac_set_state(ac_get_mode(), ac_get_temperature(), 0);
else if (comp == &fan_1_btn)
ac_set_state(ac_get_mode(), ac_get_temperature(), 1);
else if (comp == &fan_2_btn)
ac_set_state(ac_get_mode(), ac_get_temperature(), 2);
else if (comp == &fan_3_btn)
ac_set_state(ac_get_mode(), ac_get_temperature(), 3);
}
}
void cud_light_toggle()
@ -151,3 +199,40 @@ 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)
{
char temp_text[3] = {(temperature - temperature % 10) + '0', (temperature % 10) + '0', 'C'};
temperature_txt.setText(temp_text);
mode_off_btn.Set_background_image_pic(mode == 0 ? 1 : 0);
mode_fan_btn.Set_background_image_pic(mode == 1 ? 1 : 0);
mode_cool_btn.Set_background_image_pic(mode == 2 ? 1 : 0);
fan_auto_btn.Set_background_image_pic(fan_speed == 0 ? 1 : 0);
fan_1_btn.Set_background_image_pic(fan_speed == 1 ? 1 : 0);
fan_2_btn.Set_background_image_pic(fan_speed == 2 ? 1 : 0);
fan_3_btn.Set_background_image_pic(fan_speed == 3 ? 1 : 0);
}
void ac_lock_callback(String topic, String payload)
{
if (payload.equals("lock"))
{
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);
}
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);
}
}

View File

@ -59,6 +59,7 @@ void cud_light_toggle();
bool cud_light_group_state();
void cud_fan_toggle();
bool cud_fan_group_state();
void ac_lock_callback(String topic, String payload);
// ESPMega IoT Core Build-in Functions
@ -78,3 +79,4 @@ extern uint8_t ac_get_fan_speed();
extern bool standalone;
extern PubSubClient mqtt_client;
extern PubSubClientTools mqtt;
extern char MQTT_BASE_TOPIC[];