97 lines
2.5 KiB
C++
97 lines
2.5 KiB
C++
/*
|
|
* NodeManager
|
|
*/
|
|
|
|
#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
|
|
*/
|
|
|
|
// set the vcc and ground pin the sensor is connected to
|
|
void PowerManager::setPowerPins(int8_t vcc_pin, int8_t ground_pin, int wait_time) {
|
|
#if DEBUG == 1
|
|
Serial.print(F("PWR G="));
|
|
Serial.print(ground_pin);
|
|
Serial.print(F(" V="));
|
|
Serial.println(vcc_pin);
|
|
#endif
|
|
_vcc_pin = vcc_pin;
|
|
// configure the vcc pin as output and initialize to high (power on)
|
|
pinMode(_vcc_pin, OUTPUT);
|
|
digitalWrite(_vcc_pin, HIGH);
|
|
_ground_pin = ground_pin;
|
|
if (_ground_pin >= 0) {
|
|
// configure the ground pin as output and initialize to low
|
|
pinMode(_ground_pin, OUTPUT);
|
|
digitalWrite(_ground_pin, LOW);
|
|
}
|
|
_wait = wait_time;
|
|
}
|
|
|
|
// return true if power pins have been configured
|
|
bool PowerManager::isConfigured() {
|
|
if (_vcc_pin != -1) return true;
|
|
return false;
|
|
}
|
|
|
|
bool PowerManager::isPowerOn() {
|
|
return digitalRead(_vcc_pin);
|
|
}
|
|
|
|
// turn on the sensor by activating its power pins
|
|
void PowerManager::powerOn() {
|
|
if (! isConfigured()) return;
|
|
#if DEBUG == 1
|
|
Serial.print(F("ON P="));
|
|
Serial.println(_vcc_pin);
|
|
#endif
|
|
// power on the sensor by turning high the vcc pin
|
|
digitalWrite(_vcc_pin, HIGH);
|
|
// wait a bit for the device to settle down
|
|
if (_wait > 0) wait(_wait);
|
|
}
|
|
|
|
// turn off the sensor
|
|
void PowerManager::powerOff() {
|
|
if (! isConfigured()) return;
|
|
#if DEBUG == 1
|
|
Serial.print(F("OFF P="));
|
|
Serial.println(_vcc_pin);
|
|
#endif
|
|
// power off the sensor by turning low the vcc pin
|
|
digitalWrite(_vcc_pin, LOW);
|
|
}
|
|
|
|
|