diff --git a/src/user_code.cpp b/src/user_code.cpp index 161220a..a50675b 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -3,25 +3,25 @@ uint8_t user_acmode = 1; // Display Componets // Link with Dual state button object with id 2 on page 1 named bt0 -NexButton light_toggle = NexButton(1,2,"light_toggle"); -NexButton row1_master = NexButton(1,11,"row1_master"); -NexButton row2_master = NexButton(1,12,"row2_master"); -NexButton row3_master = NexButton(1,14,"row3_master"); -NexButton row4_master = NexButton(1,13,"row4_master"); -NexButton row1_lv1 = NexButton(1,3,"row1_lv1"); -NexButton row1_lv2 = NexButton(1,4,"row1_lv2"); -NexButton row2_lv1 = NexButton(1,5,"row2_lv1"); -NexButton row2_lv2 = NexButton(1,6,"row2_lv2"); -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"); +NexButton light_toggle = NexButton(1, 2, "light_toggle"); +NexButton row1_master = NexButton(1, 11, "row1_master"); +NexButton row2_master = NexButton(1, 12, "row2_master"); +NexButton row3_master = NexButton(1, 14, "row3_master"); +NexButton row4_master = NexButton(1, 13, "row4_master"); +NexButton row1_lv1 = NexButton(1, 3, "row1_lv1"); +NexButton row1_lv2 = NexButton(1, 4, "row1_lv2"); +NexButton row2_lv1 = NexButton(1, 5, "row2_lv1"); +NexButton row2_lv2 = NexButton(1, 6, "row2_lv2"); +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[] = { @@ -45,17 +45,19 @@ NexTouch *nex_listen_list[] = &ac_toggle, NULL}; -bool row_is_on(int light1_pin, int light2_pin) { +bool row_is_on(int light1_pin, int light2_pin) +{ return pwm_get_state(light1_pin) && pwm_get_state(light2_pin); } -bool row_is_on_or(int light1_pin, int light2_pin) { +bool row_is_on_or(int light1_pin, int light2_pin) +{ return pwm_get_state(light1_pin) || pwm_get_state(light2_pin); } void light_toggle_pop_callback(void *ptr) { - bool new_state = !row_is_on(ROW1_LIGHT1,ROW1_LIGHT2) && !row_is_on(ROW2_LIGHT1,ROW2_LIGHT2) && !row_is_on(ROW3_LIGHT1,ROW3_LIGHT2) && !row_is_on(ROW4_LIGHT1,ROW4_LIGHT2); + bool new_state = !row_is_on(ROW1_LIGHT1, ROW1_LIGHT2) && !row_is_on(ROW2_LIGHT1, ROW2_LIGHT2) && !row_is_on(ROW3_LIGHT1, ROW3_LIGHT2) && !row_is_on(ROW4_LIGHT1, ROW4_LIGHT2); pwm_set_state(ROW1_LIGHT1, new_state); pwm_set_state(ROW1_LIGHT2, new_state); pwm_set_state(ROW2_LIGHT1, new_state); @@ -106,14 +108,16 @@ void row4_lv2_pop_callback(void *ptr) pwm_toggle(ROW4_LIGHT2); } -void increase_temp() { +void increase_temp() +{ uint8_t current_temp = ac_get_temperature(); - ac_set_state(ac_get_mode(), current_temp+1, ac_get_fan_speed()); + ac_set_state(ac_get_mode(), current_temp + 1, ac_get_fan_speed()); } -void decrease_temp() { +void decrease_temp() +{ uint8_t current_temp = ac_get_temperature(); - ac_set_state(ac_get_mode(), current_temp-1, ac_get_fan_speed()); + ac_set_state(ac_get_mode(), current_temp - 1, ac_get_fan_speed()); } void temp_plus_pop_callback(void *ptr) @@ -135,24 +139,33 @@ void fan_pop_callback(void *ptr) void mode_pop_callback(void *ptr) { - uint8_t current_mode = ac_get_mode(); - //uint8_t new_mode = (current_mode + 1) % 3; // Loop back to 1 when reaching 2 - if(current_mode == 1) { + // uint8_t current_mode = ac_get_mode(); + user_acmode = ac_get_mode(); + // uint8_t new_mode = (current_mode + 1) % 3; // Loop back to 1 when reaching 2 + if (user_acmode == 1) + { + user_acmode = 2; ac_set_state(2, ac_get_temperature(), ac_get_fan_speed()); - } else if(current_mode == 2) { + } + else if (user_acmode == 2) + { + user_acmode = 1; ac_set_state(1, ac_get_temperature(), ac_get_fan_speed()); - } - - //ac_set_state(new_mode, ac_get_temperature(), ac_get_fan_speed()); + } + + // 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 - //val user_acmode already initialized to 1 (cool mode) - if (ac_get_mode() == 0) { + // 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 + // val user_acmode already initialized to 1 (cool mode) + if (ac_get_mode() == 0) + { ac_set_state(user_acmode, ac_get_temperature(), ac_get_fan_speed()); - } else { + } + else + { user_acmode = ac_get_mode(); ac_set_state(0, ac_get_temperature(), ac_get_fan_speed()); } @@ -167,28 +180,28 @@ void ac_toggle_pop_callback(void *ptr) void row1_master_pop_callback(void *ptr) { - bool new_state = !row_is_on(ROW1_LIGHT1,ROW1_LIGHT2); + bool new_state = !row_is_on(ROW1_LIGHT1, ROW1_LIGHT2); pwm_set_state(ROW1_LIGHT1, new_state); pwm_set_state(ROW1_LIGHT2, new_state); } void row2_master_pop_callback(void *ptr) { - bool new_state = !row_is_on(ROW2_LIGHT1,ROW2_LIGHT2); + bool new_state = !row_is_on(ROW2_LIGHT1, ROW2_LIGHT2); pwm_set_state(ROW2_LIGHT1, new_state); pwm_set_state(ROW2_LIGHT2, new_state); } void row3_master_pop_callback(void *ptr) { - bool new_state = !row_is_on(ROW3_LIGHT1,ROW3_LIGHT2); + bool new_state = !row_is_on(ROW3_LIGHT1, ROW3_LIGHT2); pwm_set_state(ROW3_LIGHT1, new_state); pwm_set_state(ROW3_LIGHT2, new_state); } void row4_master_pop_callback(void *ptr) { - bool new_state = !row_is_on(ROW4_LIGHT1,ROW4_LIGHT2); + bool new_state = !row_is_on(ROW4_LIGHT1, ROW4_LIGHT2); pwm_set_state(ROW4_LIGHT1, new_state); pwm_set_state(ROW4_LIGHT2, new_state); } @@ -249,188 +262,202 @@ void virtual_interrupt_user_callback(int pin, int state) { } -void update_lcd_row1() { - bool master_state = row_is_on_or(ROW1_LIGHT1,ROW1_LIGHT2); - //row1_master.Set_background_image_pic(master_state?10:9); +void update_lcd_row1() +{ + bool master_state = row_is_on_or(ROW1_LIGHT1, ROW1_LIGHT2); + // row1_master.Set_background_image_pic(master_state?10:9); elcd_send_stop_bit(); elcd.print("row1_master.pic="); - elcd.print(master_state?10:9); + elcd.print(master_state ? 10 : 9); elcd_send_stop_bit(); elcd.print("row1_master.pic2="); - elcd.print(master_state?10:9); + elcd.print(master_state ? 10 : 9); elcd_send_stop_bit(); bool lt1_state = pwm_get_state(ROW1_LIGHT1); bool lt2_state = pwm_get_state(ROW1_LIGHT2); - //also update the light1 and light2 buttons background to reflect the current state - //row1_lv1.Set_background_image_pic(lt1_state?6:5); - //row1_lv2.Set_background_image_pic(lt2_state?8:7); + // also update the light1 and light2 buttons background to reflect the current state + // row1_lv1.Set_background_image_pic(lt1_state?6:5); + // row1_lv2.Set_background_image_pic(lt2_state?8:7); elcd.print("row1_lv1.pic="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row1_lv2.pic="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); elcd.print("row1_lv1.pic2="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row1_lv2.pic2="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); } -void update_lcd_row2() { - bool master_state = row_is_on_or(ROW2_LIGHT1,ROW2_LIGHT2); - //row2_master.Set_background_image_pic(master_state?12:11); +void update_lcd_row2() +{ + bool master_state = row_is_on_or(ROW2_LIGHT1, ROW2_LIGHT2); + // row2_master.Set_background_image_pic(master_state?12:11); elcd_send_stop_bit(); elcd.print("row2_master.pic="); - elcd.print(master_state?12:11); + elcd.print(master_state ? 12 : 11); elcd_send_stop_bit(); elcd.print("row2_master.pic2="); - elcd.print(master_state?12:11); + elcd.print(master_state ? 12 : 11); elcd_send_stop_bit(); bool lt1_state = pwm_get_state(ROW2_LIGHT1); bool lt2_state = pwm_get_state(ROW2_LIGHT2); - //also update the light1 and light2 buttons background to reflect the current state - //row2_lv1.Set_background_image_pic(lt1_state?6:5); - //row2_lv2.Set_background_image_pic(lt2_state?8:7); + // also update the light1 and light2 buttons background to reflect the current state + // row2_lv1.Set_background_image_pic(lt1_state?6:5); + // row2_lv2.Set_background_image_pic(lt2_state?8:7); elcd.print("row2_lv1.pic="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row2_lv2.pic="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); elcd.print("row2_lv1.pic2="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row2_lv2.pic2="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); - } -void update_lcd_row3() { - bool master_state = row_is_on_or(ROW3_LIGHT1,ROW3_LIGHT2); - //row3_master.Set_background_image_pic(master_state?14:13); +void update_lcd_row3() +{ + bool master_state = row_is_on_or(ROW3_LIGHT1, ROW3_LIGHT2); + // row3_master.Set_background_image_pic(master_state?14:13); elcd_send_stop_bit(); elcd.print("row3_master.pic="); - elcd.print(master_state?14:13); + elcd.print(master_state ? 14 : 13); elcd_send_stop_bit(); elcd.print("row3_master.pic2="); - elcd.print(master_state?14:13); + elcd.print(master_state ? 14 : 13); elcd_send_stop_bit(); bool lt1_state = pwm_get_state(ROW3_LIGHT1); bool lt2_state = pwm_get_state(ROW3_LIGHT2); - //also update the light1 and light2 buttons background to reflect the current state - //row3_lv1.Set_background_image_pic(lt1_state?6:5); - //row3_lv2.Set_background_image_pic(lt2_state?8:7); + // also update the light1 and light2 buttons background to reflect the current state + // row3_lv1.Set_background_image_pic(lt1_state?6:5); + // row3_lv2.Set_background_image_pic(lt2_state?8:7); elcd.print("row3_lv1.pic="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row3_lv2.pic="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); elcd.print("row3_lv1.pic2="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row3_lv2.pic2="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); } -void update_lcd_row4() { - bool master_state = row_is_on_or(ROW4_LIGHT1,ROW4_LIGHT2); - //row4_master.Set_background_image_pic(master_state?16:15); +void update_lcd_row4() +{ + bool master_state = row_is_on_or(ROW4_LIGHT1, ROW4_LIGHT2); + // row4_master.Set_background_image_pic(master_state?16:15); elcd_send_stop_bit(); elcd.print("row4_master.pic="); - elcd.print(master_state?16:15); + elcd.print(master_state ? 16 : 15); elcd_send_stop_bit(); elcd.print("row4_master.pic2="); - elcd.print(master_state?16:15); + elcd.print(master_state ? 16 : 15); elcd_send_stop_bit(); bool lt1_state = pwm_get_state(ROW4_LIGHT1); bool lt2_state = pwm_get_state(ROW4_LIGHT2); - //also update the light1 and light2 buttons background to reflect the current state - //row4_lv1.Set_background_image_pic(lt1_state?6:5); - //row4_lv2.Set_background_image_pic(lt2_state?8:7); + // also update the light1 and light2 buttons background to reflect the current state + // row4_lv1.Set_background_image_pic(lt1_state?6:5); + // row4_lv2.Set_background_image_pic(lt2_state?8:7); elcd.print("row4_lv1.pic="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row4_lv2.pic="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); elcd.print("row4_lv1.pic2="); - elcd.print(lt1_state?6:5); + elcd.print(lt1_state ? 6 : 5); elcd_send_stop_bit(); elcd.print("row4_lv2.pic2="); - elcd.print(lt2_state?8:7); + elcd.print(lt2_state ? 8 : 7); elcd_send_stop_bit(); - } -void update_toggle_button() { - //calcuate the current state of the toggle button - bool current_state = row_is_on_or(ROW1_LIGHT1,ROW1_LIGHT2) || row_is_on_or(ROW2_LIGHT1,ROW2_LIGHT2) || row_is_on_or(ROW3_LIGHT1,ROW3_LIGHT2) || row_is_on_or(ROW4_LIGHT1,ROW4_LIGHT2); - //light_toggle.Set_background_image_pic(current_state?4:3); +void update_toggle_button() +{ + // calcuate the current state of the toggle button + bool current_state = row_is_on_or(ROW1_LIGHT1, ROW1_LIGHT2) || row_is_on_or(ROW2_LIGHT1, ROW2_LIGHT2) || row_is_on_or(ROW3_LIGHT1, ROW3_LIGHT2) || row_is_on_or(ROW4_LIGHT1, ROW4_LIGHT2); + // light_toggle.Set_background_image_pic(current_state?4:3); elcd_send_stop_bit(); elcd.print("light_toggle.pic="); - elcd.print(current_state?4:3); + elcd.print(current_state ? 4 : 3); elcd_send_stop_bit(); elcd.print("light_toggle.pic2="); - elcd.print(current_state?4:3); + elcd.print(current_state ? 4 : 3); elcd_send_stop_bit(); } -void update_lcd_ac() { - //update the current temperature +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 + // 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); - elcd_send_stop_bit(); - elcd.print("fan.pic=18"); - elcd_send_stop_bit(); - elcd.print("fan.pic2=18"); - elcd_send_stop_bit(); - } else if(fan_speed == 1) { - //fan.Set_background_image_pic(19); - elcd_send_stop_bit(); - elcd.print("fan.pic=19"); - elcd_send_stop_bit(); - elcd.print("fan.pic2=19"); - elcd_send_stop_bit(); - } else if(fan_speed == 2) { - //fan.Set_background_image_pic(21); - elcd_send_stop_bit(); - elcd.print("fan.pic=21"); - elcd_send_stop_bit(); - elcd.print("fan.pic2=21"); - elcd_send_stop_bit(); - } else if(fan_speed == 3) { - //fan.Set_background_image_pic(20); - elcd_send_stop_bit(); - elcd.print("fan.pic=20"); - elcd_send_stop_bit(); - elcd.print("fan.pic2=20"); - elcd_send_stop_bit(); - } else { - //fan.Set_background_image_pic(18); + if (fan_speed == 0) + { + // fan.Set_background_image_pic(18); elcd_send_stop_bit(); elcd.print("fan.pic=18"); elcd_send_stop_bit(); elcd.print("fan.pic2=18"); elcd_send_stop_bit(); } - //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 + else if (fan_speed == 1) + { + // fan.Set_background_image_pic(19); + elcd_send_stop_bit(); + elcd.print("fan.pic=19"); + elcd_send_stop_bit(); + elcd.print("fan.pic2=19"); + elcd_send_stop_bit(); + } + else if (fan_speed == 2) + { + // fan.Set_background_image_pic(21); + elcd_send_stop_bit(); + elcd.print("fan.pic=21"); + elcd_send_stop_bit(); + elcd.print("fan.pic2=21"); + elcd_send_stop_bit(); + } + else if (fan_speed == 3) + { + // fan.Set_background_image_pic(20); + elcd_send_stop_bit(); + elcd.print("fan.pic=20"); + elcd_send_stop_bit(); + elcd.print("fan.pic2=20"); + elcd_send_stop_bit(); + } + else + { + // fan.Set_background_image_pic(18); + elcd_send_stop_bit(); + elcd.print("fan.pic=18"); + elcd_send_stop_bit(); + elcd.print("fan.pic2=18"); + elcd_send_stop_bit(); + } + // 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); + // mode.Set_background_image_pic(mode_state==1?22:24); elcd_send_stop_bit(); - if(mode_state==1){ + if (mode_state == 1) + { elcd_send_stop_bit(); elcd.print("ac_toggle.pic=2"); elcd_send_stop_bit(); @@ -440,8 +467,9 @@ void update_lcd_ac() { elcd_send_stop_bit(); elcd.print("mode.pic2=22"); elcd_send_stop_bit(); - - } else if(mode_state==2){ + } + else if (mode_state == 2) + { elcd_send_stop_bit(); elcd.print("ac_toggle.pic=2"); elcd_send_stop_bit(); @@ -451,24 +479,28 @@ void update_lcd_ac() { elcd_send_stop_bit(); elcd.print("mode.pic2=24"); elcd_send_stop_bit(); - } else{ + } + else if (mode_state == 0) + { elcd_send_stop_bit(); elcd.print("ac_toggle.pic=1"); elcd_send_stop_bit(); elcd.print("ac_toggle.pic2=1"); - if(user_acmode==1){ + if (user_acmode == 1) + { elcd_send_stop_bit(); elcd.print("mode.pic=22"); elcd_send_stop_bit(); elcd.print("mode.pic2=22"); elcd_send_stop_bit(); } - else{ - elcd_send_stop_bit(); - elcd.print("mode.pic=24"); - elcd_send_stop_bit(); - elcd.print("mode.pic2=24"); - elcd_send_stop_bit(); + else + { + elcd_send_stop_bit(); + elcd.print("mode.pic=24"); + elcd_send_stop_bit(); + elcd.print("mode.pic2=24"); + elcd_send_stop_bit(); } } /* elcd.print("mode.pic="); @@ -488,8 +520,6 @@ void update_lcd_ac() { elcd_send_stop_bit(); */ } - - void pwm_changed_user_callback(int pin) { switch (pin) @@ -529,23 +559,25 @@ void timer_tick_callback() } } -void ac_changed_user_callback(int mode, int temperature, int fan_speed) { +void ac_changed_user_callback(int mode, int temperature, int fan_speed) +{ update_lcd_ac(); } -void mqtt_connected_user_callback() { - +void mqtt_connected_user_callback() +{ } -void user_state_request_callback() { - +void user_state_request_callback() +{ } -void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length) { - +void user_mqtt_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length) +{ } -void elcd_send_stop_bit() { +void elcd_send_stop_bit() +{ elcd.write(0xFF); elcd.write(0xFF); elcd.write(0xFF);