Skip to content

Commit f0c8aad

Browse files
authored
SensorDigitalInput review (#412)
1 parent 549f8f9 commit f0c8aad

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,14 @@ Each sensor class may expose additional methods.
583583
void computeACNoise();
584584
~~~
585585

586+
* SensorDigitalInput
587+
~~~c
588+
// Invert the value to report. E.g. report 1 if value is LOW, report 0 if HIGH (default: false)
589+
void setInvertValueToReport(bool value);
590+
// Set optional internal pull up/down
591+
void setInitialValue(int value);
592+
~~~
593+
586594
* SensorDigitalOutput / SensorRelay / SensorLatchingRelay1Pin / SensorLatchingRelay2Pins
587595
~~~c
588596
// [104] when legacy mode is enabled expect a REQ message to trigger, otherwise the default SET (default: false)

sensors/SensorDigitalInput.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,40 @@
2323
SensorDigitalInput: read the digital input of the configured pin
2424
*/
2525
class SensorDigitalInput: public Sensor {
26+
protected:
27+
bool _invert_value_to_report = false;
28+
int _initial_value = -1;
2629
public:
2730
SensorDigitalInput(int8_t pin, uint8_t child_id = 255): Sensor(pin) {
2831
_name = "DIGITAL_I";
2932
children.allocateBlocks(1);
3033
new Child(this,INT,nodeManager.getAvailableChildId(child_id),S_CUSTOM,V_CUSTOM,_name);
3134
};
35+
36+
// Invert the value to report. E.g. report 1 if value is LOW, report 0 if HIGH (default: false)
37+
void setInvertValueToReport(bool value) {
38+
_invert_value_to_report = value;
39+
};
40+
41+
// Set optional internal pull up/down
42+
void setInitialValue(int value) {
43+
_initial_value = value;
44+
};
3245

3346
// define what to do during setup
3447
void onSetup() {
3548
// set the pin for input
3649
pinMode(_pin, INPUT);
50+
// set internal pull up/down
51+
if (_initial_value > -1) digitalWrite(_pin,_initial_value);
3752
};
3853

3954
// define what to do during loop
4055
void onLoop(Child* child) {
4156
// read the value
4257
int value = digitalRead(_pin);
58+
// invert the value if needed
59+
if (_invert_value_to_report) value = !value;
4360
// store the value
4461
child->setValue(value);
4562
};

0 commit comments

Comments
 (0)