Skip to content

Commit 39101d5

Browse files
committed
Add support for buzzer (#318)
1 parent e12cb54 commit 39101d5

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

NodeManager.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void before() {
366366
Serial.begin(MY_BAUD_RATE);
367367

368368

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

NodeManagerLibrary.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,10 @@ class SensorDigitalOutput: public Sensor {
831831
void setInputIsElapsed(bool value);
832832
// [107] optionally wait for the given number of milliseconds after changing the status (default: 0)
833833
void setWaitAfterSet(int value);
834+
// [108] when switching on, turns the output off after the given number of milliseconds. For latching relay controls the pulse width (default: 0)
835+
void setPulseWidth(int value);
834836
// manually switch the output to the provided value
835-
void setStatus(Child* child, int value);
837+
void setStatus(int value);
836838
// get the current state
837839
int getStatus();
838840
void onSetup();
@@ -844,9 +846,10 @@ class SensorDigitalOutput: public Sensor {
844846
bool _legacy_mode = false;
845847
bool _input_is_elapsed = false;
846848
int _wait_after_set = 0;
849+
int _pulse_width = 0;
847850
Timer* _safeguard_timer;
848851
void _setupPin(Child* child, int pin);
849-
virtual void _setStatus(Child* child, int value);
852+
virtual void _setStatus(int value);
850853
int _getValueToWrite(int value);
851854
};
852855

@@ -864,8 +867,6 @@ class SensorRelay: public SensorDigitalOutput {
864867
class SensorLatchingRelay: public SensorRelay {
865868
public:
866869
SensorLatchingRelay(NodeManager& node_manager, int pin, int child_id = -255);
867-
// [201] set the duration of the pulse to send in ms to activate the relay (default: 50)
868-
void setPulseWidth(int value);
869870
// [202] set the pin which turns the relay off (default: the pin provided while registering the sensor)
870871
void setPinOff(int value);
871872
// [203] set the pin which turns the relay on (default: the pin provided while registering the sensor + 1)
@@ -875,8 +876,7 @@ class SensorLatchingRelay: public SensorRelay {
875876
protected:
876877
int _pin_on;
877878
int _pin_off;
878-
int _pulse_width = 50;
879-
void _setStatus(Child* child, int value);
879+
void _setStatus(int value);
880880
};
881881
#endif
882882

NodeManagerLibrary.ino

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,9 @@ void SensorDigitalOutput::setInputIsElapsed(bool value) {
12231223
void SensorDigitalOutput::setWaitAfterSet(int value) {
12241224
_wait_after_set = value;
12251225
}
1226+
void SensorDigitalOutput::setPulseWidth(int value) {
1227+
_pulse_width = value;
1228+
}
12261229

12271230
// main task
12281231
void SensorDigitalOutput::onLoop(Child* child) {
@@ -1231,7 +1234,7 @@ void SensorDigitalOutput::onLoop(Child* child) {
12311234
// update the timer
12321235
_safeguard_timer->update();
12331236
// if the time is over, turn the output off
1234-
if (_safeguard_timer->isOver()) setStatus(child,OFF);
1237+
if (_safeguard_timer->isOver()) setStatus(OFF);
12351238
}
12361239
}
12371240

@@ -1242,7 +1245,7 @@ void SensorDigitalOutput::onReceive(MyMessage* message) {
12421245
// by default handle a SET message but when legacy mode is set when a REQ message is expected instead
12431246
if ( (message->getCommand() == C_SET && ! _legacy_mode) || (message->getCommand() == C_REQ && _legacy_mode)) {
12441247
// switch the output
1245-
setStatus(child, message->getInt());
1248+
setStatus(message->getInt());
12461249
}
12471250
if (message->getCommand() == C_REQ && ! _legacy_mode) {
12481251
// just return the current status
@@ -1251,7 +1254,7 @@ void SensorDigitalOutput::onReceive(MyMessage* message) {
12511254
}
12521255

12531256
// write the value to the output
1254-
void SensorDigitalOutput::setStatus(Child* child, int value) {
1257+
void SensorDigitalOutput::setStatus(int value) {
12551258
// pre-process the input value
12561259
if (_input_is_elapsed) {
12571260
// the input provided is an elapsed time
@@ -1269,12 +1272,12 @@ void SensorDigitalOutput::setStatus(Child* child, int value) {
12691272
// if turning the output on and a safeguard timer is configured, start it
12701273
if (value == ON && _safeguard_timer->isConfigured() && ! _safeguard_timer->isRunning()) _safeguard_timer->start();
12711274
}
1272-
_setStatus(child, value);
1275+
_setStatus(value);
12731276
// wait if needed for relay drawing a lot of current
12741277
if (_wait_after_set > 0) _node->sleepOrWait(_wait_after_set);
12751278
// store the new status so it will be sent to the controller
12761279
_status = value;
1277-
((ChildInt*)child)->setValueInt(value);
1280+
((ChildInt*)children.get(1))->setValueInt(value);
12781281
}
12791282

12801283
// setup the provided pin for output
@@ -1289,19 +1292,24 @@ void SensorDigitalOutput::_setupPin(Child* child, int pin) {
12891292
}
12901293

12911294
// switch to the requested status
1292-
void SensorDigitalOutput::_setStatus(Child* child, int value) {
1295+
void SensorDigitalOutput::_setStatus(int value) {
12931296
int value_to_write = _getValueToWrite(value);
12941297
// set the value to the pin
12951298
digitalWrite(_pin, value_to_write);
12961299
#if FEATURE_DEBUG == ON
12971300
Serial.print(_name);
12981301
Serial.print(F(" I="));
1299-
Serial.print(child->child_id);
1302+
Serial.print(children.get(1)->child_id);
13001303
Serial.print(F(" P="));
13011304
Serial.print(_pin);
13021305
Serial.print(F(" V="));
13031306
Serial.println(value_to_write);
13041307
#endif
1308+
// if pulse width is set and status is on, turn it off after the configured interval
1309+
if (_pulse_width > 0 && value == ON) {
1310+
_node->sleepOrWait(_pulse_width);
1311+
digitalWrite(_pin, !value_to_write);
1312+
}
13051313
}
13061314

13071315
// reverse the value if needed based on the _on_value
@@ -1338,12 +1346,11 @@ SensorLatchingRelay::SensorLatchingRelay(NodeManager& node_manager, int pin, int
13381346
_pin_on = pin;
13391347
_pin_off = pin + 1;
13401348
children.get(1)->description = _name;
1349+
// set pulse duration
1350+
_pulse_width = 50;
13411351
}
13421352

13431353
// setter/getter
1344-
void SensorLatchingRelay::setPulseWidth(int value) {
1345-
_pulse_width = value;
1346-
}
13471354
void SensorLatchingRelay::setPinOn(int value) {
13481355
_pin_on = value;
13491356
}
@@ -1358,7 +1365,7 @@ void SensorLatchingRelay::onSetup() {
13581365
}
13591366

13601367
// switch to the requested status
1361-
void SensorLatchingRelay::_setStatus(Child* child, int value) {
1368+
void SensorLatchingRelay::_setStatus(int value) {
13621369
// select the right pin to send the pulse to
13631370
int pin = value == OFF ? _pin_off : _pin_on;
13641371
// set the value
@@ -1367,8 +1374,9 @@ void SensorLatchingRelay::_setStatus(Child* child, int value) {
13671374
_node->sleepOrWait(_pulse_width);
13681375
digitalWrite(pin, ! _on_value);
13691376
#if FEATURE_DEBUG == ON
1370-
Serial.print(F("LAT I="));
1371-
Serial.print(child->child_id);
1377+
Serial.print(_name);
1378+
Serial.print(F(" I="));
1379+
Serial.print(children.get(1)->child_id);
13721380
Serial.print(F(" P="));
13731381
Serial.print(pin);
13741382
Serial.print(F(" S="));
@@ -4159,12 +4167,12 @@ void SensorConfiguration::onReceive(MyMessage* message) {
41594167
case 105: custom_sensor->setSafeguard(request.getValueInt()); break;
41604168
case 106: custom_sensor->setInputIsElapsed(request.getValueInt()); break;
41614169
case 107: custom_sensor->setWaitAfterSet(request.getValueInt()); break;
4170+
case 108: custom_sensor->setPulseWidth(request.getValueInt()); break;
41624171
default: return;
41634172
}
41644173
if (function > 200 && strcmp(sensor->getName(),"LATCHING") == 0) {
41654174
SensorLatchingRelay* custom_sensor_2 = (SensorLatchingRelay*)sensor;
41664175
switch(function) {
4167-
case 201: custom_sensor_2->setPulseWidth(request.getValueInt()); break;
41684176
case 202: custom_sensor_2->setPinOff(request.getValueInt()); break;
41694177
case 203: custom_sensor_2->setPinOn(request.getValueInt()); break;
41704178
default: return;

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,12 @@ Each sensor class exposes additional methods.
491491
void setInputIsElapsed(bool value);
492492
// [107] optionally wait for the given number of milliseconds after changing the status (default: 0)
493493
void setWaitAfterSet(int value);
494+
// [108] when switching on, turns the output off after the given number of milliseconds. For latching relay controls the pulse width (default: 0)
495+
void setPulseWidth(int value);
494496
~~~
495497
496498
* SensorLatchingRelay (in addition to those available for SensorDigitalOutput / SensorRelay)
497499
~~~c
498-
// [201] set the duration of the pulse to send in ms to activate the relay (default: 50)
499-
void setPulseWidth(int value);
500500
// [202] set the pin which turns the relay off (default: the pin provided while registering the sensor)
501501
void setPinOff(int value);
502502
// [203] set the pin which turns the relay on (default: the pin provided while registering the sensor + 1)

0 commit comments

Comments
 (0)