Skip to content
Snippets Groups Projects
Commit 00ae05a8 authored by user2684's avatar user2684 Committed by GitHub
Browse files

Optimized functions between Sensor and NodeManager classes (#140)

parent f4edca30
No related branches found
No related tags found
No related merge requests found
......@@ -289,12 +289,6 @@ int Sensor::getType() {
void Sensor::setDescription(char* value) {
_description = value;
}
void Sensor::setAck(bool value) {
_ack = value;
}
void Sensor::setRetries(int value) {
_retries = value;
}
void Sensor::setSamples(int value) {
_samples = value;
}
......@@ -336,12 +330,6 @@ void Sensor::setFloatPrecision(int value) {
_powerManager.powerOff();
}
#endif
void Sensor::setSleepBetweenSend(int value) {
_sleep_between_send = value;
}
void Sensor::setInterruptPin(int value) {
_interrupt_pin = value;
}
int Sensor::getInterruptPin() {
return _interrupt_pin;
}
......@@ -373,7 +361,7 @@ void Sensor::presentation() {
Serial.print(F(" T="));
Serial.println(_presentation);
#endif
present(_child_id, _presentation,_description,_ack);
present(_child_id, _presentation,_description,_node_manager->getAck());
}
// call the sensor-specific implementation of before
......@@ -511,9 +499,9 @@ void Sensor::process(Request & request) {
// send a message to the network
void Sensor::_send(MyMessage & message) {
// send the message, multiple times if requested
for (int i = 0; i < _retries; i++) {
for (int i = 0; i < _node_manager->getRetries(); i++) {
// if configured, sleep beetween each send
if (_sleep_between_send > 0) sleep(_sleep_between_send);
if (_node_manager->getSleepBetweenSend() > 0) sleep(_node_manager->getSleepBetweenSend());
#if DEBUG == 1
Serial.print(F("SEND D="));
Serial.print(message.destination);
......@@ -530,7 +518,7 @@ void Sensor::_send(MyMessage & message) {
Serial.print(F(" F="));
Serial.println(message.getFloat());
#endif
send(message,_ack);
send(message,_node_manager->getAck());
}
}
......@@ -1371,9 +1359,6 @@ SensorSwitch::SensorSwitch(NodeManager* node_manager, int child_id, int pin): Se
void SensorSwitch::setMode(int value) {
_mode = value;
}
int SensorSwitch::getMode() {
return _mode;
}
void SensorSwitch::setDebounce(int value) {
_debounce = value;
}
......@@ -1383,15 +1368,15 @@ void SensorSwitch::setTriggerTime(int value) {
void SensorSwitch::setInitial(int value) {
_initial = value;
}
int SensorSwitch::getInitial() {
return _initial;
}
// what to do during before
void SensorSwitch::onBefore() {
// initialize the value
if (_mode == RISING) _value_int = LOW;
else if (_mode == FALLING) _value_int = HIGH;
// set the interrupt pin so it will be called only when waking up from that interrupt
_interrupt_pin = _pin;
_node_manager->setInterrupt(_pin,_mode,_initial);
}
// what to do during setup
......@@ -2321,6 +2306,9 @@ NodeManager::NodeManager() {
void NodeManager::setRetries(int value) {
_retries = value;
}
int NodeManager::getRetries() {
return _retries;
}
#if BATTERY_MANAGER == 1
void NodeManager::setBatteryMin(float value) {
_battery_min = value;
......@@ -2403,9 +2391,15 @@ void NodeManager::setInterrupt(int pin, int mode, int pull) {
void NodeManager::setSleepBetweenSend(int value) {
_sleep_between_send = value;
}
int NodeManager::getSleepBetweenSend() {
return _sleep_between_send;
}
void NodeManager::setAck(bool value) {
_ack = value;
}
bool NodeManager::getAck() {
return _ack;
}
void NodeManager::setGetControllerConfig(bool value) {
_get_controller_config = value;
}
......@@ -2485,15 +2479,9 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {
// ensure an interrupt pin is provided
if (pin != INTERRUPT_PIN_1 && pin != INTERRUPT_PIN_2) return -1;
// register the sensor
int index = 0;
if (sensor_type == SENSOR_SWITCH) index = registerSensor(new SensorSwitch(this,child_id, pin));
else if (sensor_type == SENSOR_DOOR) index = registerSensor(new SensorDoor(this,child_id, pin));
else if (sensor_type == SENSOR_MOTION) index = registerSensor(new SensorMotion(this,child_id, pin));
// set an interrupt on the pin and set the initial value
SensorSwitch* sensor = (SensorSwitch*)getSensor(index);
sensor->setInterruptPin(pin);
setInterrupt(pin,sensor->getMode(),sensor->getInitial());
return index;
if (sensor_type == SENSOR_SWITCH) return registerSensor(new SensorSwitch(this,child_id, pin));
else if (sensor_type == SENSOR_DOOR) return registerSensor(new SensorDoor(this,child_id, pin));
else if (sensor_type == SENSOR_MOTION) return registerSensor(new SensorMotion(this,child_id, pin));
}
#endif
#if MODULE_DS18B20 == 1
......
......@@ -425,10 +425,6 @@ class Sensor {
int getType();
// [4] description of the sensor (default: '')
void setDescription(char *value);
// set this to true if you want destination node to send ack back to this node (default: false)
void setAck(bool value);
// when queried, send the message multiple times (default: 1)
void setRetries(int value);
// [5] For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
void setSamples(int value);
// [6] If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
......@@ -445,11 +441,6 @@ class Sensor {
int getValueType();
// [11] for float values, set the float precision (default: 2)
void setFloatPrecision(int value);
// optionally sleep interval in milliseconds before sending each message to the radio network (default: 0)
void setSleepBetweenSend(int value);
// set the interrupt pin the sensor is attached to so its loop() will be executed only upon that interrupt (default: -1)
void setInterruptPin(int value);
int getInterruptPin();
#if POWER_MANAGER == 1
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
......@@ -470,6 +461,8 @@ class Sensor {
void setReportIntervalMinutes(int value);
// process a remote request
void process(Request & request);
// return the pin the interrupt is attached to
int getInterruptPin();
// define what to do at each stage of the sketch
virtual void before();
virtual void presentation();
......@@ -486,14 +479,11 @@ class Sensor {
MyMessage _msg;
MyMessage _msg_service;
NodeManager* _node_manager;
int _sleep_between_send = 0;
int _pin = -1;
int _child_id;
int _presentation = S_CUSTOM;
int _type = V_CUSTOM;
char* _description = "";
bool _ack = false;
int _retries = 1;
int _samples = 1;
int _samples_interval = 0;
bool _track_last_value = false;
......@@ -809,14 +799,12 @@ class SensorSwitch: public Sensor {
SensorSwitch(NodeManager* node_manager, int child_id, int pin);
// [101] set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
void setMode(int value);
int getMode();
// [102] milliseconds to wait before reading the input (default: 0)
void setDebounce(int value);
// [103] time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
void setTriggerTime(int value);
// [104] Set initial value on the interrupt pin (default: HIGH)
void setInitial(int value);
int getInitial();
// define what to do at each stage of the sketch
void onBefore();
void onSetup();
......@@ -1126,6 +1114,7 @@ class NodeManager {
NodeManager();
// [10] send the same service message multiple times (default: 1)
void setRetries(int value);
int getRetries();
#if BATTERY_MANAGER == 1
// [11] the expected vcc when the batter is fully discharged, used to calculate the percentage (default: 2.7)
void setBatteryMin(float value);
......@@ -1164,6 +1153,7 @@ class NodeManager {
void setInterrupt(int pin, int mode, int pull = -1);
// [20] optionally sleep interval in milliseconds before sending each message to the radio network (default: 0)
void setSleepBetweenSend(int value);
int getSleepBetweenSend();
// register a built-in sensor
int registerSensor(int sensor_type, int pin = -1, int child_id = -1);
// register a custom sensor
......@@ -1187,6 +1177,7 @@ class NodeManager {
#endif
// [21] set this to true if you want destination node to send ack back to this node (default: false)
void setAck(bool value);
bool getAck();
// request and return the current timestamp from the controller
long getTimestamp();
// Request the controller's configuration on startup (default: true)
......
......@@ -199,6 +199,7 @@ Node Manager comes with a reasonable default configuration. If you want/need to
~~~c
// [10] send the same service message multiple times (default: 1)
void setRetries(int value);
int getRetries();
#if BATTERY_MANAGER == 1
// [11] the expected vcc when the batter is fully discharged, used to calculate the percentage (default: 2.7)
void setBatteryMin(float value);
......@@ -216,6 +217,8 @@ Node Manager comes with a reasonable default configuration. If you want/need to
void setBatteryVoltsPerBit(float value);
// [18] If true, wake up by an interrupt counts as a valid cycle for battery reports otherwise only uninterrupted sleep cycles would contribute (default: true)
void setBatteryReportWithInterrupt(bool value);
// [2] Send a battery level report to the controller
void batteryReport();
#endif
// [3] define the way the node should behave. It can be (0) IDLE (stay awake withtout executing each sensors' loop), (1) SLEEP (go to sleep for the configured interval), (2) WAIT (wait for the configured interval), (3) ALWAYS_ON (stay awake and execute each sensors' loop)
void setSleepMode(int value);
......@@ -235,6 +238,7 @@ Node Manager comes with a reasonable default configuration. If you want/need to
void setInterrupt(int pin, int mode, int pull = -1);
// [20] optionally sleep interval in milliseconds before sending each message to the radio network (default: 0)
void setSleepBetweenSend(int value);
int getSleepBetweenSend();
// register a built-in sensor
int registerSensor(int sensor_type, int pin = -1, int child_id = -1);
// register a custom sensor
......@@ -258,6 +262,7 @@ Node Manager comes with a reasonable default configuration. If you want/need to
#endif
// [21] set this to true if you want destination node to send ack back to this node (default: false)
void setAck(bool value);
bool getAck();
// request and return the current timestamp from the controller
long getTimestamp();
// Request the controller's configuration on startup (default: true)
......@@ -271,8 +276,6 @@ Node Manager comes with a reasonable default configuration. If you want/need to
bool isSleepingNode();
// [1] Send a hello message back to the controller
void hello();
// [2] Send a battery level report to the controller
void batteryReport();
// [6] reboot the board
void reboot();
// [8] send NodeManager's the version back to the controller
......@@ -282,10 +285,10 @@ Node Manager comes with a reasonable default configuration. If you want/need to
// [9] wake up the board
void wakeup();
// process a remote request
void process(const char * message);
void process(Request & request);
// return the value stored at the requested index from the EEPROM
int loadFromMemory(int index);
// save the given index of the EEPROM the provided value
// [27] save the given index of the EEPROM the provided value
void saveToMemory(int index, int value);
~~~
......@@ -388,10 +391,6 @@ The following methods are available for all the sensors:
int getType();
// [4] description of the sensor (default: '')
void setDescription(char *value);
// set this to true if you want destination node to send ack back to this node (default: false)
void setAck(bool value);
// when queried, send the message multiple times (default: 1)
void setRetries(int value);
// [5] For some sensors, the measurement can be queried multiple times and an average is returned (default: 1)
void setSamples(int value);
// [6] If more then one sample has to be taken, set the interval in milliseconds between measurements (default: 0)
......@@ -408,11 +407,6 @@ The following methods are available for all the sensors:
int getValueType();
// [11] for float values, set the float precision (default: 2)
void setFloatPrecision(int value);
// optionally sleep interval in milliseconds before sending each message to the radio network (default: 0)
void setSleepBetweenSend(int value);
// set the interrupt pin the sensor is attached to so its loop() will be executed only upon that interrupt (default: -1)
void setInterruptPin(int value);
int getInterruptPin();
#if POWER_MANAGER == 1
// to save battery the sensor can be optionally connected to two pins which will act as vcc and ground and activated on demand
void setPowerPins(int ground_pin, int vcc_pin, int wait_time = 50);
......@@ -433,6 +427,8 @@ The following methods are available for all the sensors:
void setReportIntervalMinutes(int value);
// process a remote request
void process(Request & request);
// return the pin the interrupt is attached to
int getInterruptPin();
~~~
#### Sensor's specific configuration
......@@ -533,17 +529,15 @@ Each sensor class can expose additional methods.
~~~c
// [101] set the interrupt mode. Can be CHANGE, RISING, FALLING (default: CHANGE)
void setMode(int value);
int getMode();
// [102] milliseconds to wait before reading the input (default: 0)
void setDebounce(int value);
// [103] time to wait in milliseconds after a change is detected to allow the signal to be restored to its normal value (default: 0)
void setTriggerTime(int value);
// [104] Set initial value on the interrupt pin (default: HIGH)
void setInitial(int value);
int getInitial();
~~~
* SensorDs18b20**
* SensorDs18b20
~~~c
// returns the sensor's resolution in bits
int getResolution();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment