From f4f8ba7df91360319295d0661f67dfe870559028 Mon Sep 17 00:00:00 2001 From: balk77 Date: Wed, 25 Dec 2024 18:28:07 +0100 Subject: [PATCH 1/8] Addition of display item constructor --- package/base.yaml | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/package/base.yaml b/package/base.yaml index 94d30ab..8dfc8a6 100644 --- a/package/base.yaml +++ b/package/base.yaml @@ -147,4 +147,132 @@ script: id: menuselector value: !lambda 'return id(actual_page);' - script.execute: update_all + ## display item constructor + # requires all of the following parameters as input + # it requires fonts and colors to be defined + # can be called as lambda, for instance + # id(displayitem).execute(160,2,"External","size_3_label","orange",id(outside_temperature).state,1,"","size_3_value","white","°C","size_3_unit","grey","TOP_LEFT"); + # this will position a block with label "External" in size 3 and color orange + # below that a value with 1 decimal, in white, size 3 + # next to that unit "°C", size 3 in grey + # anchored at x,y = 160,2 top left corner + - id: displayitem + parameters: + x: int # x of anchor point + y: int # y of anchor point + label: string # label, to be shown above the value + labelfont: string # any of size_1_value, size_2_unit, size_3_label and everything in between + labelcolor: string # any of white, orange, red, green or grey + valuenum: float # value to display (numerical) + decimals: int # rounding of value to display + valuestr: string # if left empty it will select display the value float + valuefont: string # any of size_1_value, size_2_unit, size_3_label and everything in between + valuecolor: string # any of white, orange, red, green or grey + unit: string # displayed unit + unitfont: string # any of size_1_value, size_2_unit, size_3_label and everything in between + unitcolor: string # any of white, orange, red, green or grey + align: string # TOP_LEFT, TOP_CENTER or TOP_RIGHT + then: + lambda: |- + // Helper to get the font object from its ID string + // This lambda function maps font ID strings to their corresponding font objects. + // If the provided font_id matches a known font, it returns the appropriate font object. + // Otherwise, it returns nullptr. + auto get_font = [](const std::string &font_id) -> esphome::font::Font* { + if (font_id == "size_1_value") return id(size_1_value); + if (font_id == "size_1_label") return id(size_1_label); + if (font_id == "size_1_unit") return id(size_1_unit); + if (font_id == "size_2_value") return id(size_2_value); + if (font_id == "size_2_label") return id(size_2_label); + if (font_id == "size_2_unit") return id(size_2_unit); + if (font_id == "size_3_value") return id(size_3_value); + if (font_id == "size_3_label") return id(size_3_label); + if (font_id == "size_3_unit") return id(size_3_unit); + return nullptr; // Return nullptr if the font ID doesn't match any known font. + }; + + auto get_color = [](const std::string &color_id) -> esphome::Color { + if (color_id == "white") return id(white); + if (color_id == "orange") return id(orange); + if (color_id == "red") return id(red); + if (color_id == "green") return id(green); + if (color_id == "grey") return id(grey); + return esphome::Color::WHITE; // Default color if no match + }; + + esphome::Color labelcolor_script = get_color(labelcolor); + esphome::Color valuecolor_script = get_color(valuecolor); + esphome::Color unitcolor_script = get_color(unitcolor); + + // Resolve font objects based on the provided font IDs. + // These objects will later be used to measure text dimensions and render text on the display. + auto *labelfont_obj = get_font(labelfont); + auto *valuefont_obj = get_font(valuefont); + auto *unitfont_obj = get_font(unitfont); + + // Format the numeric value as a string with the specified number of decimals. + // snprintf safely formats the float into a fixed-width character buffer. + char value_str[40]; + // Check if valuestr is not empty + if (valuestr.empty()) { // Check if valuestr has any content + snprintf(value_str, sizeof(value_str), "%.*f", decimals, valuenum); + } else { + // If valuestr is empty, copy a default or alternative string into value_str + strncpy(value_str, valuestr.c_str(), sizeof(value_str)); // Example default value + value_str[sizeof(value_str) - 1] = '\0'; // Ensure null-termination + } + + // Helper function to calculate text dimensions. + // This lambda takes a font object and a text string as input, + // and it calculates the text's width and height (returned via reference parameters). + auto calculate_text_dimensions = [](esphome::font::Font *font, const char *text, int &width, int &baseline) { + int x_offset = 0, height = 0; + font->measure(text, &width, &x_offset, &baseline, &height); + }; + + // Calculate the dimensions of the label text. + int label_width = 0, label_height = 0; + calculate_text_dimensions(labelfont_obj, label.c_str(), label_width, label_height); + + // Calculate the dimensions of the value text. + int value_width = 0, value_height = 0; + calculate_text_dimensions(valuefont_obj, value_str, value_width, value_height); + + // Calculate the dimensions of the unit text. + int unit_width = 0, unit_height = 0; + calculate_text_dimensions(unitfont_obj, unit.c_str(), unit_width, unit_height); + + if (align == "TOP_RIGHT" ){ + // Render the label on the display at the specified position (x, y). + id(main_display).printf(x, y, labelfont_obj, labelcolor_script, TextAlign::TOP_RIGHT, "%s", label.c_str()); + + // Render the value below the label with a vertical offset equal to the label's height. + id(main_display).printf(x - unit_width, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_RIGHT, "%s", value_str); + + // Render the unit text to the right of the value text with appropriate offsets. + id(main_display).printf(x, y - 2 + label_height + value_height - unit_height, unitfont_obj, unitcolor_script, TextAlign::TOP_RIGHT, "%s", unit.c_str()); + } else if (align == "TOP_CENTER" ){ + // Calculate the maximum width of the display item, being the label or value+unit + // subtract half of that from the anchor point and position based on top left corner of the text fields + int offset = (std::max(label_width, value_width + unit_width) + 1) / 2; + + // Render the label on the display at the specified position (x, y). + id(main_display).printf(x - offset, y, labelfont_obj, labelcolor_script, TextAlign::TOP_LEFT, "%s", label.c_str()); + + // Render the value below the label with a vertical offset equal to the label's height. + id(main_display).printf(x - offset, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_LEFT, "%s", value_str); + + // Render the unit text to the right of the value text with appropriate offsets. + id(main_display).printf(x - offset + value_width, y -2+ label_height + value_height - unit_height, unitfont_obj, unitcolor_script, TextAlign::TOP_LEFT, "%s", unit.c_str()); + } else { + // Render the label on the display at the specified position (x, y). + id(main_display).printf(x, y, labelfont_obj, labelcolor_script, TextAlign::TOP_LEFT, "%s", label.c_str()); + + // Render the value below the label with a vertical offset equal to the label's height. + id(main_display).printf(x, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_LEFT, "%s", value_str); + + // Render the unit text to the right of the value text with appropriate offsets. + id(main_display).printf(x + value_width, y - 2 + label_height + value_height - unit_height , unitfont_obj, unitcolor_script, TextAlign::TOP_LEFT, "%s", unit.c_str()); + } + From 0446120b226b5055f8b5e38281bdd1917f25ac8f Mon Sep 17 00:00:00 2001 From: balk77 Date: Wed, 25 Dec 2024 18:33:52 +0100 Subject: [PATCH 2/8] Example display item --- Examples/3pages_TotalExample.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Examples/3pages_TotalExample.yml b/Examples/3pages_TotalExample.yml index b779ab2..5eb8f7a 100644 --- a/Examples/3pages_TotalExample.yml +++ b/Examples/3pages_TotalExample.yml @@ -496,6 +496,8 @@ display: switch (id(actual_page)){ case 0: id(containers).execute(); + // Example display item + id(displayitem).execute(160,2,"External","size_3_label","orange",id(outside_temperature).state,1,"","size_3_value","white","°C","size_3_unit","grey","TOP_LEFT"); break; case 1: id(music_card).execute(20, 125); From 302c4a88f8910c7af434dc2f7083a9e7deee6297 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sun, 29 Dec 2024 15:04:17 +0100 Subject: [PATCH 3/8] local settings should not be in the repo --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json From 95657fcb0818a797e3765563b02d8a017b5416a3 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sun, 29 Dec 2024 15:05:06 +0100 Subject: [PATCH 4/8] simplified/corrected some instructions on the music card --- package/cards/music.md | 2 +- package/cards/music.yaml | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/package/cards/music.md b/package/cards/music.md index a3a6989..b8c1f31 100644 --- a/package/cards/music.md +++ b/package/cards/music.md @@ -2,7 +2,7 @@ ## Usage -* Add the music card file to youre files and supply the `entity_id` +* Add the music card file to the package files and supply the `entity_id` ``` substitutions: music_card_enity_id: 'media_player.media_keuken_2' diff --git a/package/cards/music.yaml b/package/cards/music.yaml index ac54855..5f6f8f5 100644 --- a/package/cards/music.yaml +++ b/package/cards/music.yaml @@ -14,14 +14,8 @@ ## ## display: ## - id: !extend main_display -## pages: -## - id: player_page -## lambda: |- -## id(all_pages).execute(); -## // insert music card: -## // >> id(music_card).execute(x, y); -## id(music_card).execute(20, 125); -## - id: !extend bar1_left +## lambda: |- +## id(music_card).execute(20, 125); ## ##################################################### From 6bc1297eab2d17eec8d2f7d621acdc7e63c908e7 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sun, 29 Dec 2024 15:06:12 +0100 Subject: [PATCH 5/8] made it equal with the main branch --- package/base.yaml | 130 +--------------------------------------------- 1 file changed, 1 insertion(+), 129 deletions(-) diff --git a/package/base.yaml b/package/base.yaml index 8dfc8a6..514e3cb 100644 --- a/package/base.yaml +++ b/package/base.yaml @@ -147,132 +147,4 @@ script: id: menuselector value: !lambda 'return id(actual_page);' - script.execute: update_all - ## display item constructor - # requires all of the following parameters as input - # it requires fonts and colors to be defined - # can be called as lambda, for instance - # id(displayitem).execute(160,2,"External","size_3_label","orange",id(outside_temperature).state,1,"","size_3_value","white","°C","size_3_unit","grey","TOP_LEFT"); - # this will position a block with label "External" in size 3 and color orange - # below that a value with 1 decimal, in white, size 3 - # next to that unit "°C", size 3 in grey - # anchored at x,y = 160,2 top left corner - - id: displayitem - parameters: - x: int # x of anchor point - y: int # y of anchor point - label: string # label, to be shown above the value - labelfont: string # any of size_1_value, size_2_unit, size_3_label and everything in between - labelcolor: string # any of white, orange, red, green or grey - valuenum: float # value to display (numerical) - decimals: int # rounding of value to display - valuestr: string # if left empty it will select display the value float - valuefont: string # any of size_1_value, size_2_unit, size_3_label and everything in between - valuecolor: string # any of white, orange, red, green or grey - unit: string # displayed unit - unitfont: string # any of size_1_value, size_2_unit, size_3_label and everything in between - unitcolor: string # any of white, orange, red, green or grey - align: string # TOP_LEFT, TOP_CENTER or TOP_RIGHT - then: - lambda: |- - // Helper to get the font object from its ID string - // This lambda function maps font ID strings to their corresponding font objects. - // If the provided font_id matches a known font, it returns the appropriate font object. - // Otherwise, it returns nullptr. - auto get_font = [](const std::string &font_id) -> esphome::font::Font* { - if (font_id == "size_1_value") return id(size_1_value); - if (font_id == "size_1_label") return id(size_1_label); - if (font_id == "size_1_unit") return id(size_1_unit); - if (font_id == "size_2_value") return id(size_2_value); - if (font_id == "size_2_label") return id(size_2_label); - if (font_id == "size_2_unit") return id(size_2_unit); - if (font_id == "size_3_value") return id(size_3_value); - if (font_id == "size_3_label") return id(size_3_label); - if (font_id == "size_3_unit") return id(size_3_unit); - return nullptr; // Return nullptr if the font ID doesn't match any known font. - }; - - auto get_color = [](const std::string &color_id) -> esphome::Color { - if (color_id == "white") return id(white); - if (color_id == "orange") return id(orange); - if (color_id == "red") return id(red); - if (color_id == "green") return id(green); - if (color_id == "grey") return id(grey); - return esphome::Color::WHITE; // Default color if no match - }; - - esphome::Color labelcolor_script = get_color(labelcolor); - esphome::Color valuecolor_script = get_color(valuecolor); - esphome::Color unitcolor_script = get_color(unitcolor); - - // Resolve font objects based on the provided font IDs. - // These objects will later be used to measure text dimensions and render text on the display. - auto *labelfont_obj = get_font(labelfont); - auto *valuefont_obj = get_font(valuefont); - auto *unitfont_obj = get_font(unitfont); - - // Format the numeric value as a string with the specified number of decimals. - // snprintf safely formats the float into a fixed-width character buffer. - char value_str[40]; - // Check if valuestr is not empty - if (valuestr.empty()) { // Check if valuestr has any content - snprintf(value_str, sizeof(value_str), "%.*f", decimals, valuenum); - } else { - // If valuestr is empty, copy a default or alternative string into value_str - strncpy(value_str, valuestr.c_str(), sizeof(value_str)); // Example default value - value_str[sizeof(value_str) - 1] = '\0'; // Ensure null-termination - } - - // Helper function to calculate text dimensions. - // This lambda takes a font object and a text string as input, - // and it calculates the text's width and height (returned via reference parameters). - auto calculate_text_dimensions = [](esphome::font::Font *font, const char *text, int &width, int &baseline) { - int x_offset = 0, height = 0; - font->measure(text, &width, &x_offset, &baseline, &height); - }; - - // Calculate the dimensions of the label text. - int label_width = 0, label_height = 0; - calculate_text_dimensions(labelfont_obj, label.c_str(), label_width, label_height); - - // Calculate the dimensions of the value text. - int value_width = 0, value_height = 0; - calculate_text_dimensions(valuefont_obj, value_str, value_width, value_height); - - // Calculate the dimensions of the unit text. - int unit_width = 0, unit_height = 0; - calculate_text_dimensions(unitfont_obj, unit.c_str(), unit_width, unit_height); - - if (align == "TOP_RIGHT" ){ - // Render the label on the display at the specified position (x, y). - id(main_display).printf(x, y, labelfont_obj, labelcolor_script, TextAlign::TOP_RIGHT, "%s", label.c_str()); - - // Render the value below the label with a vertical offset equal to the label's height. - id(main_display).printf(x - unit_width, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_RIGHT, "%s", value_str); - - // Render the unit text to the right of the value text with appropriate offsets. - id(main_display).printf(x, y - 2 + label_height + value_height - unit_height, unitfont_obj, unitcolor_script, TextAlign::TOP_RIGHT, "%s", unit.c_str()); - } else if (align == "TOP_CENTER" ){ - // Calculate the maximum width of the display item, being the label or value+unit - // subtract half of that from the anchor point and position based on top left corner of the text fields - int offset = (std::max(label_width, value_width + unit_width) + 1) / 2; - - // Render the label on the display at the specified position (x, y). - id(main_display).printf(x - offset, y, labelfont_obj, labelcolor_script, TextAlign::TOP_LEFT, "%s", label.c_str()); - - // Render the value below the label with a vertical offset equal to the label's height. - id(main_display).printf(x - offset, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_LEFT, "%s", value_str); - - // Render the unit text to the right of the value text with appropriate offsets. - id(main_display).printf(x - offset + value_width, y -2+ label_height + value_height - unit_height, unitfont_obj, unitcolor_script, TextAlign::TOP_LEFT, "%s", unit.c_str()); - } else { - // Render the label on the display at the specified position (x, y). - id(main_display).printf(x, y, labelfont_obj, labelcolor_script, TextAlign::TOP_LEFT, "%s", label.c_str()); - - // Render the value below the label with a vertical offset equal to the label's height. - id(main_display).printf(x, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_LEFT, "%s", value_str); - - // Render the unit text to the right of the value text with appropriate offsets. - id(main_display).printf(x + value_width, y - 2 + label_height + value_height - unit_height , unitfont_obj, unitcolor_script, TextAlign::TOP_LEFT, "%s", unit.c_str()); - } - - + \ No newline at end of file From 3b5cbb362c38a7fe1a44205d3762b44051342a54 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sun, 29 Dec 2024 15:06:38 +0100 Subject: [PATCH 6/8] added display item card --- package/cards/display_item.md | 38 ++++++++ package/cards/display_item.yaml | 155 ++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 package/cards/display_item.md create mode 100644 package/cards/display_item.yaml diff --git a/package/cards/display_item.md b/package/cards/display_item.md new file mode 100644 index 0000000..2b7fe3e --- /dev/null +++ b/package/cards/display_item.md @@ -0,0 +1,38 @@ +# ESPHome Button+ - Music card + +## Usage + +* Add the music card file to the package files + ``` + package: + remote_package_files: + url: https://github.com/dixi83/ESPHome_ButtonPlus + files: [,'package/cards/display_item.yaml'] + ``` +* Add the display item card to the (main) display `lambda` and give it the position and [parameters](#parameters) you want + It requires fonts and colors to be defined, this can be called as lambda, for instance: + ``` + //id(display_item).execute(x, y, label, labelfont, labelcolor, valuenum, decimals, valuestr, valuefont, valuecolor, unit, unitfont, unitcolor, align) + id(display_item).execute(160, 2, "External", "size_3_label", "orange", id(outside_temperature).state, 1, "", "size_3_value", "white", "°C", "size_3_unit", "grey", "TOP_LEFT"); + ``` + This will position a block with label "External" in size 3 and color orange, below that a value with 1 decimal, in white, size 3. Next to that unit "°C", size 3 in grey, anchored at x,y = 160,2 top left corner + + +## Parameters: + +``` +x: int x of anchor point +y: int y of anchor point +label: string label, to be shown above the value +labelfont: string any of size_1_value, size_2_unit, size_3_label and everything in between +labelcolor: string any of white, orange, red, green or grey +valuenum: float value to display (numerical) +decimals: int rounding of value to display +valuestr: string if left empty it will select display the value float +valuefont: string any of size_1_value, size_2_unit, size_3_label and everything in between +valuecolor: string any of white, orange, red, green or grey +unit: string displayed unit +unitfont: string any of size_1_value, size_2_unit, size_3_label and everything in between +unitcolor: string any of white, orange, red, green or grey +align: string TOP_LEFT, TOP_CENTER or TOP_RIGHT +``` diff --git a/package/cards/display_item.yaml b/package/cards/display_item.yaml new file mode 100644 index 0000000..444f976 --- /dev/null +++ b/package/cards/display_item.yaml @@ -0,0 +1,155 @@ + +##################################################### +## +## FILENAME: cards/display_item.yaml +## NAME: Display item constructor +## DESCRIPTION: creates display items like +## the original firmware +## PARAMETERS: +## x: int x of anchor point +## y: int y of anchor point +## label: string label, to be shown above the value +## labelfont: string any of size_1_value, size_2_unit, size_3_label and everything in between +## labelcolor: string any of white, orange, red, green or grey +## valuenum: float value to display (numerical) +## decimals: int rounding of value to display +## valuestr: string if left empty it will select display the value float +## valuefont: string any of size_1_value, size_2_unit, size_3_label and everything in between +## valuecolor: string any of white, orange, red, green or grey +## unit: string displayed unit +## unitfont: string any of size_1_value, size_2_unit, size_3_label and everything in between +## unitcolor: string any of white, orange, red, green or grey +## align: string TOP_LEFT, TOP_CENTER or TOP_RIGHT +## USAGE: +## package: +## remote_package_files: +## url: https://github.com/dixi83/ESPHome_ButtonPlus +## files: [,'package/cards/display_item.yaml'] +## +## display: +## - id: !extend main_display +## lambda: |- +## id(display_item).execute(160,2,"External","size_3_label","orange",id(outside_temperature).state,1,"","size_3_value","white","°C","size_3_unit","grey","TOP_LEFT"); +## +##################################################### + +script: + - id: display_item + parameters: + x: int # x of anchor point + y: int # y of anchor point + label: string # label, to be shown above the value + labelfont: string # any of size_1_value, size_2_unit, size_3_label and everything in between + labelcolor: string # any of white, orange, red, green or grey + valuenum: float # value to display (numerical) + decimals: int # rounding of value to display + valuestr: string # if left empty it will select display the value float + valuefont: string # any of size_1_value, size_2_unit, size_3_label and everything in between + valuecolor: string # any of white, orange, red, green or grey + unit: string # displayed unit + unitfont: string # any of size_1_value, size_2_unit, size_3_label and everything in between + unitcolor: string # any of white, orange, red, green or grey + align: string # TOP_LEFT, TOP_CENTER or TOP_RIGHT + then: + lambda: |- + // Helper to get the font object from its ID string + // This lambda function maps font ID strings to their corresponding font objects. + // If the provided font_id matches a known font, it returns the appropriate font object. + // Otherwise, it returns nullptr. + auto get_font = [](const std::string &font_id) -> esphome::font::Font* { + if (font_id == "size_1_value") return id(size_1_value); + if (font_id == "size_1_label") return id(size_1_label); + if (font_id == "size_1_unit") return id(size_1_unit); + if (font_id == "size_2_value") return id(size_2_value); + if (font_id == "size_2_label") return id(size_2_label); + if (font_id == "size_2_unit") return id(size_2_unit); + if (font_id == "size_3_value") return id(size_3_value); + if (font_id == "size_3_label") return id(size_3_label); + if (font_id == "size_3_unit") return id(size_3_unit); + return nullptr; // Return nullptr if the font ID doesn't match any known font. + }; + + auto get_color = [](const std::string &color_id) -> esphome::Color { + if (color_id == "white") return id(white); + if (color_id == "orange") return id(orange); + if (color_id == "red") return id(red); + if (color_id == "green") return id(green); + if (color_id == "grey") return id(grey); + return esphome::Color::WHITE; // Default color if no match + }; + + esphome::Color labelcolor_script = get_color(labelcolor); + esphome::Color valuecolor_script = get_color(valuecolor); + esphome::Color unitcolor_script = get_color(unitcolor); + + // Resolve font objects based on the provided font IDs. + // These objects will later be used to measure text dimensions and render text on the display. + auto *labelfont_obj = get_font(labelfont); + auto *valuefont_obj = get_font(valuefont); + auto *unitfont_obj = get_font(unitfont); + + // Format the numeric value as a string with the specified number of decimals. + // snprintf safely formats the float into a fixed-width character buffer. + char value_str[40]; + // Check if valuestr is not empty + if (valuestr.empty()) { // Check if valuestr has any content + snprintf(value_str, sizeof(value_str), "%.*f", decimals, valuenum); + } else { + // If valuestr is empty, copy a default or alternative string into value_str + strncpy(value_str, valuestr.c_str(), sizeof(value_str)); // Example default value + value_str[sizeof(value_str) - 1] = '\0'; // Ensure null-termination + } + + // Helper function to calculate text dimensions. + // This lambda takes a font object and a text string as input, + // and it calculates the text's width and height (returned via reference parameters). + auto calculate_text_dimensions = [](esphome::font::Font *font, const char *text, int &width, int &baseline) { + int x_offset = 0, height = 0; + font->measure(text, &width, &x_offset, &baseline, &height); + }; + + // Calculate the dimensions of the label text. + int label_width = 0, label_height = 0; + calculate_text_dimensions(labelfont_obj, label.c_str(), label_width, label_height); + + // Calculate the dimensions of the value text. + int value_width = 0, value_height = 0; + calculate_text_dimensions(valuefont_obj, value_str, value_width, value_height); + + // Calculate the dimensions of the unit text. + int unit_width = 0, unit_height = 0; + calculate_text_dimensions(unitfont_obj, unit.c_str(), unit_width, unit_height); + + if (align == "TOP_RIGHT" ){ + // Render the label on the display at the specified position (x, y). + id(main_display).printf(x, y, labelfont_obj, labelcolor_script, TextAlign::TOP_RIGHT, "%s", label.c_str()); + + // Render the value below the label with a vertical offset equal to the label's height. + id(main_display).printf(x - unit_width, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_RIGHT, "%s", value_str); + + // Render the unit text to the right of the value text with appropriate offsets. + id(main_display).printf(x, y - 2 + label_height + value_height - unit_height, unitfont_obj, unitcolor_script, TextAlign::TOP_RIGHT, "%s", unit.c_str()); + } else if (align == "TOP_CENTER" ){ + // Calculate the maximum width of the display item, being the label or value+unit + // subtract half of that from the anchor point and position based on top left corner of the text fields + int offset = (std::max(label_width, value_width + unit_width) + 1) / 2; + + // Render the label on the display at the specified position (x, y). + id(main_display).printf(x - offset, y, labelfont_obj, labelcolor_script, TextAlign::TOP_LEFT, "%s", label.c_str()); + + // Render the value below the label with a vertical offset equal to the label's height. + id(main_display).printf(x - offset, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_LEFT, "%s", value_str); + + // Render the unit text to the right of the value text with appropriate offsets. + id(main_display).printf(x - offset + value_width, y -2+ label_height + value_height - unit_height, unitfont_obj, unitcolor_script, TextAlign::TOP_LEFT, "%s", unit.c_str()); + } else { + // Render the label on the display at the specified position (x, y). + id(main_display).printf(x, y, labelfont_obj, labelcolor_script, TextAlign::TOP_LEFT, "%s", label.c_str()); + + // Render the value below the label with a vertical offset equal to the label's height. + id(main_display).printf(x, y - 2 + label_height , valuefont_obj, valuecolor_script, TextAlign::TOP_LEFT, "%s", value_str); + + // Render the unit text to the right of the value text with appropriate offsets. + id(main_display).printf(x + value_width, y - 2 + label_height + value_height - unit_height , unitfont_obj, unitcolor_script, TextAlign::TOP_LEFT, "%s", unit.c_str()); + } + \ No newline at end of file From 6525a0b40073133910bfa068e003f236fc65f1a8 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sun, 29 Dec 2024 15:54:43 +0100 Subject: [PATCH 7/8] Update base.yaml --- package/base.yaml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/package/base.yaml b/package/base.yaml index 514e3cb..6edfb66 100644 --- a/package/base.yaml +++ b/package/base.yaml @@ -143,8 +143,4 @@ script: - text_sensor.template.publish: id: active_page_name_ha state: !lambda 'return id(active_page_name);' - - number.set: - id: menuselector - value: !lambda 'return id(actual_page);' - - script.execute: update_all - \ No newline at end of file + From cc88e7ba21dbfaeabf8a80d385308b233d4bc144 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sun, 29 Dec 2024 16:03:09 +0100 Subject: [PATCH 8/8] Update base.yaml --- package/base.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/package/base.yaml b/package/base.yaml index 6edfb66..34d67a6 100644 --- a/package/base.yaml +++ b/package/base.yaml @@ -143,4 +143,3 @@ script: - text_sensor.template.publish: id: active_page_name_ha state: !lambda 'return id(active_page_name);' -