blinky1/pattern.c

193 lines
4.4 KiB
C

#include "pattern.h"
#include "led.h"
#include "PontCoopScheduler.h"
#include <stdlib.h>
#include <stdbool.h>
#include "myrand.h"
uint8_t temperature = 20;
const uint8_t TEMPERATURE_THRESHOLD = 20;
tColor lockedColor = OFF;
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);
}
*/
/*
* BLUE
void patternExec() {
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);
srand(13);
state = 1;
break;
case 1:
// select new column, row 1
column = ((uint8_t) rand()) & 0x3;
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;
}
}
*/
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
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, (lockedColor == RED) ? 2 : 1, lockedColor);
stayCnt = 0;
state = 2;
break;
case 2:
// stay
if (stayCnt >= stay) {
state = 3;
}
stayCnt++;
break;
case 3:
// 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++;
break;
case 5:
// 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++;
break;
case 7:
// same column, row 0 off
ledSetMatrix(column, (lockedColor == RED) ? 0 : 3, OFF);
state = _patternChangeColor() ? 1 : 0;
stay = _patternStayCnt();
break;
default:
state = 0;
break;
}
}
void patternInit() {
schAdd(patternExec, NULL, 0, 100);
}