diff --git a/examples/companion_radio/ui-new/SensorPlaceholders.h b/examples/companion_radio/ui-new/SensorPlaceholders.h index e244b236..c1b652a7 100644 --- a/examples/companion_radio/ui-new/SensorPlaceholders.h +++ b/examples/companion_radio/ui-new/SensorPlaceholders.h @@ -1,7 +1,7 @@ #pragma once -// Populates a KeyboardWidget's placeholder list with sensor types currently -// detected by SensorManager. Always adds {loc} and {time} via begin(); this -// helper appends the sensor-specific entries on top. +// Populates a KeyboardWidget's placeholder list based on live sensor data. +// Only adds placeholders for types that actually returned a value right now. +// Always adds {loc} and {time} via begin(); this helper appends the rest. #include "KeyboardWidget.h" #include @@ -9,8 +9,23 @@ inline void kbAddSensorPlaceholders(KeyboardWidget& kb, SensorManager* sm) { if (!sm) return; - uint8_t types[16]; - int tc = sm->getAvailableLPPTypes(types, 16); + + CayenneLPP lpp(100); + sm->querySensors(0xFF, lpp); + + // collect unique LPP types that have actual data + uint8_t present[16]; + int pc = 0; + { + LPPReader r(lpp.getBuffer(), lpp.getSize()); + uint8_t ch, type; + while (r.readHeader(ch, type)) { + bool already = false; + for (int i = 0; i < pc; i++) if (present[i] == type) { already = true; break; } + if (!already && pc < 16) present[pc++] = type; + r.skipData(type); + } + } static const struct { uint8_t t; const char* ph; } MAP[] = { { LPP_TEMPERATURE, "{temp}" }, @@ -24,8 +39,8 @@ inline void kbAddSensorPlaceholders(KeyboardWidget& kb, SensorManager* sm) { }; for (const auto& m : MAP) { - for (int i = 0; i < tc; i++) { - if (types[i] == m.t) { kb.addPlaceholder(m.ph); break; } + for (int i = 0; i < pc; i++) { + if (present[i] == m.t) { kb.addPlaceholder(m.ph); break; } } } }