Buttons with function pointer when press or release
This commit is contained in:
		| @@ -86,9 +86,16 @@ void Factory_init() { | ||||
|     BUTTON_initHW(b3()); | ||||
| } | ||||
|  | ||||
| void lON(){ | ||||
|     LED_on(l9()); | ||||
| } | ||||
| void lOFF() { | ||||
|     LED_off(l9()); | ||||
| } | ||||
|  | ||||
| //connect objects if required | ||||
| void Factory_build() { | ||||
|      | ||||
|     BUTTON_setEventFunctions(b1(), &lON, &lOFF); | ||||
| } | ||||
|  | ||||
| //start all state machines | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| /* this is the Factory class */ | ||||
|  | ||||
|  | ||||
| #ifndef FACTORY_ONCE | ||||
| #define FACTORY_ONCE | ||||
| #ifndef FACTORY_H | ||||
| #define FACTORY_H | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <stdbool.h> | ||||
|   | ||||
| @@ -1,9 +1,7 @@ | ||||
|  | ||||
| #include "../mcc_generated_files/mcc.h" | ||||
| #include "../board/led/led.h" | ||||
| #include "../app/factory/factory.h" | ||||
| #include "../xf/xf.h" | ||||
| #include "../board/button/button.h" | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -8,7 +8,6 @@ | ||||
|  | ||||
| #include "button.h" | ||||
| #include "../../mcc_generated_files/pin_manager.h" | ||||
| #include "../led/led.h" | ||||
| #include "../../app/factory/factory.h" | ||||
|  | ||||
| /** | ||||
| @@ -82,31 +81,29 @@ bool BUTTON_processEvent(Event * 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; | ||||
|                 } | ||||
|                 POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); | ||||
|             } | ||||
|             break; | ||||
|         case ST_PBPOLL: | ||||
|             break; | ||||
|         case ST_PBRELEASED: | ||||
|             if(evid == evPBPoll) { | ||||
|                 POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); | ||||
|                 if(BUTTON_isPressed(me)) { | ||||
|                     me->state = ST_PBPRESSED; | ||||
|                 } | ||||
|                 POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0); | ||||
|             } | ||||
|              | ||||
|             break; | ||||
|         case ST_PBPRESSED: | ||||
|             if(evid == evPBPoll) { | ||||
|                 POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);                 | ||||
|                 if(!BUTTON_isPressed(me)){ | ||||
|                     me->state = ST_PBRELEASED; | ||||
|                 } | ||||
|                 POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);                 | ||||
|                      | ||||
|             } | ||||
|             break; | ||||
| @@ -116,16 +113,19 @@ bool BUTTON_processEvent(Event * ev) { | ||||
|         switch(me->state){ | ||||
|             case ST_PBINIT: | ||||
|                 break; | ||||
|             case ST_PBPOLL: | ||||
|                 break; | ||||
|             case ST_PBRELEASED: | ||||
|                 LED_off(l10()); | ||||
|                 if(me->fButtonRelease != NULL) me->fButtonRelease(); | ||||
|                 break; | ||||
|             case ST_PBPRESSED: | ||||
|                 LED_on(l10()); | ||||
|                 if(me->fButtonPress != NULL) me->fButtonPress(); | ||||
|                 break; | ||||
|         } | ||||
|         processed = true; | ||||
|     } | ||||
|     return processed; | ||||
| } | ||||
|  | ||||
| void BUTTON_setEventFunctions(BUTTON* me, void (*fPress)(void), void (*fRelease)(void)) { | ||||
|     me->fButtonPress = fPress; | ||||
|     me->fButtonRelease = fRelease; | ||||
| } | ||||
| @@ -6,7 +6,7 @@ | ||||
|  *  | ||||
|  */ | ||||
| #ifndef BUTTON_H | ||||
| #define	BUTTON_H | ||||
| #define BUTTON_H | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <stdbool.h> | ||||
| @@ -14,13 +14,14 @@ | ||||
|  | ||||
| #define PB_POLL_TIME 20 | ||||
|  | ||||
| typedef enum {ST_PBINIT, ST_PBWAIT, ST_PBPOLL, ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES; | ||||
| typedef enum {ST_PBINIT,  ST_PBRELEASED, ST_PBPRESSED} BUTTON_STATES; | ||||
| typedef enum {evPBInit=100, evPBPoll} BUTTON_EVENTS; | ||||
|  | ||||
| typedef struct { | ||||
|     uint8_t id; | ||||
|     BUTTON_STATES state; | ||||
|  | ||||
|     void (*fButtonPress)(void); | ||||
|     void (*fButtonRelease)(void); | ||||
| } BUTTON; | ||||
|  | ||||
| void BUTTON_init(BUTTON* me, uint8_t id); | ||||
| @@ -28,5 +29,6 @@ void BUTTON_initHW(BUTTON* me); | ||||
| bool BUTTON_isPressed(BUTTON* me); | ||||
| void BUTTON_startBehaviour(BUTTON* me); | ||||
| bool BUTTON_processEvent(Event* ev); | ||||
| void BUTTON_setEventFunctions(BUTTON* me, void (*fPress)(void), void (*fRelease)(void)); | ||||
|  | ||||
| #endif	/* BUTTON_H */ | ||||
| @@ -6,7 +6,7 @@ | ||||
|  */ | ||||
|  | ||||
| #ifndef LED_H | ||||
| #define	LED_H | ||||
| #define LED_H | ||||
|  | ||||
| #include <stdint.h> | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user