#include "power.h" #include #include #include #include #define VccOffTreshold 3000 #define VccOnTreshold 3100 #define SSD1306CommandOff 0xAE #define SSD1306CommandOn 0xAF PowerState powerState = BatteryLow; uint16_t vcc = 0; uint8_t screenInvalidated = 0; // Forward declarations uint16_t readVCC(); void checkPower() { vcc = readVCC(); switch (powerState) { case On: // Turn display off below 3v. It holds up surprisingly well, but at around // 1.5v it does corrupt the screen and requires reinitialization when the // voltage is turned back up. // // ...although by then the battery would be damaged, but still, turning off at // 3v means we're still in the safe range when we go into battery saving mode // and the reinitialization afterwards prevents any issues. if (vcc < VccOffTreshold) { setPowerState(BatteryLow); // TODO go into a sleep cycle until the battery is recharged _delay_ms(10); } break; case BatteryLow: if (vcc > VccOnTreshold) setPowerState(On); else { // TODO continue sleep cycle _delay_ms(10); } break; case ManualOff: // TODO go into sleep mode _delay_ms(10); break; } } void setPowerState(PowerState newState) { if (newState == powerState) return; switch (newState) { case On: ssd1306_send_command(SSD1306CommandOn); break; case BatteryLow: case ManualOff: if (powerState == On) ssd1306_send_command(SSD1306CommandOff); break; } powerState = newState; } uint8_t getScreenInvalidated() { if (screenInvalidated) { screenInvalidated = 0; return 1; } return 0; } void setScreenInvalidated() { screenInvalidated = 1; } // Source: http://21stdigitalhome.blogspot.nl/2014/10/trinket-attiny85-internal-temperature.html // // I've tried many versions and none seemed to work with my ATTiny85-20SU's. // For example: // https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ // https://github.com/cano64/ArduinoSystemStatus/blob/master/SystemStatus.cpp // http://www.avrfreaks.net/forum/attiny-adc-using-internal-ref-measure-vcc-problem // // The key for me was in: ADMUX = 0x0c | _BV(REFS2); uint16_t readVCC() { ADCSRA |= _BV(ADEN); // Read 1.1V reference against AVcc // set the reference to Vcc and the measurement to the internal 1.1V reference /* #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 */ ADMUX = 0x0c | _BV(REFS2); _delay_ms(100); ADCSRA |= _BV(ADSC); while (bit_is_set(ADCSRA,ADSC)); uint16_t result = ADC; ADCSRA &= ~(_BV(ADEN)); return result == 0 ? 0 : 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 }