From 1b926c1ab96b4a461be1d1c2d903470b5ec921ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 6 Jun 2023 11:31:31 +0200 Subject: [PATCH] add buttons in Factory --- .../kb28/blinkerProject/app/factory/factory.c | 88 ++++++++++++++--- .../kb28/blinkerProject/app/factory/factory.h | 41 ++++++-- .../ch/kb28/blinkerProject/app/lebblinker.c | 27 ++---- 03-software/ch/kb28/blinkerProject/app/main.c | 30 ++++-- .../kb28/blinkerProject/board/button/button.c | 68 +++++++++++++ .../kb28/blinkerProject/board/button/button.h | 27 ++++++ 03-software/nbproject/configurations.xml | 96 ++++++++++++++++++- 7 files changed, 329 insertions(+), 48 deletions(-) create mode 100644 03-software/ch/kb28/blinkerProject/board/button/button.c create mode 100644 03-software/ch/kb28/blinkerProject/board/button/button.h diff --git a/03-software/ch/kb28/blinkerProject/app/factory/factory.c b/03-software/ch/kb28/blinkerProject/app/factory/factory.c index 05f36cb..79b4cb3 100755 --- a/03-software/ch/kb28/blinkerProject/app/factory/factory.c +++ b/03-software/ch/kb28/blinkerProject/app/factory/factory.c @@ -6,16 +6,50 @@ static Factory theFactory; //all the getters -LED* l() -{ - return &theFactory.l_; +LED* l1() { + return &theFactory.l1_; } -LED* l2(){ +LED* l2() { return &theFactory.l2_; } +LED* l3() { + return &theFactory.l3_; +} +LED* l4() { + return &theFactory.l4_; +} +LED* l5() { + return &theFactory.l5_; +} +LED* l6() { + return &theFactory.l6_; +} +LED* l7() { + return &theFactory.l7_; +} +LED* l8() { + return &theFactory.l8_; +} +LED* l9() { + return &theFactory.l9_; +} +LED* l10() { + return &theFactory.l10_; +} -LEDBlinker* lb() { - return &theFactory.lb_; +BUTTON* b1() { + return &theFactory.b1_; +} +BUTTON* b2() { + return &theFactory.b2_; +} +BUTTON* b3() { + return &theFactory.b3_; +} + + +LEDBlinker* lb1() { + return &theFactory.lb1_; } LEDBlinker* lb2() { @@ -23,22 +57,46 @@ LEDBlinker* lb2() { } //initialize all objects -void Factory_init() -{ - LED_init(l(),LID); - LED_init(l2(),2); - LED_initHW(l()); +void Factory_init() { + LED_init(l1(), 1); + LED_init(l2(), 2); + LED_init(l3(), 3); + LED_init(l4(), 4); + LED_init(l5(), 5); + LED_init(l6(), 6); + LED_init(l7(), 7); + LED_init(l8(), 8); + LED_init(l9(), 9); + LED_init(l10(), 10); + + BUTTON_init(b1(), 1); + BUTTON_init(b2(), 2); + BUTTON_init(b3(), 3); + + + LED_initHW(l1()); LED_initHW(l2()); + LED_initHW(l3()); + LED_initHW(l4()); + LED_initHW(l5()); + LED_initHW(l6()); + LED_initHW(l7()); + LED_initHW(l8()); + LED_initHW(l9()); + LED_initHW(l10()); + + BUTTON_initHW(b1()); + BUTTON_initHW(b2()); + BUTTON_initHW(b3()); } //connect objects if required -void Factory_build() -{ - +void Factory_build() { + } //start all state machines void Factory_start() { - LEDBlinker_startBehaviour(lb()); + LEDBlinker_startBehaviour(lb1()); LEDBlinker_startBehaviour(lb2()); } diff --git a/03-software/ch/kb28/blinkerProject/app/factory/factory.h b/03-software/ch/kb28/blinkerProject/app/factory/factory.h index c7aa6fc..1b0ad58 100755 --- a/03-software/ch/kb28/blinkerProject/app/factory/factory.h +++ b/03-software/ch/kb28/blinkerProject/app/factory/factory.h @@ -8,15 +8,27 @@ #include #include "../../board/led/led.h" +#include "../../board/button/button.h" #include "../ledblinker.h" -#define LID 5 +#define LID 1 -struct Factory_ -{ - LED l_; +struct Factory_ { + LED l1_; LED l2_; - LEDBlinker lb_; + LED l3_; + LED l4_; + LED l5_; + LED l6_; + LED l7_; + LED l8_; + LED l9_; + LED l10_; + BUTTON b1_; + BUTTON b2_; + BUTTON b3_; + + LEDBlinker lb1_; LEDBlinker lb2_; }; @@ -28,9 +40,22 @@ void Factory_build(); void Factory_start(); //these are global getters for our objects -LED* l(); -LED* l2(); -LEDBlinker* lb(); +LED* l1(); +LED* l2(); +LED* l3(); +LED* l4(); +LED* l5(); +LED* l6(); +LED* l7(); +LED* l8(); +LED* l9(); +LED* l10(); + +BUTTON* b1(); +BUTTON* b2(); +BUTTON* b3(); + +LEDBlinker* lb1(); LEDBlinker* lb2(); #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 4b8a358..72f0e40 100644 --- a/03-software/ch/kb28/blinkerProject/app/lebblinker.c +++ b/03-software/ch/kb28/blinkerProject/app/lebblinker.c @@ -2,40 +2,33 @@ #include "factory/factory.h" #include "../board/led/led.h" -void LEDBlinker_init(LEDBlinker* me) -{ +void LEDBlinker_init(LEDBlinker* me) { me->state = ST_LBINIT; } -void LEDBlinker_startBehaviour(LEDBlinker* me) -{ +void LEDBlinker_startBehaviour(LEDBlinker* me) { POST(me, &LEDBlinker_processEvent, evLBInit,0,0); } -bool LEDBlinker_processEvent(Event* ev) -{ +bool LEDBlinker_processEvent(Event* ev) { bool processed = false; LEDBlinker* me = (LEDBlinker*)Event_getTarget(ev); LBSTATES oldState = me->state; evIDT evid = Event_getId(ev); - switch (me->state) - { + switch (me->state) { case ST_LBINIT: - if (evid == evLBInit) - { + if (evid == evLBInit) { me->state = ST_LBOFF; } break; case ST_LBOFF: - if (evid == evLBTMOn) - { + if (evid == evLBTMOn) { me->state = ST_LBON; } break; case ST_LBON: - if (evid == evLBTMOff) - { + if (evid == evLBTMOff) { me->state = ST_LBOFF; } break; @@ -43,10 +36,8 @@ bool LEDBlinker_processEvent(Event* ev) break; } - if (oldState != me->state) - { - switch (me->state) - { + if (oldState != me->state) { + switch (me->state) { case ST_LBINIT: break; case ST_LBOFF: diff --git a/03-software/ch/kb28/blinkerProject/app/main.c b/03-software/ch/kb28/blinkerProject/app/main.c index d9ea4c5..9f552ac 100644 --- a/03-software/ch/kb28/blinkerProject/app/main.c +++ b/03-software/ch/kb28/blinkerProject/app/main.c @@ -3,6 +3,7 @@ #include "../board/led/led.h" #include "../app/factory/factory.h" #include "../xf/xf.h" +#include "../board/button/button.h" bool test(Event* ev); @@ -35,13 +36,30 @@ void main(void) Factory_init(); Factory_build(); - Factory_start(); + //Factory_start(); - TMR0_SetInterruptHandler(XF_decrementAndQueueTimers); + //TMR0_SetInterruptHandler(XF_decrementAndQueueTimers); + - - while (1) - { - XF_executeOnce(); + 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()); + } + } } \ 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 new file mode 100644 index 0000000..c3ec596 --- /dev/null +++ b/03-software/ch/kb28/blinkerProject/board/button/button.c @@ -0,0 +1,68 @@ +/** + * @file button.c + * @author Rémi Heredero (remi@heredero.ch) + * @version 0.1 + * @date 2023-06-06 + * + */ + +#include "button.h" +#include "../../mcc_generated_files/pin_manager.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; + +} + +/** + * @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; + } +} \ 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 new file mode 100644 index 0000000..c3fc5a0 --- /dev/null +++ b/03-software/ch/kb28/blinkerProject/board/button/button.h @@ -0,0 +1,27 @@ +/** + * @file button.h + * @author Rémi Heredero (remi@heredero.ch) + * @version 0.1 + * @date 2023-06-06 + * + */ +#ifndef BUTTON_H +#define BUTTON_H + +#include +#include + +typedef enum {ST_PBINIT, ST_PBWAIT, ST_PBPOLL, ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES; +typedef enum {evPBInit, evPBPoll} BUTTON_EVENTS; + +typedef struct { + uint8_t id; + BUTTON_STATES state; + +} BUTTON; + +void BUTTON_init(BUTTON* me, uint8_t id); +void BUTTON_initHW(BUTTON* me); +bool BUTTON_isPressed(BUTTON* me); + +#endif /* BUTTON_H */ \ No newline at end of file diff --git a/03-software/nbproject/configurations.xml b/03-software/nbproject/configurations.xml index fc6fca4..3205f56 100644 --- a/03-software/nbproject/configurations.xml +++ b/03-software/nbproject/configurations.xml @@ -11,6 +11,9 @@ ch/kb28/blinkerProject/app/ledblinker.h + + ch/kb28/blinkerProject/board/button/button.h + ch/kb28/blinkerProject/board/led/led.h @@ -47,6 +50,9 @@ ch/kb28/blinkerProject/app/main.c + + ch/kb28/blinkerProject/board/button/button.c + ch/kb28/blinkerProject/board/led/led.c @@ -84,7 +90,7 @@ PIC18F87K22 - noID + PICkit3PlatformTool XC8 2.41 2 @@ -199,6 +205,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +