Archived
Template
1
0

update button class with automatic generated file with XFTGenerator

This commit is contained in:
Rémi Heredero 2023-07-12 14:53:31 +02:00
parent 7dee82956d
commit 2dd13a3621
10 changed files with 308 additions and 282 deletions

View File

@ -1,137 +0,0 @@
/**
* @author R<EFBFBD>mi Heredero (remi@heredero.ch)
* @version. 1.0.0
* @date 2023-06-15
*/
#include "button.h"
#include "../src/mcc_generated_files/pin_manager.h"
#include "../src/app/factory.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;
me->press.fCallBack = NULL;
me->release.fCallBack = NULL;
}
/**
* @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;
}
}
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) {
POST(me, &BUTTON_processEvent, evPBPoll, 0, 0);
if(BUTTON_isPressed(me)) {
me->state = ST_PBPRESSED;
} else {
me->state = ST_PBRELEASED;
}
}
break;
case ST_PBRELEASED:
if(evid == evPBPoll) {
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
if(BUTTON_isPressed(me)) {
me->state = ST_PBPRESSED;
}
}
break;
case ST_PBPRESSED:
if(evid == evPBPoll) {
POST(me, &BUTTON_processEvent, evPBPoll, PB_POLL_TIME, 0);
if(!BUTTON_isPressed(me)){
me->state = ST_PBRELEASED;
}
}
break;
}
if(oldState != me->state) {
switch(me->state){
case ST_PBINIT:
break;
case ST_PBRELEASED:
if(me->release.fCallBack != NULL) me->release.fCallBack(me->release.param);
break;
case ST_PBPRESSED:
if(me->press.fCallBack != NULL) me->press.fCallBack(me->press.param);
break;
}
processed = true;
}
return processed;
}
void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack fRelease) {
me->press = fPress;
me->release = fRelease;
}
buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param){
buttonCallBack c;
c.fCallBack = f;
c.param = param;
return c;
}

View File

@ -1,97 +0,0 @@
/**
* @author R<EFBFBD>mi Heredero (remi@heredero.ch)
* @version. 1.0.0
* @date 2023-06-15
*/
#ifndef BUTTON_H
#define BUTTON_H
#include <stdint.h>
#include <stdbool.h>
#include "../src/xf/xf.h"
#define PB_POLL_TIME 20 // Poll time for BUTTON
typedef enum {
ST_PBINIT,
ST_PBRELEASED,
ST_PBPRESSED
} BUTTON_STATES;
typedef enum {
evPBInit=50,
evPBPoll
} BUTTON_EVENTS;
// Calback function
typedef void (*fButtonCallback)(void*);
typedef struct {
fButtonCallback fCallBack;
void* param;
} buttonCallBack;
typedef struct {
uint8_t id; // Id of the button
BUTTON_STATES state; // Actual state
buttonCallBack press; // Callback for the rising edge of the button
buttonCallBack release; // Callback for the falling edge of the button
} BUTTON;
/**
* @brief Initialize the button
*
* @param me button itself
* @param id The id of the button
*/
void BUTTON_init(BUTTON* me, uint8_t id);
/**
* @brief Initialize the hardware of the button
*
* @param me button itself
*/
void BUTTON_initHW(BUTTON* me);
/**
* @brief Set both callback event functions
*
* @param me button itself
* @param fPress callback function when the button have a rising edge
* @param release callback function whent the have a falling edge
*/
void BUTTON_setEventFunctions(BUTTON* me, buttonCallBack fPress, buttonCallBack release);
/**
* @brief Check if the button is pressed
* The function returns true if the button is pressed, false otherwise
*
* @param me button itself
* @return true if the button is pressed
* @return false if the button is not pressed
*/
bool BUTTON_isPressed(BUTTON* me);
/**
* @biref Start state machine of the BUTTON
*
* @param me the button itself
*/
void BUTTON_startBehaviour(BUTTON* me);
/**
* @brief State machine of the BUTTON
*
* @param ev event to process on the state machine
*/
bool BUTTON_processEvent(Event* ev);
/**
* @brief Define a callback for BUTTON
*
* @param f callback function
* @param param callback parameter for the function
* @return the callback struct
*/
buttonCallBack BUTTON_defineCallBack(fButtonCallback f, void* param);
#endif /* BUTTON_H */

