diff --git a/NodeManager.cpp b/NodeManager.cpp index e73a9518826fd492b63bdc8f950784e6e21891ac..f7a8ed102ade190648fbd328239f4aa5171a7132 100644 --- a/NodeManager.cpp +++ b/NodeManager.cpp @@ -4,36 +4,6 @@ #include "NodeManager.h" -/*************************************** - Global functions -*/ - -// return vcc in V -float getVcc() { - #ifndef MY_GATEWAY_ESP8266 - // Measure Vcc against 1.1V Vref - #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - ADMUX = (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)); - #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) - ADMUX = (_BV(MUX5) | _BV(MUX0)); - #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) - ADMUX = (_BV(MUX3) | _BV(MUX2)); - #else - ADMUX = (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)); - #endif - // Vref settle - wait(70); - // Do conversion - ADCSRA |= _BV(ADSC); - while (bit_is_set(ADCSRA, ADSC)) {}; - // return Vcc in mV - return (float)((1125300UL) / ADC) / 1000; - #else - return (float)0; - #endif -} - - /*************************************** PowerManager */ @@ -46,26 +16,26 @@ void PowerManager::setPowerPins(int ground_pin, int vcc_pin, int wait_time) { Serial.print(F(" V=")); Serial.println(vcc_pin); #endif - // configure the vcc pin as output and initialize to high (power on) - _vcc_pin = vcc_pin; - pinMode(_vcc_pin, OUTPUT); - digitalWrite(_vcc_pin, HIGH); - // configure the ground pin as output and initialize to low - _ground_pin = ground_pin; - pinMode(_ground_pin, OUTPUT); - digitalWrite(_ground_pin, LOW); + if (_ground_pin > 0) { + // configure the ground pin as output and initialize to low + _ground_pin = ground_pin; + pinMode(_ground_pin, OUTPUT); + digitalWrite(_ground_pin, LOW); + } + if (_vcc_pin > 0) { + // configure the vcc pin as output and initialize to high (power on) + _vcc_pin = vcc_pin; + pinMode(_vcc_pin, OUTPUT); + digitalWrite(_vcc_pin, HIGH); + } + // save wait time _wait = wait_time; } -// return true if power pins have been configured -bool PowerManager::isConfigured() { - if (_vcc_pin != -1 && _ground_pin != -1) return true; - return false; -} // turn on the sensor by activating its power pins void PowerManager::powerOn() { - if (! isConfigured()) return; + if (_vcc_pin == -1) return; #if DEBUG == 1 Serial.print(F("ON P=")); Serial.println(_vcc_pin); @@ -78,7 +48,7 @@ void PowerManager::powerOn() { // turn off the sensor void PowerManager::powerOff() { - if (! isConfigured()) return; + if (_vcc_pin == -1) return; #if DEBUG == 1 Serial.print(F("OFF P=")); Serial.println(_vcc_pin); @@ -768,7 +738,7 @@ void SensorML8511::onSetup() { void SensorML8511::onLoop() { // read the voltage int uvLevel = analogRead(_pin); - int refLevel = getVcc()*1024/3.3; + int refLevel = _node_manager->getVcc()*1024/3.3; //Use the 3.3V power pin as a reference to get a very accurate output value from sensor float outputVoltage = 3.3 / refLevel * uvLevel; //Convert the voltage to a UV intensity level @@ -2997,6 +2967,31 @@ void NodeManager::saveToMemory(int index, int value) { saveState(index+EEPROM_USER_START, value); } +// return vcc in V +float NodeManager::getVcc() { + #ifndef MY_GATEWAY_ESP8266 + // Measure Vcc against 1.1V Vref + #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + ADMUX = (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)); + #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + ADMUX = (_BV(MUX5) | _BV(MUX0)); + #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = (_BV(MUX3) | _BV(MUX2)); + #else + ADMUX = (_BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)); + #endif + // Vref settle + wait(70); + // Do conversion + ADCSRA |= _BV(ADSC); + while (bit_is_set(ADCSRA, ADSC)) {}; + // return Vcc in mV + return (float)((1125300UL) / ADC) / 1000; + #else + return (float)0; + #endif +} + // send a message to the network void NodeManager::_send(MyMessage & message) { // send the message, multiple times if requested diff --git a/NodeManager.h b/NodeManager.h index 47f8ec3233188dbe545f4838c5d0e84f1eab4b26..d71114c57abbde08f8154f5cebfd302e971437ec 100644 --- a/NodeManager.h +++ b/NodeManager.h @@ -332,10 +332,6 @@ class PowerManager { void powerOn(); // turns the power pins on void powerOff(); - // returns the Vcc voltge - float getVcc(); - // turns true if power pins are configured - bool isConfigured(); private: int _vcc_pin = -1; int _ground_pin = -1; @@ -1205,6 +1201,8 @@ class NodeManager { int loadFromMemory(int index); // [27] save the given index of the EEPROM the provided value void saveToMemory(int index, int value); + // return vcc in V + float getVcc(); // hook into the main sketch functions void before(); void presentation(); @@ -1221,7 +1219,6 @@ class NodeManager { bool _battery_internal_vcc = true; int _battery_pin = -1; float _battery_volts_per_bit = 0.003363075; - float _getVcc(); #endif #if POWER_MANAGER == 1 // to optionally controller power pins diff --git a/README.md b/README.md index faef3cad279f5de39fe330250e8f0001e813a6f4..65f1d1ea8a87c00b89a001938315d13da51dd641 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,8 @@ Node Manager comes with a reasonable default configuration. If you want/need to int loadFromMemory(int index); // [27] save the given index of the EEPROM the provided value void saveToMemory(int index, int value); + // return vcc in V + float getVcc(); ~~~ For example