Urbanite: Parking aid system
fsm_button.c File Reference

Button FSM main file. More...

#include <stdlib.h>
#include "port_button.h"
#include "port_system.h"
#include "fsm.h"
#include "fsm_button.h"

Data Structures

struct  fsm_button_t
 Structure of the Button FSM. More...
 

Functions

static bool check_button_pressed (fsm_t *p_this)
 Check if the button has been pressed. More...
 
static bool check_button_released (fsm_t *p_this)
 Check if the button has been released. More...
 
static bool check_timeout (fsm_t *p_this)
 Check if the debounce-time has passed. More...
 
static void do_store_tick_pressed (fsm_t *p_this)
 Store the system tick when the button was pressed. More...
 
static void do_set_duration (fsm_t *p_this)
 Store the duration of the button press. More...
 
static void fsm_button_init (fsm_button_t *p_fsm_button, uint32_t debounce_time, uint32_t button_id)
 Initialize a button FSM. More...
 
fsm_button_tfsm_button_new (uint32_t debounce_time, 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...
 

Variables

static fsm_trans_t fsm_trans_button []
 Array representing the transitions table of the FSM button. More...
 

Detailed Description

Button FSM main file.

Author
Sistemas Digitales II
Date
2025-01-01

Function Documentation

◆ check_button_pressed()

static bool check_button_pressed ( fsm_t *  p_this)
static

Check if the button has been pressed.

TODO alumnos:

✅ 1. Call function port_button_get_pressed() and retrieve the status
✅ 2. Return the status

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_button_t.
Returns
true
false

◆ check_button_released()

static bool check_button_released ( fsm_t *  p_this)
static

Check if the button has been released.

TODO alumnos:

✅ 1. Call function port_button_get_pressed() and retrieve the status
✅ 2. Return the inverse of the status

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_button_t.
Returns
true
false

◆ check_timeout()

static bool check_timeout ( fsm_t *  p_this)
static

Check if the debounce-time has passed.

TODO alumnos:

✅ 1. Call function port_system_get_millis() and retrieve the current system tick
✅ 2. Check if the current system tick is higher than the field next_timeout
✅ 3. Return true if it is higher, otherwise return false

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_button_t.
Returns
true
false

◆ do_set_duration()

static void do_set_duration ( fsm_t *  p_this)
static

Store the duration of the button press.

TODO alumnos:

✅ 1. Call function port_system_get_millis() and retrieve the current system tick
✅ 2. Update the field duration considering it and the field tick_pressed
✅ 3. Update the field next_timeout considering the current tick and the field debounce_time_ms

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_button_t.

◆ do_store_tick_pressed()

static void do_store_tick_pressed ( fsm_t *  p_this)
static

Store the system tick when the button was pressed.

TODO alumnos:

✅ 1. Call function port_system_get_millis() and retrieve the current system tick
✅ 2. Store it in the field tick_pressed
✅ 3. Update the field next_timeout considering the current tick and the field debounce_time_ms

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_button_t.

◆ 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_init()

static void fsm_button_init ( fsm_button_t p_fsm_button,
uint32_t  debounce_time,
uint32_t  button_id 
)
static

Initialize a button FSM.

This function initializes the default values of the FSM struct and calls to the port to initialize the associated HW given the ID.

This FSM implements an anti-debounce mechanism. Debounces (or very fast button presses) lasting less than the debounce_time_ms are filtered out.

The FSM stores the duration of the last button press. The user should ask for it using the function fsm_button_get_duration().

At start and reset, the duration value must be 0 ms. A value of 0 ms means that there has not been a new button press.

Attention
The user is required to reset the duration value once it has been read. Otherwise, this value may be misinterpreted by the user, if successive calls are made without having pressed the button. In such a case we would be reading past information. In order to reset the value, the function fsm_button_reset_duration() must be called.

In other words, the status flag of this FSM is the variable duration. A duration of 0 means that no new button has been pressed, a value other than 0 means that it has been pressed and the value is its duration, so it is the user's responsibility to clear this status flag.

The FSM contains information of the button ID. This ID is a unique identifier that is managed by the user in the port. That is where the user provides identifiers and HW information for all the buttons on his system. The FSM does not have to know anything of the underlying HW.

Note
Both unit and integration tests (example) are provided for this button library are available in this GitHub repository: https://github.com/sdg2DieUpm/urbanite/tree/urbanite_v1_test

TODO alumnos:

✅ 1. Call the fsm_init() to initialize the FSM. Pass the address of the fsm_t struct and the transition table
✅ 2. Initialize the fields debounce_time_ms, button_id of p_fsm_button with the received values
✅ 3. Initialize the fields tick_pressed, duration of p_fsm_button with 0
✅ 4. Call the function port_button_init()

Parameters
p_fsm_buttonPointer to the button FSM.
debounce_timeAnti-debounce time in milliseconds
button_idUnique button identifier number

◆ 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.

Variable Documentation

◆ fsm_trans_button

fsm_trans_t fsm_trans_button[]
static

Array representing the transitions table of the FSM button.