View File

@ -6,6 +6,7 @@
projectFiles="true">
<logicalFolder name="app" displayName="app" projectFiles="true">
<itemPath>src/app/factory.h</itemPath>
<itemPath>src/app/app.h</itemPath>
</logicalFolder>
<logicalFolder name="board" displayName="board" projectFiles="true">
<itemPath>src/board/led.h</itemPath>
@ -40,6 +41,7 @@
<logicalFolder name="app" displayName="app" projectFiles="true">
<itemPath>src/app/factory.c</itemPath>
<itemPath>src/app/main.c</itemPath>
<itemPath>src/app/app.c</itemPath>
</logicalFolder>
<logicalFolder name="board" displayName="board" projectFiles="true">
<itemPath>src/board/led.c</itemPath>
@ -195,6 +197,9 @@
<property key="program-the-device-with-default-config-words" value="true"/>
<property key="remove-unused-sections" value="true"/>
</HI-TECH-LINK>
<PICkit3PlatformTool>
<property key="firmware.download.all" value="false"/>
</PICkit3PlatformTool>
<XC8-CO>
<property key="coverage-enable" value=""/>
<property key="stack-guidance" value="false"/>

216
src/app/app.c Normal file
View File

@ -0,0 +1,216 @@
/**
* @author R<EFBFBD>mi Heredero (remi@heredero.ch)
* @version 1.0.0
* @date 2023-06-15
*/
#include "app.h"
#include "factory.h"
#include "../middleware/blinker.h"
#include "../middleware/clickHandler.h"
void clickL(APP* me){
POST(me, &APP_processEvent, evAPclickL, 0, 0);
}
void clickLn(APP* me){
POST(me, &APP_processEvent, evAPclickLn, 0, 0);
}
void clickR(APP* me){
POST(me, &APP_processEvent, evAPclickR, 0, 0);
}
void clickRn(APP* me){
POST(me, &APP_processEvent, evAPclickRn, 0, 0);
}
void endL(APP* me){
POST(me, &APP_processEvent, evAPendL, 0, 0);
}
void endR(APP* me){
POST(me, &APP_processEvent, evAPendR, 0, 0);
}
void longClick(APP* me){
POST(me, &APP_processEvent, evAPlongClick, 0, 0);
}
void APP_init(APP* me){
me->state = STAP_INIT;
CLICK_HANDLER_setSingleClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&clickL, me));
CLICK_HANDLER_setSingleClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&clickR, me));
CLICK_HANDLER_setDoubleClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&clickLn, me));
CLICK_HANDLER_setDoubleClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&clickRn, me));
CLICK_HANDLER_setLongClickCallback(ch1(), CLICK_HANDLER_defineCallBack(&longClick, me));
CLICK_HANDLER_setLongClickCallback(ch3(), CLICK_HANDLER_defineCallBack(&longClick, me));
BLINKER_setFinished(blL(), BLINKER_defineCallBack(&endL, me));
BLINKER_setFinished(blR(), BLINKER_defineCallBack(&endR, me));
}
void APP_startBehaviour(APP* me){
POST(me, &APP_processEvent, evAPinit, 0, 0);
}
bool APP_processEvent(Event* ev){
bool processed = false;
APP* me = (APP*) Event_getTarget(ev);
APP_STATES oldState = me->state;
evIDT evid = Event_getId(ev);
switch(me->state) { // In state
case STAP_INIT:
if(evid == evAPinit) {
me->state = STAP_WAIT;
}
break;
case STAP_WAIT:
if(evid == evAPclickL) {
me->state = STAP_BLINKER_L;
BLINKER_blink(blL());
}
if(evid == evAPclickLn) {
me->state = STAP_BLINKER_L;
BLINKER_blinkN(blL());
}
if(evid == evAPclickR) {
me->state = STAP_BLINKER_R;
BLINKER_blink(blR());
}
if(evid == evAPclickRn) {
me->state = STAP_BLINKER_R;
BLINKER_blinkN(blR());
}
if(evid == evAPlongClick) {
me->state = STAP_BLINKER_W;
BLINKER_blink(blB());
}
break;
case STAP_BLINKER_L:
if(evid == evAPclickR){
me->state = STAP_BLINKER_LR;
BLINKER_blink(blR());
}
if(evid == evAPclickRn){
me->state = STAP_BLINKER_LR;
BLINKER_blinkN(blR());
}
if(evid == evAPclickL) {
me->state = STAP_WAIT;
BLINKER_endBlink(blL());
}
if(evid == evAPendL) {
me->state = STAP_WAIT;
}
if(evid == evAPlongClick) {
me->state = STAP_BLINKER_W;
BLINKER_endBlink(blL());
BLINKER_blink(blB());
}
break;
case STAP_BLINKER_R:
if(evid == evAPclickL){
me->state = STAP_BLINKER_LR;
BLINKER_blink(blL());
}
if(evid == evAPclickLn){
me->state = STAP_BLINKER_LR;
BLINKER_blinkN(blL());
}
if(evid == evAPclickR) {
me->state = STAP_WAIT;
BLINKER_endBlink(blR());
}
if(evid == evAPendR) {
me->state = STAP_WAIT;
}
if(evid == evAPlongClick) {
me->state = STAP_BLINKER_W;
BLINKER_endBlink(blR());
BLINKER_blink(blB());
}
break;
case STAP_BLINKER_LR:
if(evid == evAPclickR) {
me->state = STAP_BLINKER_L;
BLINKER_endBlink(blR());
}
if(evid == evAPendR) {
me->state = STAP_BLINKER_L;
}
if(evid == evAPclickL) {
me->state = STAP_BLINKER_R;
BLINKER_endBlink(blL());
}
if(evid == evAPendL) {
me->state = STAP_BLINKER_R;
}
if(evid == evAPlongClick) {
me->state = STAP_BLINKER_W;
BLINKER_endBlink(blL());
BLINKER_endBlink(blR());
BLINKER_blink(blB());
}
break;
case STAP_BLINKER_W:
if(evid == evAPlongClick){
me->state = STAP_WAIT;
BLINKER_endBlink(blB());
}
break;
}
if(oldState != me->state) {
switch(oldState) { // on exit
case STAP_INIT:
break;
case STAP_WAIT:
break;
case STAP_BLINKER_L:
break;
case STAP_BLINKER_R:
break;
case STAP_BLINKER_LR:
break;
case STAP_BLINKER_W:
break;
}
switch(me->state) { // on entry
case STAP_INIT:
break;
case STAP_WAIT:
break;
case STAP_BLINKER_L:
break;
case STAP_BLINKER_R:
break;
case STAP_BLINKER_LR:
break;
case STAP_BLINKER_W:
break;
}
processed = true;
}
return processed;
}

