/*
 * Copyright (c) 2026 Martin Javier Klöckner <mklockner@fi.uba.ar>
 *
 * See file `LICENSE` for full details
 */

#ifndef TASK_INC_TASK_LEDS_ATTRIBUTE_H_
#define TASK_INC_TASK_LEDS_ATTRIBUTE_H_

#ifdef __cplusplus
extern "C" {
#endif

/* Led Statechart - State Transition Table */
/* +-----------------------+-----------------------+-------------+-----------------------+-----------------------+
 * | Current               | Event                 |             | Next                  |                       |
 * | State                 | (Parameters)          | [Guard]     | State                 | Actions               |
 * |=======================+=======================+=============+=======================+=======================|
 * | INICIAL               |                       |             | ST_LED_XX_OFF         | led = LED_OFF         |
 * |                       |                       |             |                       | op_led(led)           |
 * |-----------------------+-----------------------+-------------+-----------------------+-----------------------|
 * | ST_LED_XX_OFF         | EV_LED_XX_ON          |             | ST_LED_XX_ON          | led = LED_ON          |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       |-----------------------+-------------+-----------------------+-----------------------|
 * |                       | EV_LED_XX_BLINK       |             | ST_LED_XX_BLINK_ON    | tick = tick_max       |
 * |                       |                       |             |                       | led = LED_ON          |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       |-----------------------+-------------+-----------------------+-----------------------|
 * |                       | EV_LED_XX_PULSE       |             | ST_LED_XX_PULSE       | tick = tick_max       |
 * |                       |                       |             |                       | led = LED_ON          |
 * |                       |                       |             |                       | op_led(led)           |
 * |-----------------------+-----------------------+-------------+-----------------------+-----------------------|
 * | ST_LED_XX_ON          | EV_LED_XX_OFF         |             | ST_LED_XX_OFF         |  led = LED_OFF        |
 * |                       |                       |             |                       | op_led(led)           |
 * |-----------------------+-----------------------+-------------+-----------------------+-----------------------|
 * | ST_LED_XX_BLINK_ON    | EV_LED_XX_OFF         |             | ST_LED_XX_OFF         | led = LED_OFF         |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       +-----------------------+-------------+-----------------------+-----------------------|
 * |                       | EV_LED_XX_ON          |             | ST_LED_XX_ON          | led = LED_ON          |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       +-----------------------+-------------+-----------------------+-----------------------|
 * |                       |                       | [tick == 0] | ST_LED_XX_BLINK_OFF   | tick = tick_max       |
 * |                       |                       |             |                       | led = LED_OFF         |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       +-----------------------+-------------+-----------------------+-----------------------|
 * |                       |                       | [tick >  0] | ST_LED_XX_BLINK_ON    | tick--                |
 * |-----------------------+-----------------------+-------------+-----------------------+-----------------------|
 * | ST_LED_XX_BLINK_OFF   | EV_LED_XX_OFF         |             | ST_LED_XX_OFF         | led = LED_OFF         |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       +-----------------------+-------------+-----------------------+-----------------------|
 * |                       | EV_LED_XX_ON          |             | ST_LED_XX_ON          | led = LED_ON          |
 * |                       |                       |             |                       | op_led(led)           |
 * |                       +-----------------------+-------------+-----------------------+-----------------------|
 * |                       |                       | [tick >  0] | ST_LED_XX_BLINK_OFF   | tick--                |
 * |                       +-----------------------+-------------+-----------------------+-----------------------|
 * |                       |                       | [tick == 0] | ST_LED_XX_BLINK_ON    | tick = tick_max       |
 * |                       |                       |             |                       | led = LED_ON          |
 * |                       |                       |             |                       | op_led(led)           |
 * |-----------------------+-----------------------+-------------+-----------------------+-----------------------|
 * | ST_LED_XX_PULSE       |                       | [tick == 0] | ST_LED_XX_OFF         | tick = tick_max       |
 * |                       |                       |             |                       | led = LED_OFF         |
 * |                       |                       |             |                       | op_led(led)           |
 * |-----------------------+-----------------------+-------------+-----------------------+-----------------------|
 * |                       |                       | [tick >  0] | ST_LED_XX_PULSE       | tick--                |
 * +-----------------------+-----------------------+-------------+-----------------------+-----------------------+ */

/* Events to excite Task Led */
typedef enum {
    EV_LED_XX_OFF,
    EV_LED_XX_ON,
    EV_LED_XX_NOT_BLINK,
    EV_LED_XX_BLINK,
    EV_LED_XX_PULSE
} task_leds_ev_t;

/* States of Task Led */
typedef enum {
    ST_LED_XX_OFF,
    ST_LED_XX_ON,
    ST_LED_XX_BLINK_ON,
    ST_LED_XX_BLINK_OFF,
    ST_LED_XX_PULSE
} task_leds_st_t;

/* Identifier of Task Led */
typedef enum {
    ID_LED_A,
    ID_LED_B
} task_leds_id_t;

typedef struct
{
    task_leds_id_t identifier;
    GPIO_TypeDef *gpio_port;
    uint16_t pin;
    GPIO_PinState led_on;
    GPIO_PinState led_off;
    uint32_t tick_blink;
    uint32_t tick_pulse;
} task_leds_cfg_t;

typedef struct
{
    uint32_t tick;
    task_leds_st_t state;
    task_leds_ev_t event;
    bool flag;
} task_leds_dta_t;

extern task_leds_dta_t task_leds_dta_list[];

#ifdef __cplusplus
}
#endif

#endif /* TASK_INC_TASK_LEDS_ATTRIBUTE_H_ */
