Skip to content

Commit

Permalink
Button page confirm handled by ESPHome
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardtfn committed Dec 5, 2024
1 parent 3601e5e commit 9ea9e68
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
19 changes: 11 additions & 8 deletions components/nspanel_ha_blueprint/standard_page_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ namespace nspanel_ha_blueprint {
// Pointer to the static button's array, allocated in PSRAM if possible
PageButtonsButton* buttonpage_buttons;

char* get_button_name(const uint8_t button_id) {
// Determine the base component name
static char base_component[9];
snprintf(base_component, sizeof(base_component), "button%02d", (button_id % 8) + 1);
return base_component;
}

// Function to set up all buttons in the PSRAM
void setup_pagebuttons_buttons() {
if (!PageButtonsButtonInitilized) {
Expand All @@ -52,18 +59,14 @@ namespace nspanel_ha_blueprint {
// Calculate page and button identifiers
const uint8_t page_id = (i / 8) + 12; // PAGE_ID_BUTTONPAGE01: '12'

// Determine the base component name
char base_component[15];
snprintf(base_component, sizeof(base_component), "button%02d", (i % 8) + 1);

// Use the component ID to identify each component in the Nextion, with suffixes
char bri_component[20];
char icon_component[20];
char label_component[20];

snprintf(bri_component, sizeof(bri_component), "%sbri", base_component);
snprintf(icon_component, sizeof(icon_component), "%sicon", base_component);
snprintf(label_component, sizeof(label_component), "%stext", base_component);
snprintf(bri_component, sizeof(bri_component), "%sbri", get_button_name(i));
snprintf(icon_component, sizeof(icon_component), "%sicon", get_button_name(i));
snprintf(label_component, sizeof(label_component), "%stext", get_button_name(i));

// Assign pointers to the respective DisplayComponent using get_component
buttonpage_buttons[i].bri = get_component(page_id, bri_component);
Expand All @@ -84,7 +87,7 @@ namespace nspanel_ha_blueprint {
return (index % 8) + 1; // Calculate the button ID within the page (1-based button numbering)
}

bool is_page_enabled(const uint8_t buttonpage_index, const uint32_t mask) {
bool is_button_page_enabled(const uint8_t buttonpage_index, const uint32_t mask) {
// Ensure the buttonpage_index is within the valid range (1-4)
if (buttonpage_index < 1 || buttonpage_index > 4) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion components/nspanel_ha_blueprint/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace nspanel_ha_blueprint {
std::string process_time_format(const std::string &time_format, int current_hour,
const std::string &meridiem_am, const std::string &meridiem_pm);

inline uint32_t decode_utf8(const char* bytes) {
uint32_t decode_utf8(const char* bytes) {
if (!bytes || bytes[0] == '\0') {
return 0;
}
Expand Down
29 changes: 25 additions & 4 deletions esphome/nspanel_esphome_standard_page_buttons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ display:
then:
- lambda: |-
if (is_page_in_group(BUTTONS_PAGE_MASK, page_id)) { // Button's pages
if (component_id >= 42 and component_id <= 49) { // Buttons
const uint8_t button_id = (8*(page_id-${PAGE_ID_BUTTONPAGE01}))+(component_id-41);
if (component_id >= 38 and component_id <= 45) { // Buttons
const uint8_t button_id = (8*(page_id-${PAGE_ID_BUTTONPAGE01}))+(component_id-37);
ESP_LOGI("display", "Button %" PRIu8 " %s", button_id, touch_event ? "pressed" : "released");
if (touch_event)
button_press->execute(button_id);
button_press->execute(page_id, button_id);
else
button_release->execute(button_id);
button_release->execute(page_id, button_id);
}
}
Expand All @@ -100,28 +100,40 @@ script:
- id: button_click_long
mode: parallel
parameters:
page_id: uint8_t
button_id: uint8_t
then:
- lambda: |-
ha_button->execute(page_id, get_button_name(button_id), "long_click");
- id: button_click_short
mode: parallel
parameters:
page_id: uint8_t
button_id: uint8_t
then:
# Extended by:
# nspanel_esphome_standard_page_confirm.yaml
- lambda: |-
if (not is_button_in_mask(button_id, PageButtonsMaskConfirm))
ha_button->execute(page_id, get_button_name(button_id), "short_click");
- id: button_press
mode: restart
parameters:
page_id: uint8_t
button_id: uint8_t
then:
- delay: ${BUTTON_LONG_PRESS_DELAY}
- script.execute:
id: button_click_long
page_id: !lambda return page_id;
button_id: !lambda return button_id;

- id: button_release
mode: parallel
parameters:
page_id: uint8_t
button_id: uint8_t
then:
- if:
Expand All @@ -131,12 +143,21 @@ script:
- script.stop: button_press
- script.execute:
id: button_click_short
page_id: !lambda return page_id;
button_id: !lambda return button_id;

- id: !extend boot_package_register
then:
- lambda: boot_package_register_add->execute(${BOOT_STEP_PAGE_BUTTONS}, "PAGE_BUTTONS", "Page Buttons");

- id: !extend dump_config
then:
- lambda: |-
// Check for requirements
#if !defined(NSPANEL_HA_BLUEPRINT_STANDARD_PAGE_BUTTONS)
#error "The package nspanel_esphome_standard_page_buttons.yaml is required."
#endif
- id: page_buttonpage
mode: restart
then:
Expand Down
12 changes: 12 additions & 0 deletions esphome/nspanel_esphome_standard_page_confirm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ script:
then:
- lambda: boot_package_register_add->execute(${BOOT_STEP_PAGE_CONFIRM}, "PAGE_CONFIRM", "Page Confirm");

- id: !extend button_click_short
then:
- lambda: |-
if (is_button_in_mask(button_id, PageButtonsMaskConfirm)) {
goto_page_id->execute(${PAGE_ID_CONFIRM}, true);
set_component_value->execute(${PAGE_ID_CONFIRM}, "page_id", page_id);
set_component_text->execute(${PAGE_ID_CONFIRM}, "page_name", page_names[page_id]);
set_component_text->execute(${PAGE_ID_CONFIRM}, "component", get_button_name(button_id));
static DisplayComponent* confirm_body = get_component(${PAGE_ID_CONFIRM}, "body");
display_component_update_text->execute(confirm_body, buttonpage_buttons[button_id-1].label->text);
}
- id: !extend page_changed
then:
- lambda: |-
Expand Down
2 changes: 1 addition & 1 deletion nspanel_blueprint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3697,7 +3697,7 @@ blueprint:
max: 60000
unit_of_measurement: milliseconds
media_player_update_delay:
name: Media player - Delay (msec) after entity updade call?
name: Media player - Delay (msec) after entity update call?
default: 500
selector:
number:
Expand Down

0 comments on commit 9ea9e68

Please sign in to comment.