|
Simone: a game of visual memory and speed
|
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_t * | fsm_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... | |
RGB light system FSM main file.
| 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_tand overwrite the results into the color.
| p_color | Pointer to the color to be corrected. |
| intensity_perc | Linear input intensity, in the range [0, 100]. |
|
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
| p_this | Pointer to an fsm_t struct than contains an fsm_rgb_light_t. |
|
static |
Check if the RGB light is set to be inactive (OFF).
TODO alumnos:
✅ 1. Return the inverse of the flag
status
| p_this | Pointer to an fsm_t struct than contains an fsm_rgb_light_t. |
|
static |
Check if a new color has to be set.
TODO alumnos:
✅ 1. Return the flag
new_color
| p_this | Pointer to an fsm_t struct than contains an fsm_rgb_light_t. |
|
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 functionport_rgb_light_set_rgb()with the RGB LED ID and the color
✅ 3. Reset the flagnew_colorto 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
| p_this | Pointer to an fsm_t struct than contains an fsm_rgb_light_t. |
|
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 flagidleto indicate that the RGB light system is not idle
| p_this | Pointer to an fsm_t struct than contains an fsm_rgb_light_t. |
|
static |
Turn the RGB light system ON for the first time.
TODO alumnos:
✅ 1. Call function
port_rgb_light_set_rgb()with the RGB LED ID with no color (all the duty cycles to 0).
| p_this | Pointer to an fsm_t struct than contains an fsm_rgb_light_t. |
| 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
trueif the RGB light system is active and it is not idle. Otherwise, returnfalse.
| p_fsm | Pointer to an fsm_rgb_light_t struct. |
| 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.
| p_fsm | Pointer to an fsm_rgb_light_t struct. |
| 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 thefsm_tstruct.
| p_fsm | Pointer to an fsm_rgb_light_t struct. |
| 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.
| p_fsm | Pointer to an fsm_rgb_light_t struct. |
|
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().
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.
TODO alumnos:
✅ 1. Call the
fsm_init()to initialize the FSM. Pass the address of thefsm_tstruct 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.
| p_fsm_rgb_light | Pointer to the RGB light FSM. |
| rgb_light_id | Unique RGB light identifier number. |
| 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.
| rgb_light_id | RGB light ID. Must be unique. |
| 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
colorandintensity_percfields of the RGB light system FSM.
✅ 2. Set thenew_colorfield accordingly to indicate that a new color has to be set.
| p_fsm | Pointer to an fsm_rgb_light_t struct. |
| color | RGB color to set. |
| intensity_perc | Intensity in percentage to set. |
| 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
statuswith the received value
| p_fsm | Pointer to an fsm_rgb_light_t struct. |
| pause | Status of the RGB light system. true if the RGB light system is paused, false if the RGB light system is active. |
|
static |
Array representing the transitions table of the FSM RGB light.