Store the gain and time tables as uint16_t so reads need no enum cast through a pointer

This commit is contained in:
J. Nick Koston
2026-10-13 21:52:02 -10:00
parent b2d57cf2be
commit cc08228936
+11 -16
View File
@@ -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<typename T> static T read_entry(const T *entry) {
static_assert(sizeof(T) == sizeof(uint16_t));
return static_cast<T>(progmem_read_uint16(reinterpret_cast<const uint16_t *>(entry)));
}
template<typename T, size_t size> 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<typename T, size_t size> T get_next(const uint16_t (&array)[size], const T val) {
size_t i = 0;
size_t idx = std::numeric_limits<size_t>::max();
while (idx == std::numeric_limits<size_t>::max() && i < size) {
if (read_entry(&array[i]) == val) {
if (progmem_read_uint16(&array[i]) == val) {
idx = i;
break;
}
@@ -30,14 +25,14 @@ template<typename T, size_t size> T get_next(const T (&array)[size], const T val
}
if (idx == std::numeric_limits<size_t>::max() || i + 1 >= size)
return val;
return read_entry(&array[i + 1]);
return static_cast<T>(progmem_read_uint16(&array[i + 1]));
}
template<typename T, size_t size> T get_prev(const T (&array)[size], const T val) {
template<typename T, size_t size> T get_prev(const uint16_t (&array)[size], const T val) {
size_t i = size - 1;
size_t idx = std::numeric_limits<size_t>::max();
while (idx == std::numeric_limits<size_t>::max() && i > 0) {
if (read_entry(&array[i]) == val) {
if (progmem_read_uint16(&array[i]) == val) {
idx = i;
break;
}
@@ -45,7 +40,7 @@ template<typename T, size_t size> T get_prev(const T (&array)[size], const T val
}
if (idx == std::numeric_limits<size_t>::max() || i == 0)
return val;
return read_entry(&array[i - 1]);
return static_cast<T>(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);