Add nextion_components.h

This commit is contained in:
Edward Firmo
2024-02-23 12:04:47 +01:00
parent 7f09156414
commit 55ea00b50a
5 changed files with 254 additions and 126 deletions

View File

@@ -0,0 +1,51 @@
// nextion_components.h
#pragma once
#include <string>
namespace nspanel_ha_blueprint {
struct PageComponent {
std::string page;
std::string component_id;
bool is_current_page;
};
/**
* Extracts the page name and component ID from a given input string.
* Handles a special case where "alarm_control_panel" should be shortened to "alarm".
*
* @param input The input string containing either the combined page and component ID or just the component ID.
* @param defaultPage The default page name to use if the input string does not specify a page.
* @return A PageComponent struct containing the extracted or default page name, the component ID, and a flag indicating if it's the current page.
*/
PageComponent extractPageAndComponent(const std::string& input, const std::string& defaultPage) {
size_t dotPos = input.find(".");
PageComponent result;
if (dotPos != std::string::npos) {
// Extract page and component_id from the input string
result.page = input.substr(0, dotPos);
result.component_id = input.substr(dotPos + 1);
result.is_current_page = false; // Since there's a dot, it's assumed not to be the current page
// Check for the special case of "alarm_control_panel"
if (result.page == "alarm_control_panel") {
result.page = "alarm";
}
} else {
// No dot found, the entire input is considered as component_id
result.page = defaultPage;
result.component_id = input;
result.is_current_page = true; // No specific page mentioned, so it's the current page
}
// Check if the resolved page matches the defaultPage indicating it's the current page
if (result.page == defaultPage) {
result.is_current_page = true;
}
return result;
}
} // namespace nspanel_ha_blueprint

View File

@@ -0,0 +1,68 @@
// pages.h
#pragma once
#include <array>
#include <cstring>
#include <cstdint>
#include <utility> // For std::pair
namespace nspanel_ha_blueprint {
/**
* @file pages.h
* Defines constants and functions related to page names for "NSPanel HA Blueprint" project..
*/
// Constants
/**
* A compile-time constant array containing the names of pages.
* These names correspond to various pages of the Nextion TFT file in use,
* such as settings, home, weather information, and more.
*/
constexpr std::array<const char*, 27> page_names = {
"home",
"weather01",
"weather02",
"weather03",
"weather04",
"weather05",
"climate",
"settings",
"boot",
"screensaver",
"light",
"cover",
"buttonpage01",
"buttonpage02",
"buttonpage03",
"buttonpage04",
"notification",
"qrcode",
"entitypage01",
"entitypage02",
"entitypage03",
"entitypage04",
"fan",
"alarm",
"keyb_num",
"media_player",
"confirm"
};
/**
* Retrieves the index of a given page name within the page_names array.
*
* @param page_name The name of the page to find.
* @return The index of the page_name in the page_names array. If the page_name
* is not found, returns UINT8_MAX as an indicator that no matching page was found.
*/
inline uint8_t get_page_id(const std::string& page_name) {
for (uint8_t i = 0; i < page_names.size(); ++i) {
if (strcmp(page_names[i], page_name.c_str()) == 0) {
return i; // Return the index if found
}
}
return UINT8_MAX; // Return UINT8_MAX if not found
}
} // namespace nspanel_ha_blueprint