Skip to content

Commit

Permalink
Release v1.5.1
Browse files Browse the repository at this point in the history
* Decreased the number of items of the _sensors array #117
* Fixed gateway crashing on node check-in
  • Loading branch information
user2684 authored Jul 2, 2017
1 parent 00946ad commit b108178
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
17 changes: 11 additions & 6 deletions NodeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2093,6 +2093,7 @@ int NodeManager::registerSensor(int sensor_type, int pin, int child_id) {

// attach a built-in or custom sensor to this manager
int NodeManager::registerSensor(Sensor* sensor) {
if (sensor->getChildId() > MAX_SENSORS) return;
#if DEBUG == 1
Serial.print(F("REG I="));
Serial.print(sensor->getChildId());
Expand All @@ -2115,12 +2116,14 @@ int NodeManager::registerSensor(Sensor* sensor) {

// un-register a sensor to this manager
void NodeManager::unRegisterSensor(int sensor_index) {
if (sensor_index > MAX_SENSORS) return;
// unlink the pointer to this sensor
_sensors[sensor_index] == 0;
}

// return a sensor given its index
Sensor* NodeManager::get(int child_id) {
if (child_id > MAX_SENSORS) return 0;
// return a pointer to the sensor from the given child_id
return _sensors[child_id];
}
Expand All @@ -2130,6 +2133,7 @@ Sensor* NodeManager::getSensor(int child_id) {

// assign a different child id to a sensor'
bool NodeManager::renameSensor(int old_child_id, int new_child_id) {
if (old_child_id > MAX_SENSORS || new_child_id > MAX_SENSORS) return;
// ensure the old id exists and the new is available
if (_sensors[old_child_id] == 0 || _sensors[new_child_id] != 0) return false;
// assign the sensor to new id
Expand Down Expand Up @@ -2193,7 +2197,7 @@ void NodeManager::before() {
if (! _battery_internal_vcc && _battery_pin > -1) analogReference(INTERNAL);
#endif
// setup individual sensors
for (int i = 0; i < 255; i++) {
for (int i = 1; i <= MAX_SENSORS; i++) {
if (_sensors[i] == 0) continue;
// call each sensor's setup()
_sensors[i]->before();
Expand All @@ -2217,7 +2221,7 @@ void NodeManager::presentation() {
_process("BATTERY");
#endif
// present each sensor
for (int i = 0; i < 255; i++) {
for (int i = 1; i <= MAX_SENSORS; i++) {
if (_sensors[i] == 0) continue;
// call each sensor's presentation()
if (_sleep_between_send > 0) sleep(_sleep_between_send);
Expand All @@ -2242,7 +2246,7 @@ void NodeManager::setup() {
_send(_msg.set("STARTED"));
#endif
// run setup for all the registered sensors
for (int i = 0; i < 255; i++) {
for (int i = 1; i <= MAX_SENSORS; i++) {
if (_sensors[i] == 0) continue;
// call each sensor's setup()
_sensors[i]->setup();
Expand All @@ -2261,7 +2265,7 @@ void NodeManager::loop() {
if (_auto_power_pins) powerOn();
#endif
// run loop for all the registered sensors
for (int i = 0; i < 255; i++) {
for (int i = 1; i <= MAX_SENSORS; i++) {
// skip not configured sensors
if (_sensors[i] == 0) continue;
// if waking up from an interrupt skip all the sensor without that interrupt configured
Expand Down Expand Up @@ -2296,7 +2300,7 @@ void NodeManager::receive(const MyMessage &message) {
_process(message.getString());
}
// dispatch the message to the registered sensor
else if (_sensors[message.sensor] != 0) {
else if (message.sensor <= MAX_SENSORS && _sensors[message.sensor] != 0) {
#if POWER_MANAGER == 1
// turn on the pin powering all the sensors
if (_auto_power_pins) powerOn();
Expand Down Expand Up @@ -2609,12 +2613,13 @@ void NodeManager::_present(int child_id, int type) {

// return the next available child_id
int NodeManager::_getAvailableChildId() {
for (int i = 1; i < 255; i++) {
for (int i = 1; i <= MAX_SENSORS; i++) {
if (i == CONFIGURATION_CHILD_ID) continue;
if (i == BATTERY_CHILD_ID) continue;
// empty place, return it
if (_sensors[i] == 0) return i;
}
return MAX_SENSORS;
}

// guess the initial value of a digital output based on the configured interrupt mode
Expand Down
37 changes: 20 additions & 17 deletions NodeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Arduino.h>

// define NodeManager version
#define VERSION "1.5"
#define VERSION "1.5.1"

/***********************************
Constants
Expand Down Expand Up @@ -51,6 +51,10 @@
/***********************************
Default configuration settings
*/
// if enabled, enable debug messages on serial port
#ifndef DEBUG
#define DEBUG 1
#endif

// if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
#ifndef POWER_MANAGER
Expand All @@ -69,29 +73,15 @@
#define PERSIST 0
#endif

// if enabled, enable debug messages on serial port
#ifndef DEBUG
#define DEBUG 1
#endif

// if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle
#ifndef SERVICE_MESSAGES
#define SERVICE_MESSAGES 1
#define SERVICE_MESSAGES 0
#endif
// if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
#ifndef BATTERY_SENSOR
#define BATTERY_SENSOR 1
#endif

// the child id used to allow remote configuration
#ifndef CONFIGURATION_CHILD_ID
#define CONFIGURATION_CHILD_ID 200
#endif
// the child id used to report the battery voltage to the controller
#ifndef BATTERY_CHILD_ID
#define BATTERY_CHILD_ID 201
#endif

// Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR, SENSOR_MQ, SENSOR_ACS712
#ifndef MODULE_ANALOG_INPUT
#define MODULE_ANALOG_INPUT 0
Expand Down Expand Up @@ -149,6 +139,19 @@
#define MODULE_MCP9808 0
#endif

// the child id used to allow remote configuration
#ifndef CONFIGURATION_CHILD_ID
#define CONFIGURATION_CHILD_ID 200
#endif
// the child id used to report the battery voltage to the controller
#ifndef BATTERY_CHILD_ID
#define BATTERY_CHILD_ID 201
#endif
// define the maximum number of sensors that can be managed
#ifndef MAX_SENSORS
#define MAX_SENSORS 10
#endif

/***********************************
Sensors types
*/
Expand Down Expand Up @@ -1063,7 +1066,7 @@ class NodeManager {
int _interrupt_2_pull = -1;
int _last_interrupt_pin = -1;
long _timestamp = -1;
Sensor* _sensors[255] = {0};
Sensor* _sensors[MAX_SENSORS+1] = {0};
bool _ack = false;
void _process(const char * message);
void _sleep();
Expand Down

0 comments on commit b108178

Please sign in to comment.