From 4e2a90e52fad71e30d727616ac25e81d4982190c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 4 Feb 2019 22:42:30 +0100 Subject: [PATCH] red/blue switch --- pattern.c | 80 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/pattern.c b/pattern.c index 8654b4e..1a8d963 100644 --- a/pattern.c +++ b/pattern.c @@ -2,11 +2,14 @@ #include "led.h" #include "PontCoopScheduler.h" #include +#include #include "myrand.h" uint8_t temperature = 20; +const uint8_t TEMPERATURE_THRESHOLD = 20; +tColor lockedColor = OFF; void patternSetTemperature(uint8_t 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() { static uint8_t state = 0; static uint8_t column = 0; static uint8_t stay = 0; + static uint8_t stayCnt = 0; + static uint8_t baseRow = 0; switch (state) { case 0: // initialize - ledSetMatrix(0, 3, RED); - ledSetMatrix(1, 3, RED); - ledSetMatrix(2, 3, RED); - ledSetMatrix(3, 3, RED); + for (uint8_t c = 0; c <= 3; c++) { + for (uint8_t r = 0; r <= 3; r++) { + ledSetMatrix(c, r, OFF); + } + } + + _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); state = 1; break; case 1: // select new column, row 2 column = myrand() & 0x3; - ledSetMatrix(column, 2, RED); - stay = myrand() & 0xf; + ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor); + stayCnt = 0; state = 2; break; case 2: // stay - if (stay == 0) { + if (stayCnt >= stay) { state = 3; } - stay--; + stayCnt++; break; case 3: // same column, row 1 - ledSetMatrix(column, 1, RED); - ledSetMatrix(column, 2, OFF); - stay = myrand() & 0xf; + ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, lockedColor); + ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, OFF); + stayCnt = 0; state = 4; break; case 4: // stay - if (stay == 0) { + if (stayCnt >= stay) { state = 5; } - stay--; + stayCnt++; break; case 5: // same column, row 0 - ledSetMatrix(column, 0, RED); - ledSetMatrix(column, 1, OFF); - stay = myrand() & 0xf; + ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, lockedColor); + ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, OFF); + stayCnt = 0; state = 6; break; case 6: // stay - if (stay == 0) { + if (stayCnt >= stay) { state = 7; } - stay--; + stayCnt++; break; case 7: // same column, row 0 off - ledSetMatrix(column, 0, OFF); - state = 1; + ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF); + state = _patternChangeColor() ? 1 : 0; + stay = _patternStayCnt(); break; default: state = 0;