/** * @author Rémi Heredero * @version 1.0.0 * @date August 2023 * @file alive.c */ #include "alive.h" 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; 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){ BLINKER_startBehaviour(&me->debugBlinker); POST(me, &ALIVE_processEvent, evALinitChecker, 10, 0); } void ALIVE_startBehaviourSender(ALIVE* me){ 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: 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; 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; ALIVE_emitPoll(me, me->aliveTime*20, 0); } else { me->state = STAL_DEAD; } me->isAlive = false; } break; case STAL_DEAD: 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){ me->state = STAL_BREAK; } } else { 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: BLINKER_endBlink(&me->debugBlinker); break; case STAL_DEAD: break; case STAL_ALIVE: BLINKER_endBlink(&me->debugBlinker); break; case STAL_BREAK: break; } switch (me->state) { // onEntry case STAL_INIT: break; case STAL_SETUP: 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: 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: LED_off(&me->debugLed); BLINKER_emitBlink(&me->debugBlinker, 0); 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); } 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); }