Move components to root

This commit is contained in:
Edward Firmo
2024-02-22 23:56:16 +01:00
parent b96ef8fde2
commit 02d571703f
6 changed files with 1 additions and 1 deletions

View File

@@ -0,0 +1,18 @@
# __init__.py
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.core import coroutine_with_priority
CODEOWNERS = ["@edwardtfn"]
nspanel_ha_blueprint_ns = cg.esphome_ns.namespace('nspanel_ha_blueprint')
CONFIG_SCHEMA = cv.All(
cv.Schema({}),
)
@coroutine_with_priority(1.0)
async def to_code(config):
cg.add_define("USE_NSPANEL_HA_BLUEPRINT")
cg.add_global(nspanel_ha_blueprint_ns.using)

View File

@@ -0,0 +1,67 @@
// page_names.h
#pragma once
#include <array>
#include <cstring>
#include <cstdint>
namespace nspanel_ha_blueprint {
/**
* @file page_names.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

View File

@@ -0,0 +1,44 @@
// relays.h
#pragma once
#include <cstdint>
namespace nspanel_ha_blueprint {
/**
* @enum RelaySettings
* Represents the settings for relays as individual bits within a uint8_t value. Each
* enum value corresponds to a specific bit position that represents a distinct setting
* for the relays. This allows for efficient storage and manipulation of multiple relay
* settings within a single byte.
*
* Bits are allocated as follows:
* - Bit 0: Relay 1 - Local control enabled.
* - Bit 1: Relay 1 - Fallback mode enabled.
* - Bits 2-3: Reserved for future use.
* - Bit 4: Relay 2 - Local control enabled.
* - Bit 5: Relay 2 - Fallback mode enabled.
* - Bits 6-7: Reserved for future use.
*
* Usage involves bitwise operations to set, clear, and check these settings within a
* uint8_t variable. This approach enables compact representation and easy manipulation
* of relay settings.
*/
enum RelaySettings {
Relay1_Local = 1 << 0, ///< Bit 0: Enables local control for Relay 1.
Relay1_Fallback = 1 << 1, ///< Bit 1: Enables fallback mode for Relay 1.
// Bits 2 and 3 are reserved for future expansion.
Relay2_Local = 1 << 4, ///< Bit 4: Enables local control for Relay 2.
Relay2_Fallback = 1 << 5, ///< Bit 5: Enables fallback mode for Relay 2.
// Bits 6 and 7 are reserved for future expansion.
};
void update_relay_setting(uint8_t& settings, bool condition, RelaySettings flag) {
if (condition) {
settings |= flag; // Set bit
} else {
settings &= ~flag; // Clear bit
}
}
} // namespace nspanel_ha_blueprint

View File

@@ -0,0 +1,48 @@
// text.h
#pragma once
#include <cstring>
#include <string>
namespace nspanel_ha_blueprint {
/**
* Copies the contents of a std::string to a fixed-size char array, ensuring
* null termination of the string within the array. This function template
* automatically deduces the size of the destination char array at compile time,
* minimizing the risk of buffer overflow. It's designed for use with fixed-size
* char arrays only, not pointers or dynamically allocated memory.
*
* Template Parameter:
* N - The size of the destination char array. This value is deduced automatically
* and must be greater than 0.
*
* Parameters:
* dest - A reference to the destination char array where the string should be copied.
* The array must have a size that can accommodate the source string plus a
* null terminator. If the source string is longer than the destination array,
* it will be truncated.
* src - The source std::string to copy. This string's contents are copied into the
* destination array up to the array's capacity minus one, to leave space for
* the null terminator.
*
* Usage Example:
* char destination[11];
* std::string source = "Hello";
* nspanel_ha_blueprint::copyStringToCharArray(destination, source);
*
* Note: The destination array is always null-terminated, even if the source string
* is truncated to fit into the array.
*/
template<size_t N>
void copyStringToCharArray(char (&dest)[N], const std::string& src) {
static_assert(N > 0, "Destination array size must be greater than 0.");
// Copy up to N - 1 characters to ensure there's room for the null terminator
std::strncpy(dest, src.c_str(), N - 1);
// Manually null-terminate the destination array
dest[N - 1] = '\0';
}
} // namespace nspanel_ha_blueprint

View File

@@ -0,0 +1,25 @@
// versioning.h
#pragma once
#include <cstdio> // For sscanf
namespace nspanel_ha_blueprint {
/**
* Compares two version strings by major and minor version numbers.
* Assumes version strings are in the format "major.minor".
*
* @param version1 First version string to compare.
* @param version2 Second version string to compare.
* @return true if the major and minor versions are equal, false otherwise.
*/
inline bool compare_versions(const char* version1, const char* version2) {
int major1 = 0, minor1 = 0;
int major2 = 0, minor2 = 0;
sscanf(version1, "%d.%d", &major1, &minor1);
sscanf(version2, "%d.%d", &major2, &minor2);
return (major1 == major2) && (minor1 == minor2);
}
} // namespace nspanel_ha_blueprint