add blinker

This commit is contained in:
Rémi Heredero 2023-08-30 20:22:44 +02:00
parent 7550d1907b
commit 00130b03ee
5 changed files with 376 additions and 0 deletions

View File

@ -30,6 +30,10 @@ LED* l8() {
return &theFactory.l8_; return &theFactory.l8_;
} }
BLINKER* b1() {
return &theFactory.b1_;
}
ALIVE* ALcontroller(){ ALIVE* ALcontroller(){
return &theFactory.ALcontroller_; return &theFactory.ALcontroller_;
} }
@ -58,6 +62,8 @@ void Factory_init() {
LED_initHW(l6()); LED_initHW(l6());
LED_initHW(l7()); LED_initHW(l7());
LED_initHW(l8()); LED_initHW(l8());
BLINKER_init(b1());
CAN_init(); CAN_init();
CAN_setSender(1); CAN_setSender(1);
@ -78,6 +84,9 @@ void Factory_build() {
ALIVE_onAlive(ALcontroller(), CM_CONTROLLER_ALIVE, NULL); 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_onSetup(ALjoy(), CM_JOY_SETUP, NULL);
ALIVE_setAliveTime(ALjoy(), KART_CST.JOYSTICK_ALIVE_TIME); ALIVE_setAliveTime(ALjoy(), KART_CST.JOYSTICK_ALIVE_TIME);
ALIVE_onBorn(ALjoy(), LED_on, l1()); ALIVE_onBorn(ALjoy(), LED_on, l1());
@ -88,6 +97,9 @@ void Factory_build() {
//start all state machines //start all state machines
void Factory_start() { void Factory_start() {
CAN_startBehaviour(); CAN_startBehaviour();
BLINKER_startBehaviour(b1());
BLINKER_emitBlink(b1(), 0);
ALIVE_startBehaviourSender(ALcontroller()); ALIVE_startBehaviourSender(ALcontroller());
ALIVE_startBehaviourChecker(ALjoy()); ALIVE_startBehaviourChecker(ALjoy());

View File

@ -19,6 +19,7 @@
#include "../../middleware/alive.h" #include "../../middleware/alive.h"
#include "../../middleware/can_interface.h" #include "../../middleware/can_interface.h"
#include "../../middleware/eeprom.h" #include "../../middleware/eeprom.h"
#include "../../middleware/blinker.h"
typedef struct { typedef struct {
@ -31,6 +32,8 @@ typedef struct {
LED l7_; LED l7_;
LED l8_; LED l8_;
BLINKER b1_;
ALIVE ALcontroller_; ALIVE ALcontroller_;
ALIVE ALjoy_; ALIVE ALjoy_;
@ -51,6 +54,8 @@ LED* l6();
LED* l7(); LED* l7();
LED* l8(); LED* l8();
BLINKER* b1();
ALIVE* ALcontroller(); ALIVE* ALcontroller();
ALIVE* ALjoy(); ALIVE* ALjoy();

View File

@ -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;
}

View File

@ -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

View File

@ -27,6 +27,7 @@
<logicalFolder name="middleware" displayName="middleware" projectFiles="true"> <logicalFolder name="middleware" displayName="middleware" projectFiles="true">
<itemPath>middleware/can_interface.h</itemPath> <itemPath>middleware/can_interface.h</itemPath>
<itemPath>middleware/alive.h</itemPath> <itemPath>middleware/alive.h</itemPath>
<itemPath>middleware/blinker.h</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="xf" displayName="xf" projectFiles="true"> <logicalFolder name="xf" displayName="xf" projectFiles="true">
<itemPath>xf/event.h</itemPath> <itemPath>xf/event.h</itemPath>
@ -63,6 +64,7 @@
<logicalFolder name="middleware" displayName="middleware" projectFiles="true"> <logicalFolder name="middleware" displayName="middleware" projectFiles="true">
<itemPath>middleware/can_interface.c</itemPath> <itemPath>middleware/can_interface.c</itemPath>
<itemPath>middleware/alive.c</itemPath> <itemPath>middleware/alive.c</itemPath>
<itemPath>middleware/blinker.c</itemPath>
</logicalFolder> </logicalFolder>
<logicalFolder name="xf" displayName="xf" projectFiles="true"> <logicalFolder name="xf" displayName="xf" projectFiles="true">
<itemPath>xf/event.c</itemPath> <itemPath>xf/event.c</itemPath>
@ -214,6 +216,7 @@
<property key="debugoptions.debug-startup" value="Use system settings"/> <property key="debugoptions.debug-startup" value="Use system settings"/>
<property key="debugoptions.reset-behaviour" value="Use system settings"/> <property key="debugoptions.reset-behaviour" value="Use system settings"/>
<property key="debugoptions.useswbreakpoints" value="false"/> <property key="debugoptions.useswbreakpoints" value="false"/>
<property key="firmware.download.all" value="false"/>
<property key="hwtoolclock.frcindebug" value="false"/> <property key="hwtoolclock.frcindebug" value="false"/>
<property key="memories.aux" value="false"/> <property key="memories.aux" value="false"/>
<property key="memories.bootflash" value="true"/> <property key="memories.bootflash" value="true"/>