From c847c8348b7992baa59cc7461c14f87453023083 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 20 Mar 2017 23:12:31 -0400 Subject: [PATCH] add checkBatteryLow() function --- src/Arduboy2.cpp | 24 +++++++++++++++++++++++- src/Arduboy2.h | 15 +++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Arduboy2.cpp b/src/Arduboy2.cpp index e3666bb..8ab771d 100644 --- a/src/Arduboy2.cpp +++ b/src/Arduboy2.cpp @@ -130,6 +130,27 @@ void Arduboy2Base::bootLogo() // Virtual function overridden by derived class void Arduboy2Base::bootLogoExtra() { } +bool Arduboy2Base::checkBatteryLow() { + bool batteryLow; + uint16_t voltage; + + voltage = rawADC(ADC_VOLTAGE); + power_adc_disable(); // turn the ADC back off + + // some voltage values for reference + // 4262mv - fully charged (plugged in) + // 41??mv - fully charged + voltage = 1125300L / voltage; + batteryLow = voltage < 3700; + + if (batteryLow) { + TXLED1; + } else { + TXLED0; + } + return batteryLow; +} + /* Frame management */ void Arduboy2Base::setFrameRate(uint8_t rate) @@ -188,13 +209,14 @@ int Arduboy2Base::cpuLoad() void Arduboy2Base::initRandomSeed() { - power_adc_enable(); // ADC on randomSeed(~rawADC(ADC_TEMP) * ~rawADC(ADC_VOLTAGE) * ~micros() + micros()); power_adc_disable(); // ADC off } uint16_t Arduboy2Base::rawADC(uint8_t adc_bits) { + power_adc_enable(); // ADC on + ADMUX = adc_bits; // we also need MUX5 for temperature check if (adc_bits == ADC_TEMP) { diff --git a/src/Arduboy2.h b/src/Arduboy2.h index d2e90fc..f9eae9c 100644 --- a/src/Arduboy2.h +++ b/src/Arduboy2.h @@ -253,6 +253,17 @@ class Arduboy2Base : public Arduboy2Core // information after the logo stops scrolling down. virtual void bootLogoExtra(); + /** \brief + * Checks the battery voltage level. + * + * \return true if the battery is running low + * + * \details + * This is called periodically by the frame management loop and will + * light the TX LED (yellow) if the battery is detected to be low. + */ + static bool checkBatteryLow(); + /** \brief * Clear the display buffer. * @@ -695,7 +706,7 @@ class Arduboy2Base : public Arduboy2Core int cpuLoad(); // Useful for getting raw approximate voltage values. - uint16_t rawADC(uint8_t adc_bits); + static uint16_t rawADC(uint8_t adc_bits); /** \brief * Test if the specified buttons are pressed. @@ -1074,7 +1085,7 @@ class Arduboy2 : public Print, public Arduboy2Base * * \details * This function is called by the `bootLogo()` function. - * + * * If a unit name has been saved in system EEPROM, it will be displayed at * the bottom of the screen. This function pauses for a short time to allow * the name to be seen.