iot-firmware/src/user_code.cpp

295 lines
7.2 KiB
C++
Raw Normal View History

2023-09-29 16:18:21 +00:00
#include <user_code.hpp>
2023-09-29 17:56:25 +00:00
2023-10-02 11:45:59 +00:00
// AC Control Locking
char AC_LOCK_TOPIC[75];
2023-10-02 19:01:55 +00:00
char AC_LOCK_REPORT_TOPIC[75];
2023-10-02 11:45:59 +00:00
#define AC_LOCK_ADDRESS 15002
bool ac_lock = false;
2023-09-30 11:09:03 +00:00
// Display Componets
2023-10-02 19:01:55 +00:00
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");
2023-10-02 09:04:44 +00:00
2023-09-30 11:09:03 +00:00
// List of Component ID Message to listen to
NexTouch *nex_listen_list[] =
{
2023-10-02 09:04:44 +00:00
&lt_bt,
&fan_bt,
&puri_bt,
&up_bt,
&down_bt,
&mode_off_btn,
&mode_fan_btn,
&mode_cool_btn,
&fan_auto_btn,
&fan_1_btn,
&fan_2_btn,
&fan_3_btn,
2023-09-30 11:09:03 +00:00
NULL};
2023-09-30 09:29:16 +00:00
2023-09-29 17:06:49 +00:00
/*
This Code will run right after ESPMega PRO's
Peripheral Initialization Routine
*/
2023-09-29 17:37:31 +00:00
void user_pre_init()
{
2023-09-30 09:29:16 +00:00
nexInit();
2023-09-29 16:18:21 +00:00
}
2023-09-29 17:06:49 +00:00
/*
This code will run after every component is initialized
*/
2023-09-29 17:37:31 +00:00
void user_init()
{
2023-10-02 19:01:55 +00:00
elcd.print("page dashboard");
elcd_sendstop();
2023-10-02 11:45:59 +00:00
memcpy(AC_LOCK_TOPIC, MQTT_BASE_TOPIC, 20);
2023-10-02 19:01:55 +00:00
strcat(AC_LOCK_TOPIC, "/ac/lock/set");
memcpy(AC_LOCK_REPORT_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_LOCK_REPORT_TOPIC, "/ac/lock");
2023-10-02 11:45:59 +00:00
ac_lock = ESPMega_FRAM.read8(AC_LOCK_ADDRESS);
ac_lock = ac_lock > 1 ? 0 : ac_lock;
2023-10-02 19:01:55 +00:00
elcd.print("dashboard.pic=");
elcd.print(ac_lock ? "2" : "1");
elcd_sendstop();
2023-10-02 11:45:59 +00:00
mqtt.subscribe(AC_LOCK_TOPIC, ac_lock_callback);
2023-10-02 09:04:44 +00:00
lt_bt.attachPop(lt_btn_cb, &lt_bt);
fan_bt.attachPop(fan_btn_cb, &fan_bt);
2023-10-02 11:00:01 +00:00
puri_bt.attachPop(puri_btn_cb, &puri_bt);
2023-10-02 16:19:28 +00:00
up_bt.attachPop(temp_up_btn_cb, &up_bt);
down_bt.attachPop(temp_down_btn_cb, &down_bt);
mode_off_btn.attachPop(mode_off_btn_cb, &mode_off_btn);
mode_fan_btn.attachPop(mode_fan_btn_cb, &mode_fan_btn);
mode_cool_btn.attachPop(mode_cool_btn_cb, &mode_cool_btn);
fan_auto_btn.attachPop(fan_auto_btn_cb, &fan_auto_btn);
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);
2023-10-02 19:01:55 +00:00
ac_update_lcd();
pwm_update_lcd();
timer_tick_callback();
2023-09-29 16:18:21 +00:00
}
2023-09-29 17:06:49 +00:00
/*
This code will run once every event loop
*/
2023-09-29 17:37:31 +00:00
void user_loop()
{
2023-09-30 09:29:16 +00:00
nexLoop(nex_listen_list);
2023-09-29 16:18:21 +00:00
}
2023-09-29 17:06:49 +00:00
/*
This code will run when an input pin changed state
*/
2023-09-29 17:37:31 +00:00
void virtual_interrupt_user_callback(int pin, int state)
{
2023-10-02 16:19:28 +00:00
if (pin >= 0 & pin <= 7)
{
2023-10-02 11:45:59 +00:00
pwm_toggle(pin);
}
2023-09-29 16:18:21 +00:00
}
2023-09-29 17:06:49 +00:00
2023-09-30 09:29:16 +00:00
void pwm_changed_user_callback(int pin)
{
2023-10-02 19:01:55 +00:00
pwm_update_lcd();
2023-09-30 08:42:44 +00:00
}
2023-09-29 17:06:49 +00:00
/*
2023-09-29 17:37:31 +00:00
This code will run every 15 seconds
2023-09-29 17:06:49 +00:00
*/
2023-09-29 17:37:31 +00:00
void timer_tick_callback()
{
2023-10-02 11:00:01 +00:00
rtctime_t time = ESPMega_getTime();
2023-10-02 19:01:55 +00:00
elcd.printf("clock.txt=\"%02d:%02d\"", time.hours, time.minutes);
elcd_sendstop();
2023-09-29 16:18:21 +00:00
}
2023-09-30 09:29:16 +00:00
2023-10-02 11:00:01 +00:00
void lt_btn_cb(void *comp)
{
cud_light_toggle();
2023-10-02 09:04:44 +00:00
}
2023-10-02 11:00:01 +00:00
void fan_btn_cb(void *comp)
{
cud_fan_toggle();
2023-10-02 09:04:44 +00:00
}
2023-10-02 11:00:01 +00:00
void puri_btn_cb(void *comp)
{
pwm_toggle(AIR_PURIFIER_PIN);
2023-10-02 09:04:44 +00:00
}
2023-10-02 16:19:28 +00:00
void temp_up_btn_cb(void *comp)
{
2023-10-02 19:01:55 +00:00
Serial.println("UPCB");
2023-10-02 16:19:28 +00:00
if (!ac_lock)
{
ac_set_state(ac_get_mode(), ac_get_temperature() + 1, ac_get_fan_speed());
}
2023-10-02 19:01:55 +00:00
Serial.println("UPCBEND");
2023-10-02 16:19:28 +00:00
}
void temp_down_btn_cb(void *comp)
2023-10-02 11:00:01 +00:00
{
2023-10-02 19:01:55 +00:00
Serial.println("DOWNCB");
2023-10-02 11:45:59 +00:00
if (!ac_lock)
{
2023-10-02 16:19:28 +00:00
ac_set_state(ac_get_mode(), ac_get_temperature() - 1, ac_get_fan_speed());
}
2023-10-02 19:01:55 +00:00
Serial.println("DOWNCBEND");
2023-10-02 16:19:28 +00:00
}
void mode_cool_btn_cb(void *comp)
{
if (!ac_lock)
ac_set_state(1, ac_get_temperature(), ac_get_fan_speed());
}
void mode_fan_btn_cb(void *comp)
{
if (!ac_lock)
ac_set_state(2, ac_get_temperature(), ac_get_fan_speed());
}
void mode_off_btn_cb(void *comp)
{
if (!ac_lock)
ac_set_state(0, ac_get_temperature(), ac_get_fan_speed());
}
void fan_auto_btn_cb(void *comp)
{
if (!ac_lock)
{
ac_set_state(ac_get_mode(), ac_get_temperature(), 0);
}
}
void fan_1_btn_cb(void *comp)
{
if (!ac_lock)
{
ac_set_state(ac_get_mode(), ac_get_temperature(), 3);
2023-10-02 11:45:59 +00:00
}
2023-10-02 09:04:44 +00:00
}
2023-10-02 16:19:28 +00:00
void fan_2_btn_cb(void *comp)
2023-10-02 11:00:01 +00:00
{
2023-10-02 11:45:59 +00:00
if (!ac_lock)
{
2023-10-02 16:19:28 +00:00
ac_set_state(ac_get_mode(), ac_get_temperature(), 2);
2023-10-02 11:45:59 +00:00
}
2023-10-02 11:00:01 +00:00
}
2023-10-02 16:19:28 +00:00
void fan_3_btn_cb(void *comp)
2023-10-02 11:00:01 +00:00
{
2023-10-02 11:45:59 +00:00
if (!ac_lock)
{
2023-10-02 16:19:28 +00:00
ac_set_state(ac_get_mode(), ac_get_temperature(), 1);
2023-10-02 11:45:59 +00:00
}
2023-10-02 09:04:44 +00:00
}
2023-10-02 11:00:01 +00:00
void cud_light_toggle()
{
bool new_state = !cud_light_group_state();
pwm_set_state(LIGHT1_PIN, new_state);
pwm_set_state(LIGHT2_PIN, new_state);
pwm_set_state(LIGHT3_PIN, new_state);
pwm_set_state(LIGHT4_PIN, new_state);
}
bool cud_light_group_state()
{
return pwm_get_state(LIGHT1_PIN) || pwm_get_state(LIGHT2_PIN) || pwm_get_state(LIGHT3_PIN) || pwm_get_state(LIGHT4_PIN);
}
void cud_fan_toggle()
{
bool new_state = !cud_fan_group_state();
pwm_set_state(FAN1_PIN, new_state);
pwm_set_state(FAN2_PIN, new_state);
pwm_set_state(FAN3_PIN, new_state);
}
bool cud_fan_group_state()
{
return pwm_get_state(FAN1_PIN) || pwm_get_state(FAN2_PIN) || pwm_get_state(FAN3_PIN);
2023-10-02 11:45:59 +00:00
}
2023-10-02 19:01:55 +00:00
void ac_changed_user_callback(uint8_t mode, uint8_t temperature, uint8_t fan_speed)
2023-10-02 11:45:59 +00:00
{
2023-10-02 19:01:55 +00:00
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();
2023-10-02 11:45:59 +00:00
}
void ac_lock_callback(String topic, String payload)
{
if (payload.equals("lock"))
{
ac_lock = true;
ESPMega_FRAM.write8(AC_LOCK_ADDRESS, ac_lock);
2023-10-02 19:01:55 +00:00
elcd.print("dashboard.pic=");
elcd.print(ac_lock ? "2" : "1");
elcd_sendstop();
2023-10-02 11:45:59 +00:00
}
else if (payload.equals("unlock"))
{
ac_lock = false;
ESPMega_FRAM.write8(AC_LOCK_ADDRESS, ac_lock);
2023-10-02 19:01:55 +00:00
elcd.print("dashboard.pic=");
elcd.print(ac_lock ? "2" : "1");
elcd_sendstop();
2023-10-02 11:45:59 +00:00
}
2023-10-02 19:01:55 +00:00
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();
2023-09-30 09:29:16 +00:00
}