Initial commit
This commit is contained in:
66
306-controller_interface.X/board/button/button.c
Normal file
66
306-controller_interface.X/board/button/button.c
Normal file
@ -0,0 +1,66 @@
|
||||
|
||||
#include "button.h"
|
||||
#include "../../mcc_generated_files/pin_manager.h"
|
||||
|
||||
void Button_init(Button* me, uint8_t id, bool isPullUp)
|
||||
{
|
||||
me->id = id;
|
||||
me->isPullUp = isPullUp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize the Driver
|
||||
*
|
||||
*/
|
||||
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)
|
||||
{
|
||||
case 1:
|
||||
value = IO_RA7_GetValue();
|
||||
break;
|
||||
case 2:
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
25
306-controller_interface.X/board/button/button.h
Normal file
25
306-controller_interface.X/board/button/button.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef Button_ONCE
|
||||
#define Button_ONCE
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
* this is the declaration of the Button class
|
||||
*/
|
||||
|
||||
struct Button_
|
||||
{
|
||||
uint8_t id;
|
||||
bool isPullUp;
|
||||
};
|
||||
|
||||
typedef struct Button_ Button;
|
||||
|
||||
void Button_init(Button* me, uint8_t id, bool isPullUp);
|
||||
void Button_initHW(Button* me);
|
||||
uint8_t Button_read(Button* me);
|
||||
void Button_setId(Button* me, uint8_t id);
|
||||
uint8_t Button_getId(Button* me);
|
||||
|
||||
#endif
|
129
306-controller_interface.X/board/button/buttonsm.c
Normal file
129
306-controller_interface.X/board/button/buttonsm.c
Normal file
@ -0,0 +1,129 @@
|
||||
#include "buttonsm.h"
|
||||
|
||||
/*
|
||||
* this is the init method of the ButtonSM class
|
||||
*/
|
||||
void ButtonSM_init(ButtonSM* me, Button* button)
|
||||
{
|
||||
me->state = ST_BSMINIT;
|
||||
me->button = button;
|
||||
|
||||
me->actualState = ST_BSMINIT;
|
||||
me->observer = NULL;
|
||||
me->observerCB = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* this is the state machine method of the ButtonSM class
|
||||
*/
|
||||
bool ButtonSM_processEvent(Event* ev)
|
||||
{
|
||||
ButtonSM* me = (ButtonSM*)ev->target;
|
||||
bool processed = false;
|
||||
BSMState oldState = me->state;
|
||||
|
||||
switch (me->state)
|
||||
{
|
||||
case ST_BSMINIT:
|
||||
if (Event_getId(ev) == evBSMInit)
|
||||
{
|
||||
me->state = ST_BSMWAIT;
|
||||
}
|
||||
break;
|
||||
case ST_BSMWAIT:
|
||||
if (Event_getId(ev) == evBSMPollTM)
|
||||
{
|
||||
me->state = ST_BSMPOLL;
|
||||
}
|
||||
break;
|
||||
case ST_BSMPOLL:
|
||||
if (Event_getId(ev) == evBSMDefault)
|
||||
{
|
||||
if (Button_read(me->button)==HIGH)
|
||||
{
|
||||
me->state = ST_BSMPRESSED;
|
||||
}
|
||||
else
|
||||
{
|
||||
me->state = ST_BSMRELEASED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST_BSMPRESSED:
|
||||
if (Event_getId(ev) == evBSMDefault)
|
||||
{
|
||||
me->state = ST_BSMWAIT;
|
||||
}
|
||||
break;
|
||||
case ST_BSMRELEASED:
|
||||
if (Event_getId(ev) == evBSMDefault)
|
||||
{
|
||||
me->state = ST_BSMWAIT;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (oldState != me->state)
|
||||
{
|
||||
processed = true;
|
||||
switch (me->state)
|
||||
{
|
||||
case ST_BSMINIT:
|
||||
break;
|
||||
case ST_BSMWAIT:
|
||||
POST(me, &ButtonSM_processEvent, evBSMPollTM,POLLTM,0);
|
||||
break;
|
||||
case ST_BSMPOLL:
|
||||
POST(me, &ButtonSM_processEvent, evBSMDefault,0,0);
|
||||
break;
|
||||
case ST_BSMPRESSED:
|
||||
POST(me, &ButtonSM_processEvent, evBSMDefault,0,0);
|
||||
if (me->actualState != ST_BSMPRESSED)
|
||||
{
|
||||
if (me->observerCB != NULL)
|
||||
{
|
||||
me->observerCB(me->observer,Button_getId(me->button),true);
|
||||
me->actualState = ST_BSMPRESSED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST_BSMRELEASED:
|
||||
POST(me, &ButtonSM_processEvent, evBSMDefault,0,0);
|
||||
if (me->actualState != ST_BSMRELEASED)
|
||||
{
|
||||
if (me->observerCB != NULL)
|
||||
{
|
||||
me->observerCB(me->observer,Button_getId(me->button),false);
|
||||
me->actualState = ST_BSMRELEASED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
|
||||
/*
|
||||
* this is the start method for the
|
||||
* state machine of the ButtonSM class
|
||||
*/
|
||||
void ButtonSM_startBehaviour(ButtonSM* me)
|
||||
{
|
||||
POST(me, &ButtonSM_processEvent, evBSMInit,0,0);
|
||||
me->actualState = Button_read(me->button)==HIGH?ST_BSMPRESSED:ST_BSMRELEASED;
|
||||
}
|
||||
|
||||
/*
|
||||
* this is the method to set the object and the
|
||||
* call back method of the ButtonSM class
|
||||
* this method will be called whenever the
|
||||
* button changes its state
|
||||
* as parameters to the callback method will be passed
|
||||
* the object address, the button id and its state
|
||||
* if the call back method does not belong to a class,
|
||||
* then the object address must be set to NULL
|
||||
*/
|
||||
void ButtonSM_setObserver(ButtonSM* me, void* observer, buttonObserverCBT observerCB)
|
||||
{
|
||||
me->observer = observer;
|
||||
me->observerCB = observerCB;
|
||||
}
|
69
306-controller_interface.X/board/button/buttonsm.h
Normal file
69
306-controller_interface.X/board/button/buttonsm.h
Normal file
@ -0,0 +1,69 @@
|
||||
#ifndef BUTTONSM_DEF
|
||||
#define BUTTONSM_DEF
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../../xf/xf.h"
|
||||
#include "button.h"
|
||||
|
||||
/*
|
||||
* these are the events of the
|
||||
* button state machine
|
||||
* be sure to make the first event
|
||||
* in the enumeration different from 0
|
||||
*/
|
||||
typedef enum BSMEvent
|
||||
{
|
||||
evBSMInit = 10,
|
||||
evBSMDefault,
|
||||
evBSMPollTM
|
||||
} BSMEvent;
|
||||
|
||||
/*
|
||||
* these are the states of the
|
||||
* button state machine
|
||||
*/
|
||||
typedef enum BSMSTate_
|
||||
{
|
||||
ST_BSMINIT,
|
||||
ST_BSMWAIT,
|
||||
ST_BSMPOLL,
|
||||
ST_BSMPRESSED,
|
||||
ST_BSMRELEASED
|
||||
} BSMState;
|
||||
|
||||
/*
|
||||
* the associated button will be polled
|
||||
* each 50 ms. do not make this time
|
||||
* shorter than TICKINTERVAL
|
||||
*/
|
||||
#define POLLTM 50
|
||||
|
||||
/*
|
||||
* this is the prototype type of the callback function
|
||||
* that will be called when the associated button
|
||||
* changes from released to pressed or inverse.
|
||||
*/
|
||||
typedef void (*buttonObserverCBT)(void*, uint8_t, bool);
|
||||
|
||||
/*
|
||||
* this is the declaration of the ButtonSM class
|
||||
*/
|
||||
struct ButtonSM_
|
||||
{
|
||||
BSMState state;
|
||||
Button* button;
|
||||
BSMState actualState;
|
||||
|
||||
buttonObserverCBT observerCB;
|
||||
void* observer;
|
||||
};
|
||||
|
||||
typedef struct ButtonSM_ ButtonSM;
|
||||
|
||||
|
||||
void ButtonSM_init(ButtonSM* me, Button* button);
|
||||
void ButtonSM_startBehaviour(ButtonSM* me);
|
||||
bool ButtonSM_processEvent(Event* ev);
|
||||
void ButtonSM_setObserver(ButtonSM* me, void* observer, buttonObserverCBT observerCB);
|
||||
#endif
|
Reference in New Issue
Block a user