40
src/app/app.h Normal file
View File

@ -0,0 +1,40 @@
/**
* @author Rémi Heredero (remi@heredero.ch)
* @version 1.0.0
* @date 2023-06-17
*/
#ifndef APP_H
#define APP_H
#include "../xf/xf.h"
typedef enum {
STAP_INIT,
STAP_WAIT,
STAP_BLINKER_L,
STAP_BLINKER_R,
STAP_BLINKER_LR,
STAP_BLINKER_W
} APP_STATES;
typedef enum {
evAPinit = 150,
evAPclickL,
evAPclickLn,
evAPclickR,
evAPclickRn,
evAPendL,
evAPendR,
evAPlongClick
} APP_EVENTS;
typedef struct {
APP_STATES state; // Actual state
} APP;
void APP_init(APP* me);
void APP_startBehaviour(APP* me);
bool APP_processEvent(Event* ev);
#endif /* APP_H */

View File

@ -72,6 +72,10 @@ BLINKER* blB() {
return &theFactory.blB_;
}
APP* app() {
return &theFactory.app_;
}
//initialize all objects
void Factory_init() {
LED_init(l1(), 1);
@ -113,6 +117,7 @@ void Factory_init() {
BLINKER_init(blR());
BLINKER_init(blB());
APP_init(app());
}
@ -152,4 +157,5 @@ void Factory_start() {
BLINKER_starBehaviour(blL());
BLINKER_starBehaviour(blR());
BLINKER_starBehaviour(blB());
APP_startBehaviour(app());
}

View File

@ -14,6 +14,7 @@
#include "../board/button.h"
#include "../middleware/clickHandler.h"
#include "../middleware/blinker.h"
#include "app.h"
typedef struct {
LED l1_;
@ -35,6 +36,7 @@ typedef struct {
BLINKER blL_;
BLINKER blR_;
BLINKER blB_;
APP app_;
} Factory;
@ -66,5 +68,6 @@ BLINKER* blL();
BLINKER* blR();
BLINKER* blB();
APP* app();
#endif

View File

@ -6,16 +6,13 @@
*/
#include "button.h"
#include "../mcc_generated_files/pin_manager.h"
#include "../app/factory.h"
#include "led.h"
void BUTTON_init(BUTTON* me, uint8_t id){
me->id = id;
me->state = STPB_INIT;
me->poll_time = 20;
me->press.f = NULL;
me->release.f = NULL;
me->pollTime = 20;
me->released.f = NULL;
me->pressed.f = NULL;
}
void BUTTON_initHW(BUTTON* me) {
@ -64,7 +61,7 @@ bool BUTTON_processEvent(Event* ev) {
switch (me->state) { // onState
case STPB_INIT:
if (ev->id == evPBinit) {
POST(me, &BUTTON_processEvent, evPBpoll, me->poll_time, 0);
BUTTON_emitPoll(me, me->pollTime);
if (BUTTON_isPressed(me)) {
me->state = STPB_PRESSED;
} else {
@ -75,7 +72,7 @@ bool BUTTON_processEvent(Event* ev) {
case STPB_RELEASED:
if (ev->id == evPBpoll) {
POST(me, &BUTTON_processEvent, evPBpoll, me->poll_time, 0);
BUTTON_emitPoll(me, me->pollTime);
if (BUTTON_isPressed(me)) {
me->state = STPB_PRESSED;
}
@ -84,7 +81,7 @@ bool BUTTON_processEvent(Event* ev) {
case STPB_PRESSED:
if (ev->id == evPBpoll) {
POST(me, &BUTTON_processEvent, evPBpoll, me->poll_time, 0);
BUTTON_emitPoll(me, me->pollTime);
if (!BUTTON_isPressed(me)) {
me->state = STPB_RELEASED;
}
@ -109,14 +106,14 @@ bool BUTTON_processEvent(Event* ev) {
break;
case STPB_RELEASED:
if (me->release.f != NULL) {
me->release.f(me->release.p);
if (me->released.f != NULL) {
me->released.f(me->released.p);
}
break;
case STPB_PRESSED:
if (me->press.f != NULL) {
me->press.f(me->press.p);
if (me->pressed.f != NULL) {
me->pressed.f(me->pressed.p);
}
break;
}
@ -130,14 +127,14 @@ bool BUTTON_processEvent(Event* ev) {
* Callbacks *
*************/
void BUTTON_onPress(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p){
me->press.f = f;
me->press.p = p;
void BUTTON_onReleased(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p) {
me->released.f = f;
me->released.p = p;
}
void BUTTON_onRelease(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p){
me->release.f = f;
me->release.p = p;
void BUTTON_onPressed(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p) {
me->pressed.f = f;
me->pressed.p = p;
}
/************
@ -156,6 +153,6 @@ void BUTTON_setId(BUTTON* me, uint8_t v) {
me->id = v;
}
void BUTTON_setPoll_time(BUTTON* me, uint16_t v) {
me->poll_time = v;
void BUTTON_setPollTime(BUTTON* me, uint8_t v) {
me->pollTime = v;
}

View File

@ -29,14 +29,15 @@ typedef struct {
typedef struct {
BUTTON_STATES state;
uint8_t id;
uint16_t poll_time;
BUTTON_CALLBACK press;
BUTTON_CALLBACK release;
uint8_t pollTime;
BUTTON_CALLBACK released;
BUTTON_CALLBACK pressed;
} BUTTON;
/**
* Initialize the BUTTON
* @param me the BUTTON itself
* @param id the id of the BUTTON
*/
void BUTTON_init(BUTTON* me, uint8_t id);
@ -74,20 +75,20 @@ bool BUTTON_processEvent(Event* ev);
*************/
/**
* Set the callback function to call when the BUTTON is pressed
* Set the callback function to call when the BUTTON is entering state released
* @param me the BUTTON itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void BUTTON_onPress(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p);
void BUTTON_onReleased(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p);
/**
* Set the callback function to call when the BUTTON is released
* Set the callback function to call when the BUTTON is entering state pressed
* @param me the BUTTON itself
* @param f the function to call
* @param p the param(s) to pass to the function
*/
void BUTTON_onRelease(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p);
void BUTTON_onPressed(BUTTON* me, BUTTON_CALLBACK_FUNCTION f, void* p);
/************
* EMITTERS *
@ -115,6 +116,6 @@ void BUTTON_setId(BUTTON* me, uint8_t v);
* @param me the BUTTON itself
* @param v the poll time to set
*/
void BUTTON_setPoll_time(BUTTON* me, uint16_t v);
void BUTTON_setPollTime(BUTTON* me, uint8_t v);
#endif

View File

@ -28,8 +28,8 @@ void CLICK_HANDLER_init(CLICK_HANDLER* me, BUTTON* b){
me->singleClick.fCallBack = NULL;
me->doubleClick.fCallBack = NULL;
BUTTON_onPress(b, CLICK_HANDLER_BUTTON_PRESS, me);
BUTTON_onRelease(b, CLICK_HANDLER_BUTTON_RELEASE, me);
BUTTON_onPressed(b, CLICK_HANDLER_BUTTON_PRESS, me);
BUTTON_onReleased(b, CLICK_HANDLER_BUTTON_RELEASE, me);
}
void CLICK_HANDLER_startBehaviour(CLICK_HANDLER* me){
@ -71,12 +71,8 @@ bool CLICK_HANDLER_processEvent(Event* ev) {
case STCH_WAIT:
if(evid == evCHpbPress) {
LED_on(l2());
//me->state = STCH_LONG_CLICK;
//POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0);
}
if(evid==evCHpbRelease) {
LED_off(l2());
me->state = STCH_LONG_CLICK;
POST(me, &CLICK_HANDLER_processEvent, evCHtimer, CH_CLICK_TIMER, 0);
}
break;
@ -90,9 +86,7 @@ bool CLICK_HANDLER_processEvent(Event* ev) {
if(evid == evCHtimer) {
me->state = STCH_WAIT;
LED_on(l4());
if(me->longClick.fCallBack != NULL) {
LED_on(l5());
me->longClick.fCallBack(me->longClick.param);
}
}
@ -103,12 +97,10 @@ bool CLICK_HANDLER_processEvent(Event* ev) {
case STCH_SINGLE_CLICK:
if(evid == evCHpbPress) {
LED_on(l2());
me->state = STCH_DOUBLE_CLICK;
}
if(evid == evCHtimer) {
LED_on(l3());
me->state = STCH_WAIT;
if(me->singleClick.fCallBack != NULL) {
me->singleClick.fCallBack(me->singleClick.param);