add param for function pointer

This commit is contained in:
Rémi Heredero 2023-06-13 10:36:24 +02:00
parent 06ad7c948b
commit ffc2f67b53
3 changed files with 41 additions and 15 deletions

View File

@ -86,20 +86,30 @@ void Factory_init() {
BUTTON_initHW(b3());
}
void lON(){
LED_on(l9());
//typedef void (*fButtonCallback)(void*);
buttonCallBack myFunction;
void lON(void * l){
LED* l_ = (LED*)l;
LED_on(l_);
}
void lOFF() {
LED_off(l9());
void lOFF(void* l) {
LED* l_ = (LED*)l;
LED_off(l_);
}
//connect objects if required
void Factory_build() {
BUTTON_setEventFunctions(b1(), &lON, &lOFF);
BUTTON_setEventFunctions(b1(), BUTTON_defineCallBack(&lON, l1()), BUTTON_defineCallBack(NULL, NULL));
BUTTON_setEventFunctions(b3(), BUTTON_defineCallBack(&lOFF, l1()), BUTTON_defineCallBack(NULL, NULL));
}
//start all state machines
void Factory_start() {
LEDBlinker_startBehaviour(lb());
//LEDBlinker_startBehaviour(lb());
BUTTON_startBehaviour(b1());
BUTTON_startBehaviour(b3());
}

View File

@ -19,7 +19,8 @@
void BUTTON_init(BUTTON* me, uint8_t id) {
me->id = id;
me->state = ST_PBINIT;
me->press.fCallBack = NULL;
me->release.fCallBack = NULL;
}
/**
@ -114,10 +115,10 @@ bool BUTTON_processEvent(Event * ev) {
case ST_PBINIT:
break;
case ST_PBRELEASED:
if(me->fButtonRelease != NULL) me->fButtonRelease();
if(me->release.fCallBack != NULL) me->release.fCallBack(me->release.param);
break;
case ST_PBPRESSED:
if(me->fButtonPress != NULL) me->fButtonPress();
if(me->press.fCallBack != NULL) me->press.fCallBack(me->press.param);
break;
}
processed = true;
@ -125,7 +126,14 @@ bool BUTTON_processEvent(Event * ev) {
return processed;
}
void BUTTON_setEventFunctions(BUTTON* me, void (*fPress)(void), void (*fRelease)(void)) {
me->fButtonPress = fPress;
me->fButtonRelease = fRelease;
void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack fRelease) {
me->press = fPress;
me->release = fRelease;
}
buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param){
buttonCallBack c;
c.fCallBack = f;
c.param = param;
return c;
}

View File

@ -17,18 +17,26 @@
typedef enum {ST_PBINIT, ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES;
typedef enum {evPBInit=100, evPBPoll} BUTTON_EVENTS;
typedef void (*fButtonCallback)(void*);
typedef struct {
fButtonCallback fCallBack;
void* param;
} buttonCallBack;
typedef struct {
uint8_t id;
BUTTON_STATES state;
void (*fButtonPress)(void);
void (*fButtonRelease)(void);
buttonCallBack press;
buttonCallBack release;
} BUTTON;
void BUTTON_init(BUTTON* me, uint8_t id);
void BUTTON_initHW(BUTTON* me);
bool BUTTON_isPressed(BUTTON* me);
void BUTTON_startBehaviour(BUTTON* me);
bool BUTTON_processEvent(Event* ev);
void BUTTON_setEventFunctions(BUTTON* me, void (*fPress)(void), void (*fRelease)(void));
void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack release);
buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param);
#endif /* BUTTON_H */