#ifndef _DM_KEY_EVENT_H_
#define _DM_KEY_EVENT_H_

#define DM_KEY_EV_SLOT_UNIT                 (50)                             ///< unit: 10ms
#define DM_KEY_EV_CLICK_SLOT_FLOOR          ((2) * DM_KEY_EV_SLOT_UNIT)     ///< unit: 10ms
#define DM_KEY_EV_CLICK_SLOT_TOP            ((4) * DM_KEY_EV_SLOT_UNIT)
#define DM_KEY_EV_DOUBLE_CLICK_INTVL        ((5) * DM_KEY_EV_SLOT_UNIT)
#define DM_KEY_EV_REPEAT_INTVL              ((2) * DM_KEY_EV_SLOT_UNIT)
#define DM_KEY_EV_LONG_PRESS_STEP           ((20) * DM_KEY_EV_REPEAT_INTVL)

#define DM_KEY_EV_COMBO_BTN_NUM             (2)                             //< max combo button number
#define DM_KEY_EV_COMBO_BTN_MARGIN          ((3) * DM_KEY_EV_SLOT_UNIT)     //< unit: 10ms

#define DM_KEY_EV_INVALID_BTN_ID            ((dm_btn_id_t)0xFFFF)

typedef enum
{
    DM_KEY_EV_ACT_PRESSED,
    DM_KEY_EV_ACT_RELEASED,
    DM_KEY_EV_ACT_CLICK,
    DM_KEY_EV_ACT_DCLICK,
    DM_KEY_EV_ACT_REPEAT,
    DM_KEY_EV_ACT_LONG_PRESS        = 10,
    DM_KEY_EV_ACT_VLONG_PRESS       = (2 * DM_KEY_EV_ACT_LONG_PRESS),
    DM_KEY_EV_ACT_NONE              = 0xFF
} dm_key_ev_action_t;


typedef struct
{
    uint16_t           event;
    dm_btn_id_t        btn[DM_KEY_EV_COMBO_BTN_NUM];    ///< unused button item should be set to 0xFFFF
    dm_key_ev_action_t action;
} dm_key_ev_map_t;

typedef struct
{
    uint8_t            scan_valid_times;                ///< when scan times after record value >= scan_valid_time,the action is repoter.
    uint8_t            scan_times;                      ///< scan times
    uint32_t           scan_interval;                   ///< scan interval (ms)
    uint32_t           btn_row_io_map;                  ///< refer to dm_button module
    uint32_t           btn_col_io_map;
} dm_key_button_cfg_t;

typedef void (*dm_key_ev_cbk_t)(uint16_t event);

typedef struct
{
    dm_key_button_cfg_t button_cfg;
    dm_key_ev_map_t*    evt_map;                         ///< map of event and button|action
                                                         ///< if combo buttons supported but not used,
                                                         ///< button id should be set to 0xFFFF
    uint16_t            evt_map_elt_num;                 ///< element number of event map
    dm_key_ev_cbk_t     cbk;                             ///< must not be NULL
} dm_key_ev_conf_t;

void dm_key_ev_init(void);
void dm_key_ev_config(dm_key_ev_conf_t* p_conf);
void key_event_fsm(uint8_t group, void* p_param);
#endif
