/** 
 * @file    scope_ring.h
 * @brief   Scope ring module
 * @author  Kenny Tran
 * @date    Apr 05 2023
 * @version 0.0.1
 *
 * @section LICENSES
 * Copyright (C) Morgan Advanced Programmable Systems, Inc.
 */


#ifndef SCOPE_RING_H_
#define SCOPE_RING_H_


/*--------------------------------------------------------------------------------------------------
 * INCLUDE FILES
 *------------------------------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>

#include "../lib/rgb.h"


/*--------------------------------------------------------------------------------------------------
 * MACROS & CONSTANTS
 *------------------------------------------------------------------------------------------------*/
#define SCOPE_RING_TASK_PERIOD_MS               (10)
#define SCOPE_RING_STACK_SIZE                   (2048)
#define SCOPE_RING_THREAD_PRIORITY              (11)

#define SCOPE_RING_BRIGHTNESS_STEPS             (10)
#define SCOPE_RING_BRIGHTNESS_GAMMA_CF          (1.6)

#define SCOPE_RING_BRIGHTNESS_STEP_DEFAULT      (5)
#define SCOPE_RING_INDICATORS_COLOR_DEFAULT     (RGB_COLOR_RED)
#define SCOPE_RING_LEVEL_COLOR_DEFAULT          (RGB_COLOR_GREEN)
#define SCOPE_RING_BACKGROUND_COLOR_DEFAULT     (RGB_COLOR_WHITE)
#define SCOPE_RING_LEVEL_VISCOSITY_DEFAULT      (0.03)
#define SCOPE_RING_LEVEL_SENSITIVITY_DEFAULT    (0.5)
#define SCOPE_RING_HYS_ANGLE_THRESHOLD          (0.25)

#define SCOPE_RING_LEVEL_OPTION_DEFAULT         (SCOPE_RING_LEVEL_OPTION_0)

#define SCOPE_RING_LEVEL_SENSITIVITY_MIN        (0.25)
#define SCOPE_RING_LEVEL_SENSITIVITY_MAX        (3.0)

#define SCOPE_RING_FILENAME                     ("scope_ring")


/*--------------------------------------------------------------------------------------------------
 * TYPE DEFINITIONS & ENUMERATIONS
 *------------------------------------------------------------------------------------------------*/
typedef enum SCOPE_RING_led
{
    SCOPE_RING_LED_BACKGROUND = 0,
    SCOPE_RING_LED_INDICATORS,
    SCOPE_RING_LED_LEVEL
} SCOPE_RING_led_e;

typedef enum SCOPE_RING_level_option
{
    SCOPE_RING_LEVEL_OPTION_0 = 0,
    SCOPE_RING_LEVEL_OPTION_1,
    SCOPE_RING_LEVEL_OPTION_2,

    SCOPE_RING_LEVEL_OPTION_COUNT,
    SCOPE_RING_LEVEL_OPTION_INVALID = SCOPE_RING_LEVEL_OPTION_COUNT
} SCOPE_RING_level_option_e;

typedef enum SCOPE_RING_state
{
    SCOPE_RING_STATE_OFF = 0,
    SCOPE_RING_STATE_ON,

    SCOPE_RING_STATE_SET_FLAT,
    SCOPE_RING_STATE_FLAT_CALIBRATION,
    SCOPE_RING_STATE_SET_BACK,
    SCOPE_RING_STATE_BACK_CALIBRATION,
    SCOPE_RING_STATE_SET_FRONT,
    SCOPE_RING_STATE_FRONT_CALIBRATION,
    
    SCOPE_RING_STATE_CONFIG_LEVEL_COLOR,
    SCOPE_RING_STATE_CONFIG_LEVEL_BRIGHTNESS,
    SCOPE_RING_STATE_CONFIG_INDICATORS_COLOR,
    SCOPE_RING_STATE_CONFIG_INDICATORS_BRIGHTNESS,
    SCOPE_RING_STATE_CONFIG_BACKGROUND_COLOR,
    SCOPE_RING_STATE_CONFIG_BACKGROUND_BRIGHTNESS,
    SCOPE_RING_STATE_CONFIG_LEVEL_SENSITIVITY,
    SCOPE_RING_STATE_CONFIG_LEVEL_OPTION,
    
    SCOPE_RING_STATE_RAINBOW,
    SCOPE_RING_STATE_CHARGING,
    SCOPE_RING_STATE_CHARGER_CONNECTED,
    SCOPE_RING_STATE_CHARGER_DISCONNECTED,
    SCOPE_RING_STATE_POWER_ON,
    SCOPE_RING_STATE_POWER_OFF,
    SCOPE_RING_STATE_BLE_ENABLED,
    SCOPE_RING_STATE_BLE_DISABLED,

    SCOPE_RING_STATE_INVALID
} SCOPE_RING_state_e;

typedef struct SCOPE_RING_config
{
    RGB_color_e indicators_color      : 8;
    uint8_t     indicators_brightness : 8;
    RGB_color_e level_color           : 8;
    uint8_t     level_brightness      : 8;
    RGB_color_e background_color      : 8;
    uint8_t     background_brightness : 8;
    uint8_t     level_option          : 4;
    float       level_viscosity;
    float       level_sensitivity;
} __attribute__((packed, aligned(1))) SCOPE_RING_config_s;


/*--------------------------------------------------------------------------------------------------
 * PRIVATE CONSTANTS & VARIABLES
 *------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------
 * PRIVATE FUNCTION PROTOTYPES
 *------------------------------------------------------------------------------------------------*/


/*--------------------------------------------------------------------------------------------------
 * PUBLIC FUNCTION PROTOTYPES
 *------------------------------------------------------------------------------------------------*/
void SCOPE_RING_init (void);

/* Set duration_ms to 0 to run indefinitely. */
void SCOPE_RING_state_set (SCOPE_RING_state_e state, uint32_t duration_ms);

SCOPE_RING_state_e SCOPE_RING_state_get (bool *p_finshed);

void SCOPE_RING_color_set (SCOPE_RING_led_e led, RGB_color_e color);

void SCOPE_RING_brightness_set (SCOPE_RING_led_e led, uint8_t step, bool save_settings);

void SCOPE_RING_battery_indicator_set (float percentage);

void SCOPE_RING_level_option_set (SCOPE_RING_level_option_e option);

void SCOPE_RING_level_viscosity_set (float alpha);

void SCOPE_RING_level_sensitivity_set (float sensitivity);

SCOPE_RING_config_s SCOPE_RING_config_get (void);

void SCOPE_RING_task_suspend (void);

void SCOPE_RING_task_resume (void);

void SCOPE_RING_task_install (void);

void SCOPE_RING_cli_cmd (int32_t argc, char **argv);


#endif /* SCOPE_RING_H_ */
