Initial commit

This commit is contained in:
2023-08-22 09:22:00 +02:00
commit 2fcfcb12cd
35 changed files with 21863 additions and 0 deletions

View File

@ -0,0 +1,66 @@
#include "button.h"
#include "../../mcc_generated_files/pin_manager.h"
void Button_init(Button* me, uint8_t id, bool isPullUp)
{
me->id = id;
me->isPullUp = isPullUp;
}
/**
* @brief Initialize the Driver
*
*/
void Button_initHW(Button* me)
{
}
//read the state of the button
//maybe you have to adjust the
//low level calls
uint8_t Button_read(Button* me)
{
uint8_t value = LOW;
switch (me->id)
{
case 1:
value = IO_RA7_GetValue();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
}
if (me->isPullUp == true)
{
value=value==LOW?HIGH:LOW;
}
return value;
}
//id getter
uint8_t Button_getId(Button* me)
{
return me->id;
}
//id setter
void Button_setId(Button* me, uint8_t id)
{
me->id = id;
}

View File

@ -0,0 +1,25 @@
#ifndef Button_ONCE
#define Button_ONCE
#include <stdint.h>
#include <stdbool.h>
/*
* this is the declaration of the Button class
*/
struct Button_
{
uint8_t id;
bool isPullUp;
};
typedef struct Button_ Button;
void Button_init(Button* me, uint8_t id, bool isPullUp);
void Button_initHW(Button* me);
uint8_t Button_read(Button* me);
void Button_setId(Button* me, uint8_t id);
uint8_t Button_getId(Button* me);
#endif

View File

@ -0,0 +1,129 @@
#include "buttonsm.h"
/*
* this is the init method of the ButtonSM class
*/
void ButtonSM_init(ButtonSM* me, Button* button)
{
me->state = ST_BSMINIT;
me->button = button;
me->actualState = ST_BSMINIT;
me->observer = NULL;
me->observerCB = NULL;
}
/*
* this is the state machine method of the ButtonSM class
*/
bool ButtonSM_processEvent(Event* ev)
{
ButtonSM* me = (ButtonSM*)ev->target;
bool processed = false;
BSMState oldState = me->state;
switch (me->state)
{
case ST_BSMINIT:
if (Event_getId(ev) == evBSMInit)
{
me->state = ST_BSMWAIT;
}
break;
case ST_BSMWAIT:
if (Event_getId(ev) == evBSMPollTM)
{
me->state = ST_BSMPOLL;
}
break;
case ST_BSMPOLL:
if (Event_getId(ev) == evBSMDefault)
{
if (Button_read(me->button)==HIGH)
{
me->state = ST_BSMPRESSED;
}
else
{
me->state = ST_BSMRELEASED;
}
}
break;
case ST_BSMPRESSED:
if (Event_getId(ev) == evBSMDefault)
{
me->state = ST_BSMWAIT;
}
break;
case ST_BSMRELEASED:
if (Event_getId(ev) == evBSMDefault)
{
me->state = ST_BSMWAIT;
}
break;
}
if (oldState != me->state)
{
processed = true;
switch (me->state)
{
case ST_BSMINIT:
break;
case ST_BSMWAIT:
POST(me, &ButtonSM_processEvent, evBSMPollTM,POLLTM,0);
break;
case ST_BSMPOLL:
POST(me, &ButtonSM_processEvent, evBSMDefault,0,0);
break;
case ST_BSMPRESSED:
POST(me, &ButtonSM_processEvent, evBSMDefault,0,0);
if (me->actualState != ST_BSMPRESSED)
{
if (me->observerCB != NULL)
{
me->observerCB(me->observer,Button_getId(me->button),true);
me->actualState = ST_BSMPRESSED;
}
}
break;
case ST_BSMRELEASED:
POST(me, &ButtonSM_processEvent, evBSMDefault,0,0);
if (me->actualState != ST_BSMRELEASED)
{
if (me->observerCB != NULL)
{
me->observerCB(me->observer,Button_getId(me->button),false);
me->actualState = ST_BSMRELEASED;
}
}
break;
}
}
return processed;
}
/*
* this is the start method for the
* state machine of the ButtonSM class
*/
void ButtonSM_startBehaviour(ButtonSM* me)
{
POST(me, &ButtonSM_processEvent, evBSMInit,0,0);
me->actualState = Button_read(me->button)==HIGH?ST_BSMPRESSED:ST_BSMRELEASED;
}
/*
* this is the method to set the object and the
* call back method of the ButtonSM class
* this method will be called whenever the
* button changes its state
* as parameters to the callback method will be passed
* the object address, the button id and its state
* if the call back method does not belong to a class,
* then the object address must be set to NULL
*/
void ButtonSM_setObserver(ButtonSM* me, void* observer, buttonObserverCBT observerCB)
{
me->observer = observer;
me->observerCB = observerCB;
}

