This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
SummerSchool2-Controller/306-controller_interface.X/middleware/alive.c

282 lines
7.2 KiB
C
Raw Permalink Normal View History

/**
* @author R<EFBFBD>mi Heredero
* @version 1.0.0
* @date August 2023
* @file alive.c
*/
#include "alive.h"
2023-09-05 06:52:27 +00:00
void ALIVE_init(ALIVE* me, uint8_t led){
me->state = STAL_INIT;
me->isAlive = false;
me->checker = false;
me->sender = false;
me->haveBreak = true;
me->aliveTime = 10;
me->setup.f = NULL;
me->born.f = NULL;
me->wait.f = NULL;
me->dead.f = NULL;
me->alive.f = NULL;
me->break_cb.f = NULL;
2023-09-05 06:52:27 +00:00
LED_init(&me->debugLed, led);
LED_initHW(&me->debugLed);
BLINKER_init(&me->debugBlinker);
BLINKER_setTimeOn(&me->debugBlinker, 50);
BLINKER_setTimeOff(&me->debugBlinker, 50);
BLINKER_onOn(&me->debugBlinker, LED_on, &me->debugLed);
BLINKER_onOff(&me->debugBlinker, LED_off, &me->debugLed);
}
void ALIVE_startBehaviourChecker(ALIVE* me){
2023-09-05 06:52:27 +00:00
BLINKER_startBehaviour(&me->debugBlinker);
POST(me, &ALIVE_processEvent, evALinitChecker, 10, 0);
}
void ALIVE_startBehaviourSender(ALIVE* me){
2023-09-05 06:52:27 +00:00
BLINKER_startBehaviour(&me->debugBlinker);
POST(me, &ALIVE_processEvent, evALinitSender, 10, 0);
}
bool ALIVE_processEvent(Event* ev) {
bool processed = false;
ALIVE* me = (ALIVE*)Event_getTarget(ev);
ALIVE_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
uint64_t data = Event_getData(ev);
switch (me->state) { // onState
case STAL_INIT:
2023-09-05 06:52:27 +00:00
LED_on(&me->debugLed);
if (ev->id == evALinitChecker) {
me->state = STAL_SETUP;
}
if (ev->id == evALinitSender) {
me->state = STAL_ALIVE;
ALIVE_emitPoll(me, me->aliveTime*10, 0);
}
break;
case STAL_SETUP:
if (ev->id == evALborn) {
me->state = STAL_BORN;
}
break;
case STAL_BORN:
if (ev->id == evALready) {
me->state = STAL_WAIT;
2023-09-06 19:03:23 +00:00
ALIVE_emitPoll(me, me->aliveTime*20, 0);
}
break;
case STAL_WAIT:
if (ev->id == evALpoll) {
if (me->aliveTime == 0) {
if (me->haveBreak){
me->state = STAL_BREAK;
}
} else if (me->isAlive){
me->state = STAL_WAIT;
2023-09-06 19:03:23 +00:00
ALIVE_emitPoll(me, me->aliveTime*20, 0);
} else {
me->state = STAL_DEAD;
}
me->isAlive = false;
}
break;
case STAL_DEAD:
2023-08-31 18:01:39 +00:00
if (ev->id == evALresurrect) {
me->state = STAL_SETUP;
}
break;
case STAL_ALIVE:
if (ev->id == evALpoll) {
if (me->alive.f != NULL) {
me->alive.f(me->alive.p);
}
if (me->aliveTime == 0) {
if (me->haveBreak){
2023-09-01 11:23:26 +00:00
me->state = STAL_BREAK;
}
} else {
2023-09-06 19:03:23 +00:00
ALIVE_emitPoll(me, me->aliveTime*20, 0);
}
}
break;
case STAL_BREAK:
if (ev->id == evALstart) {
ALIVE_emitPoll(me, me->aliveTime*10, 0);
if (me->checker) {
me->state = STAL_WAIT;
}
if (me->sender) {
me->state = STAL_ALIVE;
}
}
break;
}
if(oldState != me->state){
switch (oldState) { // onExit
case STAL_INIT:
break;
case STAL_SETUP:
break;
case STAL_BORN:
break;
case STAL_WAIT:
2023-09-05 06:52:27 +00:00
BLINKER_endBlink(&me->debugBlinker);
break;
case STAL_DEAD:
break;
case STAL_ALIVE:
2023-09-05 06:52:27 +00:00
BLINKER_endBlink(&me->debugBlinker);
break;
case STAL_BREAK:
break;
}
switch (me->state) { // onEntry
case STAL_INIT:
break;
case STAL_SETUP:
2023-09-01 14:32:37 +00:00
me->checker = true;
if (me->setup.f != NULL) {
me->setup.f(me->setup.p);
}
break;
case STAL_BORN:
if (me->born.f != NULL) {
me->born.f(me->born.p);
}
break;
case STAL_WAIT:
2023-09-05 06:52:27 +00:00
LED_off(&me->debugLed);
BLINKER_emitBlink(&me->debugBlinker, 0);
if (me->wait.f != NULL) {
me->wait.f(me->wait.p);
}
break;
case STAL_DEAD:
if (me->dead.f != NULL) {
me->dead.f(me->dead.p);
}
break;
case STAL_ALIVE:
2023-09-05 06:52:27 +00:00
LED_off(&me->debugLed);
BLINKER_emitBlink(&me->debugBlinker, 0);
2023-09-01 14:32:37 +00:00
me->sender = true;
break;
case STAL_BREAK:
if (me->break_cb.f != NULL) {
me->break_cb.f(me->break_cb.p);
}
break;
}
processed = true;
}
return processed;
}
/*************
* Callbacks *
*************/
void ALIVE_onSetup(ALIVE* me, ALIVE_CALLBACK_FUNCTION f, void* p) {
me->setup.f = f;
me->setup.p = p;
}
void ALIVE_onBorn(ALIVE* me, ALIVE_CALLBACK_FUNCTION f, void* p) {
me->born.f = f;
me->born.p = p;
}
void ALIVE_onWait(ALIVE* me, ALIVE_CALLBACK_FUNCTION f, void* p) {
me->wait.f = f;
me->wait.p = p;
}
void ALIVE_onDead(ALIVE* me, ALIVE_CALLBACK_FUNCTION f, void* p) {
me->dead.f = f;
me->dead.p = p;
}
void ALIVE_onAlive(ALIVE* me, ALIVE_CALLBACK_FUNCTION f, void* p) {
me->alive.f = f;
me->alive.p = p;
}
void ALIVE_onBreak(ALIVE* me, ALIVE_CALLBACK_FUNCTION f, void* p) {
me->break_cb.f = f;
me->break_cb.p = p;
}
/************
* EMITTERS *
************/
void ALIVE_emitInitSender(ALIVE* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_processEvent, evALinitSender, t, data);
}
void ALIVE_emitBorn(ALIVE* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_processEvent, evALborn, t, data);
}
void ALIVE_emitReady(ALIVE* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_processEvent, evALready, t, data);
}
void ALIVE_emitPoll(ALIVE* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_processEvent, evALpoll, t, data);
}
void ALIVE_emitStart(ALIVE* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_processEvent, evALstart, t, data);
}
2023-08-31 18:01:39 +00:00
void ALIVE_emitResurrect(ALIVE* me, uint16_t t, int64_t data) {
POST(me, &ALIVE_processEvent, evALresurrect, t, data);
}
/***********
* SETTERS *
***********/
void ALIVE_setIsAlive(ALIVE* me, bool v) {
me->isAlive = v;
}
void ALIVE_setHaveBreak(ALIVE* me, bool v) {
me->haveBreak = v;
}
void ALIVE_setAliveTime(ALIVE* me, uint8_t v) {
me->aliveTime = v;
}
void ALIVE_ISALIVE(ALIVE* me) {
ALIVE_setIsAlive(me, true);
}