code init

This commit is contained in:
Lurkars
2019-02-13 19:27:22 +01:00
parent 9650b482ee
commit 001591a54d
25 changed files with 2751 additions and 0 deletions
+128
View File
@@ -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);
}
+77
View File
@@ -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;
}
};
+26
View File
@@ -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();
};
+130
View File
@@ -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
+62
View File
@@ -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);
};
+82
View File
@@ -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;
}
};
+92
View File
@@ -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);
};
+50
View File
@@ -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
+96
View File
@@ -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);
};
+76
View File
@@ -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;
}
}
}
+11
View File
@@ -0,0 +1,11 @@
#include <ViewStates.h>
ViewRemoteState::ViewRemoteState(){
};
void ViewRemoteState::render(uint8_t matrix[121], ControlStates controlState)
{
// nothing, LEDs set via remote control
}
+28
View File
@@ -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);
}
+29
View File
@@ -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;
}
}
};
+142
View File
@@ -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
+28
View File
@@ -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);
}
+85
View File
@@ -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;
}
}
}
}
+219
View File
@@ -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)});
}