push button for toggle led

This commit is contained in:
Rémi Heredero 2023-06-11 22:33:09 +02:00
parent 909335e807
commit 3f1ce81ad5
8 changed files with 83 additions and 56 deletions

View File

@ -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());
}

View File

@ -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

View File

@ -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:

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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
@ -66,3 +68,64 @@ bool BUTTON_isPressed(BUTTON* me) {
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;
}

View File

@ -10,9 +10,12 @@
#include <stdint.h>
#include <stdbool.h>
#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 */

View File

@ -93,7 +93,7 @@
<platformTool>PICkit3PlatformTool</platformTool>
<languageToolchain>XC8</languageToolchain>
<languageToolchainVersion>2.41</languageToolchainVersion>
<platform>2</platform>
<platform>3</platform>
</toolsSet>
<packs>
<pack name="PIC18F-K_DFP" vendor="Microchip" version="1.7.134"/>