2019-02-02 16:54:24 +01:00
|
|
|
#include "pattern.h"
|
|
|
|
#include "led.h"
|
|
|
|
#include "PontCoopScheduler.h"
|
|
|
|
#include <stdlib.h>
|
2019-02-04 22:42:30 +01:00
|
|
|
#include <stdbool.h>
|
2019-02-03 23:19:09 +01:00
|
|
|
#include "myrand.h"
|
2019-02-02 16:54:24 +01:00
|
|
|
|
2019-02-04 10:03:10 +01:00
|
|
|
|
2019-02-09 17:54:18 +01:00
|
|
|
uint8_t temperature = 21;
|
2019-02-16 22:38:03 +01:00
|
|
|
const uint8_t TEMPERATURE_THRESHOLD = 25;
|
2019-02-04 10:03:10 +01:00
|
|
|
|
2019-02-11 13:25:01 +01:00
|
|
|
|
|
|
|
|
2019-02-04 10:03:10 +01:00
|
|
|
|
|
|
|
void patternSetTemperature(uint8_t t) {
|
|
|
|
temperature = t;
|
|
|
|
}
|
|
|
|
|
2019-02-03 22:34:29 +01:00
|
|
|
/*
|
|
|
|
* traversing the whole matrix
|
2019-02-02 16:54:24 +01:00
|
|
|
void patternExec() {
|
|
|
|
static uint8_t i = 0;
|
|
|
|
static uint8_t j = 0;
|
|
|
|
|
2019-02-02 18:03:04 +01:00
|
|
|
ledSetMatrix(i, j, OFF);
|
2019-02-02 16:54:24 +01:00
|
|
|
|
|
|
|
i++;
|
|
|
|
if (i > 3) {
|
|
|
|
i = 0;
|
|
|
|
j++;
|
|
|
|
if (j > 3) {
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 18:03:04 +01:00
|
|
|
ledSetMatrix(i, j, BLUE);
|
2019-02-02 16:54:24 +01:00
|
|
|
}
|
2019-02-03 22:34:29 +01:00
|
|
|
*/
|
|
|
|
|
2019-02-11 13:25:01 +01:00
|
|
|
/*
|
2019-02-09 17:54:18 +01:00
|
|
|
void patternTestBlueExec() {
|
2019-02-03 22:34:29 +01:00
|
|
|
static uint8_t state = 0;
|
|
|
|
static uint8_t column = 0;
|
2019-02-02 16:54:24 +01:00
|
|
|
|
2019-02-03 22:34:29 +01:00
|
|
|
switch (state) {
|
|
|
|
case 0:
|
|
|
|
// initialize
|
|
|
|
ledSetMatrix(0, 0, BLUE);
|
|
|
|
ledSetMatrix(1, 0, BLUE);
|
|
|
|
ledSetMatrix(2, 0, BLUE);
|
|
|
|
ledSetMatrix(3, 0, BLUE);
|
|
|
|
state = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// select new column, row 1
|
2019-02-09 17:54:18 +01:00
|
|
|
column = 0;
|
2019-02-03 22:34:29 +01:00
|
|
|
ledSetMatrix(column, 1, BLUE);
|
|
|
|
state = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// same column, row 2
|
|
|
|
ledSetMatrix(column, 2, BLUE);
|
|
|
|
ledSetMatrix(column, 1, OFF);
|
|
|
|
state = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// same column, row 3
|
|
|
|
ledSetMatrix(column, 3, BLUE);
|
|
|
|
ledSetMatrix(column, 2, OFF);
|
|
|
|
state = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
// same column, row 3 off
|
|
|
|
ledSetMatrix(column, 3, OFF);
|
|
|
|
state = 1;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
2019-02-03 22:34:29 +01:00
|
|
|
default:
|
|
|
|
state = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-02-11 13:25:01 +01:00
|
|
|
*/
|
2019-02-09 17:54:18 +01:00
|
|
|
|
2019-02-03 22:52:25 +01:00
|
|
|
|
|
|
|
void patternExec() {
|
|
|
|
static uint8_t state = 0;
|
|
|
|
static uint8_t column = 0;
|
2019-02-11 13:25:01 +01:00
|
|
|
static uint8_t delay = 0;
|
2019-02-04 22:42:30 +01:00
|
|
|
static uint8_t stayCnt = 0;
|
|
|
|
static uint8_t baseRow = 0;
|
2019-02-11 13:25:01 +01:00
|
|
|
static tColor lockedColor = OFF;
|
|
|
|
tColor newColor;
|
|
|
|
|
2019-02-03 22:52:25 +01:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 0:
|
2019-02-11 13:25:01 +01:00
|
|
|
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
|
2019-02-04 22:42:30 +01:00
|
|
|
for (uint8_t c = 0; c <= 3; c++) {
|
|
|
|
for (uint8_t r = 0; r <= 3; r++) {
|
|
|
|
ledSetMatrix(c, r, OFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
baseRow = (lockedColor == RED) ? 3 : 0;
|
|
|
|
|
|
|
|
ledSetMatrix(0, baseRow, lockedColor);
|
|
|
|
ledSetMatrix(1, baseRow, lockedColor);
|
|
|
|
ledSetMatrix(2, baseRow, lockedColor);
|
|
|
|
ledSetMatrix(3, baseRow, lockedColor);
|
2019-02-09 17:54:18 +01:00
|
|
|
|
2019-02-11 13:25:01 +01:00
|
|
|
state = 2;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 2:
|
2019-02-03 22:52:25 +01:00
|
|
|
// select new column, row 2
|
2019-02-03 23:19:09 +01:00
|
|
|
column = myrand() & 0x3;
|
2019-02-04 22:42:30 +01:00
|
|
|
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor);
|
|
|
|
stayCnt = 0;
|
2019-02-11 13:25:01 +01:00
|
|
|
state = 3;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 3:
|
2019-02-03 23:19:09 +01:00
|
|
|
// stay
|
2019-02-11 13:25:01 +01:00
|
|
|
if (stayCnt >= delay) {
|
|
|
|
state = 4;
|
|
|
|
} else {
|
|
|
|
stayCnt++;
|
2019-02-03 23:19:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 4:
|
2019-02-03 22:52:25 +01:00
|
|
|
// same column, row 1
|
2019-02-04 22:42:30 +01:00
|
|
|
ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, lockedColor);
|
|
|
|
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, OFF);
|
|
|
|
stayCnt = 0;
|
2019-02-11 13:25:01 +01:00
|
|
|
state = 5;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 5:
|
2019-02-03 23:19:09 +01:00
|
|
|
// stay
|
2019-02-11 13:25:01 +01:00
|
|
|
if (stayCnt >= delay) {
|
|
|
|
state = 6;
|
|
|
|
} else {
|
|
|
|
stayCnt++;
|
2019-02-03 23:19:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 6:
|
2019-02-03 22:52:25 +01:00
|
|
|
// same column, row 0
|
2019-02-04 22:42:30 +01:00
|
|
|
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, lockedColor);
|
|
|
|
ledSetMatrix(column, (lockedColor == RED) ? 1 : 2, OFF);
|
|
|
|
stayCnt = 0;
|
2019-02-11 13:25:01 +01:00
|
|
|
state = 7;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 7:
|
2019-02-03 23:19:09 +01:00
|
|
|
// stay
|
2019-02-11 13:25:01 +01:00
|
|
|
if (stayCnt >= delay) {
|
|
|
|
state = 8;
|
|
|
|
} else {
|
|
|
|
stayCnt++;
|
2019-02-03 23:19:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-02-11 13:25:01 +01:00
|
|
|
case 8:
|
2019-02-03 22:52:25 +01:00
|
|
|
// same column, row 0 off
|
2019-02-04 22:42:30 +01:00
|
|
|
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF);
|
2019-02-11 13:25:01 +01:00
|
|
|
state = 0;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = 0;
|
2019-02-11 13:25:01 +01:00
|
|
|
lockedColor = OFF;
|
2019-02-03 22:52:25 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-02 16:54:24 +01:00
|
|
|
|
|
|
|
void patternInit() {
|
2019-02-03 23:19:09 +01:00
|
|
|
schAdd(patternExec, NULL, 0, 100);
|
2019-02-02 16:54:24 +01:00
|
|
|
}
|