@@ -1202,6 +1202,8 @@ SensorDigitalOutput::SensorDigitalOutput(NodeManager& node_manager, int pin, int
1202
1202
void SensorDigitalOutput::onSetup () {
1203
1203
_setupPin (children.get (1 ), _pin);
1204
1204
_safeguard_timer = new Timer (_node);
1205
+ // report immediately
1206
+ _report_timer->unset ();
1205
1207
}
1206
1208
1207
1209
// setter/getter
@@ -1280,6 +1282,11 @@ void SensorDigitalOutput::setStatus(int value) {
1280
1282
((ChildInt*)children.get (1 ))->setValueInt (value);
1281
1283
}
1282
1284
1285
+ // toggle the status
1286
+ void SensorDigitalOutput::toggleStatus () {
1287
+ setStatus (!_status);
1288
+ }
1289
+
1283
1290
// setup the provided pin for output
1284
1291
void SensorDigitalOutput::_setupPin (Child* child, int pin) {
1285
1292
// set the pin as output and initialize it accordingly
@@ -1362,6 +1369,8 @@ void SensorLatchingRelay::setPinOff(int value) {
1362
1369
void SensorLatchingRelay::onSetup () {
1363
1370
_setupPin (children.get (1 ),_pin_on);
1364
1371
_setupPin (children.get (1 ),_pin_off);
1372
+ // report immediately
1373
+ _report_timer->unset ();
1365
1374
}
1366
1375
1367
1376
// switch to the requested status
@@ -1564,9 +1573,6 @@ SensorInterrupt::SensorInterrupt(NodeManager& node_manager, int pin, int child_i
1564
1573
void SensorInterrupt::setMode (int value) {
1565
1574
_mode = value;
1566
1575
}
1567
- void SensorInterrupt::setDebounce (int value) {
1568
- _debounce = value;
1569
- }
1570
1576
void SensorInterrupt::setTriggerTime (int value) {
1571
1577
_trigger_time = value;
1572
1578
}
@@ -1617,8 +1623,6 @@ void SensorInterrupt::onInterrupt() {
1617
1623
_counter = _counter + 1 ;
1618
1624
#endif
1619
1625
Child* child = children.get (1 );
1620
- // wait to ensure the the input is not floating
1621
- if (_debounce > 0 ) _node->sleepOrWait (_debounce);
1622
1626
// read the value of the pin
1623
1627
int value = _node->getLastInterruptValue ();
1624
1628
// process the value
@@ -1642,9 +1646,6 @@ void SensorInterrupt::onInterrupt() {
1642
1646
((ChildInt*)child)->setValueInt (value);
1643
1647
// allow the signal to be restored to its normal value
1644
1648
if (_trigger_time > 0 ) _node->sleepOrWait (_trigger_time);
1645
- } else {
1646
- // invalid
1647
- ((ChildInt*)child)->setValueInt (-255 );
1648
1649
}
1649
1650
}
1650
1651
@@ -2865,6 +2866,8 @@ void SensorDimmer::setReverse(bool value) {
2865
2866
// what to do during setup
2866
2867
void SensorDimmer::onSetup () {
2867
2868
pinMode (_pin, OUTPUT);
2869
+ // report immediately
2870
+ _report_timer->unset ();
2868
2871
}
2869
2872
2870
2873
// what to do during loop
@@ -3829,6 +3832,8 @@ SensorServo::SensorServo(NodeManager& node_manager, int pin, int child_id): Sens
3829
3832
// what to do during setup
3830
3833
void SensorServo::onSetup () {
3831
3834
_servo.attach (_pin);
3835
+ // report immediately
3836
+ _report_timer->unset ();
3832
3837
}
3833
3838
3834
3839
// what to do during loop
@@ -4052,7 +4057,7 @@ void SensorConfiguration::onReceive(MyMessage* message) {
4052
4057
case 10 : _node->setRetries (request.getValueInt ()); break ;
4053
4058
#if FEATURE_INTERRUPTS == ON
4054
4059
case 19 : _node->setSleepInterruptPin (request.getValueInt ()); break ;
4055
- case 28 : _node->setInterruptMinDelta (request.getValueInt ()); break ;
4060
+ case 28 : _node->setInterruptDebounce (request.getValueInt ()); break ;
4056
4061
#endif
4057
4062
case 21 : _node->setAck (request.getValueInt ()); break ;
4058
4063
case 22 : _node->setIsMetric (request.getValueInt ()); break ;
@@ -4185,7 +4190,6 @@ void SensorConfiguration::onReceive(MyMessage* message) {
4185
4190
SensorInterrupt* custom_sensor = (SensorInterrupt*)sensor;
4186
4191
switch (function) {
4187
4192
case 101 : custom_sensor->setMode (request.getValueInt ()); break ;
4188
- case 102 : custom_sensor->setDebounce (request.getValueInt ()); break ;
4189
4193
case 103 : custom_sensor->setTriggerTime (request.getValueInt ()); break ;
4190
4194
case 104 : custom_sensor->setInitial (request.getValueInt ()); break ;
4191
4195
case 105 : custom_sensor->setActiveState (request.getValueInt ()); break ;
@@ -4351,7 +4355,7 @@ int NodeManager::_last_interrupt_pin = -1;
4351
4355
int NodeManager::_last_interrupt_value = LOW;
4352
4356
long NodeManager::_last_interrupt_1 = millis();
4353
4357
long NodeManager::_last_interrupt_2 = millis();
4354
- long NodeManager::_interrupt_min_delta = 100 ;
4358
+ long NodeManager::_interrupt_debounce = 100 ;
4355
4359
#endif
4356
4360
4357
4361
// setter/getter
@@ -4403,8 +4407,8 @@ void NodeManager::setInterrupt(int pin, int mode, int initial) {
4403
4407
_interrupt_2_initial = initial;
4404
4408
}
4405
4409
}
4406
- void NodeManager::setInterruptMinDelta (long value) {
4407
- _interrupt_min_delta = value;
4410
+ void NodeManager::setInterruptDebounce (long value) {
4411
+ _interrupt_debounce = value;
4408
4412
}
4409
4413
#endif
4410
4414
#if FEATURE_POWER_MANAGER == ON
@@ -4606,7 +4610,7 @@ void NodeManager::loop() {
4606
4610
_message.clear ();
4607
4611
sensor->interrupt ();
4608
4612
sensor->loop (nullptr );
4609
- // reset the last interrupt pin
4613
+ // reset the last interrupt pin
4610
4614
_last_interrupt_pin = -1 ;
4611
4615
}
4612
4616
else if (_last_interrupt_pin == -1 ) {
@@ -4874,30 +4878,34 @@ int NodeManager::getAvailableChildId(int child_id) {
4874
4878
// handle an interrupt
4875
4879
void NodeManager::_onInterrupt_1 () {
4876
4880
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
4878
4884
_last_interrupt_pin = INTERRUPT_PIN_1;
4879
4885
_last_interrupt_value = digitalRead (INTERRUPT_PIN_1);
4886
+ _last_interrupt_1 = now;
4880
4887
#if FEATURE_DEBUG == ON
4881
4888
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=" ) );
4884
4891
Serial.println (_last_interrupt_value);
4885
4892
#endif
4886
- _last_interrupt_1 = now;
4887
4893
}
4888
4894
}
4889
4895
void NodeManager::_onInterrupt_2 () {
4890
4896
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
4892
4900
_last_interrupt_pin = INTERRUPT_PIN_2;
4893
4901
_last_interrupt_value = digitalRead (INTERRUPT_PIN_2);
4902
+ _last_interrupt_2 = now;
4894
4903
#if FEATURE_DEBUG == ON
4895
4904
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=" ) );
4898
4907
Serial.println (_last_interrupt_value);
4899
4908
#endif
4900
- _last_interrupt_2 = now;
4901
4909
}
4902
4910
}
4903
4911
#endif
@@ -5021,31 +5029,21 @@ void NodeManager::_sleep() {
5021
5029
int interrupt_2_pin = _interrupt_2_mode == MODE_NOT_DEFINED ? INTERRUPT_NOT_DEFINED : digitalPinToInterrupt (INTERRUPT_PIN_2);
5022
5030
// enter smart sleep for the requested sleep interval and with the configured interrupts
5023
5031
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
5024
5033
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);
5039
5039
#if FEATURE_DEBUG == ON
5040
5040
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);
5044
5042
Serial.print (F (" , V=" ));
5045
5043
Serial.println (_last_interrupt_value);
5046
5044
#endif
5047
5045
// 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;
5049
5047
}
5050
5048
#else
5051
5049
sleep (INTERRUPT_NOT_DEFINED,MODE_NOT_DEFINED,INTERRUPT_NOT_DEFINED,MODE_NOT_DEFINED,sleep_time*1000 ,_smart_sleep);
0 commit comments