add callbacks
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
|
||||
#include "../mcc_generated_files/mcc.h"
|
||||
#include "../app/factory/factory.h"
|
||||
#include "../app/factory.h"
|
||||
#include "../xf/xf.h"
|
||||
|
||||
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
/**
|
||||
* @author R<>mi Heredero (remi@heredero.ch)
|
||||
* @version. 1.0.0
|
||||
* @date 2023-06-15
|
||||
*/
|
||||
|
||||
#include "button.h"
|
||||
#include "../mcc_generated_files/pin_manager.h"
|
||||
#include "../app/factory.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize the button
|
||||
*
|
||||
* @param me The object to initialize
|
||||
* @param id The id of the button
|
||||
*/
|
||||
void BUTTON_init(BUTTON* me, uint8_t id) {
|
||||
me->id = id;
|
||||
me->state = ST_PBINIT;
|
||||
me->press.fCallBack = NULL;
|
||||
me->release.fCallBack = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize the hardware of the button
|
||||
*
|
||||
* @param me The object to initialize
|
||||
*/
|
||||
void BUTTON_initHW(BUTTON* me) {
|
||||
switch (me->id) {
|
||||
case 1:
|
||||
INPUT1_SetDigitalInput();
|
||||
break;
|
||||
case 2:
|
||||
INPUT2_SetDigitalInput();
|
||||
break;
|
||||
case 3:
|
||||
INPUT3_SetDigitalInput();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if the button is pressed
|
||||
* The function returns true if the button is pressed, false otherwise
|
||||
*
|
||||
* @param me The object to check
|
||||
* @return true if the button is pressed
|
||||
* @return false if the button is not pressed
|
||||
*/
|
||||
bool BUTTON_isPressed(BUTTON* me) {
|
||||
switch (me->id) {
|
||||
case 1:
|
||||
return INPUT1_GetValue();
|
||||
break;
|
||||
case 2:
|
||||
return INPUT2_GetValue();
|
||||
break;
|
||||
case 3:
|
||||
return INPUT3_GetValue();
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BUTTON_startBehaviour(BUTTON* me) {
|
||||
POST(me, &BUTTON_processEvent, evPBInit, 0, 0);
|
||||
}
|
||||
|
||||
bool BUTTON_processEvent(Event * ev) {
|
||||
bool processed = false;
|
||||
BUTTON* me = (BUTTON*)Event_getTarget(ev);
|
||||
BUTTON_STATES oldState = me->state;
|
||||
evIDT evid = Event_getId(ev);
|
||||
|
||||
switch(me->state){
|
||||
case ST_PBINIT:
|
||||
if (evid == evPBInit) {
|
||||
POST(me, &BUTTON_processEvent, evPBPoll, 0, 0);
|
||||
if(BUTTON_isPressed(me)) {
|
||||
me->state = ST_PBPRESSED;
|
||||
} else {
|
||||
me->state = ST_PBRELEASED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST_PBRELEASED:
|
||||
if(evid == evPBPoll) {
|
||||
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
|
||||
if(BUTTON_isPressed(me)) {
|
||||
me->state = ST_PBPRESSED;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case ST_PBPRESSED:
|
||||
if(evid == evPBPoll) {
|
||||
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
|
||||
if(!BUTTON_isPressed(me)){
|
||||
me->state = ST_PBRELEASED;
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if(oldState != me->state) {
|
||||
switch(me->state){
|
||||
case ST_PBINIT:
|
||||
break;
|
||||
case ST_PBRELEASED:
|
||||
if(me->release.fCallBack != NULL) me->release.fCallBack(me->release.param);
|
||||
break;
|
||||
case ST_PBPRESSED:
|
||||
if(me->press.fCallBack != NULL) me->press.fCallBack(me->press.param);
|
||||
break;
|
||||
}
|
||||
processed = true;
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* @author R<>mi Heredero (remi@heredero.ch)
|
||||
* @version. 1.0.0
|
||||
* @date 2023-06-15
|
||||
*/
|
||||
#ifndef BUTTON_H
|
||||
#define BUTTON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../xf/xf.h"
|
||||
|
||||
#define PB_POLL_TIME 20 // Poll time for BUTTON
|
||||
|
||||
typedef enum {
|
||||
ST_PBINIT,
|
||||
ST_PBRELEASED,
|
||||
ST_PBPRESSED
|
||||
} BUTTON_STATES;
|
||||
|
||||
typedef enum {
|
||||
evPBInit=50,
|
||||
evPBPoll
|
||||
} BUTTON_EVENTS;
|
||||
|
||||
// Calback function
|
||||
typedef void (*fButtonCallback)(void*);
|
||||
typedef struct {
|
||||
fButtonCallback fCallBack;
|
||||
void* param;
|
||||
} buttonCallBack;
|
||||
|
||||
typedef struct {
|
||||
uint8_t id; // Id of the button
|
||||
BUTTON_STATES state; // Actual state
|
||||
buttonCallBack press; // Callback for the rising edge of the button
|
||||
buttonCallBack release; // Callback for the falling edge of the button
|
||||
} BUTTON;
|
||||
|
||||
/**
|
||||
* @brief Initialize the button
|
||||
*
|
||||
* @param me button itself
|
||||
* @param id The id of the button
|
||||
*/
|
||||
void BUTTON_init(BUTTON* me, uint8_t id);
|
||||
|
||||
/**
|
||||
* @brief Initialize the hardware of the button
|
||||
*
|
||||
* @param me button itself
|
||||
*/
|
||||
void BUTTON_initHW(BUTTON* me);
|
||||
|
||||
/**
|
||||
* @brief Set both callback event functions
|
||||
*
|
||||
* @param me button itself
|
||||
* @param fPress callback function when the button have a rising edge
|
||||
* @param release callback function whent the have a falling edge
|
||||
*/
|
||||
void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack release);
|
||||
|
||||
/**
|
||||
* @brief Check if the button is pressed
|
||||
* The function returns true if the button is pressed, false otherwise
|
||||
*
|
||||
* @param me button itself
|
||||
* @return true if the button is pressed
|
||||
* @return false if the button is not pressed
|
||||
*/
|
||||
bool BUTTON_isPressed(BUTTON* me);
|
||||
|
||||
/**
|
||||
* @biref Start state machine of the BUTTON
|
||||
*
|
||||
* @param me the button itself
|
||||
*/
|
||||
void BUTTON_startBehaviour(BUTTON* me);
|
||||
|
||||
/**
|
||||
* @brief State machine of the BUTTON
|
||||
*
|
||||
* @param ev event to process on the state machine
|
||||
*/
|
||||
bool BUTTON_processEvent(Event* ev);
|
||||
|
||||
/**
|
||||
* @brief Define a callback for BUTTON
|
||||
*
|
||||
* @param f callback function
|
||||
* @param param callback parameter for the function
|
||||
* @return the callback struct
|
||||
*/
|
||||
buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param);
|
||||
|
||||
#endif /* BUTTON_H */
|
||||
@@ -1,17 +1,24 @@
|
||||
/**
|
||||
* @author R<>mi Heredero (remi@heredero.ch)
|
||||
* @author R<>mi Heredero (remi@heredero.ch)
|
||||
* @version. 1.0.0
|
||||
* @date 2023-06-15
|
||||
*/
|
||||
|
||||
#include "clickHandler.h"
|
||||
#include "../board/led.h"
|
||||
#include "../app/factory.h"
|
||||
|
||||
typedef struct {
|
||||
CLICK_HANDLER* me;
|
||||
uint16_t t;
|
||||
} foo;
|
||||
|
||||
void CLICK_HANDLER_BUTTON_PRESS(CLICK_HANDLER* me){
|
||||
POST(me, &CLICK_HANDLER_processEvent, evCHpbPress, 0, 0);
|
||||
}
|
||||
|
||||
void CLICK_HANDLER_BUTTON_RELEASE(CLICK_HANDLER * me) {
|
||||
POST(me, &CLICK_HANDLER_processEvent, evCHpbRelease, 0, 0);
|
||||
void CLICK_HANDLER_BUTTON_RELEASE(CLICK_HANDLER* me) {
|
||||
POST(me, &CLICK_HANDLER_processEvent, evCHpbRelease,0, 0);
|
||||
}
|
||||
|
||||
void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b){
|
||||
@@ -21,15 +28,8 @@ void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b){
|
||||
me->singleClick.fCallBack = NULL;
|
||||
me->doubleClick.fCallBack = NULL;
|
||||
|
||||
buttonCallBack pbPressCallBack;
|
||||
pbPressCallBack.fCallBack = CLICK_HANDLER_BUTTON_PRESS;
|
||||
pbPressCallBack.param = me;
|
||||
|
||||
buttonCallBack pbReleaseCallBack;
|
||||
pbReleaseCallBack.fCallBack = CLICK_HANDLER_BUTTON_RELEASE;
|
||||
pbReleaseCallBack.param = me;
|
||||
|
||||
BUTTON_setEventFunctions(b, pbPressCallBack, pbReleaseCallBack);
|
||||
BUTTON_onPress(b, CLICK_HANDLER_BUTTON_PRESS, me);
|
||||
BUTTON_onRelease(b, CLICK_HANDLER_BUTTON_RELEASE, me);
|
||||
}
|
||||
|
||||
void CLICK_HANDLER_startBehaviour(CLICK_HANDLER* me){
|
||||
@@ -71,8 +71,12 @@ bool CLICK_HANDLER_processEvent(Event* ev) {
|
||||
case STCH_WAIT:
|
||||
|
||||
if(evid == evCHpbPress) {
|
||||
me->state = STCH_LONG_CLICK;
|
||||
POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0);
|
||||
LED_on(l2());
|
||||
//me->state = STCH_LONG_CLICK;
|
||||
//POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0);
|
||||
}
|
||||
if(evid==evCHpbRelease) {
|
||||
LED_off(l2());
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -86,7 +90,9 @@ bool CLICK_HANDLER_processEvent(Event* ev) {
|
||||
|
||||
if(evid == evCHtimer) {
|
||||
me->state = STCH_WAIT;
|
||||
LED_on(l4());
|
||||
if(me->longClick.fCallBack != NULL) {
|
||||
LED_on(l5());
|
||||
me->longClick.fCallBack(me->longClick.param);
|
||||
}
|
||||
}
|
||||
@@ -97,10 +103,12 @@ bool CLICK_HANDLER_processEvent(Event* ev) {
|
||||
case STCH_SINGLE_CLICK:
|
||||
|
||||
if(evid == evCHpbPress) {
|
||||
LED_on(l2());
|
||||
me->state = STCH_DOUBLE_CLICK;
|
||||
}
|
||||
|
||||
if(evid == evCHtimer) {
|
||||
LED_on(l3());
|
||||
me->state = STCH_WAIT;
|
||||
if(me->singleClick.fCallBack != NULL) {
|
||||
me->singleClick.fCallBack(me->singleClick.param);
|
||||
|
||||
Reference in New Issue
Block a user