Skip to content

Commit

Permalink
Support to Nextion global components
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardtfn committed Nov 25, 2024
1 parent 67f9349 commit c07bb2d
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 75 deletions.
8 changes: 5 additions & 3 deletions components/nspanel_ha_blueprint/DisplayComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ namespace nspanel_ha_blueprint {
if (components != nullptr) {
for (auto* comp : *components) {
if (comp->page_id == page_id) {
comp->text_is_updated = false;
comp->color_is_updated = false;
comp->font_is_updated = false;
if (not comp->global) {
comp->text_is_updated = false;
comp->color_is_updated = false;
comp->font_is_updated = false;
}
comp->visible_is_updated = false;
}
}
Expand Down
1 change: 1 addition & 0 deletions components/nspanel_ha_blueprint/DisplayComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace nspanel_ha_blueprint {
uint16_t color = UINT16_MAX; // RGB565 color value, default to MAX_UINT16
uint8_t font = UINT8_MAX; // Font identifier, default to MAX_UINT8
bool visible = true; // Visibility flag, default to true
bool global = false; // Is it a global variable from Nextion perspective
bool text_is_updated = true; // Indicates if text content has been updated, default to true
bool color_is_updated = true; // Indicates if color has been updated, default to true
bool font_is_updated = true; // Indicates if font has been updated, default to true
Expand Down
4 changes: 4 additions & 0 deletions components/nspanel_ha_blueprint/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ namespace nspanel_ha_blueprint {
return code_point; // Return the decoded Unicode code point
}

inline std::string get_page_and_component(const std::string& page, const std::string& component) {
return page + "." + component;
}

} // namespace nspanel_ha_blueprint
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#####################################################################################################
##### NSPANEL ESPHOME created by Blackymas - https://github.com/Blackymas/NSPanel_HA_Blueprint #####
##### ESPHOME CORE - HARDWARE - Display - Light #####
##### ESPHOME ADD-ON - Display - Light #####
##### PLEASE only make changes if it is necessary and also the required knowledge is available. #####
##### For normal use with the Blueprint, no changes are necessary. #####
#####################################################################################################
Expand All @@ -9,7 +9,7 @@
esphome:
platformio_options:
build_flags:
- -D NSPANEL_HA_BLUEPRINT_CORE_HW_DISPLAY_LIGHT
- -D NSPANEL_HA_BLUEPRINT_ADDON_DISPLAY_LIGHT

light:
# Add the display as a light in Home Assistant
Expand Down Expand Up @@ -75,11 +75,9 @@ script:
- if:
condition:
- lambda: |-
return (brightness != int(
round(display_light->current_values.is_on() ?
return (brightness != int(round(display_light->current_values.is_on() ?
(display_light->current_values.get_brightness() * 100.0f)
: 0.0))
);
: 0.0)));
then:
- if:
condition:
Expand Down
1 change: 0 additions & 1 deletion esphome/nspanel_esphome_core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ packages:
hw_buttons: !include nspanel_esphome_core_hw_buttons.yaml
hw_buzzer: !include nspanel_esphome_core_hw_buzzer.yaml
hw_display: !include nspanel_esphome_core_hw_display.yaml
hw_display_light: !include nspanel_esphome_core_hw_display_light.yaml
hw_display_timers: !include nspanel_esphome_core_hw_display_timers.yaml
hw_memory: !include nspanel_esphome_core_hw_memory.yaml
hw_relays: !include nspanel_esphome_core_hw_relays.yaml
Expand Down
8 changes: 8 additions & 0 deletions esphome/nspanel_esphome_core_boot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ script:
completed_boot_steps: !lambda return completed_boot_steps;
- delay: 120s

- id: boot_initialize
mode: single
then:
# Extended by:
# nspanel_esphome_core_hw_display.yaml

- id: boot_progress
mode: restart
parameters:
Expand Down Expand Up @@ -113,6 +119,7 @@ script:
- id: boot_sequence
mode: single
then:
- script.execute: boot_initialize
- lambda: |-
if (!disp1->is_setup())
boot_log->execute("Boot", "Waiting for Nextion");
Expand Down Expand Up @@ -193,6 +200,7 @@ script:
then:
- lambda: |-
boot_event->stop();
boot_initialize->stop();
boot_progress->stop();
boot_progress_dump_timer->stop();
...
4 changes: 0 additions & 4 deletions esphome/nspanel_esphome_core_datetime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ script:
ESP_LOGCONFIG("${project_tag}", " Timezone (selected): %s", id(mui_timezone).c_str());
ESP_LOGCONFIG("${project_tag}", " Timezone (current): %s", time_provider->get_timezone().c_str());
- id: !extend page_home
then:
- script.execute: refresh_datetime

- id: refresh_datetime
mode: restart
then:
Expand Down
32 changes: 22 additions & 10 deletions esphome/nspanel_esphome_core_hw_buzzer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ esphome:
platformio_options:
build_flags:
- -D NSPANEL_HA_BLUEPRINT_CORE_HW_BUZZER
on_boot:
- priority: -100.44
then:
- wait_until:
condition:
- lambda: return not isnan(buzzer_gain->state);
- lambda: |-
if (not isnan(buzzer_gain->state))
buzzer->set_gain((100-(buzzer_gain->state/10.0f))/100.0f);
boot_progress->execute(${BOOT_STEP_HW_BUZZER}, "HW Buzzer");

number:
- id: buzzer_gain
Expand Down Expand Up @@ -96,6 +86,24 @@ script:
- lambda: |-
boot_progress_dump_item->execute(${BOOT_STEP_HW_BUZZER}, "HW Buzzer");
- id: !extend boot_sequence
then:
- script.execute: boot_sequence_buzzer

- id: boot_sequence_buzzer
mode: restart
then:
- wait_until:
condition:
- lambda: return not isnan(buzzer_gain->state);
timeout: 600s
- lambda: |-
if (not isnan(buzzer_gain->state))
buzzer->set_gain((100-(buzzer_gain->state/10.0f))/100.0f);
else
ESP_LOGE("script.boot_sequence_buzzer", "Buzzer gain not set");
boot_progress->execute(${BOOT_STEP_HW_BUZZER}, "HW Buzzer");
- id: !extend page_changed
then:
- lambda: |-
Expand All @@ -108,6 +116,10 @@ script:
buzzer->play("${tone_boot}");
}
- id: !extend stop_all
then:
- script.stop: boot_sequence_buzzer

switch:
- id: buzzer_touch_feedback
name: Sound - Touch screen feedback
Expand Down
69 changes: 51 additions & 18 deletions esphome/nspanel_esphome_core_hw_display.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ esphome:
build_flags:
- -D NSPANEL_HA_BLUEPRINT_CORE_HW_DISPLAY
on_boot:
- priority: 1000.5
then:
- lambda: setup_components();

- priority: 600.5
then:
- if:
Expand Down Expand Up @@ -214,6 +210,10 @@ globals:

json: # Can be replaced by web_server

logger:
logs:
script.display_component_send: INFO

number:
- id: display_brightness # SCREEN BRIGHTNESS
name: Brightness - Normal
Expand Down Expand Up @@ -300,6 +300,10 @@ script:
- lambda: |-
boot_progress_dump_item->execute(${BOOT_STEP_HW_DISPLAY}, "HW Display");
- id: !extend boot_initialize
then:
- lambda: setup_components();

- id: display_component_action
mode: parallel
parameters:
Expand Down Expand Up @@ -334,6 +338,17 @@ script:
// Text visibility
display_component_update_visibility->execute(display_component, visible);
- id: display_component_initialize
mode: parallel
parameters:
page_id: uint8_t
component: string
global: bool
then:
- lambda: |-
DisplayComponent* component_to_initialize = get_component(page_id, component.c_str());
component_to_initialize->global = global;
- id: display_component_send
mode: queued
parameters:
Expand All @@ -344,13 +359,16 @@ script:
- lambda: return (display_component->page_id == current_page_id);
timeout: 2s
- lambda: |-
if (display_component->page_id == current_page_id) {
if (display_component->page_id == current_page_id or display_component->global) {
ESP_LOGD("script.display_component_send", "Rendering page %s, component %s",
page_names[display_component->page_id], display_component->component);
display_component_send_text->execute(display_component);
display_component_send_color->execute(display_component);
display_component_send_font->execute(display_component);
display_component_send_visibility->execute(display_component);
if (not display_component->global) {
display_component_send_text->execute(display_component);
display_component_send_color->execute(display_component);
display_component_send_font->execute(display_component);
}
if (display_component->page_id == current_page_id)
display_component_send_visibility->execute(display_component);
}
- id: display_component_send_color
Expand All @@ -360,13 +378,18 @@ script:
then:
- if:
condition:
- lambda: return display_component->page_id == current_page_id;
- lambda: return (display_component->page_id == current_page_id or display_component->global);
- lambda: return not display_component->color_is_updated;
then:
- delay: !lambda return display_delay->state;
- lambda: |-
if (display_component->page_id == current_page_id and !display_component->color_is_updated) {
disp1->set_component_font_color(display_component->component, display_component->color);
if ((display_component->global or display_component->page_id == current_page_id) and
!display_component->color_is_updated) {
disp1->set_component_font_color(
(display_component->page_id == current_page_id) ? display_component->component :
get_page_and_component(page_names[display_component->page_id],
display_component->component).c_str(),
display_component->color);
display_component->color_is_updated = true;
}
Expand All @@ -377,14 +400,19 @@ script:
then:
- if:
condition:
- lambda: return display_component->page_id == current_page_id;
- lambda: return (display_component->page_id == current_page_id or display_component->global);
- lambda: return display_component->font != UINT8_MAX;
- lambda: return not display_component->font_is_updated;
then:
- delay: !lambda return display_delay->state;
- lambda: |-
if (display_component->page_id == current_page_id and !display_component->font_is_updated) {
disp1->set_component_font(display_component->component, display_component->font);
if ((display_component->global or display_component->page_id == current_page_id) and
!display_component->font_is_updated) {
disp1->set_component_font(
(display_component->page_id == current_page_id) ? display_component->component :
get_page_and_component(page_names[display_component->page_id],
display_component->component).c_str(),
display_component->font);
display_component->font_is_updated = true;
}
Expand All @@ -395,13 +423,18 @@ script:
then:
- if:
condition:
- lambda: return display_component->page_id == current_page_id;
- lambda: return (display_component->page_id == current_page_id or display_component->global);
- lambda: return not display_component->text_is_updated;
then:
- delay: !lambda return display_delay->state;
- lambda: |-
if (display_component->page_id == current_page_id and !display_component->text_is_updated) {
disp1->set_component_text(display_component->component, display_component->text);
if ((display_component->global or display_component->page_id == current_page_id) and
!display_component->text_is_updated) {
disp1->set_component_text(
(display_component->page_id == current_page_id) ? display_component->component :
get_page_and_component(page_names[display_component->page_id],
display_component->component).c_str(),
display_component->text);
display_component->text_is_updated = true;
}
Expand Down
12 changes: 1 addition & 11 deletions esphome/nspanel_esphome_core_hw_relays.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,12 @@ script:
display_component_update_visibility->execute(id(relay1_icon), relay_1->state);
display_component_update_visibility->execute(id(relay2_icon), relay_2->state);
- id: !extend set_var_bool
then:
- lambda: |-
if (component == "relay1_local_control") id(relay1_local) = val;
else if (component == "relay2_local_control") id(relay2_local) = val;
else if (component == "relay1_fallback") id(relay1_fallback) = val;
else if (component == "relay2_fallback") {
id(relay2_fallback) = val;
boot_progress->execute(${BOOT_STEP_HW_RELAYS}, "HW Relays");
}
- id: !extend set_var_number
then:
- lambda: |-
if (component == "relay_settings" and val > 0) {
uint8_t val_uint = static_cast<uint8_t>(val);
ESP_LOGW("script.set_var_number (HW Relays)", "Got relay's settings: %f -> %" PRIu8, val, val_uint);
// Relay 1 local control enabled (Bit 0)
id(relay1_local) = (val_uint & (1 << 0)) != 0;
Expand Down
18 changes: 9 additions & 9 deletions esphome/nspanel_esphome_core_hw_temperature.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ esphome:
platformio_options:
build_flags:
- -D NSPANEL_HA_BLUEPRINT_CORE_HW_TEMPERATURE
on_boot:
- priority: 900.1
then:
- lambda: |-
std::string temp_units = "${temp_units}";
id(temperature_is_celsius) = not (temp_units == "°F" ||
temp_units == "F" ||
temp_units == "°f" ||
temp_units == "f");

globals:
##### Is embedded sensor used for indoor temperature? #####
Expand Down Expand Up @@ -70,6 +61,15 @@ number:
- lambda: temp_nspanel->publish_state(temp_nspanel->raw_state);

script:
- id: !extend boot_initialize
then:
- lambda: |-
std::string temp_units = "${temp_units}";
id(temperature_is_celsius) = not (temp_units == "°F" ||
temp_units == "F" ||
temp_units == "°f" ||
temp_units == "f");
- id: !extend boot_progress_dump
then:
- lambda: |-
Expand Down
8 changes: 4 additions & 4 deletions esphome/nspanel_esphome_core_page_boot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ esphome:
platformio_options:
build_flags:
- -D NSPANEL_HA_BLUEPRINT_CORE_PAGE_BOOT
on_boot:
- priority: 601.0
then:
- script.execute: page_boot
# on_boot:
# - priority: 601.0
# then:
# - script.execute: page_boot

script:
- id: boot_log
Expand Down
6 changes: 6 additions & 0 deletions esphome/nspanel_esphome_core_page_confirm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ globals:
initial_value: '"${please_confirm}"'

script:
- id: !extend boot_initialize
then:
- lambda: |-
display_component_initialize->execute(${PAGE_ID_CONFIRM}, "title", true);
display_component_initialize->execute(${PAGE_ID_CONFIRM}, "body", true);
- id: !extend boot_progress_dump
then:
- lambda: |-
Expand Down
Loading

0 comments on commit c07bb2d

Please sign in to comment.