finish devleop can interface not yet tested
This commit is contained in:
@@ -1,66 +1,137 @@
|
||||
/**
|
||||
* @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/factory.h"
|
||||
|
||||
void Button_init(Button* me, uint8_t id, bool isPullUp)
|
||||
{
|
||||
/**
|
||||
* @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->isPullUp = isPullUp;
|
||||
me->state = ST_PBINIT;
|
||||
me->press.fCallBack = NULL;
|
||||
me->release.fCallBack = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize the Driver
|
||||
* @brief Initialize the hardware of the button
|
||||
*
|
||||
* @param me The object to initialize
|
||||
*/
|
||||
void Button_initHW(Button* me)
|
||||
{
|
||||
}
|
||||
|
||||
//read the state of the button
|
||||
//maybe you have to adjust the
|
||||
//low level calls
|
||||
uint8_t Button_read(Button* me)
|
||||
{
|
||||
uint8_t value = LOW;
|
||||
switch (me->id)
|
||||
{
|
||||
void BUTTON_initHW(BUTTON* me) {
|
||||
switch (me->id) {
|
||||
case 1:
|
||||
value = IO_RA7_GetValue();
|
||||
break;
|
||||
INPUT1_SetDigitalInput();
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
INPUT2_SetDigitalInput();
|
||||
break;
|
||||
case 3:
|
||||
break;
|
||||
case 4:
|
||||
break;
|
||||
case 5:
|
||||
break;
|
||||
case 6:
|
||||
break;
|
||||
case 7:
|
||||
break;
|
||||
case 8:
|
||||
break;
|
||||
case 9:
|
||||
break;
|
||||
case 10:
|
||||
break;
|
||||
INPUT3_SetDigitalInput();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (me->isPullUp == true)
|
||||
{
|
||||
value=value==LOW?HIGH:LOW;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
//id getter
|
||||
uint8_t Button_getId(Button* me)
|
||||
{
|
||||
return me->id;
|
||||
}
|
||||
|
||||
//id setter
|
||||
void Button_setId(Button* me, uint8_t id)
|
||||
{
|
||||
me->id = id;
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
Reference in New Issue
Block a user