diff --git a/306-controller_interface.X/app/factory/factory.c b/306-controller_interface.X/app/factory/factory.c index 2964f29..c3d018b 100644 --- a/306-controller_interface.X/app/factory/factory.c +++ b/306-controller_interface.X/app/factory/factory.c @@ -30,6 +30,10 @@ LED* l8() { return &theFactory.l8_; } +BLINKER* b1() { + return &theFactory.b1_; +} + ALIVE* ALcontroller(){ return &theFactory.ALcontroller_; } @@ -58,6 +62,8 @@ void Factory_init() { LED_initHW(l6()); LED_initHW(l7()); LED_initHW(l8()); + + BLINKER_init(b1()); CAN_init(); CAN_setSender(1); @@ -78,6 +84,9 @@ void Factory_build() { ALIVE_onAlive(ALcontroller(), CM_CONTROLLER_ALIVE, NULL); + BLINKER_onOn(b1(), LED_on, l6()); + BLINKER_onOff(b1(), LED_off, l6()); + ALIVE_onSetup(ALjoy(), CM_JOY_SETUP, NULL); ALIVE_setAliveTime(ALjoy(), KART_CST.JOYSTICK_ALIVE_TIME); ALIVE_onBorn(ALjoy(), LED_on, l1()); @@ -88,6 +97,9 @@ void Factory_build() { //start all state machines void Factory_start() { CAN_startBehaviour(); + + BLINKER_startBehaviour(b1()); + BLINKER_emitBlink(b1(), 0); ALIVE_startBehaviourSender(ALcontroller()); ALIVE_startBehaviourChecker(ALjoy()); diff --git a/306-controller_interface.X/app/factory/factory.h b/306-controller_interface.X/app/factory/factory.h index bcbc830..9835497 100644 --- a/306-controller_interface.X/app/factory/factory.h +++ b/306-controller_interface.X/app/factory/factory.h @@ -19,6 +19,7 @@ #include "../../middleware/alive.h" #include "../../middleware/can_interface.h" #include "../../middleware/eeprom.h" +#include "../../middleware/blinker.h" typedef struct { @@ -31,6 +32,8 @@ typedef struct { LED l7_; LED l8_; + BLINKER b1_; + ALIVE ALcontroller_; ALIVE ALjoy_; @@ -51,6 +54,8 @@ LED* l6(); LED* l7(); LED* l8(); +BLINKER* b1(); + ALIVE* ALcontroller(); ALIVE* ALjoy(); diff --git a/306-controller_interface.X/middleware/blinker.c b/306-controller_interface.X/middleware/blinker.c new file mode 100644 index 0000000..f8a948f --- /dev/null +++ b/306-controller_interface.X/middleware/blinker.c @@ -0,0 +1,189 @@ +/** + * @author Rémi Heredero + * @version 1.0.0 + * @date July 2023 + * @file blinker.c + */ + +#include "blinker.h" + +void BLINKER_init(BLINKER* me){ + me->state = STBL_INIT; + me->timeOn = 500; + me->timeOff = 500; + me->numberOfBlink = 3; + me->nBlinkIsOn = false; + me->remainBlinks = 3; + me->wait.f = NULL; + me->on.f = NULL; + me->off.f = NULL; + me->finished.f = NULL; +} + +void BLINKER_startBehaviour(BLINKER* me) { + POST(me, &BLINKER_processEvent, evBLinit, 0, 0); +} + +bool BLINKER_processEvent(Event* ev) { + bool processed = false; + BLINKER* me = (BLINKER*)Event_getTarget(ev); + BLINKER_STATES oldState = me->state; + evIDT evid = Event_getId(ev); + + switch (me->state) { // onState + case STBL_INIT: + if (ev->id == evBLinit) { + me->state = STBL_WAIT; + } + break; + + case STBL_WAIT: + me->remainBlinks = me->numberOfBlink; + if(evid == evBLblinkN) { + me->state = STBL_ON; + me->nBlinkIsOn = true; + BLINKER_emitTimer(me, me->timeOn); + } + if(evid == evBLblink) { + me->state = STBL_ON; + me->nBlinkIsOn = false; + BLINKER_emitTimer(me, me->timeOn); + } + break; + + case STBL_ON: + if (me->nBlinkIsOn) { + me->remainBlinks--; + } + if (evid == evBLtimer) { + me->state = STBL_OFF; + BLINKER_emitTimer(me, me->timeOff); + } + break; + + case STBL_OFF: + if (evid == evBLtimer) { + if (me->remainBlinks == 0) { + me->state = STBL_WAIT; + if (me->finished.f != NULL) { + me->finished.f(me->finished.p); + } + } else { + me->state = STBL_ON; + BLINKER_emitTimer(me, me->timeOn); + } + } + break; + } + + if(oldState != me->state){ + switch (oldState) { // onExit + case STBL_INIT: + break; + + case STBL_WAIT: + break; + + case STBL_ON: + break; + + case STBL_OFF: + break; + } + + switch (me->state) { // onEntry + case STBL_INIT: + break; + + case STBL_WAIT: + if (me->wait.f != NULL) { + me->wait.f(me->wait.p); + } + break; + + case STBL_ON: + if (me->on.f != NULL) { + me->on.f(me->on.p); + } + break; + + case STBL_OFF: + if (me->off.f != NULL) { + me->off.f(me->off.p); + } + break; + } + + processed = true; + } + return processed; +} + +/************* + * Callbacks * + *************/ + +void BLINKER_onWait(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p) { + me->wait.f = f; + me->wait.p = p; +} + +void BLINKER_onOn(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p) { + me->on.f = f; + me->on.p = p; +} + +void BLINKER_onOff(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p) { + me->off.f = f; + me->off.p = p; +} + +void BLINKER_onFinished(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p) { + me->finished.f = f; + me->finished.p = p; +} + +/************ + * EMITTERS * + ************/ + +void BLINKER_emitBlink(BLINKER* me, uint16_t t) { + POST(me, &BLINKER_processEvent, evBLblink, t, 0); +} + +void BLINKER_emitBlinkN(BLINKER* me, uint16_t t) { + POST(me, &BLINKER_processEvent, evBLblinkN, t, 0); +} + +void BLINKER_emitTimer(BLINKER* me, uint16_t t) { + POST(me, &BLINKER_processEvent, evBLtimer, t, 0); +} + +/*********** + * SETTERS * + ***********/ + +void BLINKER_setTimeOn(BLINKER* me, uint16_t v) { + me->timeOn = v; +} + +void BLINKER_setTimeOff(BLINKER* me, uint16_t v) { + me->timeOff = v; +} + +void BLINKER_setNumberOfBlink(BLINKER* me, uint8_t v) { + me->numberOfBlink = v; +} + +void BLINKER_setNBlinkIsOn(BLINKER* me, bool v) { + me->nBlinkIsOn = v; +} + +void BLINKER_setRemainBlinks(BLINKER* me, uint8_t v) { + me->remainBlinks = v; +} + + +void BLINKER_endBlink(BLINKER* me) { + me->remainBlinks = 0; +} diff --git a/306-controller_interface.X/middleware/blinker.h b/306-controller_interface.X/middleware/blinker.h new file mode 100644 index 0000000..4b2366e --- /dev/null +++ b/306-controller_interface.X/middleware/blinker.h @@ -0,0 +1,167 @@ +/** + * @author Rémi Heredero + * @version 1.0.0 + * @date July 2023 + * @file blinker.h + */ +#ifndef BLINKER_H +#define BLINKER_H + +#include "../xf/xf.h" + +typedef enum { + STBL_INIT, + STBL_WAIT, + STBL_ON, + STBL_OFF +} BLINKER_STATES; + +typedef enum { + evBLinit = 200, + evBLblink, + evBLblinkN, + evBLtimer +} BLINKER_EVENTS; + +typedef void (*BLINKER_CALLBACK_FUNCTION)(void*); +typedef struct { + BLINKER_CALLBACK_FUNCTION f; // function + void* p; // param(s) +} BLINKER_CALLBACK; + +typedef struct { + BLINKER_STATES state; //Actual state + uint16_t timeOn; // Time on + uint16_t timeOff; // Time off + uint8_t numberOfBlink; // Number of blink for this blinker when start with blinkN + bool nBlinkIsOn; // If the nBlink way is enable + uint8_t remainBlinks; // Actual remain blink + BLINKER_CALLBACK wait; + BLINKER_CALLBACK on; + BLINKER_CALLBACK off; + BLINKER_CALLBACK finished; +} BLINKER; + +/** + * Initialize the BLINKER + * @param me the BLINKER itself + */ +void BLINKER_init(BLINKER* me); + +/** + * Start the BLINKER state machine + * @param me the BLINKER itself + */ +void BLINKER_startBehaviour(BLINKER* me); + +/** + * Process the event + * @param ev the event to process + * @return true if the event is processed + */ +bool BLINKER_processEvent(Event* ev); + +/************* + * Callbacks * + *************/ + +/** + * Set the callback function to call when the BLINKER is entering state wait + * @param me the BLINKER itself + * @param f the function to call + * @param p the param(s) to pass to the function + */ +void BLINKER_onWait(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p); + +/** + * Set the callback function to call when the BLINKER is entering state on + * @param me the BLINKER itself + * @param f the function to call + * @param p the param(s) to pass to the function + */ +void BLINKER_onOn(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p); + +/** + * Set the callback function to call when the BLINKER is entering state off + * @param me the BLINKER itself + * @param f the function to call + * @param p the param(s) to pass to the function + */ +void BLINKER_onOff(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p); + +/** + * Set the callabck function to call when the BLINKER is entering state finished + * @param me the BLINKER itself + * @param f the function to call + * @param t the param(s) to pass to the function + */ +void BLINKER_onFinished(BLINKER* me, BLINKER_CALLBACK_FUNCTION f, void* p); + +/************ + * EMITTERS * + ************/ + +/** + * Emit the blink event + * @param me the BLINKER itself + * @param t time to wait in ms before triggering event + */void BLINKER_emitBlink(BLINKER* me, uint16_t t); + +/** + * Emit the blinkn event + * @param me the BLINKER itself + * @param t time to wait in ms before triggering event + */void BLINKER_emitBlinkN(BLINKER* me, uint16_t t); + +/** + * Emit the timer event + * @param me the BLINKER itself + * @param t time to wait in ms before triggering event + */void BLINKER_emitTimer(BLINKER* me, uint16_t t); + +/*********** + * SETTERS * + ***********/ + +/** + * Set the time on + * @param me the BLINKER itself + * @param v the value to set + */ +void BLINKER_setTimeOn(BLINKER* me, uint16_t v); + +/** + * Set the time off + * @param me the BLINKER itself + * @param v the value to set + */ +void BLINKER_setTimeOff(BLINKER* me, uint16_t v); + +/** + * Set the number of blink + * @param me the BLINKER itself + * @param v the value to set + */ +void BLINKER_setNumberOfBlink(BLINKER* me, uint8_t v); + +/** + * Set the nBlinkIsOn + * @param me the BLINKER itself + * @param v the value to set + */ +void BLINKER_setNBlinkIsOn(BLINKER* me, bool v); + +/** + * Set the remain blink(s) + * @param me the BLINKER itself + * @param v the value to set + */ +void BLINKER_setRemainBlinks(BLINKER* me, uint8_t v); + +/** + * Stop to blink if it was indefinitely blinking + * @param me the blinker itself + */ +void BLINKER_endBlink(BLINKER* me); + +#endif diff --git a/306-controller_interface.X/nbproject/configurations.xml b/306-controller_interface.X/nbproject/configurations.xml index 28707ef..c01e68d 100644 --- a/306-controller_interface.X/nbproject/configurations.xml +++ b/306-controller_interface.X/nbproject/configurations.xml @@ -27,6 +27,7 @@ middleware/can_interface.h middleware/alive.h + middleware/blinker.h xf/event.h @@ -63,6 +64,7 @@ middleware/can_interface.c middleware/alive.c + middleware/blinker.c xf/event.c @@ -214,6 +216,7 @@ +