fix pattern state engine
This commit is contained in:
128
pattern.c
128
pattern.c
@ -9,7 +9,8 @@
|
|||||||
uint8_t temperature = 21;
|
uint8_t temperature = 21;
|
||||||
const uint8_t TEMPERATURE_THRESHOLD = 25;
|
const uint8_t TEMPERATURE_THRESHOLD = 25;
|
||||||
|
|
||||||
tColor lockedColor = OFF;
|
|
||||||
|
|
||||||
|
|
||||||
void patternSetTemperature(uint8_t t) {
|
void patternSetTemperature(uint8_t t) {
|
||||||
temperature = t;
|
temperature = t;
|
||||||
@ -35,6 +36,7 @@ void patternExec() {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
void patternTestBlueExec() {
|
void patternTestBlueExec() {
|
||||||
static uint8_t state = 0;
|
static uint8_t state = 0;
|
||||||
static uint8_t column = 0;
|
static uint8_t column = 0;
|
||||||
@ -76,63 +78,52 @@ void patternTestBlueExec() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static bool _patternChangeColor() {
|
|
||||||
tColor newColor = OFF;
|
|
||||||
bool changed = false;
|
|
||||||
|
|
||||||
if (temperature > TEMPERATURE_THRESHOLD) {
|
|
||||||
newColor = RED;
|
|
||||||
} else {
|
|
||||||
newColor = BLUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((newColor != lockedColor) || (lockedColor == OFF)) {
|
|
||||||
lockedColor = newColor;
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t _patternStayCnt() {
|
|
||||||
uint8_t s = 0;
|
|
||||||
|
|
||||||
if (temperature < TEMPERATURE_THRESHOLD) {
|
|
||||||
s = TEMPERATURE_THRESHOLD - temperature;
|
|
||||||
} else if (temperature > (TEMPERATURE_THRESHOLD * 2)) {
|
|
||||||
s = 0;
|
|
||||||
} else {
|
|
||||||
s = (TEMPERATURE_THRESHOLD * 2) - temperature;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s == 0) {
|
|
||||||
s = 1;
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
void patternExec() {
|
void patternExec() {
|
||||||
static uint8_t state = 0;
|
static uint8_t state = 0;
|
||||||
static uint8_t column = 0;
|
static uint8_t column = 0;
|
||||||
static uint8_t stay = 0;
|
static uint8_t delay = 0;
|
||||||
static uint8_t stayCnt = 0;
|
static uint8_t stayCnt = 0;
|
||||||
static uint8_t baseRow = 0;
|
static uint8_t baseRow = 0;
|
||||||
|
static tColor lockedColor = OFF;
|
||||||
|
tColor newColor;
|
||||||
|
|
||||||
|
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 0:
|
case 0:
|
||||||
// initialize
|
if (temperature < TEMPERATURE_THRESHOLD) {
|
||||||
|
delay = TEMPERATURE_THRESHOLD - temperature;
|
||||||
|
newColor = BLUE;
|
||||||
|
} else if (temperature > (TEMPERATURE_THRESHOLD * 2)) {
|
||||||
|
delay = 0;
|
||||||
|
newColor = RED;
|
||||||
|
} else {
|
||||||
|
delay = (TEMPERATURE_THRESHOLD * 2) - temperature;
|
||||||
|
newColor = RED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (delay == 0) {
|
||||||
|
delay = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((newColor != lockedColor) || (lockedColor == OFF)) {
|
||||||
|
lockedColor = newColor;
|
||||||
|
state = 1;
|
||||||
|
} else {
|
||||||
|
state = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// initialize at start or at color change
|
||||||
for (uint8_t c = 0; c <= 3; c++) {
|
for (uint8_t c = 0; c <= 3; c++) {
|
||||||
for (uint8_t r = 0; r <= 3; r++) {
|
for (uint8_t r = 0; r <= 3; r++) {
|
||||||
ledSetMatrix(c, r, OFF);
|
ledSetMatrix(c, r, OFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_patternChangeColor();
|
|
||||||
stay = _patternStayCnt();
|
|
||||||
|
|
||||||
baseRow = (lockedColor == RED) ? 3 : 0;
|
baseRow = (lockedColor == RED) ? 3 : 0;
|
||||||
|
|
||||||
ledSetMatrix(0, baseRow, lockedColor);
|
ledSetMatrix(0, baseRow, lockedColor);
|
||||||
@ -140,58 +131,61 @@ void patternExec() {
|
|||||||
ledSetMatrix(2, baseRow, lockedColor);
|
ledSetMatrix(2, baseRow, lockedColor);
|
||||||
ledSetMatrix(3, baseRow, lockedColor);
|
ledSetMatrix(3, baseRow, lockedColor);
|
||||||
|
|
||||||
state = 1;
|
state = 2;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 2:
|
||||||
// select new column, row 2
|
// select new column, row 2
|
||||||
column = myrand() & 0x3;
|
column = myrand() & 0x3;
|
||||||
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor);
|
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor);
|
||||||
stayCnt = 0;
|
stayCnt = 0;
|
||||||
state = 2;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
// stay
|
|
||||||
if (stayCnt >= stay) {
|
|
||||||
state = 3;
|
state = 3;
|
||||||
}
|
|
||||||
stayCnt++;
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
|
// stay
|
||||||
|
if (stayCnt >= delay) {
|
||||||
|
state = 4;
|
||||||
|
} else {
|
||||||
|
stayCnt++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
// same column, row 1
|
// same column, row 1
|
||||||
ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, lockedColor);
|
ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, lockedColor);
|
||||||
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, OFF);
|
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, OFF);
|
||||||
stayCnt = 0;
|
stayCnt = 0;
|
||||||
state = 4;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
// stay
|
|
||||||
if (stayCnt >= stay) {
|
|
||||||
state = 5;
|
state = 5;
|
||||||
}
|
|
||||||
stayCnt++;
|
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
|
// stay
|
||||||
|
if (stayCnt >= delay) {
|
||||||
|
state = 6;
|
||||||
|
} else {
|
||||||
|
stayCnt++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
// same column, row 0
|
// same column, row 0
|
||||||
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, lockedColor);
|
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, lockedColor);
|
||||||
ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, OFF);
|
ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, OFF);
|
||||||
stayCnt = 0;
|
stayCnt = 0;
|
||||||
state = 6;
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
// stay
|
|
||||||
if (stayCnt >= stay) {
|
|
||||||
state = 7;
|
state = 7;
|
||||||
}
|
|
||||||
stayCnt++;
|
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
|
// stay
|
||||||
|
if (stayCnt >= delay) {
|
||||||
|
state = 8;
|
||||||
|
} else {
|
||||||
|
stayCnt++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
// same column, row 0 off
|
// same column, row 0 off
|
||||||
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF);
|
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF);
|
||||||
state = _patternChangeColor() ? 0 : 1;
|
state = 0;
|
||||||
stay = _patternStayCnt();
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
state = 0;
|
state = 0;
|
||||||
|
lockedColor = OFF;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user