From 34cc8b4eb8f7d17cac254f035d8e4e64acca2e1b Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 28 Jun 2022 02:42:43 +0200 Subject: [PATCH] Add statemachine module and use it in new bridge driver module. --- src/bridge.c | 125 +++++++++++++++++++++++++++++++++++++ src/include/bridge.h | 17 +++++ src/include/statemachine.h | 16 +++++ src/statemachine.c | 6 ++ 4 files changed, 164 insertions(+) create mode 100644 src/bridge.c create mode 100644 src/include/bridge.h create mode 100644 src/include/statemachine.h create mode 100644 src/statemachine.c diff --git a/src/bridge.c b/src/bridge.c new file mode 100644 index 0000000..3efebfd --- /dev/null +++ b/src/bridge.c @@ -0,0 +1,125 @@ +#include +#include + +#include "bridge.h" +#include "statemachine.h" +#include "pins.h" + +bdg_sm_state_t bridge_state; + +sm_trans_t bridge_sm_transitions[] = +{ + { + .from_state = HB1_GND_ON, + .to_state = HB1_PWR_ON, + .trans_fct = brdg_Hb1PowerOn + }, + { + .from_state = HB1_PWR_ON, + .to_state = HB1_PWR_OFF, + .trans_fct = brdg_Hb1PowerOff + }, + { + .from_state = HB1_PWR_OFF, + .to_state = HB2_GND_ON, + .trans_fct = brdg_Hb2GndOn + }, + { + .from_state = HB2_GND_ON, + .to_state = HB2_PWR_ON, + .trans_fct = brdg_Hb2PowerOn + }, + { + .from_state = HB2_PWR_ON, + .to_state = HB2_PWR_OFF, + .trans_fct = brdg_Hb2PowerOff + }, + { + .from_state = HB2_PWR_OFF, + .to_state = HB2_GND_ON, + .trans_fct = brdg_Hb2GndOn + } +}; + +sm_t bridge_statemachine = { + .current_state = HB1_PWR_OFF, + .transitions = bridge_sm_transitions +}; + +void adsf() +{ + switch (bridge_state) + { + case HB1_GND_ON: + setPin(&PORTB, PB6); + unsetPin(&PORTB, PB4); + _delay_us(10); + bridge_state = HB1_PWR_ON; + break; + + case HB1_PWR_ON: + setPin(&PORTB, PB7); + _delay_us(9980); + bridge_state = HB1_PWR_OFF; + break; + + case HB1_PWR_OFF: + unsetPin(&PORTB, PB7); + _delay_us(100); + bridge_state = HB2_GND_ON; + break; + + case HB2_GND_ON: + setPin(&PORTB, PB4); + unsetPin(&PORTB, PB6); + _delay_us(10); + bridge_state = HB2_PWR_ON; + break; + + case HB2_PWR_ON: + setPin(&PORTB, PB5); + _delay_us(9980); + bridge_state = HB2_PWR_OFF; + break; + + case HB2_PWR_OFF: + unsetPin(&PORTB, PB5); + _delay_us(100); + bridge_state = HB1_GND_ON; + break; + + default: + break; + } +} + + +void brdg_Hb1GndOn(void) +{ + +} + +void brdg_Hb1PowerOn(void) +{ + +} + +void brdg_Hb1PowerOff(void) +{ + +} + +void brdg_Hb2GndOn(void) +{ + +} + +void brdg_Hb2PowerOn(void) +{ + +} + +void brdg_Hb2PowerOff(void) +{ + +} diff --git a/src/include/bridge.h b/src/include/bridge.h new file mode 100644 index 0000000..52f1d7c --- /dev/null +++ b/src/include/bridge.h @@ -0,0 +1,17 @@ +typedef enum +{ + HB1_GND_ON, + HB1_PWR_ON, + HB1_PWR_OFF, + HB2_GND_ON, + HB2_PWR_ON, + HB2_PWR_OFF +} bdg_sm_state_t; + + +void brdg_Hb1GndOn(void); +void brdg_Hb1PowerOn(void); +void brdg_Hb1PowerOff(void); +void brdg_Hb2GndOn(void); +void brdg_Hb2PowerOn(void); +void brdg_Hb2PowerOff(void); diff --git a/src/include/statemachine.h b/src/include/statemachine.h new file mode 100644 index 0000000..d1bbb59 --- /dev/null +++ b/src/include/statemachine.h @@ -0,0 +1,16 @@ +typedef int sm_state_t; + +typedef struct +{ + sm_state_t from_state; + sm_state_t to_state; + void (*trans_fct)(void); +} sm_trans_t; + +typedef struct +{ + sm_state_t current_state; + sm_trans_t *transitions; +} sm_t; + +void sm_handleTransition(sm_t *statemachine, sm_state_t to); \ No newline at end of file diff --git a/src/statemachine.c b/src/statemachine.c new file mode 100644 index 0000000..2076ecf --- /dev/null +++ b/src/statemachine.c @@ -0,0 +1,6 @@ +#include "statemachine.h" + +void sm_handleTransition(sm_t *statemachine, sm_state_t to) +{ + +} \ No newline at end of file