Skip to content

Commit

Permalink
Add support for generic servo motor (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
user2684 committed Feb 25, 2018
1 parent a64dcda commit bc67e96
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NodeManager.ino
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -343,6 +345,7 @@ NodeManager node;
//SensorChirp chirp(node);
//DisplayHD44780 hd44780(node);
//SensorTTP ttp(node);
//SensorServo servo(node,6);

/***********************************
* Main Sketch
Expand Down
23 changes: 23 additions & 0 deletions NodeManagerLibrary.h
Expand Up @@ -172,6 +172,9 @@
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#endif
#ifdef USE_SERVO
#include <Servo.h>
#endif

// include third party libraries for enabled features
#ifdef MY_GATEWAY_SERIAL
Expand Down Expand Up @@ -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
*/
Expand Down
48 changes: 48 additions & 0 deletions NodeManagerLibrary.ino
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit bc67e96

Please sign in to comment.