Implemented on / off button
Implemented add / subtract buttons in counter
This commit is contained in:
parent
b54bb32e81
commit
12d45b8760
@ -1,210 +1,59 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <ssd1306xled.h>
|
||||
|
||||
#include "shared.h"
|
||||
#include "power.h"
|
||||
#include "buttons.h"
|
||||
#include "screen/counter.h"
|
||||
|
||||
|
||||
#define VccOffTreshold 3000
|
||||
#define VccOnTreshold 3100
|
||||
|
||||
|
||||
|
||||
// Forward declarations
|
||||
void checkPower();
|
||||
void waitForInput();
|
||||
|
||||
uint8_t handleGlobalInput();
|
||||
void handleCurrentScreen();
|
||||
|
||||
uint16_t readVCC();
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// Delay is required on power-on for the SSD1306 to initialize,
|
||||
_delay_ms(40);
|
||||
ssd1306_init();
|
||||
|
||||
buttons_init();
|
||||
|
||||
while (1)
|
||||
{
|
||||
checkPower();
|
||||
if (!handleGlobalInput())
|
||||
{
|
||||
if (powerState == On)
|
||||
{
|
||||
handleCurrentScreen();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void waitForInput()
|
||||
uint8_t handleGlobalInput()
|
||||
{
|
||||
// TODO go to sleep until a button is pressed
|
||||
_delay_ms(1000);
|
||||
if (button_is_released_short(&buttonOption))
|
||||
{
|
||||
// Toggle power
|
||||
if (powerState == On)
|
||||
setPowerState(ManualOff);
|
||||
else if (powerState == ManualOff)
|
||||
setPowerState(On);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
handleCounterScreen();
|
||||
}
|
147
Source/src/power.c
Normal file
147
Source/src/power.c
Normal file
@ -0,0 +1,147 @@
|
||||
#include "power.h"
|
||||
#include <stdint.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <ssd1306xled.h>
|
||||
|
||||
|
||||
|
||||
#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
|
||||
}
|
19
Source/src/power.h
Normal file
19
Source/src/power.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef __Power
|
||||
#define __Power
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { On, ManualOff, BatteryLow } PowerState;
|
||||
|
||||
|
||||
extern PowerState powerState;
|
||||
extern uint16_t vcc;
|
||||
|
||||
|
||||
extern void checkPower();
|
||||
extern void setPowerState(PowerState newState);
|
||||
|
||||
extern uint8_t getScreenInvalidated();
|
||||
extern void setScreenInvalidated();
|
||||
|
||||
#endif
|
@ -1,11 +1,37 @@
|
||||
#include "counter.h"
|
||||
#include <stdint.h>
|
||||
#include <ssd1306xled.h>
|
||||
#include "../buttons.h"
|
||||
|
||||
|
||||
uint16_t lastDrawnCounter = 9999;
|
||||
uint16_t counter = 0;
|
||||
|
||||
|
||||
// Forward declarations
|
||||
void drawCounter(uint16_t value);
|
||||
void drawDigit(uint8_t* column, uint8_t digit);
|
||||
void blankDigitColumn(uint8_t* column, uint8_t width);
|
||||
|
||||
|
||||
void handleCounterScreen()
|
||||
{
|
||||
if (button_is_pressed_or_repeated(&buttonUp))
|
||||
{
|
||||
if (counter < 999)
|
||||
counter++;
|
||||
}
|
||||
else if (button_is_pressed_or_repeated(&buttonDown))
|
||||
{
|
||||
if (counter > 0)
|
||||
counter--;
|
||||
}
|
||||
|
||||
if (counter != lastDrawnCounter || getScreenInvalidated())
|
||||
{
|
||||
drawCounter(counter);
|
||||
lastDrawnCounter = counter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
#define __ScreenCounter
|
||||
|
||||
#include <stdint.h>
|
||||
#include "shared.h"
|
||||
#include "digits.h"
|
||||
|
||||
|
||||
@ -31,9 +30,6 @@
|
||||
|
||||
|
||||
extern void handleCounterScreen();
|
||||
extern void drawCounter(uint16_t value);
|
||||
extern void drawDigit(uint8_t* column, uint8_t digit);
|
||||
extern void blankDigitColumn(uint8_t* column, uint8_t width);
|
||||
|
||||
|
||||
#endif
|
@ -1,5 +0,0 @@
|
||||
#include "shared.h"
|
||||
|
||||
PowerState powerState = BatteryLow;
|
||||
uint16_t vcc = 0;
|
||||
uint16_t counter = 0;
|
@ -1,13 +0,0 @@
|
||||
#ifndef __Shared
|
||||
#define __Shared
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { On, ManualOff, BatteryLow } PowerState;
|
||||
|
||||
|
||||
extern PowerState powerState;
|
||||
extern uint16_t vcc;
|
||||
extern uint16_t counter;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user