2023-08-23 11:45:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* @author R<EFBFBD>mi Heredero (remi@heredero.ch)
|
|
|
|
|
* @version. 1.0.0
|
|
|
|
|
* @date 2023-06-15
|
|
|
|
|
*/
|
2023-08-22 07:22:00 +00:00
|
|
|
|
|
|
|
|
|
#include "button.h"
|
|
|
|
|
#include "../../mcc_generated_files/pin_manager.h"
|
2023-08-23 11:45:34 +00:00
|
|
|
|
#include "../../app/factory/factory.h"
|
2023-08-22 07:22:00 +00:00
|
|
|
|
|
2023-08-23 11:45:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* @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) {
|
2023-08-22 07:22:00 +00:00
|
|
|
|
me->id = id;
|
2023-08-23 11:45:34 +00:00
|
|
|
|
me->state = ST_PBINIT;
|
|
|
|
|
me->press.fCallBack = NULL;
|
|
|
|
|
me->release.fCallBack = NULL;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-23 11:45:34 +00:00
|
|
|
|
* @brief Initialize the hardware of the button
|
2023-08-22 07:22:00 +00:00
|
|
|
|
*
|
2023-08-23 11:45:34 +00:00
|
|
|
|
* @param me The object to initialize
|
2023-08-22 07:22:00 +00:00
|
|
|
|
*/
|
2023-08-23 11:45:34 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-08-22 07:22:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 11:45:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* @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) {
|
2023-08-22 07:22:00 +00:00
|
|
|
|
case 1:
|
2023-08-23 11:45:34 +00:00
|
|
|
|
return INPUT1_GetValue();
|
|
|
|
|
break;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
case 2:
|
2023-08-23 11:45:34 +00:00
|
|
|
|
return INPUT2_GetValue();
|
|
|
|
|
break;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
case 3:
|
2023-08-23 11:45:34 +00:00
|
|
|
|
return INPUT3_GetValue();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
}
|
2023-08-23 11:45:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
}
|
2023-08-23 11:45:34 +00:00
|
|
|
|
return processed;
|
|
|
|
|
}
|
2023-08-22 07:22:00 +00:00
|
|
|
|
|
2023-08-23 11:45:34 +00:00
|
|
|
|
void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack fRelease) {
|
|
|
|
|
me->press = fPress;
|
|
|
|
|
me->release = fRelease;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 11:45:34 +00:00
|
|
|
|
buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param){
|
|
|
|
|
buttonCallBack c;
|
|
|
|
|
c.fCallBack = f;
|
|
|
|
|
c.param = param;
|
|
|
|
|
return c;
|
2023-08-22 07:22:00 +00:00
|
|
|
|
}
|