[P163] Add optional non-incremental counting and reset command/option

This commit is contained in:
Ton Huisman
2024-08-23 11:22:04 +02:00
parent 5c4f1323e2
commit b833ea025a
3 changed files with 43 additions and 11 deletions
+13 -3
View File
@@ -6,6 +6,8 @@
// #######################################################################################################
/** Changelog:
* 2024-08-23 tonhuisman: Add options to read new pulses only (default) instead of incrementing pulse count,
* and reset on read, to clear the incrementing pulxe count
* 2024-08-13 tonhuisman: Use pluginstats to get average over last n samples for determining event threshold
* Add highvoltage subcommand to switch the high voltage off or on
* 2024-08-12 tonhuisman: Start plugin for RadSens I2C radiation counter using RadSens library
@@ -23,6 +25,7 @@
# define PLUGIN_VALUENAME1_163 "Count"
# define PLUGIN_VALUENAME2_163 "iDynamic"
# define PLUGIN_VALUENAME3_163 "iStatic"
# define PLUGIN_VALUENAME4_163 "IncrCount"
# include "./src/PluginStructs/P163_data_struct.h"
@@ -39,7 +42,7 @@ boolean Plugin_163(uint8_t function, struct EventStruct *event, String& string)
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].ValueCount = 4;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
@@ -61,6 +64,7 @@ boolean Plugin_163(uint8_t function, struct EventStruct *event, String& string)
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_163));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_163));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_163));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_163));
break;
}
@@ -98,8 +102,12 @@ boolean Plugin_163(uint8_t function, struct EventStruct *event, String& string)
case PLUGIN_WEBFORM_LOAD:
{
addFormCheckBox(F("Use Low Power mode"), F("lpmode"), P163_GET_LOW_POWER);
addFormCheckBox(F("Enable onboard Led"), F("led"), P163_GET_LED_STATE);
addFormCheckBox(F("Read incremental count"), F("rinc"), P163_GET_READ_INCREMENT);
addFormCheckBox(F("Reset after read"), F("rst"), P163_GET_RESET_ON_READ);
addFormCheckBox(F("Use Low Power mode"), F("lpmode"), P163_GET_LOW_POWER);
addFormCheckBox(F("Enable onboard Led"), F("led"), P163_GET_LED_STATE);
addFormNumericBox(F("Events on Count-threshold"), F("chg"), P163_CFG_THRESHOLD, -1);
addUnit(F("-1 = disabled"));
@@ -115,6 +123,8 @@ boolean Plugin_163(uint8_t function, struct EventStruct *event, String& string)
case PLUGIN_WEBFORM_SAVE:
{
P163_SET_READ_INCREMENT(isFormItemChecked(F("rinc")));
P163_SET_RESET_ON_READ(isFormItemChecked(F("rst")));
P163_SET_LOW_POWER(isFormItemChecked(F("lpmode")));
P163_SET_LED_STATE(isFormItemChecked(F("led")));
P163_CFG_THRESHOLD = getFormItemInt(F("chg"));
+19 -5
View File
@@ -6,10 +6,12 @@
* Constructor
**************************************************************************/
P163_data_struct::P163_data_struct(struct EventStruct *event) {
_lowPowerMode = P163_GET_LOW_POWER;
_ledState = P163_GET_LED_STATE;
_threshold = P163_CFG_THRESHOLD;
_changeOnly = _threshold < 0;
_readIncrement = P163_GET_READ_INCREMENT;
_resetOnRead = P163_GET_RESET_ON_READ;
_lowPowerMode = P163_GET_LOW_POWER;
_ledState = P163_GET_LED_STATE;
_threshold = P163_CFG_THRESHOLD;
_changeOnly = _threshold < 0;
# if FEATURE_PLUGIN_STATS
_countAvg = P163_CFG_COUNT_AVG;
# endif // if FEATURE_PLUGIN_STATS
@@ -57,11 +59,16 @@ bool P163_data_struct::plugin_read(struct EventStruct *event) {
bool P163_data_struct::setOutputValues(struct EventStruct *event) {
bool result = false;
const uint32_t count = sensor->getNumberOfPulses();
const uint32_t count = _readIncrement ? sensor->getNumberOfPulses() : sensor->getNumberOfNewPulses();
const uint32_t incCount = sensor->getNumberOfPulses();
const float iDynamic = sensor->getRadIntensyDynamic();
const float iStatic = sensor->getRadIntensyStatic();
int32_t delta = -1; // Invalid/unset
if (_resetOnRead) {
sensor->resetPulses();
}
# if FEATURE_PLUGIN_STATS
if (hasPluginStats()) {
@@ -84,6 +91,7 @@ bool P163_data_struct::setOutputValues(struct EventStruct *event) {
UserVar.setFloat(event->TaskIndex, 1, iDynamic);
UserVar.setFloat(event->TaskIndex, 2, iStatic);
UserVar.setFloat(event->TaskIndex, 3, incCount);
return result;
}
@@ -94,12 +102,14 @@ bool P163_data_struct::setOutputValues(struct EventStruct *event) {
const char P163_subcommands[] PROGMEM =
"calibration|"
"highvoltage|"
"resetcount|"
;
enum class P163_subcmd_e : int8_t {
invalid = -1,
calibration = 0,
highvoltage = 1,
resetcount = 2,
};
bool P163_data_struct::plugin_write(struct EventStruct *event,
@@ -133,6 +143,10 @@ bool P163_data_struct::plugin_write(struct EventStruct *event,
success = true;
}
break;
case P163_subcmd_e::resetcount:
sensor->resetPulses();
success = true;
break;
}
}
return success;
+11 -3
View File
@@ -12,11 +12,17 @@
# define P163_CONFIG_FLAGS PCONFIG_ULONG(0) // All flags
# define P163_CONFIG_LOW_POWER 0 // Flag indexes
# define P163_CONFIG_LED_STATE 1
# define P163_CONFIG_READ_INCREMENT 2
# define P163_CONFIG_RESET_ON_READ 3
# define P163_GET_LOW_POWER (bitRead(P163_CONFIG_FLAGS, P163_CONFIG_LOW_POWER))
# define P163_SET_LOW_POWER(T) (bitWrite(P163_CONFIG_FLAGS, P163_CONFIG_LOW_POWER, T))
# define P163_GET_LED_STATE (bitRead(P163_CONFIG_FLAGS, P163_CONFIG_LED_STATE))
# define P163_SET_LED_STATE(T) (bitWrite(P163_CONFIG_FLAGS, P163_CONFIG_LED_STATE, T))
# define P163_GET_READ_INCREMENT (bitRead(P163_CONFIG_FLAGS, P163_CONFIG_READ_INCREMENT))
# define P163_SET_READ_INCREMENT(T) (bitWrite(P163_CONFIG_FLAGS, P163_CONFIG_READ_INCREMENT, T))
# define P163_GET_RESET_ON_READ (bitRead(P163_CONFIG_FLAGS, P163_CONFIG_RESET_ON_READ))
# define P163_SET_RESET_ON_READ(T) (bitWrite(P163_CONFIG_FLAGS, P163_CONFIG_RESET_ON_READ, T))
struct P163_data_struct : public PluginTaskData_base {
public:
@@ -46,9 +52,11 @@ private:
# if FEATURE_PLUGIN_STATS
uint8_t _countAvg = 1;
# endif // if FEATURE_PLUGIN_STATS
bool _lowPowerMode = false;
bool _ledState = true;
bool _changeOnly = false;
bool _readIncrement = false;
bool _resetOnRead = false;
bool _lowPowerMode = false;
bool _ledState = true;
bool _changeOnly = false;
bool initialized = false;
};