/**
 * \file    sys_smg_display.h
 * \brief   System Segment Display
 * 
 * \par     Copyright (c) 2019 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2019-09-02
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2019-09-02  Original Creation
 * 
 */
 
#ifndef __SYS_SMG_DISPLAY_H_
#define __SYS_SMG_DISPLAY_H_

#include "drivers.h"

/* SMG Display mode definition
 *  
 *  0xFFF: Off all leds
 *  0xFFE: Display ][
 *  0xFFD: Display |--|
 *  0xFFC: Display E0
 *  0xFFB: Display E1
 *  0xFFA: Display E2
 *  0xFF9: Display E3
 *  0xFF8: Display E4
 *  0xFF7: Display E5
 *  0xFF6: Display E6
 *  0xFF5: Display E7
 *  0xFF4: Display E8
 *  0xFF3: Display E9
 *
 *  0x1XX: Off units digit
 *  0x2XX: Off tens digit
 *  0x3XX: Off hundreds digit
 *  
 */
#define	SMG_NC		    0xFFF
#define	SMG_X		    0xFFE
#define	SMG_H		    0xFFD
#define	SMG_E0		    0xFFC
#define	SMG_E1		    0xFFB
#define	SMG_E2		    0xFFA
#define	SMG_E3		    0xFF9
#define	SMG_E4		    0xFF8
#define	SMG_E5		    0xFF7
#define	SMG_E6		    0xFF6
#define	SMG_E7		    0xFF5
#define	SMG_E8		    0xFF4
#define	SMG_E9		    0xFF3

#define	SMG_UNITS_DIGIT_OFF		    0x100
#define	SMG_TENS_DIGIT_OFF		    0x200
#define	SMG_HUNDREDS_DIGIT_OFF		0x300

#define SMG_LED_NUM     (18)

#define SMG_PIN_NUM     (5)

/* SMG PINs definition
 *  
 * PIN - GPIO ¶ÔÓ¦¹ØÏµ£º
 *  PIN1  --  GPIO19
 *  PIN2  --  GPIO22
 *  PIN3  --  GPIO20
 *  PIN4  --  GPIO18
 *  PIN5  --  GPIO21
 *
 */

#define SMG_PIN1        (19)
#define SMG_PIN2        (22)
#define SMG_PIN3        (20)
#define SMG_PIN4        (18)
#define SMG_PIN5        (21)

/* SMG PINs output data, SW could write this register to decide GPIO's output values
 */
#ifndef SMG_PIN_OUT_VAL
#define SMG_PIN_OUT_VAL
#endif
#define GPIO_DR_REG_SMG_PIN_OUT_VAL_FLDSET              (0x007C0000)
#define GPIO_DR_REG_SMG_PIN_OUT_VAL_FLDMSK              (0xFF83FFFF)


/* SMG PINs direction, 0 : input; 1 : output.
*/
#ifndef SMG_PIN_DDR
#define SMG_PIN_DDR
#endif
#define GPIO_DDR_REG_SMG_PIN_DDR_FLDSET                 (0x007C0000)
#define GPIO_DDR_REG_SMG_PIN_DDR_FLDMSK                 (0xFF83FFFF)



typedef struct SMG_CTRL_s
{
    u32_t on:1;                // 1: turn on, 0: turn off
    u32_t display_data:12;       // Segment data: 0 - 100, SMG_X, SMG_H
    u32_t scan_cnt:8;
    u32_t reserved:11;
} SMG_CTRL_t;

extern volatile SMG_CTRL_t g_smg_ctrl;

#define sys_smg_ctrl_on_set(flg) \
    do { \
        sync_enter_critical(); \
        g_smg_ctrl.on = flg; \
        sync_exit_critical(); \
    } while(0)

#define sys_smg_ctrl_on_get() \
    (g_smg_ctrl.on)


void sys_smg_display_data_set(u32_t val);

#define sys_smg_display_data_get() \
    (g_smg_ctrl.display_data)


#define sys_smg_scan_cnt_set(val) \
    do { \
        g_smg_ctrl.scan_cnt = val; \
    } while(0)

#define sys_smg_scan_cnt_get() \
    (g_smg_ctrl.scan_cnt)


typedef struct SMG_LED_PIN_CTRL_s
{
    u32_t out_bitmap; // output value
    u32_t dir_bitmap; // direction: 1 - output, 0 - input
} SMG_LED_PIN_CTRL_t;

extern const SMG_LED_PIN_CTRL_t g_smg_led_pin_defs[];

/** \brief Segment display off
 *
 */
#define sys_smg_display_off()	DRV_REG_FIELD_SET3(GPIO_DDR_REG,SMG_PIN_DDR,0x0)

/** \brief Segment display, call every timer timeout
 */
void sys_smg_display(void);

void sys_smg_display_scan(void);

#endif /* #ifndef __SYS_SMG_DISPLAY_H_ */

