Urbanite: Parking aid system
fsm_display.c File Reference

Display system FSM main file. More...

#include <stdlib.h>
#include <stdio.h>
#include "port_display.h"
#include "port_system.h"
#include "fsm.h"
#include "fsm_display.h"

Data Structures

struct  fsm_display_t
 Structure of the Display FSM. More...
 

Functions

void _compute_display_levels (rgb_color_t *p_color, int32_t distance_cm)
 Set color levels of the RGB LEDs according to the distance. More...
 
static bool check_set_new_color (fsm_t *p_this)
 Check if a new color has to be set. More...
 
static bool check_active (fsm_t *p_this)
 Check if the display is set to be active (ON), independently if it is idle or not. More...
 
static bool check_off (fsm_t *p_this)
 Check if the display is set to be inactive (OFF). More...
 
static void do_set_on (fsm_t *p_this)
 Turn the display system ON for the first time. More...
 
static void do_set_color (fsm_t *p_this)
 Set the color of the RGB LED according to the distance measured by the ultrasound sensor. More...
 
static void do_set_off (fsm_t *p_this)
 Turn the display system OFF. More...
 
static void fsm_display_init (fsm_display_t *p_fsm_display, uint32_t display_id)
 Initialize a display system FSM. More...
 
fsm_display_tfsm_display_new (uint32_t display_id)
 Create a new display FSM. More...
 
void fsm_display_destroy (fsm_display_t *p_fsm)
 Destroy a display FSM. More...
 
void fsm_display_fire (fsm_display_t *p_fsm)
 Fire the display FSM. More...
 
void fsm_display_set_distance (fsm_display_t *p_fsm, uint32_t distance_cm)
 Set the display system to show the distance in cm. More...
 
bool fsm_display_get_status (fsm_display_t *p_fsm)
 Get the status of the display FSM. More...
 
void fsm_display_set_status (fsm_display_t *p_fsm, bool status)
 Set the status of the display FSM. More...
 
bool fsm_display_check_activity (fsm_display_t *p_fsm)
 Check if the display system is active. More...
 
fsm_t * fsm_display_get_inner_fsm (fsm_display_t *p_fsm)
 Get the inner FSM of the display. More...
 
uint32_t fsm_display_get_state (fsm_display_t *p_fsm)
 Get the state of the display FSM. More...
 
void fsm_display_set_state (fsm_display_t *p_fsm, int8_t state)
 Set the state of the display FSM. More...
 

Variables

static fsm_trans_t fsm_trans_display []
 Array representing the transitions table of the FSM display. More...
 

Detailed Description

Display system FSM main file.

Author
Sistemas Digitales II
Date
2025-01-01

Function Documentation

◆ _compute_display_levels()

void _compute_display_levels ( rgb_color_t p_color,
int32_t  distance_cm 
)

Set color levels of the RGB LEDs according to the distance.

This function sets the levels of an RGB LED according to the distance measured by the ultrasound sensor. This RGB LED structure is later passed to the port_display_set_rgb() function to set the color of the RGB LED.

Warning
Be careful! The RGB LED has a maximum value of PORT_DISPLAY_RGB_MAX_VALUE. The duty cycle of the PWM signal must be set according to the levels of the RGB LED.

TODO alumnos:

✅ 1. Set the levels of the RGB LED according to the distance measured by the ultrasound sensor. To do so, follow the flowchart:

Parameters
p_colorPointer to an rgb_color_t struct that will store the levels of the RGB LED.
distance_cmDistance measured by the ultrasound sensor in centimeters.

◆ check_active()

static bool check_active ( fsm_t *  p_this)
static

Check if the display is set to be active (ON), independently if it is idle or not.

TODO alumnos:

✅ 1. Return the flag status

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_display_t.
Returns
true If the display system has been indicated to be active independently if it is idle or not.
false If the display system has been indicated to be inactive.

◆ check_off()

static bool check_off ( fsm_t *  p_this)
static

Check if the display is set to be inactive (OFF).

TODO alumnos:

✅ 1. Return the inverse of the flag status

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_display_t.
Returns
true If the display system has been indicated to be inactive.
false If the display system has been indicated to be active.

◆ check_set_new_color()

static bool check_set_new_color ( fsm_t *  p_this)
static

Check if a new color has to be set.

TODO alumnos:

✅ 1. Return the flag new_color

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_display_t.
Returns
true If a new color has to be set
false If a new color does not have to be set

◆ do_set_color()

static void do_set_color ( fsm_t *  p_this)
static

Set the color of the RGB LED according to the distance measured by the ultrasound sensor.

TODO alumnos:

✅ 1. Compute the levels of the RGB LEDs according to the distance and set the display level
✅ 2. Call function port_display_set_rgb() with the RGB LED ID and the color
✅ 3. Reset the flag new_color to indicate that the color has been set
✅ 4. Set the display system to idle. In this case, the display system is active, but while the distance is not changed, the display system is idle and can enter in a low power mode

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_display_t.

◆ do_set_off()

static void do_set_off ( fsm_t *  p_this)
static

Turn the display system OFF.

TODO alumnos:

