From cc08228936ddded748989a71f35dfbda8fb95c71 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 Oct 2026 21:52:02 -1000 Subject: [PATCH] Store the gain and time tables as uint16_t so reads need no enum cast through a pointer --- esphome/components/veml7700/veml7700.cpp | 27 ++++++++++-------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/esphome/components/veml7700/veml7700.cpp b/esphome/components/veml7700/veml7700.cpp index 2700d105122..5654b5703f5 100644 --- a/esphome/components/veml7700/veml7700.cpp +++ b/esphome/components/veml7700/veml7700.cpp @@ -12,17 +12,12 @@ static const size_t VEML_REG_SIZE = 2; static float reduce_to_zero(float a, float b) { return (a > b) ? (a - b) : 0; } -// The tables are 16-bit enums in PROGMEM, so entries are read with progmem_read_uint16() -template static T read_entry(const T *entry) { - static_assert(sizeof(T) == sizeof(uint16_t)); - return static_cast(progmem_read_uint16(reinterpret_cast(entry))); -} - -template T get_next(const T (&array)[size], const T val) { +// The tables hold the 16-bit enum values as uint16_t in PROGMEM; entries are read with progmem_read_uint16() +template T get_next(const uint16_t (&array)[size], const T val) { size_t i = 0; size_t idx = std::numeric_limits::max(); while (idx == std::numeric_limits::max() && i < size) { - if (read_entry(&array[i]) == val) { + if (progmem_read_uint16(&array[i]) == val) { idx = i; break; } @@ -30,14 +25,14 @@ template T get_next(const T (&array)[size], const T val } if (idx == std::numeric_limits::max() || i + 1 >= size) return val; - return read_entry(&array[i + 1]); + return static_cast(progmem_read_uint16(&array[i + 1])); } -template T get_prev(const T (&array)[size], const T val) { +template T get_prev(const uint16_t (&array)[size], const T val) { size_t i = size - 1; size_t idx = std::numeric_limits::max(); while (idx == std::numeric_limits::max() && i > 0) { - if (read_entry(&array[i]) == val) { + if (progmem_read_uint16(&array[i]) == val) { idx = i; break; } @@ -45,7 +40,7 @@ template T get_prev(const T (&array)[size], const T val } if (idx == std::numeric_limits::max() || i == 0) return val; - return read_entry(&array[i - 1]); + return static_cast(progmem_read_uint16(&array[i - 1])); } static uint16_t get_itime_ms(IntegrationTime time) { @@ -325,10 +320,10 @@ bool VEML7700Component::are_adjustments_required_(Readings &data) { static constexpr uint16_t LOW_INTENSITY_THRESHOLD = 100; static constexpr uint16_t HIGH_INTENSITY_THRESHOLD = 10000; - static constexpr IntegrationTime TIMES[INTEGRATION_TIMES_COUNT] PROGMEM = { - INTEGRATION_TIME_25MS, INTEGRATION_TIME_50MS, INTEGRATION_TIME_100MS, - INTEGRATION_TIME_200MS, INTEGRATION_TIME_400MS, INTEGRATION_TIME_800MS}; - static constexpr Gain GAINS[GAINS_COUNT] PROGMEM = {X_1_8, X_1_4, X_1, X_2}; + static constexpr uint16_t TIMES[INTEGRATION_TIMES_COUNT] PROGMEM = {INTEGRATION_TIME_25MS, INTEGRATION_TIME_50MS, + INTEGRATION_TIME_100MS, INTEGRATION_TIME_200MS, + INTEGRATION_TIME_400MS, INTEGRATION_TIME_800MS}; + static constexpr uint16_t GAINS[GAINS_COUNT] PROGMEM = {X_1_8, X_1_4, X_1, X_2}; if (data.als_counts <= LOW_INTENSITY_THRESHOLD) { Gain next_gain = get_next(GAINS, data.actual_gain);