diff --git a/docs/source/Plugin/P110.rst b/docs/source/Plugin/P110.rst index c6b81d6366..ca273878be 100644 --- a/docs/source/Plugin/P110.rst +++ b/docs/source/Plugin/P110.rst @@ -31,21 +31,21 @@ Configuration .. image:: P110_DeviceConfiguration.png -**Name** A unique name should be entered here. +* **Name** A unique name should be entered here. -**Enabled** The device can be disabled or enabled. When not enabled the device should not use any resources. +* **Enabled** The device can be disabled or enabled. When not enabled the device should not use any resources. I2C Options ^^^^^^^^^^^^ The available settings here depend on the build used. At least the **Force Slow I2C speed** option is available, but selections for the I2C Multiplexer can also be shown. For details see the :ref:`Hardware_page` -**I2C Address**: The address the device is using. Some boards holding this chip offer an extra connection SDO that can be used to select the address, other boards just allow to select the secondary address and keep that active until the next power-cycle of the sensor. +* **I2C Address**: The address the device is using. Some boards holding this chip offer an extra connection SDO that can be used to select the address, other boards just allow to select the secondary address and keep that active until the next power-cycle of the sensor. Device Settings ^^^^^^^^^^^^^^^^ -**Timing**: The timing setting of the sensor determines the accuracy of the measurement. There are 3 options available: +* **Timing**: The timing setting of the sensor determines the accuracy of the measurement. There are 3 options available: .. image:: P110_TimingOptions.png @@ -55,7 +55,7 @@ Device Settings *Accurate* A slower but far more accurate measurement (320 msec.) For use with a longer read interval setting (30-60 sec). -**Range**: the measuring ranges: +* **Range**: the measuring ranges: .. image:: P110_RangeOptions.png @@ -63,12 +63,22 @@ Device Settings *Long* For measurements in the 0 to 2000 millimeter range (but somewhat less accurate). +* **Send event when value unchanged**: When checked, will generate events for every **Interval**, when unchecked *and* **Interval** is set to 0, a change in **Distance** will immediately trigger events. (NB: Behavior changed since 2024/04/27, when this option was added) + +* **Trigger delta** To avoid triggering many events with only a small difference in distance the 'Trigger delta' option is available. This can be set to only trigger an event when the new distance is at least the delta less or more than the previous measurement. + +NB: This setting is ignored if 'Send event when value unchanged' is checked! +n The Data Acquisition, Send to Controller and Interval settings are standard available configuration items. Send to Controller only when one or more Controllers are configured. +``Interval`` can now be set to 0, causing events to only be generated when changed, with ``Trigger delta`` calculated in. + Values ^^^^^^ -There is only 1 value available for this sensor, with the default name ``Distance``. A formula can be set to recalculate, f.e. to centimeters using ``%value%/10``. The number of decimals is by default set to 2, but for use with millimeters distance it can be set to 0, as no decimals are provided from the measurement. +There are 2 values available for this sensor, with the default name ``Distance`` and ``Direction``. A formula can be set to recalculate the distance, f.e. to centimeters using ``%value%/10``. The number of decimals can be set to 0, when using millimeters, as no decimals are provided from the measurement. + +Value ``Direction`` holds the direction relative to the *previous* distance value: ``-1`` = smaller distance, ``0`` = unchanged distance, ``1`` = greater distance. .. Events .. ~~~~~~ @@ -83,6 +93,8 @@ Change log .. versionchanged:: 2.0 ... + |added| 2024-04-27 Add options for ``Send event when value unchanged`` and ``Trigger delta``, causing somewhat changed behavior. + |added| 2021-02-06 Moved to main repository Plugin 110 from PluginPlayground Plugin 133 |added| 2021-02-06 Refactoring to allow multiple instances of the plugin (when using an I2C multiplexer) diff --git a/docs/source/Plugin/P110_DeviceConfiguration.png b/docs/source/Plugin/P110_DeviceConfiguration.png index 9dd788d9eb..1451dd1c1a 100644 Binary files a/docs/source/Plugin/P110_DeviceConfiguration.png and b/docs/source/Plugin/P110_DeviceConfiguration.png differ diff --git a/docs/source/Plugin/P110_RangeOptions.png b/docs/source/Plugin/P110_RangeOptions.png index cc0c42266c..548c0b7766 100644 Binary files a/docs/source/Plugin/P110_RangeOptions.png and b/docs/source/Plugin/P110_RangeOptions.png differ diff --git a/docs/source/Plugin/P110_TimingOptions.png b/docs/source/Plugin/P110_TimingOptions.png index 69b4034f86..89d0c45d3d 100644 Binary files a/docs/source/Plugin/P110_TimingOptions.png and b/docs/source/Plugin/P110_TimingOptions.png differ diff --git a/docs/source/Plugin/P113.rst b/docs/source/Plugin/P113.rst index 4dc57aadea..07afa12e20 100644 --- a/docs/source/Plugin/P113.rst +++ b/docs/source/Plugin/P113.rst @@ -33,21 +33,21 @@ Configuration .. image:: P113_DeviceConfiguration.png -**Name** A unique name should be entered here. +* **Name** A unique name should be entered here. -**Enabled** The device can be disabled or enabled. When not enabled the device should not use any resources. +* **Enabled** The device can be disabled or enabled. When not enabled the device should not use any resources. I2C Options ^^^^^^^^^^^^ The available settings here depend on the build used. At least the **Force Slow I2C speed** option is available, but selections for the I2C Multiplexer can also be shown. For details see the :ref:`Hardware_page` -**I2C Address**: The address the device is using. Because of an issue with changing the I2C address of the sensor, this list is limited to 1 item, and the address isn't changed/set. +* **I2C Address**: The address the device is using. Because of an issue with changing the I2C address of the sensor, this list is limited to 1 item, and the address isn't changed/set. Device Settings ^^^^^^^^^^^^^^^^ -**Timing**: The timing setting of the sensor determines the accuracy of the measurement. There are 6 options available: +* **Timing**: The timing setting of the sensor determines the accuracy of the measurement. There are 6 options available: .. image:: P113_TimingOptions.png @@ -63,7 +63,7 @@ Device Settings *500ms* The longest integration time available. For use with long read interval settings. -**Range**: the measuring ranges: +* **Range**: the measuring ranges: .. image:: P113_RangeOptions.png @@ -71,24 +71,26 @@ Device Settings *Long (~400cm)* For measurements in the 0 to 4000 millimeter (400cm) range (but somewhat less accurate). -**Send event when value unchanged** To avoid many of the same events when the measurement is stable, this option is off by default. When enabled, every measurement will cause an event, and send the data to any enabled Controller. +* **Send event when value unchanged** To avoid many of the same events when the measurement is stable, this option is off by default. When enabled, every measurement will cause an event, and send the data to any enabled Controller. -**Trigger delta** To avoid triggering many events with only a small difference in distance the 'Trigger delta' option is available. This can be set to only trigger an event when the new distance is at least the delta less or more than the previous measurement. +* **Trigger delta** To avoid triggering many events with only a small difference in distance the 'Trigger delta' option is available. This can be set to only trigger an event when the new distance is at least the delta less or more than the previous measurement. NB: This setting is ignored if 'Send event when value unchanged' is checked! The Data Acquisition, Send to Controller and Interval settings are standard available configuration items. Send to Controller only when one or more Controllers are configured. -**Interval** By default, Interval will be set to 60 sec. Setting this to 1 or 2 seconds, usually offers a reasonable response time. +* **Interval** By default, Interval will be set to 60 sec. Setting this to 1 or 2 seconds, usually offers a reasonable response time. Values ^^^^^^ -The measured distance is available in ``Distance``. A formula can be set to recalculate, f.e. to centimeters using ``%value%/10``. The number of decimals is by default set to 2, but for use with millimeters distance it can be set to 0, as no decimals are provided from the measurement. +The measured distance is available in ``Distance``. A formula can be set to recalculate, f.e. to centimeters using ``%value%/10``. The number of decimals can be set to 0, when using millimeters, as no decimals are provided from the measurement. The Ambient lighting condition during measurement is available in ``Ambient``. The unit is kcps (Photons per second, recalculated to kilo count per second) +Value ``Direction`` holds the direction relative to the *previous* distance value: ``-1`` = smaller distance, ``0`` = unchanged distance, ``1`` = greater distance. + .. Events .. ~~~~~~ @@ -100,5 +102,8 @@ Change log ---------- .. versionchanged:: 2.0 + ... + + |added| 2024-04-27 Add value ``Direction``. |added| 2021-04-05 Added to main repository as Plugin 113 Distance - VL53L1X (400cm), based on a copy of Plugin 110 Distance - VL53L0X (200cm) diff --git a/docs/source/Plugin/P113_DeviceConfiguration.png b/docs/source/Plugin/P113_DeviceConfiguration.png index 147c4deadb..0c73a12b3c 100644 Binary files a/docs/source/Plugin/P113_DeviceConfiguration.png and b/docs/source/Plugin/P113_DeviceConfiguration.png differ diff --git a/docs/source/Plugin/P113_RangeOptions.png b/docs/source/Plugin/P113_RangeOptions.png index 6471905674..25bf3fef1c 100644 Binary files a/docs/source/Plugin/P113_RangeOptions.png and b/docs/source/Plugin/P113_RangeOptions.png differ diff --git a/docs/source/Plugin/P113_TimingOptions.png b/docs/source/Plugin/P113_TimingOptions.png index ee318e7d6e..14cb802c1e 100644 Binary files a/docs/source/Plugin/P113_TimingOptions.png and b/docs/source/Plugin/P113_TimingOptions.png differ diff --git a/src/_P110_VL53L0X.ino b/src/_P110_VL53L0X.ino index 58914d61be..0cc76a818b 100644 --- a/src/_P110_VL53L0X.ino +++ b/src/_P110_VL53L0X.ino @@ -6,12 +6,16 @@ // ###################################### stefan@clumsy.ch ########################################## // ####################################################################################################### -// Changelog: -// 2022-06-22, tonhuisman: Remove delay() call from begin(), handle delay via PLUGIN_FIFTY_PER_SECOND -// Reformat source (uncrustify) -// 2021-04-05, tonhuisman: Removed check for VL53L1X as that is not compatible with this driver (Got its own plugin P113) -// 2021-02-06, tonhuisman: Refactored to use PluginStruct to enable multiple-instance use with an I2C Multiplexer -// 2021-01-07, tonhuisman: Moved from PluginPlayground (P133) to main repo (P110), fixed some issues +/** Changelog: + * 2024-04-27 tonhuisman: Read sensor asynchronously to enable (the new default) trigger on changed value + * 2024-04-26 tonhuisman: Migrate 'Send event when value unchanged' and 'Trigger delta' settings from P113 (at last...) + * Add Direction value, -1 = closer, 0 = unchanged, 1 = further away + * 2022-06-22 tonhuisman: Remove delay() call from begin(), handle delay via PLUGIN_FIFTY_PER_SECOND + * Reformat source (uncrustify) + * 2021-04-05 tonhuisman: Removed check for VL53L1X as that is not compatible with this driver (Got its own plugin P113) + * 2021-02-06 tonhuisman: Refactored to use PluginStruct to enable multiple-instance use with an I2C Multiplexer + * 2021-01-07 tonhuisman: Moved from PluginPlayground (P133) to main repo (P110), fixed some issues + */ // needs VL53L0X library from pololu https://github.com/pololu/vl53l0x-arduino @@ -21,6 +25,7 @@ #define PLUGIN_ID_110 110 #define PLUGIN_NAME_110 "Distance - VL53L0X (200cm)" #define PLUGIN_VALUENAME1_110 "Distance" +#define PLUGIN_VALUENAME2_110 "Direction" /////////////////////////// @@ -42,9 +47,10 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) Device[deviceCount].PullUpOption = false; Device[deviceCount].InverseLogicOption = false; Device[deviceCount].FormulaOption = true; - Device[deviceCount].ValueCount = 1; + Device[deviceCount].ValueCount = 2; Device[deviceCount].SendDataOption = true; Device[deviceCount].TimerOption = true; + Device[deviceCount].TimerOptional = true; Device[deviceCount].GlobalSyncOption = true; Device[deviceCount].PluginStats = true; break; @@ -59,6 +65,7 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_GET_DEVICEVALUENAMES: { strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_110)); + strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_110)); break; } @@ -78,14 +85,14 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) break; } - # if FEATURE_I2C_GET_ADDRESS + #if FEATURE_I2C_GET_ADDRESS case PLUGIN_I2C_GET_ADDRESS: { event->Par1 = P110_I2C_ADDRESS; success = true; break; } - # endif // if FEATURE_I2C_GET_ADDRESS + #endif // if FEATURE_I2C_GET_ADDRESS case PLUGIN_WEBFORM_LOAD: { @@ -105,6 +112,14 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) const int optionValuesMode3[2] = { 0, 1 }; addFormSelector(F("Range"), F("prange"), 2, optionsMode3, optionValuesMode3, P110_RANGE); } + addFormCheckBox(F("Send event when value unchanged"), F("notchanged"), P110_SEND_ALWAYS == 1); + addFormNote(F("When checked, 'Trigger delta' setting is ignored!")); + + addFormNumericBox(F("Trigger delta"), F("delta"), P110_DELTA, 0, 100); + addUnit(F("0-100mm")); + #ifndef LIMIT_BUILD_SIZE + addFormNote(F("Minimal change in Distance to trigger an event.")); + #endif // ifndef LIMIT_BUILD_SIZE success = true; break; @@ -115,6 +130,8 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) P110_I2C_ADDRESS = getFormItemInt(F("i2cAddr")); P110_TIMING = getFormItemInt(F("ptiming")); P110_RANGE = getFormItemInt(F("prange")); + P110_SEND_ALWAYS = isFormItemChecked(F("notchanged")) ? 1 : 0; + P110_DELTA = getFormItemInt(F("delta")); success = true; break; @@ -133,12 +150,37 @@ boolean Plugin_110(uint8_t function, struct EventStruct *event, String& string) P110_data_struct *P110_data = static_cast(getPluginTaskData(event->TaskIndex)); if (nullptr != P110_data) { - long dist = P110_data->readDistance(); + const uint16_t dist = P110_data->getDistance(); + const uint16_t p_dist = UserVar.getFloat(event->TaskIndex, 0); + const int16_t direct = dist == p_dist ? 0 : (dist < p_dist ? -1 : 1); + const bool triggered = (dist > (p_dist + P110_DELTA)) || (dist < (p_dist - P110_DELTA)); + + #ifdef P110_INFO_LOG - success = P110_data->isReadSuccessful(); + if (loglevelActiveFor(LOG_LEVEL_INFO)) { + addLog(LOG_LEVEL_INFO, strformat(F("VL53L0x: Perform read: trig: %d, prev: %d, dist: %d"), triggered, p_dist, dist)); + } + #endif // ifdef P110_INFO_LOG - if (success) { + if (triggered || (P110_SEND_ALWAYS == 1)) { UserVar.setFloat(event->TaskIndex, 0, dist); // Value is classified as invalid when > 8190, so no conversion or 'split' needed + UserVar.setFloat(event->TaskIndex, 1, direct); + success = true; + } + } + break; + } + + case PLUGIN_TEN_PER_SECOND: // Handle sensor reading + { + P110_data_struct *P110_data = static_cast(getPluginTaskData(event->TaskIndex)); + + if (nullptr != P110_data) { + P110_data->readDistance(); + + if (P110_data->isReadSuccessful() && (Settings.TaskDeviceTimer[event->TaskIndex] == 0)) { // Trigger as soon as there's a valid + // measurement and 0 interval is set + Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 10); } } break; diff --git a/src/_P113_VL53L1X.ino b/src/_P113_VL53L1X.ino index b25a9813d2..20d7ea67ae 100644 --- a/src/_P113_VL53L1X.ino +++ b/src/_P113_VL53L1X.ino @@ -6,12 +6,13 @@ // ####################################################################################################### /** Changelog: - * 2023-08-11, tonhuisman: Fix issue not surfacing before, that the library right-shifts the I2C address when that is set... - * Also use new/delete on sensor object (code improvement) - * Limit the selection list of I2C addresses to 1 item, as changing the I2C address of the sensor does not work as - * intended/expected - * 2021-04-06, tonhuisman: Remove Interval optional attribute to avoid system overload, cleanup source - * 2021-04-05, tonhuisman: Add VL53L1X Time of Flight sensor to main repo (similar to but not compatible with VL53L0X) + * 2024-04-25 tonhuisman: Add Direction value (1/0/-1), code improvements + * 2023-08-11 tonhuisman: Fix issue not surfacing before, that the library right-shifts the I2C address when that is set... + * Also use new/delete on sensor object (code improvement) + * Limit the selection list of I2C addresses to 1 item, as changing the I2C address of the sensor does not work as + * intended/expected + * 2021-04-06 tonhuisman: Remove Interval optional attribute to avoid system overload, cleanup source + * 2021-04-05 tonhuisman: Add VL53L1X Time of Flight sensor to main repo (similar to but not compatible with VL53L0X) */ // needs SparkFun_VL53L1X library from https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library @@ -23,6 +24,7 @@ # define PLUGIN_NAME_113 "Distance - VL53L1X (400cm)" # define PLUGIN_VALUENAME1_113 "Distance" # define PLUGIN_VALUENAME2_113 "Ambient" +# define PLUGIN_VALUENAME3_113 "Direction" boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) @@ -40,9 +42,10 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) Device[deviceCount].PullUpOption = false; Device[deviceCount].InverseLogicOption = false; Device[deviceCount].FormulaOption = true; - Device[deviceCount].ValueCount = 2; + Device[deviceCount].ValueCount = 3; Device[deviceCount].SendDataOption = true; Device[deviceCount].TimerOption = true; + Device[deviceCount].TimerOptional = true; Device[deviceCount].GlobalSyncOption = true; Device[deviceCount].PluginStats = true; break; @@ -58,6 +61,7 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) { strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_113)); strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_113)); + strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_113)); break; } @@ -68,7 +72,7 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) const uint8_t i2cAddressValues[] = { 0x29, 0x30 }; if (function == PLUGIN_WEBFORM_SHOW_I2C_PARAMS) { - addFormSelectorI2C(F("i2c"), P113_ACTIVE_I2C_ADDRESSES, i2cAddressValues, PCONFIG(0)); + addFormSelectorI2C(F("i2c"), P113_ACTIVE_I2C_ADDRESSES, i2cAddressValues, P113_I2C_ADDRESS); } else { success = intArrayContains(P113_ACTIVE_I2C_ADDRESSES, i2cAddressValues, event->Par1); } @@ -78,7 +82,7 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) # if FEATURE_I2C_GET_ADDRESS case PLUGIN_I2C_GET_ADDRESS: { - event->Par1 = PCONFIG(0); + event->Par1 = P113_I2C_ADDRESS; success = true; break; } @@ -96,7 +100,7 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) F("500ms"), }; const int optionValuesMode2[] = { 100, 20, 33, 50, 200, 500 }; - addFormSelector(F("Timing"), F("timing"), 6, optionsMode2, optionValuesMode2, PCONFIG(1)); + addFormSelector(F("Timing"), F("timing"), 6, optionsMode2, optionValuesMode2, P113_TIMING); } { @@ -105,14 +109,16 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) F("Long (~400cm)"), }; const int optionValuesMode3[2] = { 0, 1 }; - addFormSelector(F("Range"), F("range"), 2, optionsMode3, optionValuesMode3, PCONFIG(2)); + addFormSelector(F("Range"), F("range"), 2, optionsMode3, optionValuesMode3, P113_RANGE); } - addFormCheckBox(F("Send event when value unchanged"), F("notchanged"), PCONFIG(3) == 1); + addFormCheckBox(F("Send event when value unchanged"), F("notchanged"), P113_SEND_ALWAYS == 1); addFormNote(F("When checked, 'Trigger delta' setting is ignored!")); - addFormNumericBox(F("Trigger delta"), F("delta"), PCONFIG(4), 0, 100); + addFormNumericBox(F("Trigger delta"), F("delta"), P113_DELTA, 0, 100); addUnit(F("0-100mm")); + # ifndef LIMIT_BUILD_SIZE addFormNote(F("Minimal change in Distance to trigger an event.")); + # endif // ifndef LIMIT_BUILD_SIZE success = true; break; @@ -120,11 +126,11 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_WEBFORM_SAVE: { - PCONFIG(0) = getFormItemInt(F("i2c")); - PCONFIG(1) = getFormItemInt(F("timing")); - PCONFIG(2) = getFormItemInt(F("range")); - PCONFIG(3) = isFormItemChecked(F("notchanged")) ? 1 : 0; - PCONFIG(4) = getFormItemInt(F("delta")); + P113_I2C_ADDRESS = getFormItemInt(F("i2c")); + P113_TIMING = getFormItemInt(F("timing")); + P113_RANGE = getFormItemInt(F("range")); + P113_SEND_ALWAYS = isFormItemChecked(F("notchanged")) ? 1 : 0; + P113_DELTA = getFormItemInt(F("delta")); success = true; break; @@ -132,7 +138,7 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_INIT: { - initPluginTaskData(event->TaskIndex, new (std::nothrow) P113_data_struct(PCONFIG(0), PCONFIG(1), PCONFIG(2) == 1)); + initPluginTaskData(event->TaskIndex, new (std::nothrow) P113_data_struct(P113_I2C_ADDRESS, P113_TIMING, P113_RANGE == 1)); P113_data_struct *P113_data = static_cast(getPluginTaskData(event->TaskIndex)); success = (nullptr != P113_data) && P113_data->begin(); // Start the sensor @@ -152,11 +158,14 @@ boolean Plugin_113(uint8_t function, struct EventStruct *event, String& string) if (nullptr != P113_data) { const uint16_t dist = P113_data->readDistance(); const uint16_t ambient = P113_data->readAmbient(); - const bool triggered = (dist > UserVar[event->BaseVarIndex] + PCONFIG(4)) || (dist < UserVar[event->BaseVarIndex] - PCONFIG(4)); + const uint16_t p_dist = UserVar.getFloat(event->TaskIndex, 0); + const int16_t direct = dist == p_dist ? 0 : (dist < p_dist ? -1 : 1); + const bool triggered = (dist > p_dist + P113_DELTA) || (dist < p_dist - P113_DELTA); - if (P113_data->isReadSuccessful() && (triggered || (PCONFIG(3) == 1)) && (dist != 0xFFFF)) { + if (P113_data->isReadSuccessful() && (triggered || (P113_SEND_ALWAYS == 1)) && (dist != 0xFFFF)) { UserVar.setFloat(event->TaskIndex, 0, dist); UserVar.setFloat(event->TaskIndex, 1, ambient); + UserVar.setFloat(event->TaskIndex, 2, direct); success = true; } } diff --git a/src/src/PluginStructs/P110_data_struct.cpp b/src/src/PluginStructs/P110_data_struct.cpp index 57cae4b99c..849911efaa 100644 --- a/src/src/PluginStructs/P110_data_struct.cpp +++ b/src/src/PluginStructs/P110_data_struct.cpp @@ -56,15 +56,20 @@ bool P110_data_struct::plugin_fifty_per_second() { return true; } -long P110_data_struct::readDistance() { - long dist = -1; // Invalid +uint16_t P110_data_struct::getDistance() { + return _distance; +} + +uint16_t P110_data_struct::readDistance() { + uint16_t dist = 0xFFFF; // Invalid if (initPhase != P110_initPhases::Ready) { return dist; } # ifdef P110_DEBUG_LOG if (loglevelActiveFor(LOG_LEVEL_DEBUG)) { - addLogMove(LOG_LEVEL_DEBUG, strformat(F("VL53L0X : idx: 0x%02x init: %d"), i2cAddress, initState ? 1 : 0)); + addLogMove(LOG_LEVEL_DEBUG, strformat(F("VL53L0X: idx: 0x%02x init: %d"), + i2cAddress, initState)); } # endif // P110_DEBUG_LOG @@ -77,19 +82,20 @@ long P110_data_struct::readDistance() { addLog(LOG_LEVEL_DEBUG, F("VL53L0X: TIMEOUT")); # endif // P110_DEBUG_LOG success = false; - } else if (dist >= 8190) { + } else if (dist >= 8190u) { # ifdef P110_DEBUG_LOG - addLog(LOG_LEVEL_DEBUG, F("VL53L0X: NO MEASUREMENT")); + addLog(LOG_LEVEL_DEBUG, concat(F("VL53L0X: NO MEASUREMENT: "), dist)); # endif // P110_DEBUG_LOG success = false; + } else { + _distance = dist; } # ifdef P110_INFO_LOG if (loglevelActiveFor(LOG_LEVEL_INFO)) { - addLogMove(LOG_LEVEL_INFO, - strformat(F("VL53L0X: Address: 0x%02x / Timing: %d / Long Range: %d / Distance: %d"), - i2cAddress, timing, range ? 1 : 0, dist)); + addLogMove(LOG_LEVEL_INFO, strformat(F("VL53L0X: Addr: 0x%02x / Timing: %d / Long Range: %d / success: %d / Distance: %d"), + i2cAddress, timing, range, success, dist)); } # endif // P110_INFO_LOG } diff --git a/src/src/PluginStructs/P110_data_struct.h b/src/src/PluginStructs/P110_data_struct.h index 84cc1dc57b..0c8347859b 100644 --- a/src/src/PluginStructs/P110_data_struct.h +++ b/src/src/PluginStructs/P110_data_struct.h @@ -4,21 +4,22 @@ #include "../../_Plugin_Helper.h" #ifdef USES_P110 -# define P110_INFO_LOG // Enable debugging output (INFO loglevel) +// # define P110_INFO_LOG // Enable debugging output (INFO loglevel) # define P110_DEBUG_LOG // Enable extended debugging output (DEBUG loglevel) # if defined(LIMIT_BUILD_SIZE) || defined(BUILD_NO_DEBUG) - # ifdef P110_DEBUG_LOG - # undef P110_DEBUG_LOG - # endif // ifdef P110_DEBUG_LOG +# ifdef P110_DEBUG_LOG +# undef P110_DEBUG_LOG +# endif // ifdef P110_DEBUG_LOG # endif // if defined(LIMIT_BUILD_SIZE) || defined(BUILD_NO_DEBUG) -# include # include # define P110_I2C_ADDRESS PCONFIG(0) # define P110_TIMING PCONFIG(1) # define P110_RANGE PCONFIG(2) +# define P110_SEND_ALWAYS PCONFIG(3) +# define P110_DELTA PCONFIG(4) enum class P110_initPhases : uint8_t { Ready = 0x00, @@ -32,21 +33,23 @@ struct P110_data_struct : public PluginTaskData_base { P110_data_struct(uint8_t i2c_addr, int timing, bool range); - P110_data_struct() = delete; + P110_data_struct() = delete; virtual ~P110_data_struct() = default; - bool begin(); - long readDistance(); - bool isReadSuccessful(); - bool plugin_fifty_per_second(); + bool begin(); + uint16_t readDistance(); + uint16_t getDistance(); + bool isReadSuccessful(); + bool plugin_fifty_per_second(); private: VL53L0X sensor; - uint8_t i2cAddress; - int timing; - bool range; + uint16_t _distance = 0xFFFF; + uint8_t i2cAddress; + int timing; + bool range; int32_t timeToWait = 0; P110_initPhases initPhase = P110_initPhases::Undefined; diff --git a/src/src/PluginStructs/P113_data_struct.cpp b/src/src/PluginStructs/P113_data_struct.cpp index d26ef04ea3..69e147d0d7 100644 --- a/src/src/PluginStructs/P113_data_struct.cpp +++ b/src/src/PluginStructs/P113_data_struct.cpp @@ -7,9 +7,7 @@ P113_data_struct::P113_data_struct(uint8_t i2c_addr, int timing, bool range) : i } P113_data_struct::~P113_data_struct() { - if (nullptr != sensor) { - delete sensor; - } + delete sensor; } // **************************************************************************/ @@ -19,12 +17,12 @@ bool P113_data_struct::begin() { initState = nullptr != sensor; if (initState) { - uint16_t id = sensor->getID(); + const uint16_t id = sensor->getID(); // FIXME 2023-08-11 tonhuisman: Disabled, as it seems to mess up the sensor // sensor->setI2CAddress(i2cAddress); // Initialize for configured address - uint8_t res = sensor->begin(); + const bool res = sensor->begin(); if (res) { // 0/false is NO-ERROR if (loglevelActiveFor(LOG_LEVEL_ERROR)) { @@ -43,9 +41,12 @@ bool P113_data_struct::begin() { sensor->setDistanceModeShort(); } + # ifdef P113_DEBUG + if (loglevelActiveFor(LOG_LEVEL_INFO)) { addLogMove(LOG_LEVEL_INFO, strformat(F("VL53L1X: Sensor initialized at address 0x%02x, id: 0x%04x"), i2cAddress, id)); } + # endif // ifdef P113_DEBUG } return initState; @@ -74,24 +75,19 @@ bool P113_data_struct::readAvailable() { } uint16_t P113_data_struct::readDistance() { - success = false; - - # if defined(P113_DEBUG) || defined(P113_DEBUG_DEBUG) - String log; - # endif // if defined(P113_DEBUG) || defined(P113_DEBUG_DEBUG) # ifdef P113_DEBUG_DEBUG if (loglevelActiveFor(LOG_LEVEL_DEBUG)) { - addLogMove(LOG_LEVEL_DEBUG, strformat(F("VL53L1X : idx: 0x%x init: %d"), i2cAddress, initState ? 1 : 0)); + addLogMove(LOG_LEVEL_DEBUG, strformat(F("VL53L1X: idx: 0x%02x init: %d"), i2cAddress, initState)); } # endif // P113_DEBUG_DEBUG success = true; readActive = false; - if (distance >= 8190) { + if (distance >= 8190u) { # ifdef P113_DEBUG_DEBUG - addLog(LOG_LEVEL_DEBUG, "VL53L1X: NO MEASUREMENT"); + addLog(LOG_LEVEL_DEBUG, concat(F("VL53L1X: NO MEASUREMENT"), distance)); # endif // P113_DEBUG_DEBUG success = false; } @@ -100,7 +96,7 @@ uint16_t P113_data_struct::readDistance() { if (loglevelActiveFor(LOG_LEVEL_INFO)) { addLogMove(LOG_LEVEL_INFO, strformat(F("VL53L1X: Address: 0x%02x / Timing: %d / Long Range: %d / Distance: %d"), - i2cAddress, timing, range ? 1 : 0, distance)); + i2cAddress, timing, range, distance)); } # endif // P113_DEBUG diff --git a/src/src/PluginStructs/P113_data_struct.h b/src/src/PluginStructs/P113_data_struct.h index 583cdb30b5..e49782dae8 100644 --- a/src/src/PluginStructs/P113_data_struct.h +++ b/src/src/PluginStructs/P113_data_struct.h @@ -9,15 +9,20 @@ # define P113_DEBUG_DEBUG // Enable extended debugging output (DEBUG loglevel) # endif // ifndef BUILD_NO_DEBUG -# ifdef LIMIT_BUILD_SIZE - # ifdef P113_DEBUG_DEBUG - # undef P113_DEBUG_DEBUG - # endif // ifdef P113_DEBUG_DEBUG -# endif // ifdef LIMIT_BUILD_SIZE +# if defined(LIMIT_BUILD_SIZE) || defined(BUILD_NO_DEBUG) +# ifdef P113_DEBUG_DEBUG +# undef P113_DEBUG_DEBUG +# endif // ifdef P113_DEBUG_DEBUG +# endif // if defined(LIMIT_BUILD_SIZE) || defined(BUILD_NO_DEBUG) -# include # include +# define P113_I2C_ADDRESS PCONFIG(0) +# define P113_TIMING PCONFIG(1) +# define P113_RANGE PCONFIG(2) +# define P113_SEND_ALWAYS PCONFIG(3) +# define P113_DELTA PCONFIG(4) + struct P113_data_struct : public PluginTaskData_base { public: