From 3f1ce81ad50ad21b55eeeef9bf63b59aff45cb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= <63239207+Klagarge@users.noreply.github.com> Date: Sun, 11 Jun 2023 22:33:09 +0200 Subject: [PATCH] push button for toggle led --- .../kb28/blinkerProject/app/factory/factory.c | 12 ++-- .../kb28/blinkerProject/app/factory/factory.h | 12 ++-- .../ch/kb28/blinkerProject/app/lebblinker.c | 4 +- .../ch/kb28/blinkerProject/app/ledblinker.h | 2 +- 03-software/ch/kb28/blinkerProject/app/main.c | 37 +---------- .../kb28/blinkerProject/board/button/button.c | 63 +++++++++++++++++++ .../kb28/blinkerProject/board/button/button.h | 7 ++- 03-software/nbproject/configurations.xml | 2 +- 8 files changed, 83 insertions(+), 56 deletions(-) diff --git a/03-software/ch/kb28/blinkerProject/app/factory/factory.c b/03-software/ch/kb28/blinkerProject/app/factory/factory.c index 79b4cb3..7330ef5 100755 --- a/03-software/ch/kb28/blinkerProject/app/factory/factory.c +++ b/03-software/ch/kb28/blinkerProject/app/factory/factory.c @@ -48,12 +48,8 @@ BUTTON* b3() { } -LEDBlinker* lb1() { - return &theFactory.lb1_; -} - -LEDBlinker* lb2() { - return &theFactory.lb2_; +LEDBlinker* lb() { + return &theFactory.lb_; } //initialize all objects @@ -97,6 +93,6 @@ void Factory_build() { //start all state machines void Factory_start() { - LEDBlinker_startBehaviour(lb1()); - LEDBlinker_startBehaviour(lb2()); + LEDBlinker_startBehaviour(lb()); + BUTTON_startBehaviour(b1()); } diff --git a/03-software/ch/kb28/blinkerProject/app/factory/factory.h b/03-software/ch/kb28/blinkerProject/app/factory/factory.h index 1b0ad58..1021168 100755 --- a/03-software/ch/kb28/blinkerProject/app/factory/factory.h +++ b/03-software/ch/kb28/blinkerProject/app/factory/factory.h @@ -13,7 +13,7 @@ #define LID 1 -struct Factory_ { +typedef struct { LED l1_; LED l2_; LED l3_; @@ -28,13 +28,10 @@ struct Factory_ { BUTTON b2_; BUTTON b3_; - LEDBlinker lb1_; - LEDBlinker lb2_; -}; + LEDBlinker lb_; +} Factory; -typedef struct Factory_ Factory; - void Factory_init(); void Factory_build(); void Factory_start(); @@ -55,7 +52,6 @@ BUTTON* b1(); BUTTON* b2(); BUTTON* b3(); -LEDBlinker* lb1(); -LEDBlinker* lb2(); +LEDBlinker* lb(); #endif \ No newline at end of file diff --git a/03-software/ch/kb28/blinkerProject/app/lebblinker.c b/03-software/ch/kb28/blinkerProject/app/lebblinker.c index c57b333..01d109d 100644 --- a/03-software/ch/kb28/blinkerProject/app/lebblinker.c +++ b/03-software/ch/kb28/blinkerProject/app/lebblinker.c @@ -42,12 +42,10 @@ bool LEDBlinker_processEvent(Event* ev) { break; case ST_LBOFF: LED_off(l1()); - LED_off(l2()); POST(me, &LEDBlinker_processEvent, evLBTMOn,TMOFF,0); break; case ST_LBON: - LED_on(l()); - LED_on(l2()); + LED_on(l1()); POST(me, &LEDBlinker_processEvent, evLBTMOff,TMON,0); break; default: diff --git a/03-software/ch/kb28/blinkerProject/app/ledblinker.h b/03-software/ch/kb28/blinkerProject/app/ledblinker.h index d0b6314..7512c4e 100644 --- a/03-software/ch/kb28/blinkerProject/app/ledblinker.h +++ b/03-software/ch/kb28/blinkerProject/app/ledblinker.h @@ -15,7 +15,7 @@ extern "C" { #include "../xf/xf.h" enum LBSTATES_ {ST_LBINIT, ST_LBOFF, ST_LBON}; - enum LBEVENTS_ {evLBInit=100, evLBTMOff, evLBTMOn}; + enum LBEVENTS_ {evLBInit=200, evLBTMOff, evLBTMOn}; typedef enum LBSTATES_ LBSTATES; typedef enum LBEVENTS_ LBEVENTS; diff --git a/03-software/ch/kb28/blinkerProject/app/main.c b/03-software/ch/kb28/blinkerProject/app/main.c index 9f552ac..1a5b342 100644 --- a/03-software/ch/kb28/blinkerProject/app/main.c +++ b/03-software/ch/kb28/blinkerProject/app/main.c @@ -5,20 +5,6 @@ #include "../xf/xf.h" #include "../board/button/button.h" -bool test(Event* ev); - -bool test(Event* ev) { - static bool on = true; - if (on == true){ - LED_on(l()); - on = false; - } else { - LED_off(l()); - on = true; - } - POST(NULL,test, 2, 500, 0); - return true; -} void main(void) @@ -36,30 +22,13 @@ void main(void) Factory_init(); Factory_build(); - //Factory_start(); + Factory_start(); - //TMR0_SetInterruptHandler(XF_decrementAndQueueTimers); + TMR0_SetInterruptHandler(XF_decrementAndQueueTimers); while (1) { - //XF_executeOnce(); - if (BUTTON_isPressed(b1())) { - LED_on(l3()); - } else { - LED_off(l3()); - } - - if (BUTTON_isPressed(b2())) { - LED_on(l2()); - } else { - LED_off(l2()); - } - - if (BUTTON_isPressed(b3())) { - LED_on(l1()); - } else { - LED_off(l1()); - } + XF_executeOnce(); } } \ 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 c3ec596..7b960c8 100644 --- a/03-software/ch/kb28/blinkerProject/board/button/button.c +++ b/03-software/ch/kb28/blinkerProject/board/button/button.c @@ -8,6 +8,8 @@ #include "button.h" #include "../../mcc_generated_files/pin_manager.h" +#include "../led/led.h" +#include "../../app/factory/factory.h" /** * @brief Initialize the button @@ -65,4 +67,65 @@ bool BUTTON_isPressed(BUTTON* me) { 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) { + if(BUTTON_isPressed(me)) { + me->state = ST_PBPRESSED; + } else { + me->state = ST_PBRELEASED; + } + POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); + } + break; + case ST_PBPOLL: + break; + case ST_PBRELEASED: + if(evid == evPBPoll) { + if(BUTTON_isPressed(me)) { + me->state = ST_PBPRESSED; + } + POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); + } + + break; + case ST_PBPRESSED: + if(evid == evPBPoll) { + if(!BUTTON_isPressed(me)){ + me->state = ST_PBRELEASED; + } + POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); + + } + break; + } + + if(oldState != me->state) { + switch(me->state){ + case ST_PBINIT: + break; + case ST_PBPOLL: + break; + case ST_PBRELEASED: + LED_off(l10()); + break; + case ST_PBPRESSED: + LED_on(l10()); + break; + } + processed = true; + } + return processed; } \ No newline at end of file diff --git a/03-software/ch/kb28/blinkerProject/board/button/button.h b/03-software/ch/kb28/blinkerProject/board/button/button.h index c3fc5a0..96bc106 100644 --- a/03-software/ch/kb28/blinkerProject/board/button/button.h +++ b/03-software/ch/kb28/blinkerProject/board/button/button.h @@ -10,9 +10,12 @@ #include #include +#include "../../xf/xf.h" + +#define PB_POLL_TIME 20 typedef enum {ST_PBINIT, ST_PBWAIT, ST_PBPOLL, ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES; -typedef enum {evPBInit, evPBPoll} BUTTON_EVENTS; +typedef enum {evPBInit=100, evPBPoll} BUTTON_EVENTS; typedef struct { uint8_t id; @@ -23,5 +26,7 @@ typedef struct { void BUTTON_init(BUTTON* me, uint8_t id); void BUTTON_initHW(BUTTON* me); bool BUTTON_isPressed(BUTTON* me); +void BUTTON_startBehaviour(BUTTON* me); +bool BUTTON_processEvent(Event* ev); #endif /* BUTTON_H */ \ No newline at end of file diff --git a/03-software/nbproject/configurations.xml b/03-software/nbproject/configurations.xml index 3205f56..73bef19 100644 --- a/03-software/nbproject/configurations.xml +++ b/03-software/nbproject/configurations.xml @@ -93,7 +93,7 @@ PICkit3PlatformTool XC8 2.41 - 2 + 3