Simone: a game of visual memory and speed
fsm_rgb_light.c File Reference

RGB light system FSM main file. More...

#include <stdlib.h>
#include <stdio.h>
#include "port_rgb_light.h"
#include "port_system.h"
#include "fsm.h"
#include "fsm_rgb_light.h"
#include "rgb_colors.h"

Functions

void _correct_rgb_light_levels (rgb_color_t *p_color, uint8_t intensity_perc)
 Apply correction to an RGB color based on the intensity value. 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 RGB light 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 RGB light is set to be inactive (OFF). More...
 
static void do_set_on (fsm_t *p_this)
 Turn the RGB light 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 intensity measured by the ultrasound sensor. More...
 
static void do_set_off (fsm_t *p_this)
 Turn the RGB light system OFF. More...
 
static void fsm_rgb_light_init (fsm_rgb_light_t *p_fsm_rgb_light, uint8_t rgb_light_id)
 Initialize an RGB light system FSM. More...
 
fsm_rgb_light_tfsm_rgb_light_new (uint8_t rgb_light_id)
 Create a new RGB light FSM. More...
 
void fsm_rgb_light_destroy (fsm_rgb_light_t *p_fsm)
 Destroy an RGB light FSM. More...
 
void fsm_rgb_light_fire (fsm_rgb_light_t *p_fsm)
 Fire the RGB light FSM. More...
 
void fsm_rgb_light_set_color_intensity (fsm_rgb_light_t *p_fsm, rgb_color_t color, uint8_t intensity_perc)
 Set the color and intensity of the RGB light. More...
 
bool fsm_rgb_light_get_status (fsm_rgb_light_t *p_fsm)
 Get the status of the RGB light FSM. More...
 
void fsm_rgb_light_set_status (fsm_rgb_light_t *p_fsm, bool status)
 Set the status of the RGB light FSM. More...
 
bool fsm_rgb_light_check_activity (fsm_rgb_light_t *p_fsm)
 Check if the RGB light system is active. More...
 

Variables

static fsm_trans_t fsm_trans_rgb_light []
 Array representing the transitions table of the FSM RGB light. More...
 

Detailed Description

RGB light system FSM main file.

Author
Sistemas Digitales II
Date
2026-01-01

Function Documentation

◆ _correct_rgb_light_levels()

void _correct_rgb_light_levels ( rgb_color_t p_color,
uint8_t  intensity_perc 
)

Apply correction to an RGB color based on the intensity value.

This function takes a regular color and applies a reduction based on the given intensity.

TODO alumnos:

✅ 1. For each channel of the RGB color, scale its value by the intensity given. Use float numbers to operate. ✅ 2. Save it in the color struct as an integer. To round it properly add 0.5 to the result. This 0.5 is used to round the floating-point result to the nearest integer instead of truncating, improving pwm accuracy especially at low brightness levels. Finally, cast it to uint8_t and overwrite the results into the color.

Parameters
p_colorPointer to the color to be corrected.
intensity_percLinear input intensity, in the range [0, 100].

◆ check_active()

static bool check_active ( fsm_t *  p_this)
static

Check if the RGB light 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_rgb_light_t.
Returns
true If the RGB light system has been indicated to be active independently if it is idle or not.
false If the RGB light system has been indicated to be inactive.

◆ check_off()

static bool check_off ( fsm_t *  p_this)
static

Check if the RGB light 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_rgb_light_t.
Returns
true If the RGB light system has been indicated to be inactive.
false If the RGB light 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_rgb_light_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 intensity measured by the ultrasound sensor.

TODO alumnos:

✅ 1. Correct the levels of the RGB LEDs according to the intensity . Use the _correct_rgb_light_levels().
✅ 2. Call function port_rgb_light_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 RGB light system to idle. In this case, the RGB light system is active, but while the intensity is not changed, the RGB light system is idle and can enter in a low power mode

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_rgb_light_t.

◆ do_set_off()

static void do_set_off ( fsm_t *  p_this)
static

Turn the RGB light system OFF.

TODO alumnos:

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

