From 3d0ab2259c55e06cddd7aebfaaf3a331220f77af Mon Sep 17 00:00:00 2001 From: vtHydroponics <132159689+vtHydroponics@users.noreply.github.com> Date: Sat, 2 Dec 2023 06:35:56 -0500 Subject: [PATCH] Sensor57 TSL2591 Light Sensor Commands (#20039) * Changes from 11/12/2023. Switches made, need debug * Finalized gain/integration adjustment trees * Fixed the bugs * Outputs in JSON, hex displays as hex was decimal --- .../tasmota_xsns_sensor/xsns_57_tsl2591.ino | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tasmota/tasmota_xsns_sensor/xsns_57_tsl2591.ino b/tasmota/tasmota_xsns_sensor/xsns_57_tsl2591.ino index 57d70f992..37f456bea 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_57_tsl2591.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_57_tsl2591.ino @@ -39,6 +39,9 @@ uint8_t tsl2591_type = 0; uint8_t tsl2591_valid = 0; float tsl2591_lux = 0; +tsl2591Gain_t gain_enum_array[4] = {TSL2591_GAIN_LOW,TSL2591_GAIN_MED,TSL2591_GAIN_HIGH,TSL2591_GAIN_MAX}; +tsl2591IntegrationTime_t int_enum_array[6] = {TSL2591_INTEGRATIONTIME_100MS,TSL2591_INTEGRATIONTIME_200MS,TSL2591_INTEGRATIONTIME_300MS,TSL2591_INTEGRATIONTIME_400MS,TSL2591_INTEGRATIONTIME_500MS,TSL2591_INTEGRATIONTIME_600MS}; + void Tsl2591Init(void) { // if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) { @@ -90,6 +93,26 @@ void Tsl2591Show(bool json) } } +bool tsl2591CommandSensor() { + bool serviced = true; + char argument[XdrvMailbox.data_len]; + long value = 0; + + for (uint32_t ca = 0; ca < XdrvMailbox.data_len; ca++) { + if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; } + } + + bool any_value = (strchr(XdrvMailbox.data, ',') != nullptr); + if (any_value) { value = strtol(ArgV(argument, 2), nullptr, 10); } + + tsl.setGain(gain_enum_array[XdrvMailbox.payload-1]); + tsl.setTiming(int_enum_array[value-1]); + + Response_P(PSTR("{\"Gain input\":%d,\"Gain hex\":0x%x,"),XdrvMailbox.payload,tsl.getGain()); + ResponseAppend_P(PSTR("\"Timing input\":%d,\"Timing hex\":0x0%x}"),value,tsl.getTiming()); + return serviced; +} + /*********************************************************************************************\ * Interface \*********************************************************************************************/ @@ -116,6 +139,11 @@ bool Xsns57(uint32_t function) Tsl2591Show(0); break; #endif // USE_WEBSERVER + case FUNC_COMMAND_SENSOR: + if (XSNS_57 == XdrvMailbox.index) { + result = tsl2591CommandSensor(); + } + break; } } return result;