Skip to content

Commit 06cf76d

Browse files
committed
Minor fixes to SensorInterrupt and SensorDigitalOutput (#320 #321)
1 parent 39101d5 commit 06cf76d

File tree

4 files changed

+48
-49
lines changed

4 files changed

+48
-49
lines changed

NodeManager.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ void before() {
365365
// setup the serial port baud rate
366366
Serial.begin(MY_BAUD_RATE);
367367

368+
368369

369-
370370
/*
371371
* Configure your sensors below
372372
*/

NodeManagerLibrary.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ class SensorDigitalOutput: public Sensor {
835835
void setPulseWidth(int value);
836836
// manually switch the output to the provided value
837837
void setStatus(int value);
838+
// toggle the status
839+
void toggleStatus();
838840
// get the current state
839841
int getStatus();
840842
void onSetup();
@@ -947,8 +949,6 @@ class SensorInterrupt: public Sensor {
947949
SensorInterrupt(NodeManager& node_manager, int pin, int child_id = -255);
948950
// [101] set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
949951
void setMode(int value);
950-
// [102] milliseconds to wait before reading the input (default: 0)
951-
void setDebounce(int value);
952952
// [103] time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
953953
void setTriggerTime(int value);
954954
// [104] Set initial value on the interrupt pin (default: HIGH)
@@ -967,7 +967,6 @@ class SensorInterrupt: public Sensor {
967967
void onReceive(MyMessage* message);
968968
void onInterrupt();
969969
protected:
970-
int _debounce = 0;
971970
int _trigger_time = 0;
972971
int _mode = CHANGE;
973972
int _initial = HIGH;
@@ -1783,7 +1782,7 @@ class NodeManager {
17831782
// configure the interrupt pin and mode. Mode can be CHANGE, RISING, FALLING (default: MODE_NOT_DEFINED)
17841783
void setInterrupt(int pin, int mode, int initial = -1);
17851784
// [28] ignore two consecutive interrupts if happening within this timeframe in milliseconds (default: 100)
1786-
void setInterruptMinDelta(long value);
1785+
void setInterruptDebounce(long value);
17871786
#endif
17881787
// register a sensor
17891788
void registerSensor(Sensor* sensor);
@@ -1903,7 +1902,7 @@ class NodeManager {
19031902
int _interrupt_2_initial = -1;
19041903
static int _last_interrupt_pin;
19051904
static int _last_interrupt_value;
1906-
static long _interrupt_min_delta;
1905+
static long _interrupt_debounce;
19071906
static long _last_interrupt_1;
19081907
static long _last_interrupt_2;
19091908
#endif

NodeManagerLibrary.ino

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,8 @@ SensorDigitalOutput::SensorDigitalOutput(NodeManager& node_manager, int pin, int
12021202
void SensorDigitalOutput::onSetup() {
12031203
_setupPin(children.get(1), _pin);
12041204
_safeguard_timer = new Timer(_node);
1205+
// report immediately
1206+
_report_timer->unset();
12051207
}
12061208

12071209
// setter/getter
@@ -1280,6 +1282,11 @@ void SensorDigitalOutput::setStatus(int value) {
12801282
((ChildInt*)children.get(1))->setValueInt(value);
12811283
}
12821284

1285+
// toggle the status
1286+
void SensorDigitalOutput::toggleStatus() {
1287+
setStatus(!_status);
1288+
}
1289+
12831290
// setup the provided pin for output
12841291
void SensorDigitalOutput::_setupPin(Child* child, int pin) {
12851292
// set the pin as output and initialize it accordingly
@@ -1362,6 +1369,8 @@ void SensorLatchingRelay::setPinOff(int value) {
13621369
void SensorLatchingRelay::onSetup() {
13631370
_setupPin(children.get(1),_pin_on);
13641371
_setupPin(children.get(1),_pin_off);
1372+
// report immediately
1373+
_report_timer->unset();
13651374
}
13661375

13671376
// switch to the requested status
@@ -1564,9 +1573,6 @@ SensorInterrupt::SensorInterrupt(NodeManager& node_manager, int pin, int child_i
15641573
void SensorInterrupt::setMode(int value) {
15651574
_mode = value;
15661575
}
1567-
void SensorInterrupt::setDebounce(int value) {
1568-
_debounce = value;
1569-
}
15701576
void SensorInterrupt::setTriggerTime(int value) {
15711577
_trigger_time = value;
15721578
}
@@ -1617,8 +1623,6 @@ void SensorInterrupt::onInterrupt() {
16171623
_counter = _counter + 1;
16181624
#endif
16191625
Child* child = children.get(1);
1620-
// wait to ensure the the input is not floating
1621-
if (_debounce > 0) _node->sleepOrWait(_debounce);
16221626
// read the value of the pin
16231627
int value = _node->getLastInterruptValue();
16241628
// process the value
@@ -1642,9 +1646,6 @@ void SensorInterrupt::onInterrupt() {
16421646
((ChildInt*)child)->setValueInt(value);
16431647
// allow the signal to be restored to its normal value
16441648
if (_trigger_time > 0) _node->sleepOrWait(_trigger_time);
1645-
} else {
1646-
// invalid
1647-
((ChildInt*)child)->setValueInt(-255);
16481649
}
16491650
}
16501651

@@ -2865,6 +2866,8 @@ void SensorDimmer::setReverse(bool value) {
28652866
// what to do during setup
28662867
void SensorDimmer::onSetup() {
28672868
pinMode(_pin, OUTPUT);
2869+
// report immediately
2870+
_report_timer->unset();
28682871
}
28692872

28702873
// what to do during loop
@@ -3829,6 +3832,8 @@ SensorServo::SensorServo(NodeManager& node_manager, int pin, int child_id): Sens
38293832
// what to do during setup
38303833
void SensorServo::onSetup() {
38313834
_servo.attach(_pin);
3835+
// report immediately
3836+
_report_timer->unset();
38323837
}
38333838

38343839
// what to do during loop
@@ -4052,7 +4057,7 @@ void SensorConfiguration::onReceive(MyMessage* message) {
40524057
case 10: _node->setRetries(request.getValueInt()); break;
40534058
#if FEATURE_INTERRUPTS == ON
40544059
case 19: _node->setSleepInterruptPin(request.getValueInt()); break;
4055-
case 28: _node->setInterruptMinDelta(request.getValueInt()); break;
4060+
case 28: _node->setInterruptDebounce(request.getValueInt()); break;
40564061
#endif
40574062
case 21: _node->setAck(request.getValueInt()); break;
40584063
case 22: _node->setIsMetric(request.getValueInt()); break;
@@ -4185,7 +4190,6 @@ void SensorConfiguration::onReceive(MyMessage* message) {
41854190
SensorInterrupt* custom_sensor = (SensorInterrupt*)sensor;
41864191
switch(function) {
41874192
case 101: custom_sensor->setMode(request.getValueInt()); break;
4188-
case 102: custom_sensor->setDebounce(request.getValueInt()); break;
41894193
case 103: custom_sensor->setTriggerTime(request.getValueInt()); break;
41904194
case 104: custom_sensor->setInitial(request.getValueInt()); break;
41914195
case 105: custom_sensor->setActiveState(request.getValueInt()); break;
@@ -4351,7 +4355,7 @@ int NodeManager::_last_interrupt_pin = -1;
43514355
int NodeManager::_last_interrupt_value = LOW;
43524356
long NodeManager::_last_interrupt_1 = millis();
43534357
long NodeManager::_last_interrupt_2 = millis();
4354-
long NodeManager::_interrupt_min_delta = 100;
4358+
long NodeManager::_interrupt_debounce = 100;
43554359
#endif
43564360

43574361
// setter/getter
@@ -4403,8 +4407,8 @@ void NodeManager::setInterrupt(int pin, int mode, int initial) {
44034407
_interrupt_2_initial = initial;
44044408
}
44054409
}
4406-
void NodeManager::setInterruptMinDelta(long value) {
4407-
_interrupt_min_delta = value;
4410+
void NodeManager::setInterruptDebounce(long value) {
4411+
_interrupt_debounce = value;
44084412
}
44094413
#endif
44104414
#if FEATURE_POWER_MANAGER == ON
@@ -4606,7 +4610,7 @@ void NodeManager::loop() {
46064610
_message.clear();
46074611
sensor->interrupt();
46084612
sensor->loop(nullptr);
4609-
// reset the last interrupt pin
4613+
// reset the last interrupt pin
46104614
_last_interrupt_pin = -1;
46114615
}
46124616
else if (_last_interrupt_pin == -1) {
@@ -4874,30 +4878,34 @@ int NodeManager::getAvailableChildId(int child_id) {
48744878
// handle an interrupt
48754879
void NodeManager::_onInterrupt_1() {
48764880
long now = millis();
4877-
if ( (now - _last_interrupt_1 > _interrupt_min_delta) || (now < _last_interrupt_1) ) {
4881+
// debounce the interrupt
4882+
if ( (now - _last_interrupt_1 > _interrupt_debounce) || (now < _last_interrupt_1) ) {
4883+
// register interrupt pin and value
48784884
_last_interrupt_pin = INTERRUPT_PIN_1;
48794885
_last_interrupt_value = digitalRead(INTERRUPT_PIN_1);
4886+
_last_interrupt_1 = now;
48804887
#if FEATURE_DEBUG == ON
48814888
Serial.print(F("INT P="));
4882-
Serial.print(INTERRUPT_PIN_1);
4883-
Serial.print(" V=");
4889+
Serial.print(_last_interrupt_pin);
4890+
Serial.print(F(", V="));
48844891
Serial.println(_last_interrupt_value);
48854892
#endif
4886-
_last_interrupt_1 = now;
48874893
}
48884894
}
48894895
void NodeManager::_onInterrupt_2() {
48904896
long now = millis();
4891-
if ( (now - _last_interrupt_2 > _interrupt_min_delta) || (now < _last_interrupt_2) ) {
4897+
// debounce the interrupt
4898+
if ( (now - _last_interrupt_2 > _interrupt_debounce) || (now < _last_interrupt_2) ) {
4899+
// register interrupt pin and value
48924900
_last_interrupt_pin = INTERRUPT_PIN_2;
48934901
_last_interrupt_value = digitalRead(INTERRUPT_PIN_2);
4902+
_last_interrupt_2 = now;
48944903
#if FEATURE_DEBUG == ON
48954904
Serial.print(F("INT P="));
4896-
Serial.print(INTERRUPT_PIN_2);
4897-
Serial.print(" V=");
4905+
Serial.print(_last_interrupt_pin);
4906+
Serial.print(F(", V="));
48984907
Serial.println(_last_interrupt_value);
48994908
#endif
4900-
_last_interrupt_2 = now;
49014909
}
49024910
}
49034911
#endif
@@ -5021,31 +5029,21 @@ void NodeManager::_sleep() {
50215029
int interrupt_2_pin = _interrupt_2_mode == MODE_NOT_DEFINED ? INTERRUPT_NOT_DEFINED : digitalPinToInterrupt(INTERRUPT_PIN_2);
50225030
// enter smart sleep for the requested sleep interval and with the configured interrupts
50235031
interrupt = sleep(interrupt_1_pin,_interrupt_1_mode,interrupt_2_pin,_interrupt_2_mode,sleep_time*1000,_smart_sleep);
5032+
// woke up by an interrupt
50245033
if (interrupt > -1) {
5025-
// woke up by an interrupt
5026-
int pin_number = -1;
5027-
int interrupt_mode = -1;
5028-
// map the interrupt to the pin
5029-
if (digitalPinToInterrupt(INTERRUPT_PIN_1) == interrupt) {
5030-
pin_number = INTERRUPT_PIN_1;
5031-
interrupt_mode = _interrupt_1_mode;
5032-
}
5033-
if (digitalPinToInterrupt(INTERRUPT_PIN_2) == interrupt) {
5034-
pin_number = INTERRUPT_PIN_2;
5035-
interrupt_mode = _interrupt_2_mode;
5036-
}
5037-
_last_interrupt_pin = pin_number;
5038-
_last_interrupt_value = digitalRead(pin_number);
5034+
// register the interrupt pin
5035+
if (digitalPinToInterrupt(INTERRUPT_PIN_1) == interrupt) _last_interrupt_pin = INTERRUPT_PIN_1;
5036+
if (digitalPinToInterrupt(INTERRUPT_PIN_2) == interrupt) _last_interrupt_pin = INTERRUPT_PIN_2;
5037+
// register the interrupt value
5038+
_last_interrupt_value = digitalRead(_last_interrupt_pin);
50395039
#if FEATURE_DEBUG == ON
50405040
Serial.print(F("INT P="));
5041-
Serial.print(pin_number);
5042-
Serial.print(F(", M="));
5043-
Serial.print(interrupt_mode);
5041+
Serial.print(_last_interrupt_pin);
50445042
Serial.print(F(", V="));
50455043
Serial.println(_last_interrupt_value);
50465044
#endif
50475045
// when waking up from an interrupt on the wakup pin, stop sleeping
5048-
if (_sleep_interrupt_pin == pin_number) _status = AWAKE;
5046+
if (_sleep_interrupt_pin == _last_interrupt_pin) _status = AWAKE;
50495047
}
50505048
#else
50515049
sleep(INTERRUPT_NOT_DEFINED,MODE_NOT_DEFINED,INTERRUPT_NOT_DEFINED,MODE_NOT_DEFINED,sleep_time*1000,_smart_sleep);

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ You can interact with each class provided by NodeManager through a set of API fu
233233
// configure the interrupt pin and mode. Mode can be CHANGE, RISING, FALLING (default: MODE_NOT_DEFINED)
234234
void setInterrupt(int pin, int mode, int initial = -1);
235235
// [28] ignore two consecutive interrupts if happening within this timeframe in milliseconds (default: 100)
236-
void setInterruptMinDelta(long value);
236+
void setInterruptDebounce(long value);
237237
#endif
238238
// register a sensor
239239
void registerSensor(Sensor* sensor);
@@ -493,6 +493,10 @@ Each sensor class exposes additional methods.
493493
void setWaitAfterSet(int value);
494494
// [108] when switching on, turns the output off after the given number of milliseconds. For latching relay controls the pulse width (default: 0)
495495
void setPulseWidth(int value);
496+
// manually switch the output to the provided value
497+
void setStatus(int value);
498+
// toggle the status
499+
void toggleStatus(int value);
496500
~~~
497501
498502
* SensorLatchingRelay (in addition to those available for SensorDigitalOutput / SensorRelay)
@@ -507,8 +511,6 @@ Each sensor class exposes additional methods.
507511
~~~c
508512
// [101] set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
509513
void setMode(int value);
510-
// [102] milliseconds to wait before reading the input (default: 0)
511-
void setDebounce(int value);
512514
// [103] time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
513515
void setTriggerTime(int value);
514516
// [104] Set initial value on the interrupt pin (default: HIGH)

0 commit comments

Comments
 (0)