add light control

This commit is contained in:
reaw55 2023-12-25 00:41:16 +07:00
parent b4b1f12ba8
commit b05c75a0a1
2 changed files with 220 additions and 24 deletions

View File

@ -2,23 +2,123 @@
// Display Componets // Display Componets
// Link with Dual state button object with id 2 on page 1 named bt0 // 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 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");
// List of Component ID Message to listen to // List of Component ID Message to listen to
NexTouch *nex_listen_list[] = NexTouch *nex_listen_list[] =
{ {
&light_toggle,
&row1_master, &row1_master,
&row2_master,
&row3_master,
&row4_master,
&row1_lv1,
&row1_lv2,
&row2_lv1,
&row2_lv2,
&row3_lv1,
&row3_lv2,
&row4_lv1,
&row4_lv2,
NULL}; NULL};
bool row1_is_on() { bool row_is_on(int light1_pin, int light2_pin) {
return pwm_get_state(LIGHT_THAW_NAH_THANG_KWAR) && pwm_get_state(LIGHT_THAW_NAH_THANG_SAI); 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);
pwm_set_state(ROW1_LIGHT1, new_state);
pwm_set_state(ROW1_LIGHT2, new_state);
pwm_set_state(ROW2_LIGHT1, new_state);
pwm_set_state(ROW2_LIGHT2, new_state);
pwm_set_state(ROW3_LIGHT1, new_state);
pwm_set_state(ROW3_LIGHT2, new_state);
pwm_set_state(ROW4_LIGHT1, new_state);
pwm_set_state(ROW4_LIGHT2, new_state);
}
void row1_lv1_pop_callback(void *ptr)
{
pwm_toggle(ROW1_LIGHT1);
}
void row1_lv2_pop_callback(void *ptr)
{
pwm_toggle(ROW1_LIGHT2);
}
void row2_lv1_pop_callback(void *ptr)
{
pwm_toggle(ROW2_LIGHT1);
}
void row2_lv2_pop_callback(void *ptr)
{
pwm_toggle(ROW2_LIGHT2);
}
void row3_lv1_pop_callback(void *ptr)
{
pwm_toggle(ROW3_LIGHT1);
}
void row3_lv2_pop_callback(void *ptr)
{
pwm_toggle(ROW3_LIGHT2);
}
void row4_lv1_pop_callback(void *ptr)
{
pwm_toggle(ROW4_LIGHT1);
}
void row4_lv2_pop_callback(void *ptr)
{
pwm_toggle(ROW4_LIGHT2);
}
void row1_master_pop_callback(void *ptr) void row1_master_pop_callback(void *ptr)
{ {
bool new_state = !row1_is_on(); bool new_state = !row_is_on(ROW1_LIGHT1,ROW1_LIGHT2);
pwm_set_state(LIGHT_THAW_NAH_THANG_KWAR, new_state); pwm_set_state(ROW1_LIGHT1, new_state);
pwm_set_state(LIGHT_THAW_NAH_THANG_SAI, 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);
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);
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);
pwm_set_state(ROW4_LIGHT1, new_state);
pwm_set_state(ROW4_LIGHT2, new_state);
} }
/* /*
@ -37,7 +137,19 @@ void user_init()
{ {
elcd.print("page main"); elcd.print("page main");
elcd_send_stop_bit(); elcd_send_stop_bit();
light_toggle.attachPop(light_toggle_pop_callback, &light_toggle);
row1_lv1.attachPop(row1_lv1_pop_callback, &row1_lv1);
row1_lv2.attachPop(row1_lv2_pop_callback, &row1_lv2);
row2_lv1.attachPop(row2_lv1_pop_callback, &row2_lv1);
row2_lv2.attachPop(row2_lv2_pop_callback, &row2_lv2);
row3_lv1.attachPop(row3_lv1_pop_callback, &row3_lv1);
row3_lv2.attachPop(row3_lv2_pop_callback, &row3_lv2);
row4_lv1.attachPop(row4_lv1_pop_callback, &row4_lv1);
row4_lv2.attachPop(row4_lv2_pop_callback, &row4_lv2);
row1_master.attachPop(row1_master_pop_callback, &row1_master); row1_master.attachPop(row1_master_pop_callback, &row1_master);
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);
} }
/* /*
@ -56,26 +168,84 @@ void virtual_interrupt_user_callback(int pin, int state)
} }
void update_lcd_row1() { void update_lcd_row1() {
bool master_state = row1_is_on(); bool master_state = row_is_on(ROW1_LIGHT1,ROW1_LIGHT2);
//Assume 20 off, 21 on row1_master.Set_background_image_pic(master_state?9:10);
row1_master.Set_background_image_pic(master_state?21:20); bool lt1_state = pwm_get_state(ROW1_LIGHT1);
bool lt1_state = pwm_get_state(LIGHT_THAW_NAH_THANG_KWAR); bool lt2_state = pwm_get_state(ROW1_LIGHT2);
bool lt2_state = pwm_get_state(LIGHT_THAW_NAH_THANG_SAI); //also update the light1 and light2 buttons background to reflect the current state
row1_lv1.Set_background_image_pic(lt1_state?5:6);
row1_lv2.Set_background_image_pic(lt2_state?7:8);
}
void update_lcd_row2() {
bool master_state = row_is_on(ROW2_LIGHT1,ROW2_LIGHT2);
row2_master.Set_background_image_pic(master_state?11:12);
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?5:6);
row2_lv2.Set_background_image_pic(lt2_state?7:8);
} }
void update_lcd_row3() {
bool master_state = row_is_on(ROW3_LIGHT1,ROW3_LIGHT2);
row3_master.Set_background_image_pic(master_state?13:14);
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?5:6);
row3_lv2.Set_background_image_pic(lt2_state?7:8);
}
void update_lcd_row4() {
bool master_state = row_is_on(ROW4_LIGHT1,ROW4_LIGHT2);
row4_master.Set_background_image_pic(master_state?15:16);
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?5:6);
row4_lv2.Set_background_image_pic(lt2_state?7:8);
}
void update_toggle_button() {
//calcuate the current state of the toggle button
bool current_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);
light_toggle.Set_background_image_pic(current_state?3:4);
}
void pwm_changed_user_callback(int pin) void pwm_changed_user_callback(int pin)
{ {
// switch (pin) switch (pin)
// { {
// case LIGHT_THAW_NAH_THANG_KWAR: case ROW1_LIGHT1:
// case LIGHT_THAW_NAH_THANG_SAI: case ROW1_LIGHT2:
// update_lcd_row1(); update_lcd_row1();
// break; update_toggle_button();
break;
// default: case ROW2_LIGHT1:
// break; case ROW2_LIGHT2:
// } update_lcd_row2();
update_toggle_button();
break;
case ROW3_LIGHT1:
case ROW3_LIGHT2:
update_lcd_row3();
update_toggle_button();
break;
case ROW4_LIGHT1:
case ROW4_LIGHT2:
update_lcd_row4();
update_toggle_button();
break;
default:
break;
}
} }
/* /*

View File

@ -7,10 +7,20 @@
#include "espmega_iot_external_lcd.hpp" #include "espmega_iot_external_lcd.hpp"
// I/O Assignment // I/O Assignment
#define LIGHT_THAW_NAH_THANG_KWAR 0 // Available Inputs 0-15
#define LIGHT_THAW_NAH_THANG_SAI 1 // Available Outputs 0-3, 7-11
// There are 4 rows of lights
// Each row has 2 lights
// Each light has 2 states
#define ROW1_LIGHT1 0
#define ROW1_LIGHT2 1
#define ROW2_LIGHT1 2
#define ROW2_LIGHT2 3
#define ROW3_LIGHT1 7
#define ROW3_LIGHT2 8
#define ROW4_LIGHT1 9
#define ROW4_LIGHT2 10
#define COMPUTER_DESK_CONTACTOR 12
// Bus Overclocking Configuration // Bus Overclocking Configuration
// Do not enable if you are using external I/O cards as it will cause signal integrity issues. // Do not enable if you are using external I/O cards as it will cause signal integrity issues.
@ -55,7 +65,23 @@
void elcd_send_stop_bit(); void elcd_send_stop_bit();
bool row1_is_on(); bool row1_is_on();
void update_lcd_row1(); void update_lcd_row1();
void update_lcd_row2();
void update_lcd_row3();
void update_lcd_row4();
void update_toggle_button();
void light_toggle_pop_callback(void *ptr);
void row1_lv1_pop_callback(void *ptr);
void row1_lv2_pop_callback(void *ptr);
void row2_lv1_pop_callback(void *ptr);
void row2_lv2_pop_callback(void *ptr);
void row3_lv1_pop_callback(void *ptr);
void row3_lv2_pop_callback(void *ptr);
void row4_lv1_pop_callback(void *ptr);
void row4_lv2_pop_callback(void *ptr);
void row1_master_pop_callback(void *ptr); 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);
// User Defined IoT Core Callback Functions (Required) // User Defined IoT Core Callback Functions (Required)
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);