diff --git a/archives/button.c b/archives/button.c deleted file mode 100644 index bd3aafe..0000000 --- a/archives/button.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @author R�mi Heredero (remi@heredero.ch) - * @version. 1.0.0 - * @date 2023-06-15 - */ - -#include "button.h" -#include "../src/mcc_generated_files/pin_manager.h" -#include "../src/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; -} \ No newline at end of file diff --git a/archives/button.h b/archives/button.h deleted file mode 100644 index 6b2dcba..0000000 --- a/archives/button.h +++ /dev/null @@ -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 -#include -#include "../src/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 */ \ No newline at end of file diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 9e058c9..b239181 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -6,6 +6,7 @@ projectFiles="true"> src/app/factory.h + src/app/app.h src/board/led.h @@ -40,6 +41,7 @@ src/app/factory.c src/app/main.c + src/app/app.c src/board/led.c @@ -195,6 +197,9 @@ + + + diff --git a/src/app/app.c b/src/app/app.c new file mode 100644 index 0000000..9bb99ad --- /dev/null +++ b/src/app/app.c @@ -0,0 +1,216 @@ +/** + * @author R�mi Heredero (remi@heredero.ch) + * @version 1.0.0 + * @date 2023-06-15 + */ + +#include "app.h" +#include "factory.h" +#include "../middleware/blinker.h" +#include "../middleware/clickHandler.h" + +void clickL(APP* me){ + POST(me, &APP_processEvent, evAPclickL, 0, 0); +} +void clickLn(APP* me){ + POST(me, &APP_processEvent, evAPclickLn, 0, 0); +} +void clickR(APP* me){ + POST(me, &APP_processEvent, evAPclickR, 0, 0); +} +void clickRn(APP* me){ + POST(me, &APP_processEvent, evAPclickRn, 0, 0); +} +void endL(APP* me){ + POST(me, &APP_processEvent, evAPendL, 0, 0); +} +void endR(APP* me){ + POST(me, &APP_processEvent, evAPendR, 0, 0); +} +void longClick(APP* me){ + POST(me, &APP_processEvent, evAPlongClick, 0, 0); +} + + + +void APP_init(APP* me){ + + me->state = STAP_INIT; + + CLICK_HANDLER_setSingleClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&clickL, me)); + CLICK_HANDLER_setSingleClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&clickR, me)); + + CLICK_HANDLER_setDoubleClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&clickLn, me)); + CLICK_HANDLER_setDoubleClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&clickRn, me)); + + CLICK_HANDLER_setLongClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&longClick, me)); + CLICK_HANDLER_setLongClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&longClick, me)); + + BLINKER_setFinished(blL(), BLINKER_defineCallBack(&endL, me)); + BLINKER_setFinished(blR(), BLINKER_defineCallBack(&endR, me)); + +} + +void APP_startBehaviour(APP* me){ + POST(me, &APP_processEvent, evAPinit, 0, 0); +} + +bool APP_processEvent(Event* ev){ + bool processed = false; + APP* me = (APP*) Event_getTarget(ev); + APP_STATES oldState = me->state; + evIDT evid = Event_getId(ev); + + switch(me->state) { // In state + case STAP_INIT: + if(evid == evAPinit) { + me->state = STAP_WAIT; + } + break; + + case STAP_WAIT: + if(evid == evAPclickL) { + me->state = STAP_BLINKER_L; + BLINKER_blink(blL()); + } + if(evid == evAPclickLn) { + me->state = STAP_BLINKER_L; + BLINKER_blinkN(blL()); + } + if(evid == evAPclickR) { + me->state = STAP_BLINKER_R; + BLINKER_blink(blR()); + } + if(evid == evAPclickRn) { + me->state = STAP_BLINKER_R; + BLINKER_blinkN(blR()); + } + if(evid == evAPlongClick) { + me->state = STAP_BLINKER_W; + BLINKER_blink(blB()); + } + break; + + case STAP_BLINKER_L: + if(evid == evAPclickR){ + me->state = STAP_BLINKER_LR; + BLINKER_blink(blR()); + } + if(evid == evAPclickRn){ + me->state = STAP_BLINKER_LR; + BLINKER_blinkN(blR()); + } + if(evid == evAPclickL) { + me->state = STAP_WAIT; + BLINKER_endBlink(blL()); + } + if(evid == evAPendL) { + me->state = STAP_WAIT; + } + if(evid == evAPlongClick) { + me->state = STAP_BLINKER_W; + BLINKER_endBlink(blL()); + BLINKER_blink(blB()); + } + break; + + case STAP_BLINKER_R: + if(evid == evAPclickL){ + me->state = STAP_BLINKER_LR; + BLINKER_blink(blL()); + } + if(evid == evAPclickLn){ + me->state = STAP_BLINKER_LR; + BLINKER_blinkN(blL()); + } + if(evid == evAPclickR) { + me->state = STAP_WAIT; + BLINKER_endBlink(blR()); + } + if(evid == evAPendR) { + me->state = STAP_WAIT; + } + if(evid == evAPlongClick) { + me->state = STAP_BLINKER_W; + BLINKER_endBlink(blR()); + BLINKER_blink(blB()); + } + break; + + case STAP_BLINKER_LR: + if(evid == evAPclickR) { + me->state = STAP_BLINKER_L; + BLINKER_endBlink(blR()); + } + if(evid == evAPendR) { + me->state = STAP_BLINKER_L; + } + if(evid == evAPclickL) { + me->state = STAP_BLINKER_R; + BLINKER_endBlink(blL()); + } + if(evid == evAPendL) { + me->state = STAP_BLINKER_R; + } + if(evid == evAPlongClick) { + me->state = STAP_BLINKER_W; + BLINKER_endBlink(blL()); + BLINKER_endBlink(blR()); + BLINKER_blink(blB()); + } + break; + + case STAP_BLINKER_W: + if(evid == evAPlongClick){ + me->state = STAP_WAIT; + BLINKER_endBlink(blB()); + } + break; + + } + + if(oldState != me->state) { + switch(oldState) { // on exit + case STAP_INIT: + break; + + case STAP_WAIT: + break; + + case STAP_BLINKER_L: + break; + + case STAP_BLINKER_R: + break; + + case STAP_BLINKER_LR: + break; + + case STAP_BLINKER_W: + break; + } + + switch(me->state) { // on entry + case STAP_INIT: + break; + + case STAP_WAIT: + break; + + case STAP_BLINKER_L: + break; + + case STAP_BLINKER_R: + break; + + case STAP_BLINKER_LR: + break; + + case STAP_BLINKER_W: + break; + + } + processed = true; + } + return processed; +} diff --git a/src/app/app.h b/src/app/app.h new file mode 100644 index 0000000..90b8dec --- /dev/null +++ b/src/app/app.h @@ -0,0 +1,40 @@ +/** + * @author Rémi Heredero (remi@heredero.ch) + * @version 1.0.0 + * @date 2023-06-17 + */ +#ifndef APP_H +#define APP_H + +#include "../xf/xf.h" + +typedef enum { + STAP_INIT, + STAP_WAIT, + STAP_BLINKER_L, + STAP_BLINKER_R, + STAP_BLINKER_LR, + STAP_BLINKER_W +} APP_STATES; + +typedef enum { + evAPinit = 150, + evAPclickL, + evAPclickLn, + evAPclickR, + evAPclickRn, + evAPendL, + evAPendR, + evAPlongClick +} APP_EVENTS; + +typedef struct { + APP_STATES state; // Actual state +} APP; + +void APP_init(APP* me); +void APP_startBehaviour(APP* me); +bool APP_processEvent(Event* ev); + +#endif /* APP_H */ + diff --git a/src/app/factory.c b/src/app/factory.c index 5be4cff..86301f9 100755 --- a/src/app/factory.c +++ b/src/app/factory.c @@ -72,6 +72,10 @@ BLINKER* blB() { return &theFactory.blB_; } +APP* app() { + return &theFactory.app_; +} + //initialize all objects void Factory_init() { LED_init(l1(), 1); @@ -113,6 +117,7 @@ void Factory_init() { BLINKER_init(blR()); BLINKER_init(blB()); + APP_init(app()); } @@ -152,4 +157,5 @@ void Factory_start() { BLINKER_starBehaviour(blL()); BLINKER_starBehaviour(blR()); BLINKER_starBehaviour(blB()); -} + APP_startBehaviour(app()); +} \ No newline at end of file diff --git a/src/app/factory.h b/src/app/factory.h index cad067a..f65928f 100755 --- a/src/app/factory.h +++ b/src/app/factory.h @@ -14,6 +14,7 @@ #include "../board/button.h" #include "../middleware/clickHandler.h" #include "../middleware/blinker.h" +#include "app.h" typedef struct { LED l1_; @@ -35,6 +36,7 @@ typedef struct { BLINKER blL_; BLINKER blR_; BLINKER blB_; + APP app_; } Factory; @@ -66,5 +68,6 @@ BLINKER* blL(); BLINKER* blR(); BLINKER* blB(); +APP* app(); #endif \ No newline at end of file diff --git a/src/board/button.c b/src/board/button.c index 7156c98..60185d0 100644 --- a/src/board/button.c +++ b/src/board/button.c @@ -6,16 +6,13 @@ */ #include "button.h" -#include "../mcc_generated_files/pin_manager.h" -#include "../app/factory.h" -#include "led.h" -void BUTTON_init(BUTTON* me, uint8_t id) { +void BUTTON_init(BUTTON* me, uint8_t id){ me->id = id; me->state = STPB_INIT; - me->poll_time = 20; - me->press.f = NULL; - me->release.f = NULL; + me->pollTime = 20; + me->released.f = NULL; + me->pressed.f = NULL; } void BUTTON_initHW(BUTTON* me) { @@ -60,12 +57,12 @@ bool BUTTON_processEvent(Event* ev) { BUTTON* me = (BUTTON*)Event_getTarget(ev); BUTTON_STATES oldState = me->state; evIDT evid = Event_getId(ev); - + switch (me->state) { // onState case STPB_INIT: if (ev->id == evPBinit) { - POST(me, &BUTTON_processEvent, evPBpoll, me->poll_time, 0); - if(BUTTON_isPressed(me)){ + BUTTON_emitPoll(me, me->pollTime); + if (BUTTON_isPressed(me)) { me->state = STPB_PRESSED; } else { me->state = STPB_RELEASED; @@ -75,8 +72,8 @@ bool BUTTON_processEvent(Event* ev) { case STPB_RELEASED: if (ev->id == evPBpoll) { - POST(me, &BUTTON_processEvent, evPBpoll, me->poll_time, 0); - if(BUTTON_isPressed(me)){ + BUTTON_emitPoll(me, me->pollTime); + if (BUTTON_isPressed(me)) { me->state = STPB_PRESSED; } } @@ -84,8 +81,8 @@ bool BUTTON_processEvent(Event* ev) { case STPB_PRESSED: if (ev->id == evPBpoll) { - POST(me, &BUTTON_processEvent, evPBpoll, me->poll_time, 0); - if(!BUTTON_isPressed(me)){ + BUTTON_emitPoll(me, me->pollTime); + if (!BUTTON_isPressed(me)) { me->state = STPB_RELEASED; } } @@ -109,14 +106,14 @@ bool BUTTON_processEvent(Event* ev) { break; case STPB_RELEASED: - if (me->release.f != NULL) { - me->release.f(me->release.p); + if (me->released.f != NULL) { + me->released.f(me->released.p); } break; case STPB_PRESSED: - if (me->press.f != NULL) { - me->press.f(me->press.p); + if (me->pressed.f != NULL) { + me->pressed.f(me->pressed.p); } break; } @@ -130,14 +127,14 @@ bool BUTTON_processEvent(Event* ev) { * Callbacks * *************/ -void BUTTON_onPress(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p){ - me->press.f = f; - me->press.p = p; +void BUTTON_onReleased(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p) { + me->released.f = f; + me->released.p = p; } -void BUTTON_onRelease(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p){ - me->release.f = f; - me->release.p = p; +void BUTTON_onPressed(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p) { + me->pressed.f = f; + me->pressed.p = p; } /************ @@ -156,6 +153,6 @@ void BUTTON_setId(BUTTON* me, uint8_t v) { me->id = v; } -void BUTTON_setPoll_time(BUTTON* me, uint16_t v) { - me->poll_time = v; -} \ No newline at end of file +void BUTTON_setPollTime(BUTTON* me, uint8_t v) { + me->pollTime = v; +} diff --git a/src/board/button.h b/src/board/button.h index 0a01a54..25b311b 100644 --- a/src/board/button.h +++ b/src/board/button.h @@ -29,14 +29,15 @@ typedef struct { typedef struct { BUTTON_STATES state; uint8_t id; - uint16_t poll_time; - BUTTON_CALLBACK press; - BUTTON_CALLBACK release; + uint8_t pollTime; + BUTTON_CALLBACK released; + BUTTON_CALLBACK pressed; } BUTTON; /** * Initialize the BUTTON * @param me the BUTTON itself + * @param id the id of the BUTTON */ void BUTTON_init(BUTTON* me, uint8_t id); @@ -74,20 +75,20 @@ bool BUTTON_processEvent(Event* ev); *************/ /** - * Set the callback function to call when the BUTTON is pressed + * Set the callback function to call when the BUTTON is entering state released * @param me the BUTTON itself * @param f the function to call * @param p the param(s) to pass to the function */ -void BUTTON_onPress(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p); +void BUTTON_onReleased(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p); /** - * Set the callback function to call when the BUTTON is released + * Set the callback function to call when the BUTTON is entering state pressed * @param me the BUTTON itself * @param f the function to call * @param p the param(s) to pass to the function */ -void BUTTON_onRelease(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p); +void BUTTON_onPressed(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p); /************ * EMITTERS * @@ -115,6 +116,6 @@ void BUTTON_setId(BUTTON* me, uint8_t v); * @param me the BUTTON itself * @param v the poll time to set */ -void BUTTON_setPoll_time(BUTTON* me, uint16_t v); +void BUTTON_setPollTime(BUTTON* me, uint8_t v); #endif diff --git a/src/middleware/clickHandler.c b/src/middleware/clickHandler.c index 6659183..88178d6 100644 --- a/src/middleware/clickHandler.c +++ b/src/middleware/clickHandler.c @@ -28,8 +28,8 @@ void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b){ me->singleClick.fCallBack = NULL; me->doubleClick.fCallBack = NULL; - BUTTON_onPress(b, CLICK_HANDLER_BUTTON_PRESS, me); - BUTTON_onRelease(b, CLICK_HANDLER_BUTTON_RELEASE, me); + BUTTON_onPressed(b, CLICK_HANDLER_BUTTON_PRESS, me); + BUTTON_onReleased(b, CLICK_HANDLER_BUTTON_RELEASE, me); } void CLICK_HANDLER_startBehaviour(CLICK_HANDLER* me){ @@ -71,12 +71,8 @@ bool CLICK_HANDLER_processEvent(Event* ev) { case STCH_WAIT: if(evid == evCHpbPress) { - LED_on(l2()); - //me->state = STCH_LONG_CLICK; - //POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0); - } - if(evid==evCHpbRelease) { - LED_off(l2()); + me->state = STCH_LONG_CLICK; + POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0); } break; @@ -90,9 +86,7 @@ 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); } } @@ -103,12 +97,10 @@ 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);