✅ 1. Call function port_display_set_rgb() with the RGB LED ID with no color (COLOR_OFF).
✅ 2. Reset the flag idle to indicate that the display system is not idle

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_display_t.

◆ do_set_on()

static void do_set_on ( fsm_t *  p_this)
static

Turn the display system ON for the first time.

Warning
In this version the display starts ON with no color. This is: the LED is turned ON but no color is set. In future versions you can set a default color at the beginning.

TODO alumnos:

✅ 1. Call function port_display_set_rgb() with the RGB LED ID with no color (all the duty cycles to 0).

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_display_t.

◆ fsm_display_check_activity()

bool fsm_display_check_activity ( fsm_display_t p_fsm)

Check if the display system is active.

This function checks if the display system is active.

TODO alumnos

✅ 1. Return true if the display system is active and it is not idle. Otherwise, return false.

Parameters
p_fsmPointer to an fsm_display_t struct.
Returns
true If the display system is active.
false If the display system is inactive.

◆ fsm_display_destroy()

void fsm_display_destroy ( fsm_display_t p_fsm)

Destroy a display FSM.

This function destroys a display FSM and frees the memory.

TODO alumnos:

✅ 1. Implement this function analogously to the fsm_display_destroy() function.

Parameters
p_fsmPointer to an fsm_display_t struct.

◆ fsm_display_fire()

void fsm_display_fire ( fsm_display_t p_fsm)

Fire the display FSM.

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

TODO alumnos:

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

Parameters
p_fsmPointer to an fsm_display_t struct.

◆ fsm_display_get_inner_fsm()

fsm_t* fsm_display_get_inner_fsm ( fsm_display_t p_fsm)

Get the inner FSM of the display.

This function returns the inner FSM of the display.

    💡 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_display_t struct.
Returns
fsm_t* Pointer to the inner FSM.

◆ fsm_display_get_state()

uint32_t fsm_display_get_state ( fsm_display_t p_fsm)

Get the state of the display FSM.

This function returns the current state of the display 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_display_t struct.
Returns
uint32_t Current state of the display FSM.

◆ fsm_display_get_status()

bool fsm_display_get_status ( fsm_display_t p_fsm)

Get the status of the display FSM.

This function returns the status of the display system. This function might be used for testing and debugging purposes.

TODO alumnos:

✅ 1. Retrieve and return the field status.

Parameters
p_fsmPointer to an fsm_display_t struct.
Returns
true If the display system has been indicated to be active.
false If the display system has been indicated to be paused.

◆ fsm_display_init()

static void fsm_display_init ( fsm_display_t p_fsm_display,
uint32_t  display_id 
)
static

Initialize a display system FSM.

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

The FSM stores the display level of the display system. The user should set it using the function fsm_display_set_distance().

Attention
This display system is agnostic to the ultrasound sensor or any other sensor. It only shows the status of the display system set by the user. It does not matter if the display is for a parking sensor, a door sensor, or any other sensor. The display system only shows a status according to a distance set by the user.

The FSM contains information of the RGB LED 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 RGB LEDs 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 ultrasound library are available in this GitHub repository: https://github.com/sdg2DieUpm/urbanite/tree/urbanite_v3_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 ''distance_id. \n ✅ 3. Set thedistance_cmto-1or any other invalid value in the range of the distance. \n ✅ 4. Initialize the flagsnew_color,status, andidletofalse. \n ✅ 5. Call functionport_display_init()` to initialize the HW.

Parameters
p_fsm_displayPointer to the display FSM.
display_idUnique display identifier number.

◆ fsm_display_new()

fsm_display_t* fsm_display_new ( uint32_t  display_id)

Create a new display FSM.

This function creates a new display FSM with the given display ID.

Parameters
display_idDisplay ID. Must be unique.
Returns
fsm_display_t* Pointer to the display FSM.

◆ fsm_display_set_distance()

void fsm_display_set_distance ( fsm_display_t p_fsm,
uint32_t  distance_cm 
)

Set the display system to show the distance in cm.

This function is used to set the display system to show the distance in cm.

TODO alumnos:

✅ 1. Set the distance in cm in the display system FSM.
✅ 2. Set the new_color field accordingly to indicate that a new color has to be set.

Parameters
p_fsmPointer to an fsm_display_t struct.
distance_cmDistance in cm to show in the display system.

◆ fsm_display_set_state()

void fsm_display_set_state ( fsm_display_t p_fsm,
int8_t  state 
)

Set the state of the display FSM.

This function sets the current state of the display 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.

Parameters
p_fsmPointer to an fsm_display_t struct.
stateNew state of the display FSM.

◆ fsm_display_set_status()

void fsm_display_set_status ( fsm_display_t p_fsm,
bool  pause 
)

Set the status of the display FSM.

This function is used to set the status of the display system. Indicating if the display system is active or paused.

TODO alumnos:

✅ 1. Update the field status with the received value

Parameters
p_fsmPointer to an fsm_display_t struct.
pauseStatus of the display system. true if the display system is paused, false if the display system is active.

Variable Documentation

◆ fsm_trans_display

fsm_trans_t fsm_trans_display[]
static

Array representing the transitions table of the FSM display.