GameCounter/Source/src/main.c

210 lines
4.6 KiB
C

#include <stdint.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include <ssd1306xled.h>
#include "shared.h"
#include "buttons.h"
#include "screen/counter.h"
#define VccOffTreshold 3000
#define VccOnTreshold 3100
// Forward declarations
void checkPower();
void waitForInput();
void handleCurrentScreen();
uint16_t readVCC();
int main()
{
buttons_init();
while (1)
{
checkPower();
if (powerState == On)
handleCurrentScreen();
}
return 0;
}
void waitForInput()
{
// TODO go to sleep until a button is pressed
_delay_ms(1000);
}
uint8_t lastButton = 0;
void handleCurrentScreen()
{
ssd1306_setpos(0, 0);
if (bit_is_set(PINB, BUTTON_DOWN))
{
ssd1306_string("released");
}
else
{
ssd1306_string("pressed ");
}
char value[9];
value[0] = (buttonDown.flags & BSFLastReported) ? '1' : '0';
value[1] = (buttonDown.flags & BSFPressed) ? '1' : '0';
value[2] = (buttonDown.flags & BSFRepeated) ? '1' : '0';
value[3] = (buttonDown.flags & BSFReleased) ? '1' : '0';
value[4] = (buttonDown.flags & BSFRepeatEnabled) ? '1' : '0';
value[5] = '\0';
ssd1306_setpos(0, 1);
ssd1306_string(&value[0]);
itoa(buttonDown.debounceTicks, value, 10);
ssd1306_setpos(0, 2);
ssd1306_string(&value[0]);
itoa(buttonDown.repeatTicks, value, 10);
ssd1306_setpos(0, 3);
ssd1306_string(&value[0]);
if (button_is_pressed(&buttonUp))
{
ssd1306_setpos(0, 5);
ssd1306_string("+");
}
else if (button_is_released(&buttonUp))
{
ssd1306_setpos(0, 5);
ssd1306_string(" ");
}
if (button_is_pressed(&buttonDown))
{
ssd1306_setpos(0, 5);
ssd1306_string("-");
}
else if (button_is_released(&buttonDown))
{
ssd1306_setpos(0, 5);
ssd1306_string(" ");
}
if (button_is_pressed(&buttonOption))
{
ssd1306_setpos(0, 5);
ssd1306_string("O");
}
else if (button_is_released(&buttonOption))
{
ssd1306_setpos(0, 5);
ssd1306_string(" ");
}
_delay_ms(10);
//handleCounterScreen();
}
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)
{
ssd1306_clear();
powerState = BatteryLow;
// TODO go into a sleep cycle until the battery is recharged
_delay_ms(100);
}
break;
case BatteryLow:
if (vcc > VccOnTreshold)
{
// Delay is required on power-on for the SSD1306 to initialize,
// to be sure we're simply delaying every time it's reinitialized
_delay_ms(40);
ssd1306_init();
ssd1306_clear();
powerState = On;
}
else
{
// TODO continue sleep cycle
_delay_ms(100);
}
break;
case ManualOff:
// TODO go into sleep mode
_delay_ms(100);
break;
}
}
// 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
}