red/blue switch

This commit is contained in:
2019-02-04 22:42:30 +01:00
parent d3e3967bee
commit 4e2a90e52f

View File

@ -2,11 +2,14 @@
#include "led.h" #include "led.h"
#include "PontCoopScheduler.h" #include "PontCoopScheduler.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include "myrand.h" #include "myrand.h"
uint8_t temperature = 20; uint8_t temperature = 20;
const uint8_t TEMPERATURE_THRESHOLD = 20;
tColor lockedColor = OFF;
void patternSetTemperature(uint8_t t) { void patternSetTemperature(uint8_t t) {
temperature = t; temperature = t;
@ -78,67 +81,104 @@ void patternExec() {
} }
*/ */
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() {
return temperature;
}
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 stay = 0;
static uint8_t stayCnt = 0;
static uint8_t baseRow = 0;
switch (state) { switch (state) {
case 0: case 0:
// initialize // initialize
ledSetMatrix(0, 3, RED); for (uint8_t c = 0; c <= 3; c++) {
ledSetMatrix(1, 3, RED); for (uint8_t r = 0; r <= 3; r++) {
ledSetMatrix(2, 3, RED); ledSetMatrix(c, r, OFF);
ledSetMatrix(3, 3, RED); }
}
_patternChangeColor();
stay = _patternStayCnt();
baseRow = (lockedColor == RED) ? 3 : 0;
ledSetMatrix(0, baseRow, lockedColor);
ledSetMatrix(1, baseRow, lockedColor);
ledSetMatrix(2, baseRow, lockedColor);
ledSetMatrix(3, baseRow, lockedColor);
srand(13); srand(13);
state = 1; state = 1;
break; break;
case 1: case 1:
// select new column, row 2 // select new column, row 2
column = myrand() & 0x3; column = myrand() & 0x3;
ledSetMatrix(column, 2, RED); ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor);
stay = myrand() & 0xf; stayCnt = 0;
state = 2; state = 2;
break; break;
case 2: case 2:
// stay // stay
if (stay == 0) { if (stayCnt >= stay) {
state = 3; state = 3;
} }
stay--; stayCnt++;
break; break;
case 3: case 3:
// same column, row 1 // same column, row 1
ledSetMatrix(column, 1, RED); ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, lockedColor);
ledSetMatrix(column, 2, OFF); ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, OFF);
stay = myrand() & 0xf; stayCnt = 0;
state = 4; state = 4;
break; break;
case 4: case 4:
// stay // stay
if (stay == 0) { if (stayCnt >= stay) {
state = 5; state = 5;
} }
stay--; stayCnt++;
break; break;
case 5: case 5:
// same column, row 0 // same column, row 0
ledSetMatrix(column, 0, RED); ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, lockedColor);
ledSetMatrix(column, 1, OFF); ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, OFF);
stay = myrand() & 0xf; stayCnt = 0;
state = 6; state = 6;
break; break;
case 6: case 6:
// stay // stay
if (stay == 0) { if (stayCnt >= stay) {
state = 7; state = 7;
} }
stay--; stayCnt++;
break; break;
case 7: case 7:
// same column, row 0 off // same column, row 0 off
ledSetMatrix(column, 0, OFF); ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF);
state = 1; state = _patternChangeColor() ? 1 : 0;
stay = _patternStayCnt();
break; break;
default: default:
state = 0; state = 0;