From 8c01ccb18a0f80d2e9c3f4dfda22330746a94739 Mon Sep 17 00:00:00 2001 From: reaw55 <58457329+reaw55@users.noreply.github.com> Date: Tue, 26 Dec 2023 22:37:11 +0700 Subject: [PATCH] add ac control --- src/user_code.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++-- src/user_code.hpp | 13 ++++++- 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/src/user_code.cpp b/src/user_code.cpp index 8bff375..655850f 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -15,7 +15,12 @@ NexButton row3_lv1 = NexButton(1,10,"row3_lv1"); NexButton row3_lv2 = NexButton(1,7,"row3_lv2"); NexButton row4_lv1 = NexButton(1,9,"row4_lv1"); NexButton row4_lv2 = NexButton(1,8,"row4_lv2"); - +NexButton temp_plus = NexButton(1,18,"temp_plus"); +NexButton temp_neg = NexButton(1,17,"temp_neg"); +NexButton fan = NexButton(1,16,"fan"); +NexButton mode = NexButton(1,15,"mode"); +NexButton ac_toggle = NexButton(1,1,"ac_toggle"); +NexText cur_temp = NexText(1,19,"cur_temp"); // List of Component ID Message to listen to NexTouch *nex_listen_list[] = { @@ -32,6 +37,11 @@ NexTouch *nex_listen_list[] = &row3_lv2, &row4_lv1, &row4_lv2, + &temp_plus, + &temp_neg, + &fan, + &mode, + &ac_toggle, NULL}; bool row_is_on(int light1_pin, int light2_pin) { @@ -91,7 +101,51 @@ void row4_lv2_pop_callback(void *ptr) pwm_toggle(ROW4_LIGHT2); } +void increase_temp() { + uint8_t current_temp = ac_get_temperature(); + ac_set_state(ac_get_mode(), current_temp+1, ac_get_fan_speed()); +} +void decrease_temp() { + uint8_t current_temp = ac_get_temperature(); + ac_set_state(ac_get_mode(), current_temp-1, ac_get_fan_speed()); +} + +void temp_plus_pop_callback(void *ptr) +{ + increase_temp(); +} + +void temp_neg_pop_callback(void *ptr) +{ + decrease_temp(); +} + +void fan_pop_callback(void *ptr) +{ + uint8_t current_fan_speed = ac_get_fan_speed(); + uint8_t new_fan_speed = (current_fan_speed + 1) % 4; // Loop back to 0 when reaching 3 + ac_set_state(ac_get_mode(), ac_get_temperature(), new_fan_speed); +} + +void mode_pop_callback(void *ptr) +{ + uint8_t current_mode = ac_get_mode(); + uint8_t new_mode = (current_mode + 1) % 3; // Loop back to 0 when reaching 2 + ac_set_state(new_mode, ac_get_temperature(), ac_get_fan_speed()); +} + +void ac_toggle_pop_callback(void *ptr) +{ + //this function should set the state of the AC to toggle between off (mode 0) and the same state as before store in variable acmode + if (ac_get_mode() == 0) { + ac_set_state(acmode, ac_get_temperature(), ac_get_fan_speed()); + } else { + acmode = ac_get_mode(); + ac_set_state(0, ac_get_temperature(), ac_get_fan_speed()); + } + +} void row1_master_pop_callback(void *ptr) { @@ -150,8 +204,12 @@ void user_init() row2_master.attachPop(row2_master_pop_callback, &row2_master); row3_master.attachPop(row3_master_pop_callback, &row3_master); row4_master.attachPop(row4_master_pop_callback, &row4_master); + temp_plus.attachPop(temp_plus_pop_callback, &temp_plus); + temp_neg.attachPop(temp_neg_pop_callback, &temp_neg); + fan.attachPop(fan_pop_callback, &fan); + mode.attachPop(mode_pop_callback, &mode); + ac_toggle.attachPop(ac_toggle_pop_callback, &ac_toggle); } - /* This code will run once every event loop */ @@ -218,6 +276,34 @@ void update_toggle_button() { light_toggle.Set_background_image_pic(current_state?3:4); } +void update_lcd_ac() { + //update the current temperature + char temp_str[3]; + sprintf(temp_str, "%d", ac_get_temperature()); + cur_temp.setText(temp_str); + //update the fan speed auto is 18, high 19, medium 21, low 20 + // Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low + uint8_t fan_speed = ac_get_fan_speed(); + if(fan_speed == 0) { + fan.Set_background_image_pic(18); + } else if(fan_speed == 1) { + fan.Set_background_image_pic(19); + } else if(fan_speed == 2) { + fan.Set_background_image_pic(21); + } else if(fan_speed == 3) { + fan.Set_background_image_pic(20); + } else { + fan.Set_background_image_pic(18); + } + //update the mode cool is 22, fan mode is 24, for off the toggle button act as indicator + //Mode 0: Off, 1: Cool, 2: Fan + uint8_t mode_state = ac_get_mode(); + mode.Set_background_image_pic(mode_state==1?22:24); + //update the ac toggle button + ac_toggle.Set_background_image_pic(ac_get_mode()==0?1:2); +} + + void pwm_changed_user_callback(int pin) { diff --git a/src/user_code.hpp b/src/user_code.hpp index 17be1b1..34467db 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -60,10 +60,13 @@ // Analog Module Configuration #define ANALOG_REPORTING_INTERVAL 500 +// User Defined Variables +uint8_t acmode = 0; + // User Defined Functions #define elcd ESPMega_EXTLCD void elcd_send_stop_bit(); -bool row1_is_on(); +bool row_is_on(int light1_pin, int light2_pin); void update_lcd_row1(); void update_lcd_row2(); void update_lcd_row3(); @@ -82,6 +85,14 @@ void row1_master_pop_callback(void *ptr); void row2_master_pop_callback(void *ptr); void row3_master_pop_callback(void *ptr); void row4_master_pop_callback(void *ptr); +void increase_temp(); +void decrease_temp(); +void temp_plus_pop_callback(void *ptr); +void temp_neg_pop_callback(void *ptr); +void fan_pop_callback(void *ptr); +void mode_pop_callback(void *ptr); +void ac_toggle_pop_callback(void *ptr); + // User Defined IoT Core Callback Functions (Required) void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length);