Skip to content

Commit 0ebda27

Browse files
udpated states variable type
1 parent bc12115 commit 0ebda27

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

fsm.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
/* Private macro -------------------------------------------------------------*/
4444

4545
/* Private function prototypes -----------------------------------------------*/
46-
static int get_next_state(fsm_trans_list_t *trans_list, int current_state,
47-
uint32_t elapsed_ms);
48-
static void execute_action(int current_state, fsm_actions_list_t *actions_list,
46+
static uint8_t get_next_state(fsm_trans_list_t *trans_list,
47+
uint8_t current_state, uint32_t elapsed_ms);
48+
static void execute_action(uint8_t current_state,
49+
fsm_actions_list_t *actions_list,
4950
fsm_action_type_t type);
5051
static bool eval_events(fsm_trans_t *trans);
5152
static bool eval_timeout(fsm_trans_t *trans, uint32_t elapsed_time);
@@ -56,7 +57,7 @@ static bool eval_timeout(fsm_trans_t *trans, uint32_t elapsed_time);
5657
/**
5758
* @brief Function to initialize a FSM instance.
5859
*/
59-
fsm_err_t fsm_init(fsm_t *const me, int init_state, fsm_time_t get_ms) {
60+
fsm_err_t fsm_init(fsm_t *const me, uint8_t init_state, fsm_time_t get_ms) {
6061
/* Check if the FSM instance is valid */
6162
if (me == NULL) {
6263
return FSM_ERR_INVALID_PARAM;
@@ -80,7 +81,7 @@ fsm_err_t fsm_init(fsm_t *const me, int init_state, fsm_time_t get_ms) {
8081
* @brief Function to add a transition betwen state to FSM instance.
8182
*/
8283
fsm_err_t fsm_add_transition(fsm_t *const me, fsm_trans_t **trans,
83-
int from_state, int next_state) {
84+
uint8_t from_state, uint8_t next_state) {
8485
/* Check if the FSM instance is valid */
8586
if (me == NULL) {
8687
return FSM_ERR_INVALID_PARAM;
@@ -139,8 +140,8 @@ fsm_err_t fsm_set_event_op(fsm_t *const me, fsm_trans_t *trans, fsm_op_t op) {
139140
/**
140141
* @brief Function to add an event for a transition for a FSM instance.
141142
*/
142-
fsm_err_t fsm_add_event_cmp(fsm_t *const me, fsm_trans_t *trans, int *val, int cmp,
143-
fsm_eval_t eval) {
143+
fsm_err_t fsm_add_event_cmp(fsm_t *const me, fsm_trans_t *trans, int *val,
144+
int cmp, fsm_eval_t eval) {
144145
/* Check if the FSM instance is valid */
145146
if (me == NULL) {
146147
return FSM_ERR_INVALID_PARAM;
@@ -242,19 +243,14 @@ fsm_err_t fsm_register_trans_action(fsm_t *const me, fsm_trans_t *trans,
242243
/**
243244
* @brief Function to register callbacks for a FSM state.
244245
*/
245-
fsm_err_t fsm_register_state_actions(fsm_t *const me, int state,
246+
fsm_err_t fsm_register_state_actions(fsm_t *const me, uint8_t state,
246247
fsm_action_t enter, fsm_action_t update,
247248
fsm_action_t exit) {
248249
/* Check if the FSM instance is valid */
249250
if (me == NULL) {
250251
return FSM_ERR_INVALID_PARAM;
251252
}
252253

253-
/* Check if the FSM state is valid */
254-
if (state < 0) {
255-
return FSM_ERR_INVALID_PARAM;
256-
}
257-
258254
if (state >= me->actions_list.len) {
259255
/* Allocate */
260256
fsm_action_t(*ptr)[3] =
@@ -302,7 +298,7 @@ fsm_err_t fsm_run(fsm_t *const me) {
302298

303299
/* Evaluate the transition event and get the next FSM state. If the current
304300
FSM state change then execute the exit action */
305-
int next_state =
301+
uint8_t next_state =
306302
get_next_state(&me->trans_list, me->current_state, now_ms - me->entry_ms);
307303

308304
if (next_state != me->current_state) {
@@ -316,8 +312,8 @@ fsm_err_t fsm_run(fsm_t *const me) {
316312
}
317313

318314
/* Private functions ---------------------------------------------------------*/
319-
static int get_next_state(fsm_trans_list_t *trans_list, int current_state,
320-
uint32_t elapsed_ms) {
315+
static uint8_t get_next_state(fsm_trans_list_t *trans_list,
316+
uint8_t current_state, uint32_t elapsed_ms) {
321317
for (size_t i = 0; i < trans_list->len; i++) {
322318
/* Find coincidences for current state */
323319
fsm_trans_t *trans = &trans_list->trans[i];
@@ -360,7 +356,8 @@ static int get_next_state(fsm_trans_list_t *trans_list, int current_state,
360356
return current_state;
361357
}
362358

363-
static void execute_action(int current_state, fsm_actions_list_t *actions_list,
359+
static void execute_action(uint8_t current_state,
360+
fsm_actions_list_t *actions_list,
364361
fsm_action_type_t type) {
365362
/* Check if actions type is valid*/
366363
if (type < FSM_ACTION_TYPE_ENTRY || type >= FSM_ACTION_TYPE_MAX) {

include/fsm.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ typedef struct {
8282
} fsm_actions_list_t;
8383

8484
typedef struct {
85-
int present_state;
86-
int next_state;
85+
uint8_t present_state;
86+
uint8_t next_state;
8787

8888
struct {
8989
fsm_event_t *events;
@@ -103,8 +103,8 @@ typedef struct {
103103
typedef uint32_t (*fsm_time_t)(void);
104104

105105
typedef struct {
106-
int current_state;
107-
int prev_state;
106+
uint8_t current_state;
107+
uint8_t prev_state;
108108
fsm_trans_list_t trans_list;
109109
fsm_actions_list_t actions_list;
110110
fsm_time_t get_ms;
@@ -125,7 +125,7 @@ typedef struct {
125125
* - FSM_ERR_OK: succeed
126126
* - FSM_ERR_INVALID_PARAM: invalid parameter
127127
*/
128-
fsm_err_t fsm_init(fsm_t *const me, int init_state, fsm_time_t get_ms);
128+
fsm_err_t fsm_init(fsm_t *const me, uint8_t init_state, fsm_time_t get_ms);
129129

130130
/**
131131
* @brief Function to define and add a transition betwen 2 states for a FSM
@@ -144,7 +144,7 @@ fsm_err_t fsm_init(fsm_t *const me, int init_state, fsm_time_t get_ms);
144144
* - FSM_ERR_NO_MEM: out of memory
145145
*/
146146
fsm_err_t fsm_add_transition(fsm_t *const me, fsm_trans_t **trans,
147-
int from_state, int next_state);
147+
uint8_t from_state, uint8_t next_state);
148148

149149
/**
150150
* @brief Function to set the operator to evaluate the transition events.
@@ -221,7 +221,7 @@ fsm_err_t fsm_register_trans_action(fsm_t *const me, fsm_trans_t *trans,
221221
* - FSM_ERR_INVALID_PARAM: invalid parameter
222222
* - FSM_ERR_NO_MEM: out of memory
223223
*/
224-
fsm_err_t fsm_register_state_actions(fsm_t *const me, int state,
224+
fsm_err_t fsm_register_state_actions(fsm_t *const me, uint8_t state,
225225
fsm_action_t enter, fsm_action_t update,
226226
fsm_action_t exit);
227227

0 commit comments

Comments
 (0)