From b1d19846a61d8db75df8529ef061a969797a269c Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 11 Feb 2019 13:25:01 +0100 Subject: [PATCH] fix pattern state engine --- pattern.c | 134 ++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 70 deletions(-) diff --git a/pattern.c b/pattern.c index 4b3b1b9..6a6bb4b 100644 --- a/pattern.c +++ b/pattern.c @@ -9,7 +9,8 @@ uint8_t temperature = 21; const uint8_t TEMPERATURE_THRESHOLD = 25; -tColor lockedColor = OFF; + + void patternSetTemperature(uint8_t t) { temperature = t; @@ -35,6 +36,7 @@ void patternExec() { } */ +/* void patternTestBlueExec() { static uint8_t state = 0; static uint8_t column = 0; @@ -76,63 +78,52 @@ void patternTestBlueExec() { 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() { static uint8_t state = 0; static uint8_t column = 0; - static uint8_t stay = 0; + static uint8_t delay = 0; static uint8_t stayCnt = 0; static uint8_t baseRow = 0; + static tColor lockedColor = OFF; + tColor newColor; + switch (state) { 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 r = 0; r <= 3; r++) { ledSetMatrix(c, r, OFF); } } - _patternChangeColor(); - stay = _patternStayCnt(); - baseRow = (lockedColor == RED) ? 3 : 0; ledSetMatrix(0, baseRow, lockedColor); @@ -140,58 +131,61 @@ void patternExec() { ledSetMatrix(2, baseRow, lockedColor); ledSetMatrix(3, baseRow, lockedColor); - state = 1; + state = 2; break; - case 1: + case 2: // select new column, row 2 column = myrand() & 0x3; ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor); stayCnt = 0; - state = 2; - break; - case 2: - // stay - if (stayCnt >= stay) { - state = 3; - } - stayCnt++; + state = 3; break; case 3: + // stay + if (stayCnt >= delay) { + state = 4; + } else { + stayCnt++; + } + break; + case 4: // same column, row 1 ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, lockedColor); ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, OFF); stayCnt = 0; - state = 4; - break; - case 4: - // stay - if (stayCnt >= stay) { - state = 5; - } - stayCnt++; + state = 5; break; case 5: + // stay + if (stayCnt >= delay) { + state = 6; + } else { + stayCnt++; + } + break; + case 6: // same column, row 0 ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, lockedColor); ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, OFF); stayCnt = 0; - state = 6; - break; - case 6: - // stay - if (stayCnt >= stay) { - state = 7; - } - stayCnt++; + state = 7; break; case 7: + // stay + if (stayCnt >= delay) { + state = 8; + } else { + stayCnt++; + } + break; + case 8: // same column, row 0 off ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF); - state = _patternChangeColor() ? 0 : 1; - stay = _patternStayCnt(); + state = 0; break; default: state = 0; + lockedColor = OFF; break; } }