code init
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#include <AT24C32.h>
|
||||
|
||||
void AT24C32::begin()
|
||||
{
|
||||
Wire.begin();
|
||||
};
|
||||
|
||||
uint8_t AT24C32::read(int address)
|
||||
{
|
||||
uint8_t result = 0;
|
||||
bool read = true;
|
||||
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
|
||||
if (Wire.endTransmission() == 0)
|
||||
{
|
||||
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
|
||||
Wire.write(address >> 8);
|
||||
Wire.write(address & 0xff);
|
||||
if (Wire.endTransmission() == 0)
|
||||
{
|
||||
Wire.requestFrom(AT24C32_I2C_ADDRESS, 1);
|
||||
while (Wire.available() > 0 && read)
|
||||
{
|
||||
result = Wire.read();
|
||||
read = false;
|
||||
}
|
||||
|
||||
Wire.endTransmission();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void AT24C32::write(int address, uint8_t data)
|
||||
{
|
||||
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
|
||||
if (Wire.endTransmission() == 0)
|
||||
{
|
||||
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
|
||||
Wire.write(address >> 8);
|
||||
Wire.write(address & 0xff);
|
||||
Wire.write(data);
|
||||
Wire.endTransmission();
|
||||
delay(20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef AT24C32_h
|
||||
#define AT24C32_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class AT24C32
|
||||
{
|
||||
public:
|
||||
void begin();
|
||||
uint8_t read(int address);
|
||||
void write(int address, uint8_t data);
|
||||
};
|
||||
|
||||
// I2C address
|
||||
#define AT24C32_I2C_ADDRESS 0x57
|
||||
|
||||
#define BRIGHTNESS_ADDRESS 0x0A00
|
||||
|
||||
#define COLOR_ADDRESS 0x0A01
|
||||
|
||||
#define VIEW_STATE_ADDRESS 0x0A02
|
||||
|
||||
#define TIME_MODE_ADDRESS 0x0A03
|
||||
|
||||
#define TEXT_SIZE_ADDRESS 0x0B00
|
||||
#define TEXT_ADDRESS 0x0B01
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#include "Button.h"
|
||||
|
||||
Button::Button(uint8_t _pin, uint8_t _mode)
|
||||
{
|
||||
pin = _pin;
|
||||
mode = _mode;
|
||||
}
|
||||
|
||||
void Button::begin()
|
||||
{
|
||||
if (mode == LOW)
|
||||
{
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
}
|
||||
else if (mode == HIGH)
|
||||
{
|
||||
pinMode(pin, INPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void Button::loop(ButtonCallbackFunction callback)
|
||||
{
|
||||
if (digitalRead(pin) == mode)
|
||||
{
|
||||
(*callback)();
|
||||
// debounce
|
||||
delay(250);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef Button_h
|
||||
#define Button_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
typedef void (*ButtonCallbackFunction)();
|
||||
|
||||
class Button
|
||||
{
|
||||
public:
|
||||
Button(uint8_t _pin, uint8_t _mode);
|
||||
void begin();
|
||||
void loop(ButtonCallbackFunction callback);
|
||||
|
||||
private:
|
||||
uint8_t mode;
|
||||
uint8_t pin;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,406 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#include <DS3231.h>
|
||||
|
||||
void DS3231::begin()
|
||||
{
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
uint8_t DS3231::getSeconds()
|
||||
{
|
||||
uint8_t seconds;
|
||||
// read minutes
|
||||
seconds = getRegister(DS3231_SECONDS);
|
||||
return bcdtodec(seconds);
|
||||
}
|
||||
|
||||
void DS3231::setSeconds(uint8_t seconds)
|
||||
{
|
||||
// write seconds
|
||||
setRegister(DS3231_SECONDS, dectobcd(seconds));
|
||||
}
|
||||
|
||||
void DS3231::incrementSeconds()
|
||||
{
|
||||
uint8_t seconds = getSeconds();
|
||||
|
||||
if (seconds == 58)
|
||||
{
|
||||
seconds = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
seconds += 2;
|
||||
}
|
||||
|
||||
setSeconds(seconds);
|
||||
}
|
||||
|
||||
void DS3231::decrementSeconds()
|
||||
{
|
||||
uint8_t seconds = getSeconds();
|
||||
|
||||
if (seconds == 1)
|
||||
{
|
||||
seconds = 59;
|
||||
}
|
||||
else
|
||||
{
|
||||
seconds -= 2;
|
||||
}
|
||||
|
||||
setSeconds(seconds);
|
||||
}
|
||||
|
||||
uint8_t DS3231::getMinutes()
|
||||
{
|
||||
uint8_t minutes;
|
||||
// read minutes
|
||||
minutes = getRegister(DS3231_MINUTES);
|
||||
return bcdtodec(minutes);
|
||||
}
|
||||
|
||||
void DS3231::setMinutes(uint8_t minutes)
|
||||
{
|
||||
// write minutes
|
||||
setRegister(DS3231_MINUTES, dectobcd(minutes));
|
||||
}
|
||||
|
||||
void DS3231::incrementMinutes()
|
||||
{
|
||||
uint8_t minutes = getMinutes();
|
||||
|
||||
if (minutes == 59)
|
||||
{
|
||||
minutes = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
minutes++;
|
||||
}
|
||||
|
||||
setMinutes(minutes);
|
||||
}
|
||||
|
||||
void DS3231::decrementMinutes()
|
||||
{
|
||||
uint8_t minutes = getMinutes();
|
||||
|
||||
if (minutes == 0)
|
||||
{
|
||||
minutes = 59;
|
||||
}
|
||||
else
|
||||
{
|
||||
minutes--;
|
||||
}
|
||||
|
||||
setMinutes(minutes);
|
||||
}
|
||||
|
||||
uint8_t DS3231::getHours()
|
||||
{
|
||||
uint8_t hours;
|
||||
// read hours
|
||||
hours = getRegister(DS3231_HOURS);
|
||||
return bcdtodec(hours);
|
||||
}
|
||||
|
||||
void DS3231::setHours(uint8_t hours)
|
||||
{
|
||||
// write hours
|
||||
setRegister(DS3231_HOURS, dectobcd(hours));
|
||||
}
|
||||
|
||||
void DS3231::incrementHours()
|
||||
{
|
||||
uint8_t hours = getHours();
|
||||
|
||||
if (hours == 23)
|
||||
{
|
||||
hours = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
hours++;
|
||||
}
|
||||
|
||||
setHours(hours);
|
||||
}
|
||||
|
||||
void DS3231::decrementHours()
|
||||
{
|
||||
uint8_t hours = getHours();
|
||||
|
||||
if (hours == 0)
|
||||
{
|
||||
hours = 23;
|
||||
}
|
||||
else
|
||||
{
|
||||
hours--;
|
||||
}
|
||||
|
||||
setHours(hours);
|
||||
}
|
||||
|
||||
uint8_t DS3231::getDay()
|
||||
{
|
||||
uint8_t day;
|
||||
// read day
|
||||
day = getRegister(DS3231_DAY);
|
||||
return bcdtodec(day);
|
||||
}
|
||||
|
||||
void DS3231::setDay(uint8_t day)
|
||||
{
|
||||
// write day
|
||||
setRegister(DS3231_DAY, dectobcd(day));
|
||||
}
|
||||
|
||||
uint8_t DS3231::getDate()
|
||||
{
|
||||
uint8_t date;
|
||||
// read date
|
||||
date = getRegister(DS3231_DATE);
|
||||
return bcdtodec(date);
|
||||
}
|
||||
|
||||
void DS3231::setDate(uint8_t date)
|
||||
{
|
||||
// write date
|
||||
setRegister(DS3231_DATE, dectobcd(date));
|
||||
}
|
||||
|
||||
void DS3231::incrementDate()
|
||||
{
|
||||
uint8_t date = getDate();
|
||||
date++;
|
||||
if (date > 31)
|
||||
{
|
||||
date = 1;
|
||||
}
|
||||
setDate(date);
|
||||
}
|
||||
|
||||
void DS3231::decrementDate()
|
||||
{
|
||||
uint8_t date = getDate();
|
||||
date--;
|
||||
if (date < 1)
|
||||
{
|
||||
date = 31;
|
||||
}
|
||||
setDate(date);
|
||||
}
|
||||
|
||||
uint8_t DS3231::getMonth()
|
||||
{
|
||||
uint8_t month;
|
||||
// read month
|
||||
month = getRegister(DS3231_MONTH);
|
||||
|
||||
// read month, ingore century;
|
||||
return bcdtodec(month & 0x1F);
|
||||
}
|
||||
|
||||
void DS3231::setMonth(uint8_t month)
|
||||
{
|
||||
uint8_t century = getCentury();
|
||||
|
||||
month = dectobcd(month);
|
||||
|
||||
if (century == 1)
|
||||
{
|
||||
month = month + 0x80;
|
||||
}
|
||||
|
||||
// write month
|
||||
setRegister(DS3231_MONTH, month);
|
||||
}
|
||||
|
||||
void DS3231::incrementMonth()
|
||||
{
|
||||
uint8_t month = getMonth();
|
||||
month++;
|
||||
if (month > 12)
|
||||
{
|
||||
month = 1;
|
||||
}
|
||||
setMonth(month);
|
||||
}
|
||||
|
||||
void DS3231::decrementMonth()
|
||||
{
|
||||
uint8_t month = getMonth();
|
||||
month--;
|
||||
if (month < 1)
|
||||
{
|
||||
month = 12;
|
||||
}
|
||||
setMonth(month);
|
||||
}
|
||||
|
||||
int16_t DS3231::getYear()
|
||||
{
|
||||
uint8_t century = getCentury();
|
||||
|
||||
int16_t year;
|
||||
|
||||
// read year
|
||||
year = getRegister(DS3231_YEAR);
|
||||
|
||||
year = bcdtodec(year);
|
||||
|
||||
if (century == 1)
|
||||
{
|
||||
year = 2000 + year;
|
||||
}
|
||||
else
|
||||
{
|
||||
year = 1900 + year;
|
||||
}
|
||||
|
||||
return year;
|
||||
}
|
||||
|
||||
void DS3231::setYear(int16_t year)
|
||||
{
|
||||
if (year > 1999)
|
||||
{
|
||||
year = year - 2000;
|
||||
setCentury(0x80);
|
||||
}
|
||||
else
|
||||
{
|
||||
year = year - 1900;
|
||||
setCentury(0);
|
||||
}
|
||||
|
||||
setRegister(DS3231_YEAR, dectobcd(year));
|
||||
}
|
||||
|
||||
void DS3231::incrementYear()
|
||||
{
|
||||
int16_t year = getYear();
|
||||
year++;
|
||||
if (year > 2099)
|
||||
{
|
||||
year = 1900;
|
||||
}
|
||||
setYear(year);
|
||||
}
|
||||
|
||||
void DS3231::decrementYear()
|
||||
{
|
||||
int16_t year = getYear();
|
||||
year--;
|
||||
if (year < 1900)
|
||||
{
|
||||
year = 2099;
|
||||
}
|
||||
setYear(year);
|
||||
}
|
||||
|
||||
uint8_t DS3231::getControlRegister()
|
||||
{
|
||||
return bcdtodec(getRegister(DS3231_CONTROL));
|
||||
}
|
||||
|
||||
void DS3231::setControlRegister(uint8_t value)
|
||||
{
|
||||
setRegister(DS3231_CONTROL, dectobcd(value));
|
||||
}
|
||||
|
||||
uint8_t DS3231::getStatusRegister()
|
||||
{
|
||||
return bcdtodec(getRegister(DS3231_STATUS));
|
||||
}
|
||||
|
||||
void DS3231::setStatusRegister(uint8_t value)
|
||||
{
|
||||
setRegister(DS3231_STATUS, dectobcd(value));
|
||||
}
|
||||
|
||||
uint8_t DS3231::getTemperature()
|
||||
{
|
||||
int8_t msb_temp;
|
||||
uint8_t msb_value = getRegister(DS3231_MSB_TEMP);
|
||||
uint8_t lsb_temp = getRegister(DS3231_LSB_TEMP) >> 6;
|
||||
float result;
|
||||
|
||||
if ((msb_value & 0x80) != 0)
|
||||
{
|
||||
msb_temp = msb_value | ~((1 << 8) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
msb_temp = msb_value;
|
||||
}
|
||||
|
||||
result = 0.25 * lsb_temp + msb_temp;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t DS3231::getRegister(uint8_t address)
|
||||
{
|
||||
uint8_t value;
|
||||
|
||||
Wire.beginTransmission(DS3231_I2C_ADDRESS);
|
||||
Wire.write(address);
|
||||
Wire.endTransmission();
|
||||
|
||||
// request 1 byte
|
||||
Wire.requestFrom(DS3231_I2C_ADDRESS, 1);
|
||||
|
||||
// read value
|
||||
value = Wire.read();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void DS3231::setRegister(uint8_t address, uint8_t value)
|
||||
{
|
||||
Wire.beginTransmission(DS3231_I2C_ADDRESS);
|
||||
// start at address
|
||||
Wire.write(address);
|
||||
|
||||
// write value
|
||||
Wire.write(value);
|
||||
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
uint8_t DS3231::dectobcd(uint8_t value)
|
||||
{
|
||||
return ((value / 10 * 16) + (value % 10));
|
||||
}
|
||||
|
||||
uint8_t DS3231::bcdtodec(uint8_t value)
|
||||
{
|
||||
return ((value / 16 * 10) + (value % 16));
|
||||
}
|
||||
|
||||
uint8_t DS3231::getCentury()
|
||||
{
|
||||
uint8_t century = 0;
|
||||
|
||||
uint8_t month = getRegister(DS3231_MONTH);
|
||||
|
||||
// read century from month
|
||||
century = (month & 0x80) >> 7;
|
||||
return century;
|
||||
}
|
||||
|
||||
void DS3231::setCentury(uint8_t century)
|
||||
{
|
||||
uint8_t month = getMonth();
|
||||
|
||||
month = month + century;
|
||||
|
||||
setRegister(DS3231_MONTH, month);
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#ifndef DS3231_h
|
||||
#define DS3231_h
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
/**
|
||||
TODO: alarms
|
||||
*/
|
||||
class DS3231
|
||||
{
|
||||
public:
|
||||
// begin function
|
||||
void begin();
|
||||
// get seconds
|
||||
uint8_t getSeconds();
|
||||
// set seconds
|
||||
void setSeconds(uint8_t seconds);
|
||||
// increment seconds
|
||||
void incrementSeconds();
|
||||
// decrement seconds
|
||||
void decrementSeconds();
|
||||
// get minutes
|
||||
uint8_t getMinutes();
|
||||
// set minutes
|
||||
void setMinutes(uint8_t minutes);
|
||||
// increment minutes
|
||||
void incrementMinutes();
|
||||
// decrement minutes
|
||||
void decrementMinutes();
|
||||
// get hours
|
||||
uint8_t getHours();
|
||||
// set hours
|
||||
void setHours(uint8_t hours);
|
||||
// increment hours
|
||||
void incrementHours();
|
||||
// decrement hours
|
||||
void decrementHours();
|
||||
// get day
|
||||
uint8_t getDay();
|
||||
// set day
|
||||
void setDay(uint8_t day);
|
||||
// get date
|
||||
uint8_t getDate();
|
||||
// set date
|
||||
void setDate(uint8_t date);
|
||||
// increment date
|
||||
void incrementDate();
|
||||
// decrement date
|
||||
void decrementDate();
|
||||
// get month
|
||||
uint8_t getMonth();
|
||||
// set month
|
||||
void setMonth(uint8_t month);
|
||||
// increment month
|
||||
void incrementMonth();
|
||||
// decrement month
|
||||
void decrementMonth();
|
||||
// get year
|
||||
int16_t getYear();
|
||||
// set year
|
||||
void setYear(int16_t year);
|
||||
// increment year
|
||||
void incrementYear();
|
||||
// decrement year
|
||||
void decrementYear();
|
||||
// get control register
|
||||
uint8_t getControlRegister();
|
||||
// set control register
|
||||
void setControlRegister(uint8_t value);
|
||||
// set status register
|
||||
uint8_t getStatusRegister();
|
||||
// set status register
|
||||
void setStatusRegister(uint8_t value);
|
||||
// get temperature
|
||||
uint8_t getTemperature();
|
||||
// helpers
|
||||
protected:
|
||||
// get register
|
||||
uint8_t getRegister(uint8_t address);
|
||||
// set register
|
||||
void setRegister(uint8_t address, uint8_t value);
|
||||
// decimal to binary coded decimal
|
||||
uint8_t dectobcd(uint8_t value);
|
||||
// binary coded decimal to decimal
|
||||
uint8_t bcdtodec(uint8_t value);
|
||||
// get century value
|
||||
uint8_t getCentury();
|
||||
// set century value
|
||||
void setCentury(uint8_t century);
|
||||
};
|
||||
|
||||
// I2C address
|
||||
#define DS3231_I2C_ADDRESS 0x68
|
||||
|
||||
// I2C registers
|
||||
#define DS3231_SECONDS 0x00
|
||||
#define DS3231_MINUTES 0x01
|
||||
#define DS3231_HOURS 0x02
|
||||
#define DS3231_DAY 0x03
|
||||
#define DS3231_DATE 0x04
|
||||
#define DS3231_MONTH 0x05
|
||||
#define DS3231_YEAR 0x06
|
||||
#define DS3231_ALARM1_SECONDS 0x07
|
||||
#define DS3231_ALARM1_MINUTES 0x08
|
||||
#define DS3231_ALARM1_HOURS 0x09
|
||||
#define DS3231_ALARM1_DATE 0x0A
|
||||
#define DS3231_ALARM2_MINUTES 0x0B
|
||||
#define DS3231_ALARM2_HOURS 0x0C
|
||||
#define DS3231_ALARM1_DATE 0x0D
|
||||
#define DS3231_CONTROL 0x0E
|
||||
#define DS3231_STATUS 0x0F
|
||||
#define DS3231_AGING_OFFSET 0x10
|
||||
#define DS3231_MSB_TEMP 0x11
|
||||
#define DS3231_LSB_TEMP 0x12
|
||||
|
||||
// control registers
|
||||
#define DS3231_CONTROL_A1IE 0x01
|
||||
#define DS3231_CONTROL_A2IE 0x02
|
||||
#define DS3231_CONTROL_INTCN 0x04
|
||||
#define DS3231_CONTROL_RS1 0x08
|
||||
#define DS3231_CONTROL_RS2 0x10
|
||||
#define DS3231_CONTROL_CONV 0x20
|
||||
#define DS3231_CONTROL_BBSQW 0x40
|
||||
#define DS3231_CONTROL_EOSC 0x80
|
||||
|
||||
// status registers
|
||||
#define DS3231_STATUSL_A1F 0x01
|
||||
#define DS3231_STATUSL_A2F 0x02
|
||||
#define DS3231_STATUSL_BSY 0x04
|
||||
#define DS3231_STATUSL_EN32KHZ 0x08
|
||||
#define DS3231_STATUSL_OSF 0x80
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,128 @@
|
||||
#include <ControlStates.h>
|
||||
|
||||
ControlColorState::ControlColorState(AT24C32 &_at24c32)
|
||||
{
|
||||
at24c32 = _at24c32;
|
||||
}
|
||||
|
||||
void ControlColorState::leftPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::COLOR_R:
|
||||
controlState = ControlStates::COLOR_B;
|
||||
break;
|
||||
case ControlStates::COLOR_G:
|
||||
controlState = ControlStates::COLOR_R;
|
||||
break;
|
||||
case ControlStates::COLOR_B:
|
||||
controlState = ControlStates::COLOR_G;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlColorState::rightPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::COLOR_R:
|
||||
controlState = ControlStates::COLOR_G;
|
||||
break;
|
||||
case ControlStates::COLOR_G:
|
||||
controlState = ControlStates::COLOR_B;
|
||||
break;
|
||||
case ControlStates::COLOR_B:
|
||||
controlState = ControlStates::COLOR_R;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlColorState::enterPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
controlState = ControlStates::VIEW;
|
||||
};
|
||||
|
||||
void ControlColorState::decrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::COLOR_R:
|
||||
decrementColorR();
|
||||
break;
|
||||
case ControlStates::COLOR_G:
|
||||
decrementColorG();
|
||||
break;
|
||||
case ControlStates::COLOR_B:
|
||||
decrementColorB();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlColorState::incrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::COLOR_R:
|
||||
incrementColorR();
|
||||
break;
|
||||
case ControlStates::COLOR_G:
|
||||
incrementColorG();
|
||||
break;
|
||||
case ControlStates::COLOR_B:
|
||||
incrementColorB();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlColorState::decrementColorR()
|
||||
{
|
||||
uint8_t color = at24c32.read(COLOR_ADDRESS);
|
||||
uint8_t red = (color >> 5);
|
||||
red = (red - 1) % 8;
|
||||
color = (red << 5) + (color & 0x1F);
|
||||
at24c32.write(COLOR_ADDRESS, color);
|
||||
}
|
||||
void ControlColorState::incrementColorR()
|
||||
{
|
||||
uint8_t color = at24c32.read(COLOR_ADDRESS);
|
||||
uint8_t red = (color >> 5);
|
||||
red = (red + 1) % 8;
|
||||
color = (red << 5) + (color & 0x1F);
|
||||
at24c32.write(COLOR_ADDRESS, color);
|
||||
}
|
||||
|
||||
void ControlColorState::decrementColorG()
|
||||
{
|
||||
uint8_t color = at24c32.read(COLOR_ADDRESS);
|
||||
uint8_t green = (color >> 2) & 0x07;
|
||||
green = (green - 1) % 8;
|
||||
color = (green << 2) + (color & 0xE3);
|
||||
at24c32.write(COLOR_ADDRESS, color);
|
||||
}
|
||||
|
||||
void ControlColorState::incrementColorG()
|
||||
{
|
||||
uint8_t color = at24c32.read(COLOR_ADDRESS);
|
||||
uint8_t green = (color >> 2) & 0x07;
|
||||
green = (green + 1) % 8;
|
||||
color = (green << 2) + (color & 0xE3);
|
||||
at24c32.write(COLOR_ADDRESS, color);
|
||||
}
|
||||
|
||||
void ControlColorState::decrementColorB()
|
||||
{
|
||||
uint8_t color = at24c32.read(COLOR_ADDRESS);
|
||||
uint8_t blue = color & 0x03;
|
||||
blue = (blue - 1) % 4;
|
||||
color = blue + (color & 0xFC);
|
||||
at24c32.write(COLOR_ADDRESS, color);
|
||||
}
|
||||
|
||||
void ControlColorState::incrementColorB()
|
||||
{
|
||||
uint8_t color = at24c32.read(COLOR_ADDRESS);
|
||||
uint8_t blue = color & 0x03;
|
||||
blue = (blue + 1) % 4;
|
||||
color = blue + (color & 0xFC);
|
||||
at24c32.write(COLOR_ADDRESS, color);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
|
||||
#include <ControlStates.h>
|
||||
|
||||
ControlDateState::ControlDateState(DS3231 &_ds3231)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
}
|
||||
|
||||
void ControlDateState::leftPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::DATE_YEAR:
|
||||
controlState = ControlStates::DATE_MONTH;
|
||||
break;
|
||||
case ControlStates::DATE_MONTH:
|
||||
controlState = ControlStates::DATE_DATE;
|
||||
break;
|
||||
case ControlStates::DATE_DATE:
|
||||
controlState = ControlStates::DATE_YEAR;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlDateState::rightPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::DATE_DATE:
|
||||
controlState = ControlStates::DATE_MONTH;
|
||||
break;
|
||||
case ControlStates::DATE_MONTH:
|
||||
controlState = ControlStates::DATE_YEAR;
|
||||
break;
|
||||
case ControlStates::DATE_YEAR:
|
||||
controlState = ControlStates::DATE_DATE;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlDateState::enterPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
controlState = ControlStates::VIEW;
|
||||
};
|
||||
|
||||
void ControlDateState::decrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::DATE_DATE:
|
||||
ds3231.decrementDate();
|
||||
break;
|
||||
case ControlStates::DATE_MONTH:
|
||||
ds3231.decrementMonth();
|
||||
break;
|
||||
case ControlStates::DATE_YEAR:
|
||||
ds3231.decrementYear();
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlDateState::incrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::DATE_DATE:
|
||||
ds3231.incrementDate();
|
||||
break;
|
||||
case ControlStates::DATE_MONTH:
|
||||
ds3231.incrementMonth();
|
||||
break;
|
||||
case ControlStates::DATE_YEAR:
|
||||
ds3231.incrementYear();
|
||||
break;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
|
||||
#include <ControlStates.h>
|
||||
|
||||
ControlSecondsState::ControlSecondsState(DS3231 &_ds3231)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
}
|
||||
|
||||
void ControlSecondsState::leftPressed(ViewStates &viewState, ControlStates &controlState){};
|
||||
|
||||
void ControlSecondsState::rightPressed(ViewStates &viewState, ControlStates &controlState){};
|
||||
|
||||
void ControlSecondsState::enterPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
controlState = ControlStates::VIEW;
|
||||
};
|
||||
|
||||
void ControlSecondsState::decrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
ds3231.decrementSeconds();
|
||||
};
|
||||
|
||||
void ControlSecondsState::incrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
ds3231.incrementSeconds();
|
||||
};
|
||||
@@ -0,0 +1,130 @@
|
||||
#ifndef ControlState_h
|
||||
#define ControlState_h
|
||||
|
||||
#include <States.h>
|
||||
#include <AT24C32.h>
|
||||
#include <DS3231.h>
|
||||
|
||||
/*
|
||||
abstract control state
|
||||
*/
|
||||
class ControlState
|
||||
{
|
||||
public:
|
||||
virtual void leftPressed(ViewStates &viewState, ControlStates &controlState) = 0;
|
||||
virtual void rightPressed(ViewStates &viewState, ControlStates &controlState) = 0;
|
||||
virtual void enterPressed(ViewStates &viewState, ControlStates &controlState) = 0;
|
||||
virtual void decrementPressed(ViewStates &viewState, ControlStates &controlState) = 0;
|
||||
virtual void incrementPressed(ViewStates &viewState, ControlStates &controlState) = 0;
|
||||
};
|
||||
|
||||
/*
|
||||
control view state
|
||||
*/
|
||||
class ControlViewState : public ControlState
|
||||
{
|
||||
public:
|
||||
ControlViewState(AT24C32 &_at24c32);
|
||||
void leftPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void rightPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void enterPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void decrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void incrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
|
||||
protected:
|
||||
AT24C32 at24c32;
|
||||
};
|
||||
|
||||
/*
|
||||
control time state
|
||||
*/
|
||||
class ControlTimeState : public ControlState
|
||||
{
|
||||
public:
|
||||
ControlTimeState(DS3231 &_ds3231, AT24C32 &_at24c32);
|
||||
void leftPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void rightPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void enterPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void decrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void incrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
AT24C32 at24c32;
|
||||
};
|
||||
|
||||
/*
|
||||
control seconds state
|
||||
*/
|
||||
class ControlSecondsState : public ControlState
|
||||
{
|
||||
public:
|
||||
ControlSecondsState(DS3231 &_ds3231);
|
||||
void leftPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void rightPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void enterPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void decrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void incrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
};
|
||||
|
||||
/*
|
||||
control date state
|
||||
*/
|
||||
class ControlDateState : public ControlState
|
||||
{
|
||||
public:
|
||||
ControlDateState(DS3231 &_ds3231);
|
||||
void leftPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void rightPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void enterPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void decrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void incrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
};
|
||||
|
||||
/*
|
||||
control color value state
|
||||
*/
|
||||
class ControlColorState : public ControlState
|
||||
{
|
||||
public:
|
||||
ControlColorState(AT24C32 &_at24c32);
|
||||
void leftPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void rightPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void enterPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void decrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void incrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
|
||||
protected:
|
||||
AT24C32 at24c32;
|
||||
void decrementColorR();
|
||||
void incrementColorR();
|
||||
void decrementColorG();
|
||||
void incrementColorG();
|
||||
void decrementColorB();
|
||||
void incrementColorB();
|
||||
};
|
||||
|
||||
/*
|
||||
control text state
|
||||
*/
|
||||
class ControlTextState : public ControlState
|
||||
{
|
||||
public:
|
||||
ControlTextState(AT24C32 &_at24c32);
|
||||
void leftPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void rightPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void enterPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void decrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
void incrementPressed(ViewStates &viewState, ControlStates &controlState);
|
||||
|
||||
protected:
|
||||
AT24C32 at24c32;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
|
||||
#include <ControlStates.h>
|
||||
|
||||
ControlTextState::ControlTextState(AT24C32 &_at24c32)
|
||||
{
|
||||
at24c32 = _at24c32;
|
||||
}
|
||||
|
||||
void ControlTextState::leftPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
uint8_t length = at24c32.read(TEXT_SIZE_ADDRESS);
|
||||
if (length > 2)
|
||||
{
|
||||
length--;
|
||||
at24c32.write(TEXT_SIZE_ADDRESS, length);
|
||||
}
|
||||
};
|
||||
|
||||
void ControlTextState::rightPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
uint8_t length = at24c32.read(TEXT_SIZE_ADDRESS);
|
||||
if (length < 0xFF)
|
||||
{
|
||||
length++;
|
||||
at24c32.write(TEXT_SIZE_ADDRESS, length);
|
||||
}
|
||||
};
|
||||
|
||||
void ControlTextState::enterPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
controlState = ControlStates::VIEW;
|
||||
};
|
||||
|
||||
void ControlTextState::decrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
uint8_t position = at24c32.read(TEXT_SIZE_ADDRESS) - 1;
|
||||
uint8_t char1 = at24c32.read(TEXT_ADDRESS + position);
|
||||
if (char1 > 0)
|
||||
{
|
||||
char1--;
|
||||
}
|
||||
else
|
||||
{
|
||||
char1 = 0xFF;
|
||||
}
|
||||
at24c32.write(TEXT_ADDRESS + position, char1);
|
||||
};
|
||||
|
||||
void ControlTextState::incrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
uint8_t position = at24c32.read(TEXT_SIZE_ADDRESS) - 1;
|
||||
uint8_t char1 = at24c32.read(TEXT_ADDRESS + position);
|
||||
if (char1 < 0xFF)
|
||||
{
|
||||
char1++;
|
||||
}
|
||||
else
|
||||
{
|
||||
char1 = 0;
|
||||
}
|
||||
at24c32.write(TEXT_ADDRESS + position, char1);
|
||||
};
|
||||
@@ -0,0 +1,82 @@
|
||||
|
||||
#include <ControlStates.h>
|
||||
|
||||
ControlTimeState::ControlTimeState(DS3231 &_ds3231, AT24C32 &_at24c32)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
at24c32 = _at24c32;
|
||||
}
|
||||
|
||||
void ControlTimeState::leftPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::TIME_HOURS:
|
||||
controlState = ControlStates::TIME_MODE;
|
||||
break;
|
||||
case ControlStates::TIME_MINUTES:
|
||||
controlState = ControlStates::TIME_HOURS;
|
||||
break;
|
||||
case ControlStates::TIME_MODE:
|
||||
controlState = ControlStates::TIME_MINUTES;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlTimeState::rightPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::TIME_HOURS:
|
||||
controlState = ControlStates::TIME_MINUTES;
|
||||
break;
|
||||
case ControlStates::TIME_MINUTES:
|
||||
controlState = ControlStates::TIME_MODE;
|
||||
break;
|
||||
case ControlStates::TIME_MODE:
|
||||
controlState = ControlStates::TIME_HOURS;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlTimeState::enterPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
controlState = ControlStates::VIEW;
|
||||
};
|
||||
|
||||
void ControlTimeState::decrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::TIME_HOURS:
|
||||
ds3231.decrementHours();
|
||||
break;
|
||||
case ControlStates::TIME_MINUTES:
|
||||
ds3231.decrementMinutes();
|
||||
break;
|
||||
case ControlStates::TIME_MODE:
|
||||
uint8_t mode = at24c32.read(TIME_MODE_ADDRESS);
|
||||
mode = (mode - 1) % 3;
|
||||
at24c32.write(TIME_MODE_ADDRESS, mode);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlTimeState::incrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (controlState)
|
||||
{
|
||||
case ControlStates::TIME_HOURS:
|
||||
ds3231.incrementHours();
|
||||
break;
|
||||
case ControlStates::TIME_MINUTES:
|
||||
ds3231.incrementMinutes();
|
||||
break;
|
||||
case ControlStates::TIME_MODE:
|
||||
uint8_t mode = at24c32.read(TIME_MODE_ADDRESS);
|
||||
mode = (mode + 1) % 3;
|
||||
at24c32.write(TIME_MODE_ADDRESS, mode);
|
||||
break;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,92 @@
|
||||
#include <ControlStates.h>
|
||||
|
||||
ControlViewState::ControlViewState(AT24C32 &_at24c32)
|
||||
{
|
||||
at24c32 = _at24c32;
|
||||
}
|
||||
|
||||
void ControlViewState::leftPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (viewState)
|
||||
{
|
||||
case ViewStates::TIME:
|
||||
viewState = ViewStates::LED_DEMO;
|
||||
break;
|
||||
case ViewStates::TEMPERATURE:
|
||||
viewState = ViewStates::TIME;
|
||||
break;
|
||||
case ViewStates::SECONDS:
|
||||
viewState = ViewStates::TEMPERATURE;
|
||||
break;
|
||||
case ViewStates::DATE:
|
||||
viewState = ViewStates::SECONDS;
|
||||
break;
|
||||
case ViewStates::TEXT:
|
||||
viewState = ViewStates::DATE;
|
||||
break;
|
||||
case ViewStates::LED_DEMO:
|
||||
viewState = ViewStates::TEXT;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlViewState::rightPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (viewState)
|
||||
{
|
||||
case ViewStates::TIME:
|
||||
viewState = ViewStates::TEMPERATURE;
|
||||
break;
|
||||
case ViewStates::TEMPERATURE:
|
||||
viewState = ViewStates::SECONDS;
|
||||
break;
|
||||
case ViewStates::SECONDS:
|
||||
viewState = ViewStates::DATE;
|
||||
break;
|
||||
case ViewStates::DATE:
|
||||
viewState = ViewStates::TEXT;
|
||||
break;
|
||||
case ViewStates::TEXT:
|
||||
viewState = ViewStates::LED_DEMO;
|
||||
break;
|
||||
case ViewStates::LED_DEMO:
|
||||
viewState = ViewStates::TIME;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlViewState::enterPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
switch (viewState)
|
||||
{
|
||||
case ViewStates::TIME:
|
||||
controlState = ControlStates::TIME_HOURS;
|
||||
break;
|
||||
case ViewStates::SECONDS:
|
||||
controlState = ControlStates::TIME_SECONDS;
|
||||
break;
|
||||
case ViewStates::DATE:
|
||||
controlState = ControlStates::DATE_DATE;
|
||||
break;
|
||||
case ViewStates::TEXT:
|
||||
controlState = ControlStates::TEXT;
|
||||
break;
|
||||
case ViewStates::LED_DEMO:
|
||||
controlState = ControlStates::COLOR_R;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
void ControlViewState::decrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
uint8_t value = at24c32.read(BRIGHTNESS_ADDRESS);
|
||||
value = (value - 5) % 256;
|
||||
at24c32.write(BRIGHTNESS_ADDRESS, value);
|
||||
};
|
||||
|
||||
void ControlViewState::incrementPressed(ViewStates &viewState, ControlStates &controlState)
|
||||
{
|
||||
uint8_t value = at24c32.read(BRIGHTNESS_ADDRESS);
|
||||
value = (value + 5) % 256;
|
||||
at24c32.write(BRIGHTNESS_ADDRESS, value);
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
#ifndef States_h
|
||||
#define States_h
|
||||
|
||||
/**
|
||||
view states
|
||||
*/
|
||||
enum class ViewStates
|
||||
{
|
||||
TIME,
|
||||
SECONDS,
|
||||
TEMPERATURE,
|
||||
DATE,
|
||||
TEXT,
|
||||
LED_DEMO,
|
||||
REMOTE
|
||||
};
|
||||
|
||||
/**
|
||||
control states
|
||||
*/
|
||||
enum class ControlStates
|
||||
{
|
||||
VIEW,
|
||||
TIME_HOURS,
|
||||
TIME_MINUTES,
|
||||
TIME_MODE,
|
||||
TIME_SECONDS,
|
||||
COLOR_R,
|
||||
COLOR_G,
|
||||
COLOR_B,
|
||||
DATE_DATE,
|
||||
DATE_MONTH,
|
||||
DATE_YEAR,
|
||||
TEXT
|
||||
};
|
||||
|
||||
/**
|
||||
remote control states
|
||||
*/
|
||||
enum class RemoteStates
|
||||
{
|
||||
VIEW,
|
||||
TIME,
|
||||
COLOR,
|
||||
DATE,
|
||||
TEXT,
|
||||
DRAW
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,96 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewDateState::ViewDateState(DS3231 &_ds3231, AT24C32 &_at24c32, char _delimiter) : ViewTextState(at24c32)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
at24c32 = _at24c32;
|
||||
delimiter = _delimiter;
|
||||
renderOffset = (millis() / TEXT_INTERVAL);
|
||||
};
|
||||
|
||||
void ViewDateState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
|
||||
bool const blink = (millis() / CONTROL_BLINK_INTERVAL) % 2 == 0;
|
||||
|
||||
length = 0;
|
||||
position = 0;
|
||||
|
||||
uint8_t date = ds3231.getDate();
|
||||
uint8_t month = ds3231.getMonth();
|
||||
int16_t year = ds3231.getYear();
|
||||
|
||||
if (controlState == ControlStates::DATE_DATE && blink)
|
||||
{
|
||||
length = 2;
|
||||
text[0] = (date / 10) + 0x30;
|
||||
text[1] = (date % 10) + 0x30;
|
||||
ViewTextState::writeChars(matrix);
|
||||
}
|
||||
else if (controlState == ControlStates::DATE_MONTH && blink)
|
||||
{
|
||||
length = 2;
|
||||
text[0] = (month / 10) + 0x30;
|
||||
text[1] = (month % 10) + 0x30;
|
||||
ViewTextState::writeChars(matrix);
|
||||
}
|
||||
else if (controlState == ControlStates::DATE_YEAR && blink)
|
||||
{
|
||||
if (year == 2000 || year == 1900)
|
||||
{
|
||||
length = 4;
|
||||
text[0] = ((year / 100) / 10) + 0x30;
|
||||
text[1] = ((year / 100) % 10) + 0x30;
|
||||
position = (millis() / TEXT_INTERVAL) % length;
|
||||
}
|
||||
else
|
||||
{
|
||||
length = 2;
|
||||
}
|
||||
if (year > 1999)
|
||||
{
|
||||
year = year - 2000;
|
||||
}
|
||||
else
|
||||
{
|
||||
year = year - 1900;
|
||||
}
|
||||
|
||||
text[length - 2] = (year / 10) + 0x30;
|
||||
text[length - 1] = (year % 10) + 0x30;
|
||||
|
||||
ViewTextState::writeChars(matrix);
|
||||
}
|
||||
else if (controlState != ControlStates::DATE_DATE && controlState != ControlStates::DATE_MONTH && controlState != ControlStates::DATE_YEAR)
|
||||
{
|
||||
length = 10;
|
||||
text[0] = (date / 10) + 0x30;
|
||||
text[1] = (date % 10) + 0x30;
|
||||
|
||||
text[2] = char(delimiter);
|
||||
|
||||
text[3] = (month / 10) + 0x30;
|
||||
text[4] = (month % 10) + 0x30;
|
||||
|
||||
text[5] = char(delimiter);
|
||||
|
||||
text[6] = ((year / 100) / 10) + 0x30;
|
||||
text[7] = ((year / 100) % 10) + 0x30;
|
||||
if (year > 1999)
|
||||
{
|
||||
text[8] = ((year - 2000) / 10) + 0x30;
|
||||
text[9] = ((year - 2000) % 10) + 0x30;
|
||||
}
|
||||
else
|
||||
{
|
||||
text[8] = ((year - 1900) / 10) + 0x30;
|
||||
text[9] = ((year - 1900) % 10) + 0x30;
|
||||
}
|
||||
|
||||
position = (millis() / TEXT_INTERVAL - renderOffset - 1) % length;
|
||||
ViewTextState::writeChars(matrix);
|
||||
}
|
||||
|
||||
renderBinaryClock(matrix, ds3231);
|
||||
};
|
||||
@@ -0,0 +1,76 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewLEDState::ViewLEDState(){
|
||||
|
||||
};
|
||||
|
||||
void ViewLEDState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
|
||||
if (controlState != ControlStates::COLOR_R && controlState != ControlStates::COLOR_G && controlState != ControlStates::COLOR_B)
|
||||
{
|
||||
int const color = (millis() / 500) % 256;
|
||||
|
||||
for (int i = 0; i < 121; i++)
|
||||
{
|
||||
matrix[i] = color - i % 256;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// special color edit view
|
||||
bool const blink = (millis() / CONTROL_BLINK_INTERVAL) % 2 == 0;
|
||||
|
||||
if (controlState != ControlStates::COLOR_R || blink)
|
||||
{
|
||||
uint8_t red = color >> 5;
|
||||
matrix[0] = 0x07 << 5;
|
||||
matrix[10] = 0x07 << 5;
|
||||
matrix[11] = 0x07 << 5;
|
||||
matrix[21] = 0x07 << 5;
|
||||
for (int i = 0; i < red; i++)
|
||||
{
|
||||
// red
|
||||
matrix[i + 2] = red << 5;
|
||||
matrix[i + 13] = red << 5;
|
||||
}
|
||||
}
|
||||
|
||||
if (controlState != ControlStates::COLOR_G || blink)
|
||||
{
|
||||
uint8_t green = (color >> 2) & 0x07;
|
||||
matrix[22] = 0x07 << 2;
|
||||
matrix[32] = 0x07 << 2;
|
||||
matrix[33] = 0x07 << 2;
|
||||
matrix[43] = 0x07 << 2;
|
||||
for (int i = 0; i < green; i++)
|
||||
{
|
||||
// green
|
||||
matrix[i + 24] = green << 2;
|
||||
matrix[i + 35] = green << 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (controlState != ControlStates::COLOR_B || blink)
|
||||
{
|
||||
|
||||
uint8_t blue = color & 0x03;
|
||||
matrix[44] = 0x03;
|
||||
matrix[54] = 0x03;
|
||||
matrix[55] = 0x03;
|
||||
matrix[65] = 0x03;
|
||||
for (int i = 0; i < blue; i++)
|
||||
{
|
||||
// blue
|
||||
matrix[i + 46] = blue;
|
||||
matrix[i + 57] = blue;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 77; i < 121; i++)
|
||||
{
|
||||
matrix[i] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewRemoteState::ViewRemoteState(){
|
||||
|
||||
};
|
||||
|
||||
void ViewRemoteState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
// nothing, LEDs set via remote control
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewSecondsState::ViewSecondsState(DS3231 &_ds3231, AT24C32 &_at24c32) : ViewTextState(at24c32)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
at24c32 = _at24c32;
|
||||
};
|
||||
|
||||
void ViewSecondsState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
uint8_t const seconds = ds3231.getSeconds();
|
||||
|
||||
bool const blink = (millis() / CONTROL_BLINK_INTERVAL) % 2 == 0;
|
||||
|
||||
length = 0;
|
||||
|
||||
if (controlState != ControlStates::TIME_SECONDS || blink)
|
||||
{
|
||||
length = 2;
|
||||
text[0] = (seconds / 10) + 0x30;
|
||||
text[1] = (seconds % 10) + 0x30;
|
||||
}
|
||||
|
||||
ViewTextState::writeChars(matrix);
|
||||
|
||||
renderBinaryClock(matrix, ds3231);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
void ViewState::setColor(uint8_t _color)
|
||||
{
|
||||
color = _color;
|
||||
};
|
||||
|
||||
void ViewState::renderBinaryClock(uint8_t matrix[121], DS3231 &ds3231)
|
||||
{
|
||||
uint8_t h = ds3231.getHours() % 12;
|
||||
uint8_t mm = ds3231.getMinutes();
|
||||
|
||||
for (int b = 0; b < 4; b++)
|
||||
{
|
||||
if (bitRead(h, b))
|
||||
{
|
||||
matrix[110 + b] = color;
|
||||
}
|
||||
}
|
||||
|
||||
for (int b = 0; b < 6; b++)
|
||||
{
|
||||
if (bitRead(mm, b))
|
||||
{
|
||||
matrix[115 + b] = color;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,142 @@
|
||||
#ifndef ViewState_h
|
||||
#define ViewState_h
|
||||
|
||||
#include <States.h>
|
||||
#include <DS3231.h>
|
||||
#include <AT24C32.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// I2C address
|
||||
#define TEXT_INTERVAL 1000
|
||||
#define CONTROL_BLINK_INTERVAL 250
|
||||
|
||||
/**
|
||||
led matrix entry
|
||||
*/
|
||||
struct matrix_entry
|
||||
{
|
||||
uint8_t row;
|
||||
uint8_t columnFrom;
|
||||
uint8_t columnTo;
|
||||
bool notNull;
|
||||
};
|
||||
|
||||
/**
|
||||
abstract view state
|
||||
*/
|
||||
class ViewState
|
||||
{
|
||||
public:
|
||||
virtual void render(uint8_t matrix[121], ControlStates controlState) = 0;
|
||||
void setColor(uint8_t _color);
|
||||
|
||||
protected:
|
||||
void renderBinaryClock(uint8_t matrix[121], DS3231 &ds3231);
|
||||
uint8_t color = 0xFF;
|
||||
};
|
||||
|
||||
/**
|
||||
view time state
|
||||
*/
|
||||
class ViewTimeState : public ViewState
|
||||
{
|
||||
public:
|
||||
ViewTimeState(DS3231 &_ds3231, AT24C32 &_at24c32);
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
AT24C32 at24c32;
|
||||
matrix_entry text[2];
|
||||
matrix_entry hours[12][2];
|
||||
matrix_entry minutes[12][3];
|
||||
uint8_t increment;
|
||||
uint8_t mode = 0;
|
||||
void init();
|
||||
void setWord(uint8_t matrix[121], matrix_entry word);
|
||||
void setWessi();
|
||||
void setOssi();
|
||||
};
|
||||
|
||||
/**
|
||||
view text state
|
||||
*/
|
||||
class ViewTextState : public ViewState
|
||||
{
|
||||
public:
|
||||
ViewTextState(AT24C32 &_at24c32);
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
|
||||
protected:
|
||||
void writeText(uint8_t matrix[121]);
|
||||
void writeChars(uint8_t matrix[121]);
|
||||
AT24C32 at24c32;
|
||||
uint8_t text[0xFF];
|
||||
uint8_t length = 0;
|
||||
uint8_t position = 0;
|
||||
uint8_t v_offset = 1;
|
||||
long renderOffset;
|
||||
};
|
||||
|
||||
/**
|
||||
view seconds state
|
||||
*/
|
||||
class ViewSecondsState : public ViewTextState
|
||||
{
|
||||
public:
|
||||
ViewSecondsState(DS3231 &_ds3231, AT24C32 &_at24c32);
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
};
|
||||
|
||||
/**
|
||||
view temperature state
|
||||
*/
|
||||
class ViewTemperatureState : public ViewTextState
|
||||
{
|
||||
public:
|
||||
ViewTemperatureState(DS3231 &_ds3231, AT24C32 &_at24c32);
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
};
|
||||
|
||||
/**
|
||||
view date state
|
||||
*/
|
||||
class ViewDateState : public ViewTextState
|
||||
{
|
||||
public:
|
||||
ViewDateState(DS3231 &_ds3231, AT24C32 &_at24c32, char _delimiter);
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
|
||||
protected:
|
||||
DS3231 ds3231;
|
||||
char delimiter;
|
||||
};
|
||||
|
||||
/**
|
||||
view LED demo state
|
||||
*/
|
||||
class ViewLEDState : public ViewState
|
||||
{
|
||||
public:
|
||||
ViewLEDState();
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
};
|
||||
|
||||
/**
|
||||
view Remote demo state
|
||||
*/
|
||||
class ViewRemoteState : public ViewState
|
||||
{
|
||||
public:
|
||||
ViewRemoteState();
|
||||
void render(uint8_t matrix[121], ControlStates controlState);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewTemperatureState::ViewTemperatureState(DS3231 &_ds3231, AT24C32 &_at24c32) : ViewTextState(at24c32)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
at24c32 = _at24c32;
|
||||
};
|
||||
|
||||
void ViewTemperatureState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
// °
|
||||
matrix[9] = color;
|
||||
matrix[10] = color;
|
||||
matrix[20] = color;
|
||||
matrix[21] = color;
|
||||
|
||||
int temperature = floor(ds3231.getTemperature());
|
||||
|
||||
length = 2;
|
||||
|
||||
text[0] = (temperature / 10) + 0x30;
|
||||
text[1] = (temperature % 10) + 0x30;
|
||||
|
||||
ViewTextState::writeChars(matrix);
|
||||
|
||||
renderBinaryClock(matrix, ds3231);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewTextState::ViewTextState(AT24C32 &_at24c32)
|
||||
{
|
||||
at24c32 = _at24c32;
|
||||
renderOffset = (millis() / TEXT_INTERVAL);
|
||||
};
|
||||
|
||||
void ViewTextState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
length = at24c32.read(TEXT_SIZE_ADDRESS);
|
||||
|
||||
if (controlState != ControlStates::TEXT)
|
||||
{
|
||||
for (uint8_t i = 0; i < length; i++)
|
||||
{
|
||||
text[i] = at24c32.read(TEXT_ADDRESS + i);
|
||||
}
|
||||
writeText(matrix);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool const blink = (millis() / CONTROL_BLINK_INTERVAL) % 2 == 0;
|
||||
position = 0;
|
||||
text[0] = 0x20;
|
||||
text[1] = 0x20;
|
||||
if (length > 2)
|
||||
{
|
||||
text[0] = at24c32.read(TEXT_ADDRESS + length - 2);
|
||||
if (blink)
|
||||
{
|
||||
text[1] = at24c32.read(TEXT_ADDRESS + length - 1);
|
||||
}
|
||||
}
|
||||
else if (length > 1 && blink)
|
||||
{
|
||||
text[0] = at24c32.read(TEXT_ADDRESS + length - 1);
|
||||
}
|
||||
writeChars(matrix);
|
||||
}
|
||||
}
|
||||
|
||||
void ViewTextState::writeText(uint8_t matrix[121])
|
||||
{
|
||||
position = (millis() / TEXT_INTERVAL - renderOffset - 1) % (length + 2);
|
||||
writeChars(matrix);
|
||||
}
|
||||
|
||||
void ViewTextState::writeChars(uint8_t matrix[121])
|
||||
{
|
||||
uint8_t char1 = 0x20;
|
||||
|
||||
uint8_t char2 = 0x20;
|
||||
|
||||
if (length > position)
|
||||
{
|
||||
char1 = text[position];
|
||||
}
|
||||
|
||||
if (length > position + 1)
|
||||
{
|
||||
char2 = text[position + 1];
|
||||
}
|
||||
|
||||
for (int col = 0; col < 5; col++)
|
||||
{
|
||||
// created ascii chars in 'ascii_memory.ino' to AT23C32 epromm
|
||||
uint8_t char1Byte = at24c32.read(char1 * 5 + col);
|
||||
uint8_t char2Byte = at24c32.read(char2 * 5 + col);
|
||||
|
||||
for (int row = 0; row < 8; row++)
|
||||
{
|
||||
if (!!(char1Byte & (1 << row)))
|
||||
{
|
||||
matrix[(row + v_offset) * 11 + col] = color;
|
||||
}
|
||||
|
||||
if (!!(char2Byte & (1 << row)))
|
||||
{
|
||||
matrix[(row + v_offset) * 11 + col + 6] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
|
||||
#include <ViewStates.h>
|
||||
|
||||
ViewTimeState::ViewTimeState(DS3231 &_ds3231, AT24C32 &_at24c32)
|
||||
{
|
||||
ds3231 = _ds3231;
|
||||
at24c32 = _at24c32;
|
||||
init();
|
||||
}
|
||||
|
||||
/*
|
||||
default german matrix layout
|
||||
|
||||
'E', 'S', 'K', 'I', 'S', 'T', 'L', 'F', 'Ü', 'N', 'F',
|
||||
'Z', 'E', 'H', 'N', 'Z', 'W', 'A', 'N', 'Z', 'I', 'G',
|
||||
'D', 'R', 'E', 'I', 'V', 'I', 'E', 'R', 'T', 'E', 'L',
|
||||
'T', 'A', 'N', 'A', 'C', 'H', 'V', 'O', 'R', 'J', 'M',
|
||||
'H', 'A', 'L', 'B', 'X', 'Z', 'W', 'Ö', 'L', 'F', 'P',
|
||||
'Z', 'W', 'E', 'I', 'N', 'S', 'I', 'E', 'B', 'E', 'N',
|
||||
'K', 'D', 'R', 'E', 'I', 'R', 'H', 'F', 'Ü', 'N', 'F',
|
||||
'E', 'L', 'F', 'N', 'E', 'U', 'N', 'V', 'I', 'E', 'R',
|
||||
'W', 'A', 'C', 'H', 'T', 'Z', 'E', 'H', 'N', 'R', 'S',
|
||||
'B', 'S', 'E', 'C', 'H', 'S', 'F', 'M', 'U', 'H', 'R',
|
||||
'B', 'S', 'E', 'C', 'H', 'S', 'F', 'M', 'U', 'H', 'R',
|
||||
'□', '□', '□', '□', '▣', '□', '□', '□', '□', '□', '□',
|
||||
|
||||
*/
|
||||
void ViewTimeState::init()
|
||||
{
|
||||
text[0] = {0, 0, 1, true};
|
||||
text[1] = {0, 3, 5, true};
|
||||
|
||||
hours[0][0] = {4, 5, 9, true};
|
||||
hours[0][1] = {0, 0, 0, false};
|
||||
hours[1][0] = {5, 2, 4, true};
|
||||
hours[1][1] = {5, 2, 5, true};
|
||||
hours[2][0] = {5, 0, 3, true};
|
||||
hours[2][1] = {0, 0, 0, false};
|
||||
hours[3][0] = {6, 1, 4, true};
|
||||
hours[3][1] = {0, 0, 0, false};
|
||||
hours[4][0] = {7, 7, 10, true};
|
||||
hours[4][1] = {0, 0, 0, false};
|
||||
hours[5][0] = {6, 7, 10, true};
|
||||
hours[5][1] = {0, 0, 0, false};
|
||||
hours[6][0] = {9, 1, 5, true};
|
||||
hours[6][1] = {0, 0, 0, false};
|
||||
hours[7][0] = {5, 5, 10, true};
|
||||
hours[7][1] = {0, 0, 0, false};
|
||||
hours[8][0] = {8, 1, 4, true};
|
||||
hours[8][1] = {0, 0, 0, false};
|
||||
hours[9][0] = {7, 3, 6, true};
|
||||
hours[9][1] = {0, 0, 0, false};
|
||||
hours[10][0] = {8, 5, 8, true};
|
||||
hours[10][1] = {0, 0, 0, false};
|
||||
hours[11][0] = {7, 0, 2, true};
|
||||
hours[11][1] = {0, 0, 0, false};
|
||||
|
||||
// 0
|
||||
minutes[0][0] = {9, 8, 10, true};
|
||||
minutes[0][1] = {0, 0, 0, false};
|
||||
minutes[0][2] = {0, 0, 0, false};
|
||||
// 5
|
||||
minutes[1][0] = {0, 7, 10, true};
|
||||
minutes[1][1] = {3, 2, 5, true};
|
||||
minutes[1][2] = {0, 0, 0, false};
|
||||
// 10
|
||||
minutes[2][0] = {1, 0, 3, true};
|
||||
minutes[2][1] = {3, 2, 5, true};
|
||||
minutes[2][2] = {0, 0, 0, false};
|
||||
// 15
|
||||
minutes[3][0] = {2, 4, 10, true};
|
||||
minutes[3][1] = {3, 2, 5, true};
|
||||
minutes[3][2] = {0, 0, 0, false};
|
||||
// 20
|
||||
minutes[4][0] = {1, 4, 10, true};
|
||||
minutes[4][1] = {3, 2, 5, true};
|
||||
minutes[4][2] = {0, 0, 0, false};
|
||||
// 25
|
||||
minutes[5][0] = {0, 7, 10, true};
|
||||
minutes[5][1] = {3, 6, 8, true};
|
||||
minutes[5][2] = {4, 0, 3, true};
|
||||
// 30
|
||||
minutes[6][0] = {4, 0, 3, true};
|
||||
minutes[6][1] = {0, 0, 0, false};
|
||||
minutes[6][2] = {0, 0, 0, false};
|
||||
// 35
|
||||
minutes[7][0] = {0, 7, 10, true};
|
||||
minutes[7][1] = {3, 2, 5, true};
|
||||
minutes[7][2] = {4, 0, 3, true};
|
||||
// 40
|
||||
minutes[8][0] = {1, 4, 10, true};
|
||||
minutes[8][1] = {3, 6, 8, true};
|
||||
minutes[8][2] = {0, 0, 0, false};
|
||||
// 45
|
||||
minutes[9][0] = {2, 4, 10, true};
|
||||
minutes[9][1] = {3, 6, 8, true};
|
||||
minutes[9][2] = {0, 0, 0, false};
|
||||
// 50
|
||||
minutes[10][0] = {1, 0, 3, true};
|
||||
minutes[10][1] = {3, 6, 8, true};
|
||||
minutes[10][2] = {0, 0, 0, false};
|
||||
// 55
|
||||
minutes[11][0] = {0, 7, 10, true};
|
||||
minutes[11][1] = {3, 6, 8, true};
|
||||
minutes[11][2] = {0, 0, 0, false};
|
||||
|
||||
increment = 24;
|
||||
}
|
||||
|
||||
void ViewTimeState::setWessi()
|
||||
{
|
||||
init();
|
||||
minutes[4][0] = {1, 0, 3, true};
|
||||
minutes[4][1] = {3, 6, 8, true};
|
||||
minutes[4][2] = {4, 0, 3, true};
|
||||
|
||||
minutes[8][0] = {1, 0, 3, true};
|
||||
minutes[8][1] = {3, 2, 5, true};
|
||||
minutes[8][2] = {4, 0, 3, true};
|
||||
|
||||
increment = 19;
|
||||
}
|
||||
|
||||
void ViewTimeState::setOssi()
|
||||
{
|
||||
setWessi();
|
||||
|
||||
minutes[3][0] = {2, 4, 10, true};
|
||||
minutes[3][1] = {0, 0, 0, false};
|
||||
|
||||
minutes[9][0] = {2, 0, 10, true};
|
||||
minutes[9][1] = {0, 0, 0, false};
|
||||
increment = 14;
|
||||
}
|
||||
|
||||
void ViewTimeState::setWord(uint8_t matrix[121], matrix_entry word)
|
||||
{
|
||||
int row;
|
||||
for (int i = word.columnFrom; i <= word.columnTo; i++)
|
||||
{
|
||||
row = word.row * 11 + i;
|
||||
matrix[row] = color;
|
||||
}
|
||||
}
|
||||
|
||||
void ViewTimeState::render(uint8_t matrix[121], ControlStates controlState)
|
||||
{
|
||||
|
||||
uint8_t currentMode = at24c32.read(TIME_MODE_ADDRESS);
|
||||
|
||||
if (mode != currentMode)
|
||||
{
|
||||
mode = currentMode;
|
||||
switch (mode)
|
||||
{
|
||||
case 1:
|
||||
setWessi();
|
||||
break;
|
||||
case 2:
|
||||
setOssi();
|
||||
break;
|
||||
default:
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
bool const blink = (millis() / CONTROL_BLINK_INTERVAL) % 2 == 0;
|
||||
|
||||
if (controlState != ControlStates::TIME_MODE || blink)
|
||||
{
|
||||
setWord(matrix, text[0]);
|
||||
setWord(matrix, text[1]);
|
||||
}
|
||||
|
||||
uint8_t mm = ds3231.getMinutes();
|
||||
uint8_t m = mm / 5;
|
||||
uint8_t h = ds3231.getHours() % 12;
|
||||
uint8_t s = ds3231.getSeconds();
|
||||
|
||||
// increment hours
|
||||
if (mm > increment)
|
||||
{
|
||||
h = (h + 1) % 12;
|
||||
}
|
||||
|
||||
// set hours
|
||||
if (controlState != ControlStates::TIME_HOURS || blink)
|
||||
{
|
||||
setWord(matrix, hours[h][0]);
|
||||
if (mm >= 5 && hours[h][1].notNull == true)
|
||||
{
|
||||
setWord(matrix, hours[h][1]);
|
||||
}
|
||||
}
|
||||
|
||||
// set 5 minutes
|
||||
if (controlState != ControlStates::TIME_MINUTES || blink)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (minutes[m][i].notNull == true)
|
||||
{
|
||||
setWord(matrix, minutes[m][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set minutes
|
||||
if (mm % 5 > 0)
|
||||
{
|
||||
if (controlState != ControlStates::TIME_MINUTES || blink)
|
||||
{
|
||||
setWord(matrix, {10, 0, mm % 5 - 1});
|
||||
}
|
||||
}
|
||||
|
||||
// set seconds
|
||||
setWord(matrix, {10, 5, 5 + (s / 10)});
|
||||
}
|
||||
Reference in New Issue
Block a user