Parameters
p_thisPointer to an fsm_t struct than contains an fsm_rgb_light_t.

◆ do_set_on()

static void do_set_on ( fsm_t *  p_this)
static

Turn the RGB light system ON for the first time.

Warning
In this version the RGB light 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_rgb_light_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_rgb_light_t.

◆ fsm_rgb_light_check_activity()

bool fsm_rgb_light_check_activity ( fsm_rgb_light_t p_fsm)

Check if the RGB light system is active.

This function checks if the RGB light system is active.

TODO alumnos

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

Parameters
p_fsmPointer to an fsm_rgb_light_t struct.
Returns
true If the RGB light system is active.
false If the RGB light system is inactive.

◆ fsm_rgb_light_destroy()

void fsm_rgb_light_destroy ( fsm_rgb_light_t p_fsm)

Destroy an RGB light FSM.

This function destroys an RGB light FSM and frees the memory.

TODO alumnos:

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

Parameters
p_fsmPointer to an fsm_rgb_light_t struct.

◆ fsm_rgb_light_fire()

void fsm_rgb_light_fire ( fsm_rgb_light_t p_fsm)

Fire the RGB light FSM.

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

TODO alumnos:

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

Parameters
p_fsmPointer to an fsm_rgb_light_t struct.

◆ fsm_rgb_light_get_status()

bool fsm_rgb_light_get_status ( fsm_rgb_light_t p_fsm)

Get the status of the RGB light FSM.

This function returns the status of the RGB light 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_rgb_light_t struct.
Returns
true If the RGB light system has been indicated to be active.
false If the RGB light system has been indicated to be paused.

◆ fsm_rgb_light_init()

static void fsm_rgb_light_init ( fsm_rgb_light_t p_fsm_rgb_light,
uint8_t  rgb_light_id 
)
static

Initialize an RGB light 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 RGB light level of the RGB light system. The user should set it using the function fsm_rgb_light_set_color_intensity().

Attention
This RGB light system is agnostic to the Simone game. It only shows the status of the RGB light system set by the user. It does not matter if the RGB light is for a game, a parking sensor, a door sensor, or any other device. The RGB light system only shows a status according to a level 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/simone/tree/simone_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 ''intensity_id. \n ✅ 3. Set theintensity_perctoMAX_LEVEL_INTENSITY. Initialize the color to OFF. \n ✅ 4. Initialize the flagsnew_color,status, andidletofalse. \n ✅ 5. Call functionport_rgb_light_init()` to initialize the HW.

Parameters
p_fsm_rgb_lightPointer to the RGB light FSM.
rgb_light_idUnique RGB light identifier number.

◆ fsm_rgb_light_new()

fsm_rgb_light_t* fsm_rgb_light_new ( uint8_t  rgb_light_id)

Create a new RGB light FSM.

This function creates a new RGB light FSM with the given RGB light ID.

Parameters
rgb_light_idRGB light ID. Must be unique.
Returns
fsm_rgb_light_t* Pointer to the RGB light FSM.

◆ fsm_rgb_light_set_color_intensity()

void fsm_rgb_light_set_color_intensity ( fsm_rgb_light_t p_fsm,
rgb_color_t  color,
uint8_t  intensity_perc 
)

Set the color and intensity of the RGB light.

This function sets the color and intensity of the RGB light.

TODO alumnos: ✅ 1. Set the color and intensity_perc fields of the RGB light 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_rgb_light_t struct.
colorRGB color to set.
intensity_percIntensity in percentage to set.

◆ fsm_rgb_light_set_status()

void fsm_rgb_light_set_status ( fsm_rgb_light_t p_fsm,
bool  pause 
)

Set the status of the RGB light FSM.

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

TODO alumnos:

✅ 1. Update the field status with the received value

Parameters
p_fsmPointer to an fsm_rgb_light_t struct.
pauseStatus of the RGB light system. true if the RGB light system is paused, false if the RGB light system is active.

Variable Documentation

◆ fsm_trans_rgb_light

fsm_trans_t fsm_trans_rgb_light[]
static

Array representing the transitions table of the FSM RGB light.