1st version with c implementation of state engine
This commit is contained in:
113
src/LEDState.c
Normal file
113
src/LEDState.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* LEDState.c */
|
||||
|
||||
#include "LEDDefinition.h"
|
||||
#include "LEDState.h"
|
||||
#include "AnimationConfig.h"
|
||||
|
||||
|
||||
static LEDStateEngine theLEDStateEngine;
|
||||
|
||||
void LEDStateEngine_setNumberOfLeds(int numberOfLeds) {
|
||||
if (numberOfLeds < 1 || numberOfLeds > MAX_LEDS) {
|
||||
LOG(LL_ERROR, ("Invalid number of LEDs %d", numberOfLeds));
|
||||
numberOfLeds = MAX_LEDS;
|
||||
}
|
||||
theLEDStateEngine.numberOfLeds = numberOfLeds;
|
||||
}
|
||||
|
||||
void LEDStateEngine_init(int numberOfLeds) {
|
||||
LEDStateEngine_setNumberOfLeds(numberOfLeds);
|
||||
theLEDStateEngine.comment = "";
|
||||
for (int i=0; i<numberOfLeds; ++i) {
|
||||
LEDstate_init(i, LEDDefinition_get(i));
|
||||
}
|
||||
}
|
||||
|
||||
void LEDStateEngine_addComment(char *comment) {
|
||||
theLEDStateEngine.comment = comment;
|
||||
}
|
||||
|
||||
uint16_t LEDStateEngine_getNumberOfLeds() {
|
||||
return theLEDStateEngine.numberOfLeds;
|
||||
}
|
||||
|
||||
|
||||
void LEDStateEngine_tick() {
|
||||
for (int i=0; i<theLEDStateEngine.numberOfLeds; ++i) {
|
||||
LEDState_tick(i);
|
||||
}
|
||||
}
|
||||
|
||||
void LEDState_tick(int ledNum) {
|
||||
LEDState *state = &theLEDStateEngine.ledState[ledNum];
|
||||
if (/*state->index < 0 ||*/ state->index >= theLEDStateEngine.numberOfLeds)
|
||||
return; // ignore LED entries, that are not setup correctly
|
||||
state->currentTick++;
|
||||
if (state->currentTick == state->nextTick) {
|
||||
// perform/initiate next state change
|
||||
state->currentAnimationStep = AnimationConfig_getNextAnimationStepForLED(ledNum, state->currentAnimationStep);
|
||||
state->nextTick = state->currentTick + theAnimationConfig.animationStep[state->currentAnimationStep].durationTicks;
|
||||
state->mode = theAnimationConfig.animationStep[state->currentAnimationStep].ledMode;
|
||||
switch (state->mode) {
|
||||
case LEDMode_on:
|
||||
state->currentColor = state->onColor;
|
||||
break;
|
||||
case LEDMode_off:
|
||||
state->currentColor = offColor;
|
||||
break;
|
||||
default:
|
||||
state->currentColor = dimmedRedColor;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// do animations, if necessary
|
||||
}
|
||||
|
||||
LEDState *LEDStateEngine_getLedState(int n) {
|
||||
if (n>=0 && n<theLEDStateEngine.numberOfLeds)
|
||||
return &theLEDStateEngine.ledState[n];
|
||||
LOG(LL_ERROR, ("*** LEDStateEngine_getLedState -- invalid LED number given: %d", n));
|
||||
return &theLEDStateEngine.ledState[0];
|
||||
}
|
||||
|
||||
/* LEDState */
|
||||
static int verifyLedNumber(int n) {
|
||||
if (n < 0 || n >= theLEDStateEngine.numberOfLeds) {
|
||||
LOG(LL_ERROR, ("*** Invalid LED number %d, set to zero", n));
|
||||
n = 0;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void LEDstate_initDefault(int n, LEDMode defaultMode) {
|
||||
n = verifyLedNumber(n);
|
||||
LEDState *state = &theLEDStateEngine.ledState[n];
|
||||
state->index = n;
|
||||
state->mode = defaultMode;
|
||||
state->nextTick = 0;
|
||||
state->ledDefinition = LEDDefinition_get(n);
|
||||
}
|
||||
|
||||
void LEDstate_init(int n, LEDDefinition *ledDefinition) {
|
||||
n = verifyLedNumber(n);
|
||||
LEDstate_initDefault(n, LEDMode_off);
|
||||
LEDState *state = LEDStateEngine_getLedState(n);
|
||||
state->index = n;
|
||||
state->ledDefinition = ledDefinition;
|
||||
state->onColor = ledDefinition->onColor;
|
||||
}
|
||||
|
||||
|
||||
int LEDState_getLedRed(int ledNum) {
|
||||
LEDState *state = LEDStateEngine_getLedState(ledNum);
|
||||
return state->currentColor.red;
|
||||
}
|
||||
int LEDState_getLedGreen(int ledNum) {
|
||||
LEDState *state = LEDStateEngine_getLedState(ledNum);
|
||||
return state->currentColor.green;
|
||||
}
|
||||
int LEDState_getLedBlue(int ledNum) {
|
||||
LEDState *state = LEDStateEngine_getLedState(ledNum);
|
||||
return state->currentColor.blue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user