mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-27 19:57:38 +00:00
Merge pull request #5098 from tonhuisman/bugfix/P003-countertype-should-set-value-count
[P003] Countertype to determine the number of values
This commit is contained in:
+42
-16
@@ -26,17 +26,11 @@ Supported hardware
|
||||
|
||||
|P003_usedby|
|
||||
|
||||
SETUP
|
||||
-----
|
||||
Configuration
|
||||
-------------
|
||||
|
||||
The setup dialog:
|
||||
|
||||
.. image:: P003_Setup.jpg
|
||||
:width: 664px
|
||||
:height: 378px
|
||||
:scale: 100 %
|
||||
.. image:: P003_DeviceConfiguration.png
|
||||
:alt: Pulse Counter Setup
|
||||
:align: center
|
||||
|
||||
|
||||
Task's Sensor Parameters
|
||||
@@ -45,13 +39,24 @@ Task's Sensor Parameters
|
||||
:GPIO <- Pulse:
|
||||
Select the GPIO pin at which the pulse is counted
|
||||
|
||||
:Debounce Time (mSec):
|
||||
:Debounce Time:
|
||||
Period in which you allow the signal to be unstable (bouncing) after a level change (high -> low / low -> high).
|
||||
The specific use of this parameter to filter out valid signals, depends on the Mode Type (cf. description below).
|
||||
|
||||
:Counter Type:
|
||||
(practical use of this parameter is unclear, but kept for compatibility reasons)
|
||||
Select the output values desired. Available options:
|
||||
|
||||
.. image:: P003_CounterTypeOptions.png
|
||||
|
||||
* **Delta**: Count only.
|
||||
* **Delta/Total/Time**: Count, Total count and Time since last pulse in msec.
|
||||
* **Total**: Total count only.
|
||||
* **Delta/Total**: Count and Total count.
|
||||
* **Time**: Time since last pulse in msec.
|
||||
* **Total/Time**: Total count and Time since last pulse in msec.
|
||||
* **Time/Delta**: Time since last pulse in msec. and count.
|
||||
|
||||
After saving the settings, the Values section is adjusted to show the number of values needed to output the selected Counter Type. The names are not updated!
|
||||
|
||||
:Mode Type:
|
||||
Determines if you want to count **edges** or **pulses** and
|
||||
@@ -62,21 +67,40 @@ Task's Sensor Parameters
|
||||
|
||||
See Description below.
|
||||
|
||||
Available options:
|
||||
|
||||
.. image:: P003_ModeTypeOptions.png
|
||||
|
||||
:Ignore multiple Delta = 0:
|
||||
When enabled doesn't generate events or send data to Controllers if multiple Delta (Count) values of 0 are read. The first 0 after a non-zero Count *will* be sent out to Controllers, and generate events (when Rules are enabled).
|
||||
|
||||
|
|
||||
|
||||
:Interval: The interval between reading the pulses from the counter logic. If the Interval is set to 0 *AND* **Ignore multiple Delta = 0** is enabled, the check for new pulses is performed much more often (up to ca. 50 times per second). This has the advantage that any infrequent pulse can be captured very close to the time it occurs, but also has a disadvantage, see the warning:
|
||||
|
||||
.. warning:: The Interval for this plugin can be set to **0**, but when receiving high-frequency pulses, this should better not be used, as it can overload the ESP *and* any Controller, and it's backend service, configured for the task, as it's sending data, processing the Controllers, and processing events for the task values!
|
||||
|
||||
With **Interval** set to 0, but **Ignore multiple Delta = 0** not enabled, then No data is sent to Controllers and no events are generated, unless a ``TaskRun`` command is executed for this task!
|
||||
|
||||
|
|
||||
|
||||
For setup of some specific device examples see: |P003_usedby|
|
||||
|
||||
Description
|
||||
-----------
|
||||
P003 is a general purpose pulse counter for counting high/low state changes and pulses at an ESP GPIO.
|
||||
It may be used to count water or gas meter pulses or speed measurement of fans, etc.
|
||||
It provides three values:
|
||||
It provides up to three values:
|
||||
|
||||
:``Count``:
|
||||
Number of counted pulses since last transmission (Delta)
|
||||
:``Total``:
|
||||
Total number of counted pulses (since power on or cold restart)
|
||||
Total number of counted pulses (since power on, cold restart or use of ``resetpulsecounter`` or ``setpulsecountertotal`` commands)
|
||||
:``Time``:
|
||||
Time between current and most recent pulse in milliseconds
|
||||
|
||||
.. warning:: The number of Values depends on the selected Counter Type setting, but the names are not updated when the selection is changed, tough values *are* set to the Counter Type(s) selected.
|
||||
|
||||
This allows you to simply count the pulses using ``Total`` or use Count and Time to calculate the number of pulses per time unit.
|
||||
But please note: When ``Count`` > 1, the ``Time`` only applies to the most recent of the counted pulses and you have to guess how long the previous were.
|
||||
|
||||
@@ -121,8 +145,8 @@ Electrical signal quality
|
||||
Make sure physical connections are electrically well separated so no crossover of the signals happen.
|
||||
Especially with edge pulse mode at rates above ~5'000 RPM with longer lines. Best use a cable with ground and signal twisted.
|
||||
|
||||
Statistical logging for PUSE modes
|
||||
----------------------------------
|
||||
Statistical logging for PULSE modes
|
||||
-----------------------------------
|
||||
|
||||
The **Pulse** modes provide at runtime statistical information in the log output in order to support problem finding and the tuning of debounce time.
|
||||
The interpretation is a little tricky, as it is related to the implemented step by step detection method.
|
||||
@@ -137,7 +161,7 @@ It is temporary possible to output the logging in log level ``Info`` by use of t
|
||||
:width: 688px
|
||||
:height: 20px
|
||||
:scale: 100 %
|
||||
:alt: Pule statistic log record
|
||||
:alt: Pulse statistic log record
|
||||
:align: center
|
||||
|
||||
Records like the above are written to log output, whenever a new valid pulse level was detected.
|
||||
@@ -224,6 +248,8 @@ Change log
|
||||
.. versionchanged:: 2.2
|
||||
...
|
||||
|
||||
|added| 2024-08-07 New Counter Type options, counter type now correctly sets the Values available.
|
||||
|
||||
|added|
|
||||
Added new PULSE mode types.
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -70,8 +70,6 @@ Setup
|
||||
.. image:: P003_Setup_LJ12A3_1.png
|
||||
:scale: 50 %
|
||||
|
||||
.. danger:: Only task number 1..4 is currently supported for the pulse plugin!
|
||||
|
||||
Task settings
|
||||
^^^^^^^^^^^^^
|
||||
|
||||
@@ -87,20 +85,20 @@ Sensor
|
||||
* **Counter type**: In this example the type is set to **Delta/Total/Time**.
|
||||
* **Mode type**: Pulse input is generally set to **Falling** or **PULSE low**
|
||||
|
||||
``CHANGE`` = count on signal being changed, low to high (0.8-V to 2.4+V) or high to low (2.4+V to 0.8-V)
|
||||
``Change`` = count on signal being changed, low to high (0.8-V to 2.4+V) or high to low (2.4+V to 0.8-V)
|
||||
|
||||
``RISING`` = count if signal is rising, from low to not low (0.8-V to 0.8+V)
|
||||
``Rising`` = count if signal is rising, from low to not low (0.8-V to 0.8+V)
|
||||
|
||||
``FALLING`` = count if signal is falling, from high to not high (2.4+V to 2.4-V)
|
||||
``Falling`` = count if signal is falling, from high to not high (2.4+V to 2.4-V)
|
||||
|
||||
``PULSE low`` = count if a stable low signal ended followed by a stable high signal
|
||||
``PULSE Low`` = count if a stable low signal ended followed by a stable high signal
|
||||
|
||||
``PULSE high`` = count if a stable high signal ended followed by a stable low signal.
|
||||
``PULSE Gigh`` = count if a stable high signal ended followed by a stable low signal.
|
||||
|
||||
``PULSE change`` = count if the stable signal changes from high to low or from low to high
|
||||
``PULSE Change`` = count if the stable signal changes from high to low or from low to high
|
||||
|
||||
|
||||
.. warning:: GPIO 16 (D0) is not compatible with pulse counters.
|
||||
.. warning:: ESP8266 GPIO 16 (D0) is not compatible with pulse counters.
|
||||
|
||||
Data acquisition
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 81 KiB |
@@ -13,7 +13,7 @@ TCR5000
|
||||
Introduction
|
||||
------------
|
||||
|
||||
The ESP8266 module can be used as an general purpose digital pulse counter device for various digital sensors.
|
||||
The ESP module can be used as an general purpose digital pulse counter device for various digital sensors.
|
||||
Think about optical sensors with a digital output. To be used for things like gas, water, electricity, door open/close counters, etc.
|
||||
|
||||
In this example we will connect an optical sensor. Connect the digital output to a GPIO pin on the ESP module.
|
||||
@@ -42,8 +42,6 @@ Setup
|
||||
.. image:: P003_Setup_TCR5000_1.png
|
||||
:scale: 40 %
|
||||
|
||||
.. danger:: Only task number 1..4 is currently supported for the pulse plugin!
|
||||
|
||||
Task settings
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
@@ -59,20 +57,20 @@ Sensor
|
||||
* **Counter type**: In this example the type is set to **Delta/Total/Time**.
|
||||
* **Mode type**: Pulse input is generally set to **Falling** or **PULSE low**
|
||||
|
||||
``CHANGE`` = count on signal being changed, low to high (0.8-V to 2.4+V) or high to low (2.4+V to 0.8-V)
|
||||
``Change`` = count on signal being changed, low to high (0.8-V to 2.4+V) or high to low (2.4+V to 0.8-V)
|
||||
|
||||
``RISING`` = count if signal is rising, from low to not low (0.8-V to 0.8+V)
|
||||
``Rising`` = count if signal is rising, from low to not low (0.8-V to 0.8+V)
|
||||
|
||||
``FALLING`` = count if signal is falling, from high to not high (2.4+V to 2.4-V)
|
||||
``Falling`` = count if signal is falling, from high to not high (2.4+V to 2.4-V)
|
||||
|
||||
``PULSE low`` = count if a stable low signal ended followed by a stable high signal
|
||||
``PULSE Low`` = count if a stable low signal ended followed by a stable high signal
|
||||
|
||||
``PULSE high`` = count if a stable high signal ended followed by a stable low signal.
|
||||
``PULSE High`` = count if a stable high signal ended followed by a stable low signal.
|
||||
|
||||
``PULSE change`` = count if the stable signal changes from high to low or from low to high
|
||||
``PULSE Change`` = count if the stable signal changes from high to low or from low to high
|
||||
|
||||
|
||||
.. warning:: GPIO 16 (D0) is not compatible with pulse counters.
|
||||
.. warning:: ESP8266 GPIO 16 (D0) is not compatible with pulse counters.
|
||||
|
||||
Data acquisition
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
@@ -43,8 +43,6 @@ Setup
|
||||
|
||||
.. FIXME: wrong image .. image:: P003_Setup_YFS401_1.png
|
||||
|
||||
.. danger:: Only task number 1..4 is currently supported for the pulse plugin!
|
||||
|
||||
Task settings
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
@@ -67,19 +65,19 @@ Sensor
|
||||
* **Counter type**: In this example the type is set to **Delta/Total/Time**.
|
||||
* **Mode type**: Pulse input is generally set to **Falling** or **PULSE low**
|
||||
|
||||
``CHANGE`` = count on signal being changed, low to high (0.8-V to 2.4+V) or high to low (2.4+V to 0.8-V)
|
||||
``Change`` = count on signal being changed, low to high (0.8-V to 2.4+V) or high to low (2.4+V to 0.8-V)
|
||||
|
||||
``RISING`` = count if signal is rising, from low to not low (0.8-V to 0.8+V)
|
||||
``Rising`` = count if signal is rising, from low to not low (0.8-V to 0.8+V)
|
||||
|
||||
``FALLING`` = count if signal is falling, from high to not high (2.4+V to 2.4-V)
|
||||
``Falling`` = count if signal is falling, from high to not high (2.4+V to 2.4-V)
|
||||
|
||||
``PULSE low`` = count if a stable low signal ended followed by a stable high signal
|
||||
``PULSE Low`` = count if a stable low signal ended followed by a stable high signal
|
||||
|
||||
``PULSE high`` = count if a stable high signal ended followed by a stable low signal.
|
||||
``PULSE High`` = count if a stable high signal ended followed by a stable low signal.
|
||||
|
||||
``PULSE change`` = count if the stable signal changes from high to low or from low to high
|
||||
``PULSE Change`` = count if the stable signal changes from high to low or from low to high
|
||||
|
||||
.. warning:: GPIO 16 (D0) is not compatible with pulse counters.
|
||||
.. warning:: ESP8266 GPIO 16 (D0) is not compatible with pulse counters.
|
||||
|
||||
Data acquisition
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
+131
-89
@@ -12,14 +12,26 @@
|
||||
// with pulse rates of less than 750 RPM with DebounceTime > 20ms and pulse length > 40ms. This type may
|
||||
// tolerate less good signals. After a pulse and debounce time it verifies the signal 3 times.
|
||||
|
||||
/** Changelog:
|
||||
* 2024-08-12 tonhuisman: Improved handling of 'Ignore multiple Delta = 0' by peeking the Delta value.
|
||||
* 2024-08-10 tonhuisman: Changed option to 'Ignore multiple Delta = 0', and allow Interval = 0, combined with Delta = 0, to send a pulse
|
||||
* immediately to the Controllers and generate events.
|
||||
* Moved most PLUGIN_READ logic to P003_data_struct for easy re-use.
|
||||
* 2024-08-08 tonhuisman: Add support for 'Ignore Delta = 0' setting, to not send out events and data to controllers if the Delta (Count)
|
||||
* value is 0.
|
||||
* 2024-08-07 tonhuisman: Add support for Time, Total/Time, Time/Delta Counter types. Not included in LIMIT_BUILD_SIZE builds!
|
||||
* 2024-08-06 tonhuisman: Add support for PLUGIN_GET_DEVICEVALUECOUNT and PLUGIN_GET_DEVICEVTYPE to use the correct number of values when
|
||||
* sending data to controllers. Also move Total to first value to have it sent out properly when it's the only value.
|
||||
* 2024-08-06 tonhuisman: Start changelog. Uncrustify the source.
|
||||
*/
|
||||
|
||||
# include "src/PluginStructs/P003_data_struct.h"
|
||||
|
||||
# include "src/Helpers/ESPEasy_time_calc.h"
|
||||
|
||||
#ifndef BUILD_NO_DEBUG
|
||||
# define P003_PULSE_STATS_DEFAULT_LOG_LEVEL LOG_LEVEL_DEBUG
|
||||
#endif
|
||||
# ifndef BUILD_NO_DEBUG
|
||||
# define P003_PULSE_STATS_DEFAULT_LOG_LEVEL LOG_LEVEL_DEBUG
|
||||
# endif // ifndef BUILD_NO_DEBUG
|
||||
# define P003_PULSE_STATS_ADHOC_LOG_LEVEL LOG_LEVEL_INFO
|
||||
|
||||
# define PLUGIN_003
|
||||
@@ -32,27 +44,6 @@
|
||||
# define PLUGIN_VALUENAME2_003 "Total"
|
||||
# define PLUGIN_VALUENAME3_003 "Time"
|
||||
|
||||
// ... their index in UserVar and TaskDeviceValueNames
|
||||
# define P003_IDX_pulseCounter 0
|
||||
# define P003_IDX_pulseTotalCounter 1
|
||||
# define P003_IDX_pulseTime 2
|
||||
|
||||
// ... and the following index into UserVar for storing the persisted TotalCounter
|
||||
# define P003_IDX_persistedTotalCounter 3
|
||||
|
||||
// indexes for config parameters
|
||||
# define P003_IDX_DEBOUNCETIME 0
|
||||
# define P003_IDX_COUNTERTYPE 1
|
||||
# define P003_IDX_MODETYPE 2
|
||||
|
||||
// values for WEBFORM Counter Types
|
||||
# define P003_NR_COUNTERTYPES 4
|
||||
# define P003_COUNTERTYPE_LIST { F("Delta"), F("Delta/Total/Time"), F("Total"), F("Delta/Total"), }
|
||||
# define P003_CT_INDEX_COUNTER 0
|
||||
# define P003_CT_INDEX_COUNTER_TOTAL_TIME 1
|
||||
# define P003_CT_INDEX_TOTAL 2
|
||||
# define P003_CT_INDEX_COUNTER_TOTAL 3
|
||||
|
||||
|
||||
boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
{
|
||||
@@ -72,6 +63,7 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
Device[deviceCount].ValueCount = PLUGIN_NR_VALUENAMES_003;
|
||||
Device[deviceCount].SendDataOption = true;
|
||||
Device[deviceCount].TimerOption = true;
|
||||
Device[deviceCount].TimerOptional = true;
|
||||
Device[deviceCount].GlobalSyncOption = true;
|
||||
Device[deviceCount].PluginStats = true;
|
||||
Device[deviceCount].TaskLogsOwnPeaks = true;
|
||||
@@ -98,25 +90,103 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_SET_DEFAULTS:
|
||||
{
|
||||
Settings.TaskDeviceTimer[event->TaskIndex] = Settings.Delay; // Previous default for non-TimerOptional
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_GET_DEVICEVALUECOUNT:
|
||||
{
|
||||
switch (PCONFIG(P003_IDX_COUNTERTYPE)) {
|
||||
case P003_CT_INDEX_COUNTER:
|
||||
case P003_CT_INDEX_TOTAL:
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
case P003_CT_INDEX_TIME:
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
event->Par1 = 1;
|
||||
break;
|
||||
case P003_CT_INDEX_COUNTER_TOTAL_TIME:
|
||||
event->Par1 = 3;
|
||||
break;
|
||||
case P003_CT_INDEX_COUNTER_TOTAL:
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
case P003_CT_INDEX_TOTAL_TIME:
|
||||
case P003_CT_INDEX_TIME_COUNTER:
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
event->Par1 = 2;
|
||||
break;
|
||||
}
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_GET_DEVICEVTYPE:
|
||||
{
|
||||
switch (PCONFIG(P003_IDX_COUNTERTYPE)) {
|
||||
case P003_CT_INDEX_COUNTER:
|
||||
case P003_CT_INDEX_TOTAL:
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
case P003_CT_INDEX_TIME:
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
event->sensorType = Sensor_VType::SENSOR_TYPE_SINGLE;
|
||||
break;
|
||||
case P003_CT_INDEX_COUNTER_TOTAL_TIME:
|
||||
event->sensorType = Sensor_VType::SENSOR_TYPE_TRIPLE;
|
||||
break;
|
||||
case P003_CT_INDEX_COUNTER_TOTAL:
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
case P003_CT_INDEX_TOTAL_TIME:
|
||||
case P003_CT_INDEX_TIME_COUNTER:
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
event->sensorType = Sensor_VType::SENSOR_TYPE_DUAL;
|
||||
break;
|
||||
}
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_WEBFORM_LOAD:
|
||||
{
|
||||
addFormNumericBox(F("Debounce Time (mSec)"), F("debounce")
|
||||
addFormNumericBox(F("Debounce Time"), F("debounce")
|
||||
, PCONFIG(P003_IDX_DEBOUNCETIME));
|
||||
addUnit(F("mSec"));
|
||||
|
||||
{
|
||||
uint8_t choice = PCONFIG(P003_IDX_COUNTERTYPE);
|
||||
const __FlashStringHelper *options[P003_NR_COUNTERTYPES] = P003_COUNTERTYPE_LIST;
|
||||
addFormSelector(F("Counter Type"), F("countertype"), P003_NR_COUNTERTYPES, options, nullptr, choice);
|
||||
const uint8_t choice = PCONFIG(P003_IDX_COUNTERTYPE);
|
||||
const __FlashStringHelper *options[] = {
|
||||
F("Delta"),
|
||||
F("Delta/Total/Time"),
|
||||
F("Total"),
|
||||
F("Delta/Total"),
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
F("Time"),
|
||||
F("Total/Time"),
|
||||
F("Time/Delta"),
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
};
|
||||
addFormSelector(F("Counter Type"), F("countertype"), NR_ELEMENTS(options), options, nullptr, choice);
|
||||
|
||||
if (choice != 0) {
|
||||
addHtml(F("<span style=\"color:red\">Total count is not persistent!</span>"));
|
||||
}
|
||||
|
||||
if ((choice == P003_CT_INDEX_TOTAL)
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
|| (choice >= P003_CT_INDEX_TIME)
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
) {
|
||||
addFormNote(F("Value names are not auto-updated!"));
|
||||
}
|
||||
}
|
||||
|
||||
Internal_GPIO_pulseHelper::addGPIOtriggerMode(
|
||||
F("Mode Type"),
|
||||
F("raisetype"),
|
||||
F("Mode Type"),
|
||||
F("raisetype"),
|
||||
static_cast<Internal_GPIO_pulseHelper::GPIOtriggerMode>(PCONFIG(P003_IDX_MODETYPE)));
|
||||
|
||||
addFormCheckBox(F("Ignore multiple Delta = 0"), F("nozero"), PCONFIG(P003_IDX_IGNORE_ZERO));
|
||||
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
@@ -126,6 +196,7 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
PCONFIG(P003_IDX_DEBOUNCETIME) = getFormItemInt(F("debounce"));
|
||||
PCONFIG(P003_IDX_COUNTERTYPE) = getFormItemInt(F("countertype"));
|
||||
PCONFIG(P003_IDX_MODETYPE) = getFormItemInt(F("raisetype"));
|
||||
PCONFIG(P003_IDX_IGNORE_ZERO) = isFormItemChecked(F("nozero"));
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
@@ -147,11 +218,11 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
static_cast<P003_data_struct *>(getPluginTaskData(event->TaskIndex));
|
||||
|
||||
if (nullptr != P003_data) {
|
||||
#ifdef PULSE_STATISTIC
|
||||
#ifndef BUILD_NO_DEBUG
|
||||
# ifdef PULSE_STATISTIC
|
||||
# ifndef BUILD_NO_DEBUG
|
||||
P003_data->pulseHelper.setStatsLogLevel(P003_PULSE_STATS_DEFAULT_LOG_LEVEL);
|
||||
#endif
|
||||
#endif
|
||||
# endif // ifndef BUILD_NO_DEBUG
|
||||
# endif // ifdef PULSE_STATISTIC
|
||||
|
||||
// Restore the total counter from the unused 4th UserVar value.
|
||||
// It may be using a formula to generate the output, which makes it impossible to restore
|
||||
@@ -159,24 +230,25 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
P003_data->pulseHelper.setPulseCountTotal(UserVar[event->BaseVarIndex + P003_IDX_persistedTotalCounter]);
|
||||
|
||||
// Restore any values from the RTC-memory (persistent as long as power is on. Survives warm reset or deep sleep)
|
||||
switch (PCONFIG(P003_IDX_COUNTERTYPE))
|
||||
{
|
||||
switch (PCONFIG(P003_IDX_COUNTERTYPE)) {
|
||||
case P003_CT_INDEX_COUNTER:
|
||||
case P003_CT_INDEX_COUNTER_TOTAL:
|
||||
{
|
||||
P003_data->pulseHelper.setPulseCounter(UserVar[event->BaseVarIndex + P003_IDX_pulseCounter]);
|
||||
P003_data->pulseHelper.setPulseCounter(UserVar.getFloat(event->TaskIndex, P003_IDX_pulseCounter));
|
||||
break;
|
||||
}
|
||||
case P003_CT_INDEX_COUNTER_TOTAL_TIME:
|
||||
{
|
||||
P003_data->pulseHelper.setPulseCounter(UserVar[event->BaseVarIndex + P003_IDX_pulseCounter],
|
||||
UserVar[event->BaseVarIndex + P003_IDX_pulseTime]);
|
||||
break;
|
||||
}
|
||||
case P003_CT_INDEX_TOTAL:
|
||||
{
|
||||
break;
|
||||
}
|
||||
case P003_CT_INDEX_COUNTER_TOTAL_TIME:
|
||||
P003_data->pulseHelper.setPulseCounter(UserVar.getFloat(event->TaskIndex, P003_IDX_pulseCounter),
|
||||
UserVar.getFloat(event->TaskIndex, P003_IDX_pulseTime));
|
||||
break;
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
case P003_CT_INDEX_TIME:
|
||||
break;
|
||||
case P003_CT_INDEX_TOTAL_TIME:
|
||||
break;
|
||||
case P003_CT_INDEX_TIME_COUNTER:
|
||||
break;
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
}
|
||||
|
||||
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
|
||||
@@ -195,43 +267,7 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
static_cast<P003_data_struct *>(getPluginTaskData(event->TaskIndex));
|
||||
|
||||
if (nullptr != P003_data) {
|
||||
unsigned long pulseCounter, pulseCounterTotal;
|
||||
float pulseTime_msec;
|
||||
P003_data->pulseHelper.getPulseCounters(pulseCounter, pulseCounterTotal, pulseTime_msec);
|
||||
P003_data->pulseHelper.resetPulseCounter();
|
||||
|
||||
|
||||
// store the current counter values into UserVar (RTC-memory)
|
||||
// FIXME TD-er: Is it correct to write the first 3 UserVar values, regardless the set counter type?
|
||||
// FIXME TD-er: Must check we're interacting with the raw values in this PulseCounter plugin
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseCounter , pulseCounter);
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseTotalCounter , pulseCounterTotal);
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseTime , pulseTime_msec);
|
||||
|
||||
// Store the raw value in the unused 4th position.
|
||||
// This is needed to restore the value from RTC as it may be converted into another output value using a formula.
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_persistedTotalCounter, pulseCounterTotal);
|
||||
|
||||
switch (PCONFIG(P003_IDX_COUNTERTYPE))
|
||||
{
|
||||
case P003_CT_INDEX_COUNTER:
|
||||
case P003_CT_INDEX_TOTAL:
|
||||
{
|
||||
event->sensorType = Sensor_VType::SENSOR_TYPE_SINGLE;
|
||||
break;
|
||||
}
|
||||
case P003_CT_INDEX_COUNTER_TOTAL_TIME:
|
||||
{
|
||||
event->sensorType = Sensor_VType::SENSOR_TYPE_TRIPLE;
|
||||
break;
|
||||
}
|
||||
case P003_CT_INDEX_COUNTER_TOTAL:
|
||||
{
|
||||
event->sensorType = Sensor_VType::SENSOR_TYPE_DUAL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
success = true;
|
||||
success = P003_data->plugin_read(event);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -242,8 +278,8 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
static_cast<P003_data_struct *>(getPluginTaskData(event->TaskIndex));
|
||||
|
||||
if (nullptr != P003_data) {
|
||||
const String command = parseString(string, 1);
|
||||
bool mustCallPluginRead = false;
|
||||
const String command = parseString(string, 1);
|
||||
bool mustCallPluginRead = false;
|
||||
|
||||
const bool cmd_resetpulsecounter = equals(command, F("resetpulsecounter"));
|
||||
const bool cmd_setpulsecountertotal = equals(command, F("setpulsecountertotal"));
|
||||
@@ -300,8 +336,8 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
// i = increase the log level for regular statstic logs to "info"
|
||||
|
||||
const String subcommand = parseString(string, 2);
|
||||
const bool sub_i = equals(subcommand, F("i"));
|
||||
const bool sub_r = equals(subcommand, F("r"));
|
||||
const bool sub_i = equals(subcommand, F("i"));
|
||||
const bool sub_r = equals(subcommand, F("r"));
|
||||
|
||||
if ((sub_i) || (sub_r) || (subcommand.isEmpty())) {
|
||||
P003_data->pulseHelper.doStatisticLogging(P003_PULSE_STATS_ADHOC_LOG_LEVEL);
|
||||
@@ -313,7 +349,7 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
success = true;
|
||||
}
|
||||
# else // ifdef PULSE_STATISTIC
|
||||
success = false; // Command not available
|
||||
success = false; // Command not available
|
||||
# endif // PULSE_STATISTIC
|
||||
}
|
||||
|
||||
@@ -334,6 +370,12 @@ boolean Plugin_003(uint8_t function, struct EventStruct *event, String& string)
|
||||
if (nullptr != P003_data) {
|
||||
// step 0 will check if a new signal edge is to be processed and then schedule step 1
|
||||
P003_data->pulseHelper.doPulseStepProcessing(GPIO_PULSE_HELPER_PROCESSING_STEP_0);
|
||||
|
||||
if ((Settings.TaskDeviceTimer[event->TaskIndex] == 0) &&
|
||||
PCONFIG(P003_IDX_IGNORE_ZERO) &&
|
||||
P003_data->plugin_peek(event)) {
|
||||
Scheduler.schedule_task_device_timer(event->TaskIndex, millis() - 10);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,69 @@
|
||||
|
||||
|
||||
#ifdef USES_P003
|
||||
P003_data_struct::P003_data_struct(const Internal_GPIO_pulseHelper::pulseCounterConfig& config)
|
||||
P003_data_struct::P003_data_struct(const Internal_GPIO_pulseHelper::pulseCounterConfig& config)
|
||||
: pulseHelper(config) {}
|
||||
|
||||
bool P003_data_struct::plugin_read(struct EventStruct *event) {
|
||||
bool success = true;
|
||||
unsigned long pulseCounter, pulseCounterTotal;
|
||||
float pulseTime_msec;
|
||||
|
||||
pulseHelper.getPulseCounters(pulseCounter, pulseCounterTotal, pulseTime_msec);
|
||||
pulseHelper.resetPulseCounter();
|
||||
|
||||
|
||||
if (PCONFIG(P003_IDX_IGNORE_ZERO) && (0 == pulseCounter) && lastDeltaZero) {
|
||||
success = false;
|
||||
}
|
||||
lastDeltaZero = (0 == pulseCounter);
|
||||
|
||||
// store the current counter values into UserVar (RTC-memory)
|
||||
// FIXME TD-er: Must check we're interacting with the raw values in this PulseCounter plugin
|
||||
// For backward compatibility, set all values
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseCounter, pulseCounter);
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseTotalCounter, pulseCounterTotal);
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseTime, pulseTime_msec);
|
||||
|
||||
switch (PCONFIG(P003_IDX_COUNTERTYPE)) {
|
||||
case P003_CT_INDEX_COUNTER: // No updates
|
||||
case P003_CT_INDEX_COUNTER_TOTAL: // No updates
|
||||
break;
|
||||
case P003_CT_INDEX_TOTAL: // Replace first value
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseCounter, pulseCounterTotal);
|
||||
break;
|
||||
case P003_CT_INDEX_COUNTER_TOTAL_TIME: // No updates
|
||||
break;
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
case P003_CT_INDEX_TIME: // Replace first value
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseCounter, pulseTime_msec);
|
||||
break;
|
||||
case P003_CT_INDEX_TOTAL_TIME: // Replace first 2 values
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseCounter, pulseCounterTotal);
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseTotalCounter, pulseTime_msec);
|
||||
break;
|
||||
case P003_CT_INDEX_TIME_COUNTER: // Replace first 2 values
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseCounter, pulseTime_msec);
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_pulseTotalCounter, pulseCounter);
|
||||
break;
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
}
|
||||
|
||||
// Store the raw value in the unused 4th position.
|
||||
// This is needed to restore the value from RTC as it may be converted into another output value using a formula.
|
||||
UserVar.setFloat(event->TaskIndex, P003_IDX_persistedTotalCounter, pulseCounterTotal);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool P003_data_struct::plugin_peek(struct EventStruct *event) {
|
||||
unsigned long pulseCounter, pulseCounterTotal;
|
||||
float pulseTime_msec;
|
||||
|
||||
pulseHelper.getPulseCounters(pulseCounter, pulseCounterTotal, pulseTime_msec);
|
||||
|
||||
const bool success = (PCONFIG(P003_IDX_IGNORE_ZERO) && (0 != pulseCounter));
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
#endif // ifdef USES_P003
|
||||
|
||||
@@ -4,6 +4,39 @@
|
||||
#include "../../_Plugin_Helper.h"
|
||||
#ifdef USES_P003
|
||||
|
||||
# ifndef P003_USE_EXTRA_COUNTERTYPES
|
||||
# ifdef LIMIT_BUILD_SIZE
|
||||
# define P003_USE_EXTRA_COUNTERTYPES 0 // Don't include for limited builds
|
||||
# else // ifdef LIMIT_BUILD_SIZE
|
||||
# define P003_USE_EXTRA_COUNTERTYPES 1
|
||||
# endif // ifdef LIMIT_BUILD_SIZE
|
||||
# endif // ifndef P003_USE_EXTRA_COUNTERTYPES
|
||||
|
||||
// ... their index in UserVar and TaskDeviceValueNames
|
||||
# define P003_IDX_pulseCounter 0
|
||||
# define P003_IDX_pulseTotalCounter 1
|
||||
# define P003_IDX_pulseTime 2
|
||||
|
||||
// ... and the following index into UserVar for storing the persisted TotalCounter
|
||||
# define P003_IDX_persistedTotalCounter 3
|
||||
|
||||
// indexes for config parameters
|
||||
# define P003_IDX_DEBOUNCETIME 0
|
||||
# define P003_IDX_COUNTERTYPE 1
|
||||
# define P003_IDX_MODETYPE 2
|
||||
# define P003_IDX_IGNORE_ZERO 3
|
||||
|
||||
// values for WEBFORM Counter Types
|
||||
# define P003_CT_INDEX_COUNTER 0
|
||||
# define P003_CT_INDEX_COUNTER_TOTAL_TIME 1
|
||||
# define P003_CT_INDEX_TOTAL 2
|
||||
# define P003_CT_INDEX_COUNTER_TOTAL 3
|
||||
# if P003_USE_EXTRA_COUNTERTYPES
|
||||
# define P003_CT_INDEX_TIME 4
|
||||
# define P003_CT_INDEX_TOTAL_TIME 5
|
||||
# define P003_CT_INDEX_TIME_COUNTER 6
|
||||
# endif // if P003_USE_EXTRA_COUNTERTYPES
|
||||
|
||||
|
||||
# include "../Helpers/_Internal_GPIO_pulseHelper.h"
|
||||
|
||||
@@ -13,7 +46,12 @@ struct P003_data_struct : public PluginTaskData_base {
|
||||
|
||||
virtual ~P003_data_struct() = default;
|
||||
|
||||
bool plugin_read(struct EventStruct *event);
|
||||
bool plugin_peek(struct EventStruct *event);
|
||||
|
||||
Internal_GPIO_pulseHelper pulseHelper;
|
||||
|
||||
bool lastDeltaZero = false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user