Urbanite: Parking aid system
|
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_t * | fsm_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... | |
Button FSM main file.
|
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
p_this | Pointer to an fsm_t struct than contains an fsm_button_t . |
|
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
p_this | Pointer to an fsm_t struct than contains an fsm_button_t . |
|
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 fieldnext_timeout
✅ 3. Returntrue
if it is higher, otherwise returnfalse
p_this | Pointer to an fsm_t struct than contains an fsm_button_t . |
|
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 fieldduration
considering it and the fieldtick_pressed
✅ 3. Update the fieldnext_timeout
considering the current tick and the fielddebounce_time_ms
p_this | Pointer to an fsm_t struct than contains an fsm_button_t . |
|
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 fieldtick_pressed
✅ 3. Update the fieldnext_timeout
considering the current tick and the fielddebounce_time_ms
p_this | Pointer to an fsm_t struct than contains an fsm_button_t . |
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 (fieldf
of the struct).
✅ 2. Returnfalse
if the current state isBUTTON_RELEASED
. Otherwise, returntrue
.
p_fsm | Pointer to an fsm_button_t struct. |
void fsm_button_destroy | ( | fsm_button_t * | p_fsm | ) |
Destroy a button FSM.
This function destroys a button FSM and frees the memory.
p_fsm | Pointer to an fsm_button_t struct. |
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 thefsm_t
struct.
p_fsm | Pointer to an fsm_button_t struct. |
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
p_fsm | Pointer to an fsm_button_t struct. |
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
p_fsm | Pointer to an fsm_button_t struct. |
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.
p_fsm | Pointer to an fsm_button_t struct. |
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 (fieldf
of the struct).
p_fsm | Pointer to an fsm_button_t struct. |
|
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.
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.
TODO alumnos:
✅ 1. Call the
fsm_init()
to initialize the FSM. Pass the address of thefsm_t
struct and the transition table
✅ 2. Initialize the fieldsdebounce_time_ms
,button_id
ofp_fsm_button
with the received values
✅ 3. Initialize the fieldstick_pressed
,duration
ofp_fsm_button
with 0
✅ 4. Call the functionport_button_init()
p_fsm_button | Pointer to the button FSM. |
debounce_time | Anti-debounce time in milliseconds |
button_id | Unique button identifier number |
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.
debounce_time_ms | Debounce time in milliseconds. |
button_id | Button ID. Must be unique. |
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
p_fsm | Pointer to an fsm_button_t struct. |
|
static |
Array representing the transitions table of the FSM button.