From bc67e960cc89112000025d50c722b1bde84f9c79 Mon Sep 17 00:00:00 2001 From: user2684 Date: Sun, 25 Feb 2018 17:07:22 +0100 Subject: [PATCH] Add support for generic servo motor (#253) --- NodeManager.ino | 3 +++ NodeManagerLibrary.h | 23 ++++++++++++++++++++ NodeManagerLibrary.ino | 48 ++++++++++++++++++++++++++++++++++++++++++ README.md | 8 ++++++- 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/NodeManager.ino b/NodeManager.ino index d4aaf2aa..ec32c007 100755 --- a/NodeManager.ino +++ b/NodeManager.ino @@ -82,6 +82,7 @@ SensorSI7021 | 2 | USE_SI7021 | SI7021 sensor, return tempera SensorChirp | 3 | USE_CHIRP | Chirp soil moisture sensor (includes temperature and light sensors) | https://github.com/Apollon77/I2CSoilMoistureSensor DisplayHD44780 | 1 | USE_HD44780 | Supports most Hitachi HD44780 based LCDs, by default displays values of all sensors and children | https://github.com/cyberang3l/NewLiquidCrystal SensorTTP | 1 | USE_TTP | TTP226/TTP229 Touch control sensor | - +SensorServo | 1 | USE_SERVO | Control a generic Servo motor sensor | - NodeManager provides useful built-in features which can be disabled if you need to save some storage for your code. To enable/disable a buil-in feature: @@ -262,6 +263,7 @@ FEATURE_SD | OFF | allow for reading from and writing to SD //#define USE_CHIRP //#define USE_HD44780 //#define USE_TTP +//#define USE_SERVO /*********************************** * NodeManager advanced settings @@ -343,6 +345,7 @@ NodeManager node; //SensorChirp chirp(node); //DisplayHD44780 hd44780(node); //SensorTTP ttp(node); +//SensorServo servo(node,6); /*********************************** * Main Sketch diff --git a/NodeManagerLibrary.h b/NodeManagerLibrary.h index 812d4f65..e5c18a40 100644 --- a/NodeManagerLibrary.h +++ b/NodeManagerLibrary.h @@ -172,6 +172,9 @@ #include #include #endif +#ifdef USE_SERVO + #include +#endif // include third party libraries for enabled features #ifdef MY_GATEWAY_SERIAL @@ -1607,6 +1610,26 @@ class SensorTTP: public Sensor { }; #endif +/* + * Servo motor sensor + */ +#ifdef USE_SERVO +class SensorServo: public Sensor { + public: + SensorServo(NodeManager& node_manager, int pin, int child_id = -255); + // set the servo to the given percentage + void setPercentage(int value); + // define what to do at each stage of the sketch + void onSetup(); + void onLoop(Child* child); + void onReceive(MyMessage* message); + protected: + Servo _servo; + int _value; +}; +#endif + + /*************************************** NodeManager: manages all the aspects of the node */ diff --git a/NodeManagerLibrary.ino b/NodeManagerLibrary.ino index efe2fa5f..833402b1 100644 --- a/NodeManagerLibrary.ino +++ b/NodeManagerLibrary.ino @@ -3709,6 +3709,54 @@ int SensorTTP::_fetchData() { } #endif +/* + * SensorServo + */ +#ifdef USE_SERVO +// constructor +SensorServo::SensorServo(NodeManager& node_manager, int pin, int child_id): Sensor(node_manager, pin) { + _name = "SERVO"; + children.allocateBlocks(1); + new ChildInt(this, _node->getAvailableChildId(child_id), S_DIMMER, V_PERCENTAGE ,_name); +} + +// what to do during setup +void SensorServo::onSetup() { + _servo.attach(_pin); +} + +// what to do during loop +void SensorServo::onLoop(Child *child) { +} + +// what to do as the main task when receiving a message +void SensorServo::onReceive(MyMessage* message) { + Child* child = getChild(message->sensor); + if (child == nullptr) return; + if (message->getCommand() == C_SET) setPercentage(message->getInt()); + if (message->getCommand() == C_REQ) ((ChildInt*)child)->setValueInt(_value); +} + +// set the servo to the given percentage +void SensorServo::setPercentage(int value) { + _value = value; + // set the servo to the given value + _servo.write(map(_value,0,100,0,180)); + // set the value so to send it back + Child* child = children.get(1); + if (child == nullptr) return; + ((ChildInt*)child)->setValueInt(_value); + #ifdef NODEMANAGER_DEBUG + Serial.print(_name); + Serial.print(F(" I=")); + Serial.print(child->child_id); + Serial.print(F(" P=")); + Serial.print(_pin); + Serial.print(F(" V=")); + Serial.println(_value); + #endif +} +#endif /* SensorConfiguration diff --git a/README.md b/README.md index 9f9fe437..dc7da38e 100755 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ SensorSI7021 | 2 | USE_SI7021 | SI7021 sensor, return tempera SensorChirp | 3 | USE_CHIRP | Chirp soil moisture sensor (includes temperature and light sensors) | https://github.com/Apollon77/I2CSoilMoistureSensor DisplayHD44780 | 1 | USE_HD44780 | Supports most Hitachi HD44780 based LCDs, by default displays values of all sensors and children | https://github.com/cyberang3l/NewLiquidCrystal SensorTTP | 1 | USE_TTP | TTP226/TTP229 Touch control sensor | - +SensorServo | 1 | USE_SERVO | Control a generic Servo motor sensor | - ### Advanced features @@ -650,7 +651,6 @@ Each sensor class exposes additional methods. void setBacklight(uint8_t value); ~~~ - * SensorTTP ~~~c // set the passcode length. Passcode will be sent to the controller only after this number of digits have been pressed (default: 4) @@ -665,6 +665,12 @@ Each sensor class exposes additional methods. void setRstPin(int value); ~~~ +* SensorServo +~~~c + // set the servo to the given percentage + void setPercentage(int value); +~~~ + ### Remote API If SensorConfiguration is added to NodeManager, the API can be also called remotely. SensorConfiguration exposes child id 200 that can be used to interact with the service by sending `V_CUSTOM` type of messages and commands within the payload. For each `REQ` message, the node will respond with a `SET` message if successful.