From 8cc4c9089ff5ad5c95e9201c2ff2364032d407e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 14 Jun 2023 14:27:57 +0200 Subject: [PATCH] add clickHandler --- 02-UML/ST_BUTTON.uxf | 134 ++++++++------- 02-UML/ST_CLICK_HANDLER.uxf | 155 ++++++++++++++++++ .../kb28/blinkerProject/app/factory/factory.c | 47 ++++-- .../kb28/blinkerProject/app/factory/factory.h | 10 +- .../kb28/blinkerProject/board/button/button.c | 4 +- .../kb28/blinkerProject/board/button/button.h | 28 +++- .../ch/kb28/blinkerProject/board/led/led.c | 20 +-- .../ch/kb28/blinkerProject/board/led/led.h | 4 +- .../blinkerProject/middleware/clickHandler.c | 152 +++++++++++++++++ .../blinkerProject/middleware/clickHandler.h | 55 +++++++ 03-software/nbproject/configurations.xml | 5 +- 11 files changed, 513 insertions(+), 101 deletions(-) create mode 100644 02-UML/ST_CLICK_HANDLER.uxf create mode 100644 03-software/ch/kb28/blinkerProject/middleware/clickHandler.c create mode 100644 03-software/ch/kb28/blinkerProject/middleware/clickHandler.h diff --git a/02-UML/ST_BUTTON.uxf b/02-UML/ST_BUTTON.uxf index 7dc6a10..ae28134 100644 --- a/02-UML/ST_BUTTON.uxf +++ b/02-UML/ST_BUTTON.uxf @@ -1,13 +1,13 @@ - 14 + 20 UMLSpecialState - 546 - 140 - 28 - 28 + 1160 + 200 + 40 + 40 type=initial @@ -15,10 +15,10 @@ UMLState - 210 - 364 - 196 - 56 + 680 + 520 + 280 + 80 ST_PBPRESSED @@ -26,10 +26,10 @@ UMLState - 714 - 364 - 196 - 56 + 1400 + 520 + 280 + 80 ST_PBRELEASED @@ -37,10 +37,10 @@ Relation - 798 - 406 - 126 - 168 + 1520 + 580 + 180 + 240 lt=-> evPBPoll @@ -49,10 +49,10 @@ evPBPoll Relation - 546 - 154 - 126 - 126 + 1160 + 220 + 180 + 180 lt=-> evPBInit @@ -61,10 +61,10 @@ evPBInit UMLSpecialState - 532 - 252 - 56 - 56 + 1140 + 360 + 80 + 80 type=decision @@ -72,10 +72,10 @@ evPBInit Relation - 294 - 266 - 266 - 126 + 800 + 380 + 380 + 180 lt=-> m1=[pB==1] @@ -84,10 +84,10 @@ m1=[pB==1] Relation - 294 - 406 - 126 - 126 + 800 + 580 + 180 + 180 lt=-> evPBPoll @@ -96,10 +96,10 @@ evPBPoll Relation - 574 - 266 - 266 - 126 + 1200 + 380 + 380 + 180 lt=-> m1=[pB==0] @@ -108,10 +108,10 @@ m1=[pB==0] UMLSpecialState - 280 - 504 - 56 - 56 + 780 + 720 + 80 + 80 type=decision @@ -119,10 +119,10 @@ m1=[pB==0] UMLSpecialState - 784 - 546 - 56 - 56 + 1500 + 780 + 80 + 80 type=decision @@ -130,10 +130,10 @@ m1=[pB==0] Relation - 350 - 308 - 462 - 308 + 880 + 440 + 660 + 440 lt=-> m1=[pB==1] / pbPress() @@ -142,10 +142,10 @@ m1=[pB==1] / pbPress() Relation - 322 - 308 - 462 - 266 + 840 + 440 + 660 + 380 lt=-> @@ -155,10 +155,10 @@ m1=[pB==0] / pbRelease() Relation - 154 - 308 - 154 - 266 + 600 + 440 + 220 + 380 lt=-> m1=[else] @@ -167,13 +167,25 @@ m1=[else] Relation - 826 - 308 - 154 - 308 + 1560 + 440 + 220 + 440 lt=-> m1=[else] 10.0;190.0;90.0;190.0;90.0;10.0;30.0;10.0;30.0;40.0 + + UMLNote + + 1000 + 880 + 400 + 100 + + pbRelease() & pbPress() +are CallBack Functions + + diff --git a/02-UML/ST_CLICK_HANDLER.uxf b/02-UML/ST_CLICK_HANDLER.uxf new file mode 100644 index 0000000..aa595a9 --- /dev/null +++ b/02-UML/ST_CLICK_HANDLER.uxf @@ -0,0 +1,155 @@ + + + 20 + + UMLSpecialState + + 700 + 20 + 40 + 40 + + type=initial + + + + Relation + + 700 + 40 + 180 + 180 + + lt=-> +evCHInit + 10.0;10.0;10.0;70.0 + + + UMLState + + 580 + 180 + 280 + 80 + + ST_WAIT + + + + Relation + + 700 + 240 + 200 + 180 + + lt=-> +evPBPress + 10.0;10.0;10.0;70.0 + + + Relation + + 700 + 440 + 240 + 180 + + lt=-> +evPBRelease + 10.0;10.0;10.0;70.0 + + + UMLState + + 540 + 380 + 360 + 80 + + ST_LONG_CLICK + + + + UMLState + + 540 + 580 + 360 + 80 + + ST_SINGLE_CLICK + + + + UMLState + + 540 + 780 + 360 + 80 + + ST_DOUBLE_CLICK + + + + Relation + + 700 + 640 + 200 + 180 + + lt=-> +evPBPress + 10.0;10.0;10.0;70.0 + + + Relation + + 840 + 200 + 560 + 280 + + lt=-> +m1=evCHtimer / chLongClick() + 30.0;110.0;260.0;110.0;260.0;10.0;10.0;10.0 + + + Relation + + 840 + 200 + 560 + 480 + + lt=-> +m1=evCHtimer / chSingleClick() + 30.0;210.0;260.0;210.0;260.0;10.0;10.0;10.0 + + + Relation + + 840 + 200 + 560 + 660 + + lt=-> +m1=evCHtimer / chDoubleClick() + 30.0;300.0;260.0;300.0;260.0;10.0;10.0;10.0 + + + Relation + + 840 + 200 + 560 + 700 + + lt=-> +m1=evPBRelease / chDoubleClick() + 30.0;320.0;260.0;320.0;260.0;10.0;10.0;10.0 + + diff --git a/03-software/ch/kb28/blinkerProject/app/factory/factory.c b/03-software/ch/kb28/blinkerProject/app/factory/factory.c index 6980348..9af49b6 100755 --- a/03-software/ch/kb28/blinkerProject/app/factory/factory.c +++ b/03-software/ch/kb28/blinkerProject/app/factory/factory.c @@ -47,6 +47,16 @@ BUTTON* b3() { return &theFactory.b3_; } +CLICK_HANDLER* ch1() { + return &theFactory.ch1_; +} +CLICK_HANDLER* ch2() { + return &theFactory.ch2_; +} +CLICK_HANDLER* ch3() { + return &theFactory.ch3_; +} + LEDBlinker* lb() { return &theFactory.lb_; @@ -69,6 +79,10 @@ void Factory_init() { BUTTON_init(b2(), 2); BUTTON_init(b3(), 3); + CLICK_HANDLER_init(ch1(), b1()); + CLICK_HANDLER_init(ch2(), b2()); + CLICK_HANDLER_init(ch3(), b3()); + LED_initHW(l1()); LED_initHW(l2()); @@ -86,30 +100,29 @@ void Factory_init() { BUTTON_initHW(b3()); } -//typedef void (*fButtonCallback)(void*); -buttonCallBack myFunction; - -void lON(void * l){ - LED* l_ = (LED*)l; - LED_on(l_); -} - - -void lOFF(void* l) { - LED* l_ = (LED*)l; - LED_off(l_); -} - - //connect objects if required void Factory_build() { - BUTTON_setEventFunctions(b1(), BUTTON_defineCallBack(&lON, l1()), BUTTON_defineCallBack(NULL, NULL)); - BUTTON_setEventFunctions(b3(), BUTTON_defineCallBack(&lOFF, l1()), BUTTON_defineCallBack(NULL, NULL)); + //BUTTON_setEventFunctions(b1(), BUTTON_defineCallBack(&LED_on, l1()), BUTTON_defineCallBack(NULL, NULL)); + //BUTTON_setEventFunctions(b3(), BUTTON_defineCallBack(&LED_off, l1()), BUTTON_defineCallBack(NULL, NULL)); + + + CLICK_HANDLER_setSingleClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&LED_on, l1())); + CLICK_HANDLER_setSingleClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&LED_on, l3())); + + CLICK_HANDLER_setDoubleClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&LED_off, l1())); + CLICK_HANDLER_setDoubleClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&LED_off, l3())); + + CLICK_HANDLER_setLongClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&LED_on, l2())); + CLICK_HANDLER_setLongClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&LED_off, l2())); } //start all state machines void Factory_start() { //LEDBlinker_startBehaviour(lb()); BUTTON_startBehaviour(b1()); + BUTTON_startBehaviour(b2()); BUTTON_startBehaviour(b3()); + CLICK_HANDLER_startBehaviour(ch1()); + CLICK_HANDLER_startBehaviour(ch2()); + CLICK_HANDLER_startBehaviour(ch3()); } diff --git a/03-software/ch/kb28/blinkerProject/app/factory/factory.h b/03-software/ch/kb28/blinkerProject/app/factory/factory.h index baed0e7..b58dbdc 100755 --- a/03-software/ch/kb28/blinkerProject/app/factory/factory.h +++ b/03-software/ch/kb28/blinkerProject/app/factory/factory.h @@ -10,8 +10,7 @@ #include "../../board/led/led.h" #include "../../board/button/button.h" #include "../ledblinker.h" -#define LID 1 - +#include "../../middleware/clickHandler.h" typedef struct { LED l1_; @@ -27,6 +26,9 @@ typedef struct { BUTTON b1_; BUTTON b2_; BUTTON b3_; + CLICK_HANDLER ch1_; + CLICK_HANDLER ch2_; + CLICK_HANDLER ch3_; LEDBlinker lb_; } Factory; @@ -52,6 +54,10 @@ BUTTON* b1(); BUTTON* b2(); BUTTON* b3(); +CLICK_HANDLER* ch1(); +CLICK_HANDLER* ch2(); +CLICK_HANDLER* ch3(); + LEDBlinker* lb(); #endif \ No newline at end of file diff --git a/03-software/ch/kb28/blinkerProject/board/button/button.c b/03-software/ch/kb28/blinkerProject/board/button/button.c index d9a4d59..e1fef5e 100644 --- a/03-software/ch/kb28/blinkerProject/board/button/button.c +++ b/03-software/ch/kb28/blinkerProject/board/button/button.c @@ -1,8 +1,8 @@ /** * @file button.c * @author Rémi Heredero (remi@heredero.ch) - * @version 0.1 - * @date 2023-06-06 + * @version 1 + * @date 2023-06-13 * */ diff --git a/03-software/ch/kb28/blinkerProject/board/button/button.h b/03-software/ch/kb28/blinkerProject/board/button/button.h index 3061c36..02bc92f 100644 --- a/03-software/ch/kb28/blinkerProject/board/button/button.h +++ b/03-software/ch/kb28/blinkerProject/board/button/button.h @@ -1,8 +1,8 @@ /** * @file button.h * @author Rémi Heredero (remi@heredero.ch) - * @version 0.1 - * @date 2023-06-06 + * @version 1 + * @date 2023-06-13 * */ #ifndef BUTTON_H @@ -30,13 +30,33 @@ typedef struct { buttonCallBack release; } BUTTON; - +/** + * @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); + +/** + * @brief Initialize the hardware of the button + * + * @param me The object to initialize + */ void BUTTON_initHW(BUTTON* me); + +/** + * @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 + */ +void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack release); bool BUTTON_isPressed(BUTTON* me); void BUTTON_startBehaviour(BUTTON* me); bool BUTTON_processEvent(Event* ev); -void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack release); buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param); #endif /* BUTTON_H */ \ No newline at end of file diff --git a/03-software/ch/kb28/blinkerProject/board/led/led.c b/03-software/ch/kb28/blinkerProject/board/led/led.c index 0ca2d90..a202097 100644 --- a/03-software/ch/kb28/blinkerProject/board/led/led.c +++ b/03-software/ch/kb28/blinkerProject/board/led/led.c @@ -1,8 +1,7 @@ #include "led.h" #include "../../mcc_generated_files/pin_manager.h" -void LED_init(LED* me, uint8_t id) -{ +void LED_init(LED* me, uint8_t id) { me->id = id; } @@ -10,15 +9,13 @@ void LED_init(LED* me, uint8_t id) * @brief Initialize the Driver * */ -void LED_initHW(LED* me) -{ +void LED_initHW(LED* me) { LED_off(me); } -void LED_on(LED* me) -{ - switch (me->id) - { +void LED_on(void* me) { + LED* l = (LED*) me; + switch (l->id) { case 1: OUTPUT1_SetHigh(); break; @@ -53,10 +50,9 @@ void LED_on(LED* me) } -void LED_off(LED* me) -{ - switch (me->id) - { +void LED_off(void* me) { + LED* l = (LED*) me; + switch (l->id) { case 1: OUTPUT1_SetLow(); break; diff --git a/03-software/ch/kb28/blinkerProject/board/led/led.h b/03-software/ch/kb28/blinkerProject/board/led/led.h index e05b2b7..cd7be86 100644 --- a/03-software/ch/kb28/blinkerProject/board/led/led.h +++ b/03-software/ch/kb28/blinkerProject/board/led/led.h @@ -19,8 +19,8 @@ typedef struct LED_ LED; void LED_init(LED* me, uint8_t id); void LED_initHW(LED* me); -void LED_on(LED* me); -void LED_off(LED* me); +void LED_on(void* me); +void LED_off(void* me); #endif /* LED_H */ diff --git a/03-software/ch/kb28/blinkerProject/middleware/clickHandler.c b/03-software/ch/kb28/blinkerProject/middleware/clickHandler.c new file mode 100644 index 0000000..63542db --- /dev/null +++ b/03-software/ch/kb28/blinkerProject/middleware/clickHandler.c @@ -0,0 +1,152 @@ +/** + * @file clickHandler.c + * @author Rémi Heredero (remi@heredero.ch) + * @version 0.1 + * @date 2023-06-13 + */ + +#include "clickHandler.h" + +void CLICK_HANDLER_BUTTON_PRESS(CLICK_HANDLER* me){ + POST(me, &CLICK_HANDLER_processEvent, evCHpbPress, 0, 0); +} + +void CLICK_HANDLER_BUTTON_RELEASE(CLICK_HANDLER * me) { + POST(me, &CLICK_HANDLER_processEvent, evCHpbRelease, 0, 0); +} + +void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b){ + me->button = b; + me->state = STCH_INIT; + me->longClick.fCallBack = NULL; + me->singleClick.fCallBack = NULL; + me->doubleClick.fCallBack = NULL; + + buttonCallBack pbPressCallBack; + pbPressCallBack.fCallBack = CLICK_HANDLER_BUTTON_PRESS; + pbPressCallBack.param = me; + + buttonCallBack pbReleaseCallBack; + pbReleaseCallBack.fCallBack = CLICK_HANDLER_BUTTON_RELEASE; + pbReleaseCallBack.param = me; + + BUTTON_setEventFunctions(b, pbPressCallBack, pbReleaseCallBack); +} + +void CLICK_HANDLER_startBehaviour(CLICK_HANDLER* me){ + POST(me, &CLICK_HANDLER_processEvent, evCHinit, 0, 0); +} + +clickHandlerCallBack CLICK_HANDLER_defineCallBack(fClickHandlerCallBack f, void* param) { + clickHandlerCallBack c; + c.fCallBack = f; + c.param = param; + return c; +} + +void CLICK_HANDLER_setLongClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack) { + me->longClick = callBack; +} + +void CLICK_HANDLER_setSingleClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack) { + me->singleClick = callBack; +} + +void CLICK_HANDLER_setDoubleClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack) { + me->doubleClick = callBack; +} + +bool CLICK_HANDLER_processEvent(Event* ev) { + bool processed = false; + CLICK_HANDLER* me = (CLICK_HANDLER*)Event_getTarget(ev); + CLICK_HANDLER_STATES oldState = me->state; + evIDT evid = Event_getId(ev); + + switch(me->state) { + case STCH_INIT: + if(evid == evCHinit) { + me->state = STCH_WAIT; + } + break; + + case STCH_WAIT: + + if(evid == evCHpbPress) { + me->state = STCH_LONG_CLICK; + POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0); + } + + break; + + + case STCH_LONG_CLICK: + + if(evid == evCHpbRelease) { + me->state = STCH_SINGLE_CLICK; + } + + if(evid == evCHtimer) { + me->state = STCH_WAIT; + if(me->longClick.fCallBack != NULL) { + me->longClick.fCallBack(me->longClick.param); + } + } + + break; + + + case STCH_SINGLE_CLICK: + + if(evid == evCHpbPress) { + me->state = STCH_DOUBLE_CLICK; + } + + if(evid == evCHtimer) { + me->state = STCH_WAIT; + if(me->singleClick.fCallBack != NULL) { + me->singleClick.fCallBack(me->singleClick.param); + } + } + + break; + + + case STCH_DOUBLE_CLICK: + + if(evid == evCHpbRelease){ + me->state = STCH_WAIT; + if(me->doubleClick.fCallBack != NULL){ + me->doubleClick.fCallBack(me->doubleClick.param); + } + } + + if(evid == evCHtimer) { + me->state = STCH_WAIT; + if(me->doubleClick.fCallBack != NULL) { + me->doubleClick.fCallBack(me->doubleClick.param); + } + } + + break; + + } + + if(oldState != me->state) { + switch(oldState) { + case STCH_INIT: + break; + + case STCH_WAIT: + break; + + case STCH_LONG_CLICK: + break; + + case STCH_SINGLE_CLICK: + break; + + case STCH_DOUBLE_CLICK: + break; + } + } +} diff --git a/03-software/ch/kb28/blinkerProject/middleware/clickHandler.h b/03-software/ch/kb28/blinkerProject/middleware/clickHandler.h new file mode 100644 index 0000000..4f219db --- /dev/null +++ b/03-software/ch/kb28/blinkerProject/middleware/clickHandler.h @@ -0,0 +1,55 @@ +/** + * @file clickHandler.h + * @author Rémi Heredero (remi@heredero.ch) + * @version 0.1 + * @date 2023-06-13 + */ + +#ifndef CLICKHANDLER_H +#define CLICKHANDLER_H + +#include "../xf/xf.h" +#include "../board/button/button.h" + +#define CH_CLICK_TIMER 250 + +typedef enum { + STCH_INIT, + STCH_WAIT, + STCH_LONG_CLICK, + STCH_SINGLE_CLICK, + STCH_DOUBLE_CLICK +} CLICK_HANDLER_STATES; + +typedef enum { + evCHinit = 150, + evCHtimer, + evCHpbPress, + evCHpbRelease +} CLICK_HANDLER_EVENTS; + +typedef void (*fClickHandlerCallBack)(void*); +typedef struct { + fClickHandlerCallBack fCallBack; + void* param; +} clickHandlerCallBack; + +typedef struct { + BUTTON* button; + CLICK_HANDLER_STATES state; + clickHandlerCallBack longClick; + clickHandlerCallBack singleClick; + clickHandlerCallBack doubleClick; +}CLICK_HANDLER; + +void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b); +clickHandlerCallBack CLICK_HANDLER_defineCallBack(fClickHandlerCallBack f, void* param); +void CLICK_HANDLER_startBehaviour(CLICK_HANDLER* me); +void CLICK_HANDLER_setLongClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack); +void CLICK_HANDLER_setSingleClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack); +void CLICK_HANDLER_setDoubleClickCallback(CLICK_HANDLER* me, clickHandlerCallBack callBack); +bool CLICK_HANDLER_processEvent(Event* ev); + + +#endif /* CLICKHANDLER_H */ + diff --git a/03-software/nbproject/configurations.xml b/03-software/nbproject/configurations.xml index 73bef19..571e132 100644 --- a/03-software/nbproject/configurations.xml +++ b/03-software/nbproject/configurations.xml @@ -28,6 +28,7 @@ ch/kb28/blinkerProject/mcc_generated_files/device_config.h + ch/kb28/blinkerProject/middleware/clickHandler.h ch/kb28/blinkerProject/xf/ireactive.h @@ -67,6 +68,7 @@ ch/kb28/blinkerProject/mcc_generated_files/tmr0.c + ch/kb28/blinkerProject/middleware/clickHandler.c ch/kb28/blinkerProject/xf/xf.c @@ -93,7 +95,7 @@ PICkit3PlatformTool XC8 2.41 - 3 + 2 @@ -215,6 +217,7 @@ +