View File

@ -0,0 +1,69 @@
#ifndef BUTTONSM_DEF
#define BUTTONSM_DEF
#include <stdint.h>
#include <stdbool.h>
#include "../../xf/xf.h"
#include "button.h"
/*
* these are the events of the
* button state machine
* be sure to make the first event
* in the enumeration different from 0
*/
typedef enum BSMEvent
{
evBSMInit = 10,
evBSMDefault,
evBSMPollTM
} BSMEvent;
/*
* these are the states of the
* button state machine
*/
typedef enum BSMSTate_
{
ST_BSMINIT,
ST_BSMWAIT,
ST_BSMPOLL,
ST_BSMPRESSED,
ST_BSMRELEASED
} BSMState;
/*
* the associated button will be polled
* each 50 ms. do not make this time
* shorter than TICKINTERVAL
*/
#define POLLTM 50
/*
* this is the prototype type of the callback function
* that will be called when the associated button
* changes from released to pressed or inverse.
*/
typedef void (*buttonObserverCBT)(void*, uint8_t, bool);
/*
* this is the declaration of the ButtonSM class
*/
struct ButtonSM_
{
BSMState state;
Button* button;
BSMState actualState;
buttonObserverCBT observerCB;
void* observer;
};
typedef struct ButtonSM_ ButtonSM;
void ButtonSM_init(ButtonSM* me, Button* button);
void ButtonSM_startBehaviour(ButtonSM* me);
bool ButtonSM_processEvent(Event* ev);
void ButtonSM_setObserver(ButtonSM* me, void* observer, buttonObserverCBT observerCB);
#endif

View File

@ -0,0 +1,103 @@
#include "led.h"
#include "../../mcc_generated_files/pin_manager.h"
void LED_init(LED* me, uint8_t id)
{
me->id = id;
}
/**
* @brief Initialize the Driver
*
*/
void LED_initHW(LED* me)
{
LED_off(me);
}
/*
* for the on and the off methods:
* if the output is push pull, it depends if the
* load is connect to ground or vcc.
* in this case, the load is connected to vcc,
* so on and off are inverted. Change the code as it
* is convenient for your hardware
*/
//switch on the led
//maybe you have to adjust your
//low level calls
void LED_on(LED* me)
{
switch (me->id)
{
case 1:
IO_RB0_SetLow();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
}
}
//switch off the led
//maybe you have to adjust your
//low level calls
void LED_off(LED* me)
{
switch (me->id)
{
case 1:
IO_RB0_SetHigh();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
}
}
void LED_setState(LED* me, uint8_t state)
{
if (state == HIGH)
{
LED_on(me);
}
if (state == LOW)
{
LED_off(me);
}
}

View File

@ -0,0 +1,23 @@
#ifndef LED_ONCE
#define LED_ONCE
#include <stdint.h>
/*
* this is the declaration of the Led class
*/
struct LED_
{
//has a gpo
uint8_t id;
};
typedef struct LED_ LED;
void LED_init(LED* me, uint8_t id);
void LED_initHW(LED* me);
void LED_on(LED* me);
void LED_off(LED* me);
void LED_setState(LED* me,uint8_t state);
#endif