Urbanite: Parking aid system
fsm_button.h File Reference

Header for fsm_button.c file. More...

#include <stdint.h>
#include <stdbool.h>
#include "fsm.h"

Typedefs

typedef struct fsm_button_t fsm_button_t
 Structure to define the Button FSM.
 

Enumerations

enum  FSM_BUTTON {
  BUTTON_RELEASED = 0,
  BUTTON_RELEASED_WAIT,
  BUTTON_PRESSED,
  BUTTON_PRESSED_WAIT
}
 Enumerator for the button finite state machine. More...
 

Functions

fsm_button_tfsm_button_new (uint32_t debounce_time_ms, uint32_t button_id)
 Create a new button FSM. More...
 
void fsm_button_destroy (fsm_button_t *p_fsm)
 Destroy a button FSM. More...
 
void fsm_button_fire (fsm_button_t *p_fsm)
 Fire the button FSM. More...
 
fsm_t * fsm_button_get_inner_fsm (fsm_button_t *p_fsm)
 Get the inner FSM of the button. More...
 
uint32_t fsm_button_get_state (fsm_button_t *p_fsm)
 Get the state of the button FSM. More...
 
uint32_t fsm_button_get_duration (fsm_button_t *p_fsm)
 Return the duration of the last button press. More...
 
void fsm_button_reset_duration (fsm_button_t *p_fsm)
 Reset the duration of the last button press. More...
 
uint32_t fsm_button_get_debounce_time_ms (fsm_button_t *p_fsm)
 Get the debounce time of the button FSM. More...
 
bool fsm_button_check_activity (fsm_button_t *p_fsm)
 Check if the button FSM is active, or not. More...
 

Detailed Description

Header for fsm_button.c file.

Author
Sistemas Digitales II
Date
2025-01-01

Enumeration Type Documentation

◆ FSM_BUTTON

enum FSM_BUTTON

Enumerator for the button finite state machine.

This enumerator defines the different states that the button finite state machine can be in. Each state represents a specific condition or step in the button press process.

Enumerator
BUTTON_RELEASED 

Starting state. Also comes here when the button has been released

BUTTON_RELEASED_WAIT 

State to perform the anti-debounce mechanism for a falling edge

BUTTON_PRESSED 

State while the button is being pressed

BUTTON_PRESSED_WAIT 

State to perform the anti-debounce mechanism for a rising edge

Function Documentation

◆ fsm_button_check_activity()

bool fsm_button_check_activity ( fsm_button_t p_fsm)

Check if the button FSM is active, or not.

The button is inactive when it is in the status BUTTON_RELEASED.

TODO alumnos Version 4:

✅ 1. Get the field current_state of the FSM (field f of the struct).
✅ 2. Return false if the current state is BUTTON_RELEASED. Otherwise, return true.

Parameters
p_fsmPointer to an fsm_button_t struct.
Returns
true
false

◆ fsm_button_destroy()

void fsm_button_destroy ( fsm_button_t p_fsm)

Destroy a button FSM.

This function destroys a button FSM and frees the memory.

Parameters
p_fsmPointer to an fsm_button_t struct.

◆ fsm_button_fire()

void fsm_button_fire ( fsm_button_t p_fsm)

Fire the button FSM.

This function is used to fire the button FSM. It is used to check the transitions and execute the actions of the button FSM.

TODO alumnos:

✅ 1. Call the fsm_fire() function. Pass the address of the fsm_t struct.

Parameters
p_fsmPointer to an fsm_button_t struct.

◆ fsm_button_get_debounce_time_ms()

uint32_t fsm_button_get_debounce_time_ms ( fsm_button_t p_fsm)

Get the debounce time of the button FSM.

This function returns the debounce time of the button FSM.

TODO alumnos:

✅ 1. Retrieve and return the field debounce_time_ms

Parameters
p_fsmPointer to an fsm_button_t struct.
Returns
uint32_t Debounce time in milliseconds.

◆ fsm_button_get_duration()

uint32_t fsm_button_get_duration ( fsm_button_t p_fsm)

Return the duration of the last button press.

TODO alumnos:

✅ 1. Retrieve and return the field duration

Parameters
p_fsmPointer to an fsm_button_t struct.
Returns
uint32_t Duration of the last button press in milliseconds.

◆ fsm_button_get_inner_fsm()

fsm_t* fsm_button_get_inner_fsm ( fsm_button_t p_fsm)

Get the inner FSM of the button.

This function returns the inner FSM of the button.

    💡 This function is important because the struct is private and external functions such as those of the unit tests cannot access the inner FSM directly.

TODO alumnos:

✅ 1. Return the address of the f field of the struct.

Parameters
p_fsmPointer to an fsm_button_t struct.
Returns
fsm_t* Pointer to the inner FSM.

◆ fsm_button_get_state()

uint32_t fsm_button_get_state ( fsm_button_t p_fsm)

Get the state of the button FSM.

This function returns the current state of the button FSM.

    💡 This function is important because the struct is private and external functions such as those of the unit tests cannot access the state of the FSM directly.

TODO alumnos:

✅ 1. Retrieve and return the field current_state of the FSM (field f of the struct).

Parameters
p_fsmPointer to an fsm_button_t struct.
Returns
uint32_t Current state of the button FSM.

◆ fsm_button_new()

fsm_button_t* fsm_button_new ( uint32_t  debounce_time_ms,
uint32_t  button_id 
)

Create a new button FSM.

This function creates a new button FSM with the given debounce time and button ID.

Parameters
debounce_time_msDebounce time in milliseconds.
button_idButton ID. Must be unique.
Returns
fsm_button_t* Pointer to the button FSM.

◆ fsm_button_reset_duration()

void fsm_button_reset_duration ( fsm_button_t p_fsm)

Reset the duration of the last button press.

TODO alumnos:

✅ 1. Set to 0 the field duration

Parameters
p_fsmPointer to an fsm_button_t struct.