blinky1/pattern.c

197 lines
4.7 KiB
C

#include "pattern.h"
#include "led.h"
#include "PontCoopScheduler.h"
#include <stdlib.h>
#include <stdbool.h>
#include "myrand.h"
uint8_t temperature = 21;
const uint8_t TEMPERATURE_THRESHOLD = 25;
void patternSetTemperature(uint8_t t) {
temperature = t;
}
/*
* traversing the whole matrix
void patternExec() {
static uint8_t i = 0;
static uint8_t j = 0;
ledSetMatrix(i, j, OFF);
i++;
if (i > 3) {
i = 0;
j++;
if (j > 3) {
j = 0;
}
}
ledSetMatrix(i, j, BLUE);
}
*/
/*
void patternTestBlueExec() {
static uint8_t state = 0;
static uint8_t column = 0;
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
column = 0;
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;
break;
default:
state = 0;
break;
}
}
*/
void patternExec() {
static uint8_t state = 0;
static uint8_t column = 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:
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);
}
}
baseRow = (lockedColor == RED) ? 3 : 0;
ledSetMatrix(0, baseRow, lockedColor);
ledSetMatrix(1, baseRow, lockedColor);
ledSetMatrix(2, baseRow, lockedColor);
ledSetMatrix(3, baseRow, lockedColor);
state = 2;
break;
case 2:
// select new column, row 2
column = myrand() & 0x3;
ledSetMatrix(column, (lockedColor == RED) ? 2 : 1, lockedColor);
stayCnt = 0;
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 = 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 = 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 = 0;
break;
default:
state = 0;
lockedColor = OFF;
break;
}
}
void patternInit() {
schAdd(patternExec, NULL, 0, 100);
}