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; | ||||
| } | ||||
| @@ -1,25 +1,97 @@ | ||||
| #ifndef Button_ONCE | ||||
| #define Button_ONCE | ||||
| /** | ||||
|  * @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" | ||||
|  | ||||
| /* | ||||
|  * this is the declaration of the Button class | ||||
| #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); | ||||
|  | ||||
| struct Button_ | ||||
| { | ||||
|   uint8_t id; | ||||
|   bool isPullUp; | ||||
| }; | ||||
| /** | ||||
|  * @brief Initialize the hardware of the button | ||||
|  *  | ||||
|  * @param me button itself | ||||
|  */ | ||||
| void BUTTON_initHW(BUTTON* me); | ||||
|  | ||||
| typedef struct Button_ Button; | ||||
| /** | ||||
|  * @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); | ||||
|  | ||||
| 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); | ||||
| /** | ||||
|  * @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); | ||||
|  | ||||
| #endif | ||||
| /** | ||||
|  * @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,129 +0,0 @@ | ||||
| #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; | ||||
| } | ||||
| @@ -1,69 +0,0 @@ | ||||
| #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