#include <stdio.h>      /*標準輸入輸出定義*/
#include <stdlib.h>     /*標準函數庫定義*/
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>

#include <sys/ioctl.h>
#include <sys/timeb.h>

#include "Module_LcmControl.h"
#include "../Log/log.h"
#include "../ShareMemory/shmMem.h"
#include "../Define/define.h"
#include "../Config.h"
#include "../SelectGun/SelectGun.h"
#include "../CSU/main.h"
#define uSEC_VAL                                (1000000)
//------------------------------------------------------------------------------
//struct SysConfigAndInfo         *ShmSysConfigAndInfo;
//struct StatusCodeData           *ShmStatusCodeData;
static struct SysConfigData *pSysConfig = NULL;
static struct SysInfoData *pSysInfo = NULL;
static struct WARNING_CODE_INFO *pSysWarning = NULL;

static struct FanModuleData *ShmFanModuleData;
static struct PrimaryMcuData *ShmPrimaryMcuData;
static SelectGunInfo *ShmSelectGunInfo = NULL;
static struct ChargingInfoData *pDcChargingInfo = NULL;
static DcCommonInfo *ShmDcCommonData            = NULL;

short _currentPage              = _PAGE_NONE;
uint8_t _totalCount;
uint8_t _showInformIndex = 0;
float ChargeMaxPower_0 = 0;
float ChargeMaxPower_1 = 0;
bool _battery_display_ani = false;
int _port;
//char* pPortName         = "/dev/ttyO2";
char *pPortName           = "/dev/ttyS3";
char *moduleName          = "DMT80480T070_09WT";
bool is_show = false;
uint8_t _everyPageRollChange;
uint8_t _btn_press = 0;
short _btn_press_id = 0;
struct timeval returnIdleTimer;
int _Text_Running_Count = 0;
int Battery_Test = 0;
extern void UpdateLcmFunction(DcCommonInfo *ShmDcCommonData,int _lcmport);
//==========================================
// Open and Close RS232 and R/W
//==========================================
unsigned long GetClockTimeoutValue(struct timespec _start_time)
{
    struct timespec ts_end;
    unsigned long ret = 0;

    clock_gettime(CLOCK_MONOTONIC, &ts_end);

    ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec/ 1000)));

    return ret;
}
int CreateCommunicationLcmPort()
{
    int fd;
    struct termios tios;

    fd = open(pPortName, O_RDWR);
    if (fd <= 0) {
        log_error("open /dev/ttyS3 NG ");
        return -1;
    }
    ioctl(fd, TCGETS, &tios);
    tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
    tios.c_lflag = 0;
    tios.c_iflag = 0;
    tios.c_oflag = 0;
    tios.c_cc[VMIN] = 0;
    tios.c_cc[VTIME] = (uint8_t) 5;
    tios.c_lflag = 0;
    tcflush(fd, TCIFLUSH);
    ioctl(fd, TCSETS, &tios);

    return fd;
}

void CloseCommunicationLcmPort()
{
    close(_port);
}

void GetDeviceInfoStatus(short address, uint8_t len)
{
    uint8_t cmd[8];
    memset(cmd, 0x00, sizeof(cmd));
    uint8_t msg[11];
    memset(msg, 0x00, sizeof(msg));

    cmd[0] = CMD_TITLE_1;
    cmd[1] = CMD_TITLE_2;
    cmd[2] = 0x04;
    cmd[3] = CMD_MULTI_READ;
    cmd[4] = (address >> 8) & 0xff;
    cmd[5] = (address >> 0) & 0xff;
    cmd[6] = len;

    WriteCmdToLcm(cmd, ARRAY_SIZE(cmd));
    usleep(1000);
    ReadMsgFromLcm(msg, ARRAY_SIZE(msg));
}
void WriteCmdToLcm(uint8_t *cmd, uint8_t cmdLen)
{
    int len = write(_port, cmd, cmdLen);
    if (len < sizeof(cmd)) {
        log_error("Write cmd to LCM Failure. ");
    }
}

void CheckScreenModePress()
{

}

void CheckMemberSelectPress()
{

}
void CheckDonatePress()
{

}
void CheckDonateSelectPress()
{

}
void CheckIdlePress()
{
    pSysInfo->SystemPage = _PAGE_AUTHORIZE;
    log_info("LCM Enter Authorize Page");
}
void CheckTouchPress(short id)
{
    GetDeviceInfoStatus(id,1);

    if (_btn_press >= 1 && _btn_press_id == id) {
        ChangeDisplay2Value(id, 0);
        switch (id) {
        case _Button_LeftGun_Select:
            if(pSysInfo->CurGunSelected != LEFT_GUN_NUM) {
                pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(LEFT_GUN_NUM);
                log_info("LCM left Gun down...............................%x %x %x",
                        pSysInfo->CurGunSelected,pDcChargingInfo->SystemStatus,pSysInfo->SystemPage);
                if (pDcChargingInfo->SystemStatus == S_IDLE &&
                    (pSysInfo->SystemPage <_PAGE_REFUND_SENSEING ||
                    pSysInfo->SystemPage > _PAGE_REFUND_NONE ||
                    pSysInfo->SystemPage != _PAGE_PAY_ETICKET_SUCCESS ||
                    pSysInfo->SystemPage != _PAGE_PAY_MPAY_SUCCESS) ) {
                    log_info("Reset LCM to IDLE");
                    pSysInfo->SystemPage = _PAGE_AUTHORIZE;
                    gettimeofday(&returnIdleTimer, NULL);
                }

            }
            pSysInfo->CurGunSelected = LEFT_GUN_NUM;
            break;
        case _Button_RightGun_Select:
            if (pSysInfo->CurGunSelected != RIGHT_GUN_NUM) {
                pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(RIGHT_GUN_NUM);
                log_info("LCM right Gun down...............................%x %x %x",
                        pSysInfo->CurGunSelected,pDcChargingInfo->SystemStatus,pSysInfo->SystemPage);
                if (pDcChargingInfo->SystemStatus == S_IDLE &&
                    (pSysInfo->SystemPage <_PAGE_REFUND_SENSEING ||
                    pSysInfo->SystemPage > _PAGE_REFUND_NONE ||
                    pSysInfo->SystemPage != _PAGE_PAY_ETICKET_SUCCESS ||
                    pSysInfo->SystemPage != _PAGE_PAY_MPAY_SUCCESS) ) {
                    log_info("Reset LCM to IDLE");
                    pSysInfo->SystemPage = _PAGE_AUTHORIZE;
                    gettimeofday(&returnIdleTimer, NULL);
                }
            }
            pSysInfo->CurGunSelected = RIGHT_GUN_NUM;
            break;
        case _Button_Screen_Mode:
            break;
        case _Button_Charge:
            break;
        case _Button_Refund:
            ShmDcCommonData->_RefundRequest = true;
            break;
        case _Button_Return:
            ShmDcCommonData->_RefundCancel = true;
            break;
        case _Button_Member:
            break;
        case _Button_Member_None:
            break;
        case _Button_Return_Home:
            break;
        case _Button_Donate:
            ShmDcCommonData->_InvoiceRequest = true;
            break;
        case _Button_Donate_Select0:
            break;
        case _Button_Donate_Select1:
            break;
        case _Button_Donate_Select2:
            break;
        case _Button_Donate_Select3:
            break;
        case _Button_Donate_Confirm:
            break;
        case _Button_Authorize:
            CheckIdlePress();
            break;
        } // switch
    }   //if (_btn_press >= 3)
}

void CheckLCMPressed()
{
    pid_t Pid = fork();
    int i = 0;
    int index = 0;
    if ( Pid == 0 ) {
        while (1) {
            pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
            CheckTouchPress(_Button_LeftGun_Select);
            CheckTouchPress(_Button_RightGun_Select);
            switch (pSysInfo->SystemPage) {
            case _PAGE_IDLE:
                CheckTouchPress(_Button_Authorize);
                break;
            case _PAGE_AUTHORIZE:
                CheckScreenModePress();
                break;
            case _PAGE_FUNCTION_SELECT:
                CheckTouchPress(_Button_Charge);
                CheckTouchPress(_Button_Return);
                break;
            case _PAGE_MEMBER_SELECT:
                CheckMemberSelectPress();
                break;
            case _PAGE_DONATE:
                CheckDonatePress();
                break;
            case _PAGE_DONATE_SELECT:
                CheckDonateSelectPress();
                break;
// ********************** Test *******************
///*
            case _PAGE_PRECHARGE:
            	/*
                if ((i/100) %2 == 0)
                    ChangeDisplay2Value(0x109A,i%100);
                else
                    ChangeDisplay2Value(0x109A,100-(i%100));
                    */
                break;
            case _PAGE_CHARGING:
                pDcChargingInfo->PresentChargingVoltage = i;
                pDcChargingInfo->PresentChargingPower = i;
                pDcChargingInfo->PresentChargedEnergy = i;
                pDcChargingInfo->PresentChargingCurrent =i;
                pDcChargingInfo->PresentChargedDuration = i;
                pDcChargingInfo->EvBatterySoc = i/6;
                break;
            case _PAGE_COMPLETE:
                pDcChargingInfo->PresentChargedEnergy = i;
                pDcChargingInfo->ChargingFee = i;
                pDcChargingInfo->EvBatterySoc = i/6;
                pSysConfig->BillingData.isBilling = TRUE;
                break;

// ************************************************ */
            } // switch
            usleep(5000);
            ///*
            i++;
            if (i == 600 ) {
                index >= 22 ? index = 1 : index++;
                pSysInfo->SystemPage = 22;
                i = 0;
                /*
                if (pSysInfo->SystemPage == _PAGE_PRECHARGE)
                    gettimeofday(&pDcChargingInfo->PreChargeTimer, NULL);
                    */


            }//*/

        } //while
    } // if pid
    log_info("Create LCM fork:%d",Pid);
}
void ReadMsgFromLcm(uint8_t *msg, uint8_t readLen)
{
    read(_port, msg, readLen);

//  5a : CMD_TITLE_1
//  a5 : CMD_TITLE_2
//  5
//  81 : CMD_WRITE
//  3  : CMD_REGISTER
//  2  : Data length
//  0  : High byte
//  1  : Low byte

//  printf("-------------------------------------------- \n");
//  printf("msg = %x \n", *msg);            // A5
//  printf("msg = %x \n", *(msg + 1));      // 5A
//  printf("msg = %x \n", *(msg + 2));      // Len : [3] ~ [6] + Data Len
//  printf("msg = %x \n", *(msg + 3));      // cmd : 0x83
//  printf("msg = %x \n", *(msg + 4));      // addr : H
//  printf("msg = %x \n", *(msg + 5));      // addr : L
//  printf("msg = %x \n", *(msg + 6));      // Data Len
//
//  printf("msg = %x \n", *(msg + 7));
//  printf("msg = %x \n", *(msg + 8));
//  printf("msg = %x \n", *(msg + 9));
//  printf("msg = %x \n", *(msg + 10));
//  printf("msg = %x \n", *(msg + 11));
//  printf("msg = %x \n", *(msg + 12));
//  printf("msg = %x \n", *(msg + 13));
//  printf("msg = %x \n", *(msg + 14));
    /*
    for(uint8_t i = 0 ; i<readLen+3; i++) {
        log_info("Read data[%d]:0x%x",i,msg[i]);
    }*/

    if(*msg == CMD_TITLE_1 && *(msg + 1) == CMD_TITLE_2)
    {
        if(*(msg + 3) == CMD_WRITE)
        {
            switch (*(msg + 4))
            {
                case CMD_REGISTER:
                {
                    // 頁面
                    if(strcmp((char *)pSysInfo->LcmHwRev, "") != EQUAL)
                        strcpy((char *)pSysInfo->LcmHwRev, moduleName);

                    _currentPage = *(msg + 7);
                    log_info("Current Page:%d",_currentPage);
//                  if (_currentPage != 1 && _currentPage != 5 && _currentPage != 6 && _currentPage != 7 && _currentPage != 8)
//                      printf("_currentPage = %d \n", _currentPage);
                }
                break;
            }
        }
        else if (*(msg + 3) == CMD_MULTI_READ)
        {
            short key = ((short)(*(msg + 4) << 8) + *(msg + 5));
            if(strcmp((char *)pSysInfo->LcmHwRev, "") != EQUAL)
                strcpy((char *)pSysInfo->LcmHwRev, moduleName);
            if (key == 0x0014)
                _currentPage = *(msg + 8);
            if ( key >= _Button_LeftGun_Select && key <=_Button_Authorize ) {
                _btn_press_id = key;
                _btn_press = *(msg + 8);
            }
//          switch ((unsigned short) (*(msg + 4) << 8) + (unsigned short) *(msg + 5))
//          {
//              case BUTTON_GUN_INDEX:
//              {
//                  // 當前選的槍號
//                  _curGunIndex = (*(msg + 8));
//              }
//              break;
//          }
        }
    }
}
void GetHrFormTimeString(char* time,char* hr)
{
    //char tm[] = "2021-12-06 17:29:08:084";
    for(int i = 0 ; i < 2 ; i++) {
        hr[i] = time[i+11];
    }
    if( atoi(hr) == NULL ) {
        strcmp(hr,"");
    }
}
void GetMinFormTimeString(char* time,char* min)
{
    //char tm[] = "2021-12-06 17:29:08:084";
    for(int i = 0 ; i < 2 ; i++) {
        min[i] = time[i+14];
    }
    if( atoi(min) == NULL || atoi(min) > 60) {
        strcmp(min,"");
    }
}
//================================================
// Function
//================================================
void ChangeToOtherPage(short newPage)
{

    uint8_t cmd[10];
    memset(cmd, 0x00, sizeof(cmd));
    uint8_t msg[9];
    memset(msg, 0x00, sizeof(msg));

    cmd[0] = CMD_TITLE_1;
    cmd[1] = CMD_TITLE_2;
    cmd[2] = 0x07;
    cmd[3] = 0x82;
    cmd[4] = 0x00;
    cmd[5] = 0x84;
    cmd[6] = 0x5A;
    cmd[7] = 0x01;
    cmd[8] = newPage >> 8;
    cmd[9] = newPage & 0x00FF;

    WriteCmdToLcm(cmd, ARRAY_SIZE(cmd));
    usleep(5000);
    ReadMsgFromLcm(msg, ARRAY_SIZE(msg));
}

void ChangeBackLight(bool islight)
{
    uint8_t value = 0x01;
    uint8_t msg[9];
    memset(msg, 0x00, sizeof(msg));

    // 0x00 ~ 0x40
    if (islight)
    {
        value = 0x20;
    }
    uint8_t cmd[7];
    memset(cmd, 0x00, sizeof(cmd));

    cmd[0] = CMD_TITLE_1;
    cmd[1] = CMD_TITLE_2;
    cmd[2] = 0x03;
    cmd[3] = CMD_READ;
    cmd[4] = CMD_BACKLIGHT;
    cmd[5] = value;

    WriteCmdToLcm(cmd, ARRAY_SIZE(cmd));
    usleep(10000);
    ReadMsgFromLcm(msg, ARRAY_SIZE(msg));
}

void GetCurrentPage()
{
    uint8_t cmd[7];
    memset(cmd, 0x00, sizeof(cmd));
    uint8_t msg[9];
    memset(msg, 0x00, sizeof(msg));

    cmd[0] = CMD_TITLE_1;
    cmd[1] = CMD_TITLE_2;
    cmd[2] = 0x04;              // 底下總長度
    cmd[3] = 0x83;
    cmd[4] = 0x00;
    cmd[5] = 0x14;
    cmd[6] = 0x01;

    WriteCmdToLcm(cmd, ARRAY_SIZE(cmd));
    usleep(5000);
    ReadMsgFromLcm(msg, ARRAY_SIZE(msg));
}


void DisplayValueToLcm(short address, uint8_t *data, uint8_t len)
{

    uint8_t cmd[256];
    memset(cmd, 0x00, sizeof(cmd));
    uint8_t msg[9];
    memset(msg, 0x00, sizeof(msg));

    cmd[0] = CMD_TITLE_1;
    cmd[1] = CMD_TITLE_2;
    cmd[2] = 0x03 + len;
    cmd[3] = CMD_MULTI_WRITE;
    cmd[4] = address >> 8;
    cmd[5] = address & 0x00FF;

    for(uint8_t count = 0; count < len; count++)
    {
        cmd[6 + count] = *(data + count);
    }

    WriteCmdToLcm(cmd, cmd[2] + 3);
    usleep(10000);
    ReadMsgFromLcm(msg, ARRAY_SIZE(msg));
}

void ChangeDisplay2Value(short address, short value)
{
    uint8_t data[2];
    data[0] = value >> 8;
    data[1] = value & 0x00FF;
    //log_info("Addr:0x%x, value:%d",address,value);
    DisplayValueToLcm(address, data, sizeof(data));
}

//================================================
// Warning process
//================================================
void string2ByteArray(uint8_t *input, uint8_t *output)
{
    int loop;
    int i;

    loop = 0;
    i = 0;

    while (input[loop] != '\0') {
        output[i++] = input[loop++];
    }
    output[loop] = '\0';
}

void RefreshProgressAnimation(uint8_t progress_index)
{
    if(_everyPageRollChange % 2) {
        progress_index+=1;
    }

    _everyPageRollChange++;
}
//================================================
// Change current page
//================================================
void ChangeCurPage()
{
    //log_info("cur = %d ,system = %d, lcm = %d ",_currentPage, pSysInfo->SystemPage, pSysInfo->PageIndex);
    struct ChargingInfoData *pDcChargingInfo_0 = (struct ChargingInfoData *)GetDcChargingInfoData(LEFT_GUN_NUM);
    struct ChargingInfoData *pDcChargingInfo_1 = (struct ChargingInfoData *)GetDcChargingInfoData(RIGHT_GUN_NUM);

    pSysInfo->PageIndex = pSysInfo->SystemPage;
    if (pSysInfo->CurGunSelected == RIGHT_GUN_NUM &&
            pSysInfo->SystemPage != _PAGE_IDLE &&
            pSysInfo->SystemPage != _PAGE_MAINTAIN &&
            pSysInfo->SystemPage != _PAGE_ERROR ) {
        pSysInfo->PageIndex = pSysInfo->SystemPage + 30;
    }

    if (_currentPage != pSysInfo->PageIndex) {
        switch (pSysInfo->SystemPage) {
        case _PAGE_AUTHORIZE:
			gettimeofday(&returnIdleTimer, NULL);
            break;
        case _PAGE_PLUGIN:
            break;
        case _PAGE_PRECHARGE:
            break;
        case _PAGE_CHARGING:
            break;
        case _PAGE_COMPLETE:
            break;
        }
        _currentPage = pSysInfo->PageIndex;
        ChangeToOtherPage(pSysInfo->PageIndex);
    }
}

/*
 * View Page
 *
 */

unsigned long GetTimeoutValue(struct timeval _sour_time)
{
    struct timeval _end_time;
    gettimeofday(&_end_time, NULL);

    return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
}
void showGunWorkingType()
{
    if (pSysInfo->CurGunSelected == LEFT_GUN_NUM) {
        ChangeDisplay2Value(_LeftGun_Title,_ICON_LeftGunShow);
        // Set Left Gun
        ChangeDisplay2Value(_LeftGun_Status,_ICON_LeftGun_on);

        pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(LEFT_GUN_NUM);
        if (pDcChargingInfo->CCSGunType == _TYPE_CCS1_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS1_Natural) {
            ChangeDisplay2Value(_LeftGun_Type,_ICON_LeftGun_on_CCCS1);

        } else if (pDcChargingInfo->CCSGunType == _TYPE_CCS2_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS2_Natural) {
            ChangeDisplay2Value(_LeftGun_Type,_ICON_LeftGun_on_CCS2);
        } else if (pDcChargingInfo->Type == _Type_Chademo) {
            ChangeDisplay2Value(_LeftGun_Type,_ICON_LeftGun_on_CHAdeMo);
        }

        // Set Right Gun
        ChangeDisplay2Value(_RightGun_Status,_ICON_RightGun_off);
        pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(RIGHT_GUN_NUM);
        if (pDcChargingInfo->CCSGunType == _TYPE_CCS1_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS1_Natural) {
            ChangeDisplay2Value(_RightGun_Type,_ICON_RightGun_off_CCS1);

        } else if (pDcChargingInfo->CCSGunType == _TYPE_CCS2_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS2_Natural) {
            ChangeDisplay2Value(_RightGun_Type,_ICON_RightGun_off_CCS2);
        } else if (pDcChargingInfo->Type == _Type_Chademo) {
            ChangeDisplay2Value(_RightGun_Type,_ICON_RightGun_off_CHAdeMo);
        }
    } else {
        // Left Gun
        ChangeDisplay2Value(_RightGun_Title,_ICON_RightGunShow);
        ChangeDisplay2Value(_LeftGun_Status,_ICON_LeftGun_off);
        pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(LEFT_GUN_NUM);
        if (pDcChargingInfo->CCSGunType == _TYPE_CCS1_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS1_Natural) {
            ChangeDisplay2Value(_LeftGun_Type,_ICON_LeftGun_off_CCS1);

        } else if (pDcChargingInfo->CCSGunType == _TYPE_CCS2_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS2_Natural) {
            ChangeDisplay2Value(_LeftGun_Type,_ICON_LeftGun_off_CCS2);
        } else if (pDcChargingInfo->Type == _Type_Chademo) {
            ChangeDisplay2Value(_LeftGun_Type,_ICON_LeftGun_off_CHAdeMo);
        }
        // Right Gun
        ChangeDisplay2Value(_RightGun_Status,_ICON_RightGun_on);
        pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(RIGHT_GUN_NUM);
        if (pDcChargingInfo->CCSGunType == _TYPE_CCS1_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS1_Natural) {
            ChangeDisplay2Value(_RightGun_Type,_ICON_RightGun_on_CCS1);

        } else if (pDcChargingInfo->CCSGunType == _TYPE_CCS2_Liquid ||
                pDcChargingInfo->CCSGunType == _TYPE_CCS2_Natural) {
            ChangeDisplay2Value(_RightGun_Type,_ICON_RightGun_on_CCS2);
        } else if (pDcChargingInfo->Type == _Type_Chademo) {
            ChangeDisplay2Value(_RightGun_Type,_ICON_RightGun_on_CHAdeMo);
        }
    }

}
void ChangeQrCode_Idle(char *input)
{
    int len = strlen(input);
    uint8_t cmd[len];

    int loop = 0;
    int i = 0;

    while (input[loop] != '\0') {
        cmd[i++] = input[loop++];
    }

    DisplayValueToLcm(_QR_CODE_, cmd, len + 1);
}
void ShowHomePage()
{
    // Show QR Code
    if (pSysConfig->isQRCode) {
        if (pSysConfig->QRCodeMadeMode == NO) {
            //uint8_t len = strlen((char *)pSysConfig->SystemId);
            ChangeQrCode_Idle((char *)pSysConfig->SystemId);
        } else {
            //uint8_t len = strlen((char *)pSysConfig->QRCodeContent);
            ChangeQrCode_Idle((char *)pSysConfig->QRCodeContent);
        }
        //ChangeQrCode_Idle((char *)pSysConfig->SystemId);
    }
}
void ChangeRemainTime(int sec)
{
    int h, m, s;
    uint8_t cmd[10];
    uint8_t value[10];

    memset(cmd, 0x00, sizeof(cmd));

//  srand(time(NULL));
//  int min = 0;
//  int max = 65536;
//  sec = rand() % (max - min + 1) + min;

    h = (sec / 3600);
    m = (sec - (3600 * h)) / 60;
    s = (sec - (3600 * h) - (m * 60));
    sprintf((char *)value, "%02d:%02d:%02d", h, m, s);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_ChargeTime_, cmd, sizeof(cmd));
}
void ChangeChargingPowerValue(float pow)
{
    uint8_t cmd[10];
    uint8_t value[10];

    memset(cmd, 0x00, sizeof(cmd));

    sprintf((char *) value, "%.1f",pow);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_Power_, cmd, sizeof(cmd));
}
void ChangeChargingVoltageValue(float vol)
{
    uint8_t cmd[10];
    uint8_t value[10];

    memset(cmd, 0x00, sizeof(cmd));

    sprintf((char *) value, "%d", (int)vol);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_Voltage_, cmd, sizeof(cmd));
}

void ChangeChargingCurrenteValue(float vol)
{
    uint8_t cmd[10];
    uint8_t value[10];

    memset(cmd, 0x00, sizeof(cmd));

    sprintf((char *) value, "%d", (int)vol);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_Current_, cmd, sizeof(cmd));
}
void ChangeChargingFeeValue(float fee)
{
    uint8_t cmd[10];
    uint8_t value[10];

    memset(cmd, 0x00, sizeof(cmd));

    sprintf((char *) value, "%d", (int)fee);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_Cost_, cmd, sizeof(cmd));
}
void ChangeChargingEnergyValue(float energy)
{
    uint8_t cmd[10];
    uint8_t value[10];

    memset(cmd, 0x00, sizeof(cmd));
    if (energy >= 0.05) {
        energy -= 0.05;
    }
    sprintf((char *) value, "%d", (int)energy);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_Rate_, cmd, sizeof(cmd));
}
uint8_t _battery_display_count = 0;
void ChangeBattMapAndValue(short page, int soc)
{
    if (page == _PAGE_CHARGING) {

        _battery_display_count++;
        if (_battery_display_count == 3 ) {
            if (_battery_display_ani == 0)
                _battery_display_ani= TRUE;
            else
                _battery_display_ani= FALSE ;
            _battery_display_count = 0;
        }
        if (soc < 25) {
            if (_battery_display_ani) {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_0);
            } else {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_1);
            }
        } else if (soc >= 25 && soc < 50) {
            if (_battery_display_ani) {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_1);
            } else {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_2);
            }
        } else if (soc >= 50 && soc < 75) {
            if (_battery_display_ani) {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_2);
            } else {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_3);
            }
        } else if (soc >= 75 && soc < 100) {
            if (_battery_display_ani) {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_3);
            } else {
                ChangeDisplay2Value(_Battery_Progress_0, _ICON_battery_4);
            }
        }
    } else if (page == _PAGE_COMPLETE) {
        if (soc < 20) {
            ChangeDisplay2Value(_Battery_Progress_1, _ICON_text_battery_0);
        } else if (soc >= 20 && soc < 40) {
            ChangeDisplay2Value(_Battery_Progress_1, _ICON_text_battery_1);
        } else if (soc >= 40 && soc < 60) {
            ChangeDisplay2Value(_Battery_Progress_1, _ICON_text_battery_2);
        } else if (soc >= 60 && soc < 80) {
            ChangeDisplay2Value(_Battery_Progress_1, _ICON_text_battery_3);
        } else if (soc >= 80 && soc <= 100) {
            ChangeDisplay2Value(_Battery_Progress_1, _ICON_text_battery_4);
        }
    }

    uint8_t cmd[5];
    uint8_t value[5];

    memset(cmd, 0x00, sizeof(cmd));
    memset(value, 0x00, sizeof(value));
    sprintf((char *)value, "%d", soc);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_String_BatterySoc_, cmd, sizeof(cmd));
}
unsigned long GetPreChargeTimeoutValue(struct timeval _sour_time)
{
    struct timeval _end_time;
    gettimeofday(&_end_time, NULL);

    return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
}
void ChangeTimeValue(uint8_t time)
{
    uint8_t cmd[2];
    uint8_t value[2];

    memset(cmd, 0x00, sizeof(cmd));
    sprintf((char *) value, "%d", time);
    string2ByteArray(value, cmd);
    DisplayValueToLcm(_PreChargeTimer, cmd, sizeof(cmd));
}
void CabinetChangeLCMProcess()
{
    pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
    if ( (pDcChargingInfo->ChangeLcmPage == _CHANGE_REFUNDAMOUNT ||
            pDcChargingInfo->ChangeLcmPage == _CHANGE_PREPAYMENTINFO ||
            pDcChargingInfo->ChangeLcmPage == _CHANGE_PAYMENT_FAIL) &&
            ShmDcCommonData->_ChangeLCMRequest != TRUE) {
        return;
    }
    switch(pDcChargingInfo->ChangeLcmPage) {
    case _CHANGE_REMOTESTART:
        break;
    case _CHANGE_REFUNDING:
        pSysInfo->SystemPage = _PAGE_REFUNDING;
        break;
    case _CHANGE_REFUNDAMOUNT:
        pSysInfo->SystemPage = _PAGE_REFUND_COMPLETE;
        ShmDcCommonData->_ChangeLCMRequest = FALSE;
        break;
    case _CHANGE_NOREFUND:
        pSysInfo->SystemPage = _PAGE_REFUND_NONE;
        break;
    case _CHANGE_PREPAYMENTINFO:
        pSysInfo->SystemPage = _PAGE_PAY_ETICKET_SUCCESS;
        ShmDcCommonData->_ChangeLCMRequest = FALSE;
        break;
    case _CHANGE_PAYMENT_OK:
        pSysInfo->SystemPage = _PAGE_PAY_MPAY_SUCCESS;
        break;
    case _CHANGE_PAYMENT_FAIL:
        pSysInfo->SystemPage = _PAGE_PAY_FAIL;
        ShmDcCommonData->_ChangeLCMRequest = FALSE;
        break;
    }
}
void ProcessPageInfo()
{
    // Show Gun Working and Type
    float _current;
    uint8_t precharg_time;
    showGunWorkingType();
    for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++) {
        if (pSysInfo->CurGunSelected == i) {
            pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
            CabinetChangeLCMProcess();

            switch (pSysInfo->SystemPage) {
            case _PAGE_AUTHORIZE:
                ShowHomePage();
                log_info("Time:%d",GetTimeoutValue(returnIdleTimer) /uSEC_VAL);
                if (GetTimeoutValue(returnIdleTimer) /uSEC_VAL >= RETURNIDLE_Timeout ) {
                    pSysInfo->SystemPage = _PAGE_IDLE;
                    gettimeofday(&returnIdleTimer, NULL);
                }
                break;
            case _PAGE_PLUGIN:
            	_Text_Running_Count = 0;
                break;
            case _PAGE_PRECHARGE:
                if ((_Text_Running_Count/100) %2 == 0)
                    ChangeDisplay2Value(_Text_PrepareToCharge,_Text_Running_Count%100);
                else
                    ChangeDisplay2Value(_Text_PrepareToCharge,100-(_Text_Running_Count%100));
                _Text_Running_Count >= 200 ? _Text_Running_Count = 0 : _Text_Running_Count++;
                break;
            case _PAGE_CHARGING:
                if (pDcChargingInfo->PresentChargingVoltage >= 0 &&
                        pDcChargingInfo->PresentChargingVoltage <= 950) {

                    ChangeChargingVoltageValue(pDcChargingInfo->PresentChargingVoltage);
                }
                if (pDcChargingInfo->PresentChargingCurrent >= 0) {

                    ChangeChargingCurrenteValue(pDcChargingInfo->PresentChargingCurrent);
                }
                if (pDcChargingInfo->PresentChargingPower >= 0 &&
                        pDcChargingInfo->PresentChargingPower <= POWER_MAX_KW) {

                    ChangeChargingPowerValue(pDcChargingInfo->PresentChargingPower);
                } else {
                    ChangeChargingPowerValue(0);
                }
                if (pDcChargingInfo->PresentChargedEnergy >= 0.1 &&
                        pDcChargingInfo->PresentChargedEnergy <= ENERGY_MAX_KWH) {

                    ChangeChargingEnergyValue(pDcChargingInfo->PresentChargedEnergy);
                } else {
                    ChangeChargingEnergyValue(0);
                }
                if (pDcChargingInfo->PresentChargedDuration >= 0 &&
                        pDcChargingInfo->PresentChargedDuration <= TIME_MAX_SEC) {
                    ChangeRemainTime(pDcChargingInfo->PresentChargedDuration);
                } else {
                    ChangeRemainTime(0);
                }
                ChangeBattMapAndValue(_currentPage, pDcChargingInfo->EvBatterySoc);
                break;
            case _PAGE_COMPLETE:
                if (pDcChargingInfo->PresentChargedEnergy >= 0.1 &&
                        pDcChargingInfo->PresentChargedEnergy <= ENERGY_MAX_KWH) {
                    ChangeChargingEnergyValue(pDcChargingInfo->PresentChargedEnergy);
                } else {
                    ChangeChargingEnergyValue(0);
                }
                if (pSysConfig->BillingData.isBilling &&
                        pDcChargingInfo->ChargingFee >= 0) {
                    ChangeChargingFeeValue(pDcChargingInfo->ChargingFee);
                }
                ChangeBattMapAndValue(_currentPage, pDcChargingInfo->EvBatterySoc);
                break;
            case _PAGE_REFUND_SENSEING:
                break;
            case _PAGE_PLUGOUT:
            	ChangeDisplay2Value(0x20B0, Battery_Test);
            	Battery_Test >= 100 ? Battery_Test = 0 : Battery_Test++;
            	break;
            }
        }
    }
}
void ChangeWarningFunc()
{
    uint8_t cmd[7] = {0};
    uint8_t i = 0;
    //uint8_t j = 0;
    //log_info("ChangeWarningFunc ");
    // 最多一次五筆
    //log_info("LCM PageIndex = %d ", pSysWarning->PageIndex);
    //log_info("WarningCount = %d ", pSysWarning->WarningCount);
//#if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
    for (i = 0; (i + pSysWarning->PageIndex * 5) < pSysWarning->WarningCount; i++) {
        //log_info("Warming Code[%d]:%s",i,&pSysWarning->WarningCode[i][0]);
        memset(cmd, 0x00, sizeof(cmd));
        if ((i) >= 5) {
            break;
        }

        //error code
        string2ByteArray(&pSysWarning->WarningCode[i + pSysWarning->PageIndex * 5][0], cmd);
        DisplayValueToLcm(0x3020 + ((i) * 6), cmd, sizeof(cmd));
        //警告標示
        memset(cmd, 0x00, sizeof(cmd));

        cmd[0] = 0x00;
        cmd[1] = 85;
        DisplayValueToLcm(0x1090 + ((i) * 2), cmd, 2);
    }

    memset(cmd, 0x00, sizeof(cmd));
    for (; i < 5; i++) {
        DisplayValueToLcm(0x3020 + ((i) * 6), cmd, sizeof(cmd));
        DisplayValueToLcm(0x1090 + ((i) * 2), cmd, 2);
    }
}

void ShowCabientVersionDefaultText()
{
    char value[16];
    memset(value, 0x00, sizeof(value));
    sprintf((char *)value,"%s","Cabient:");
    DisplayValueToLcm(_Version_Cabient_Name_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","BootLoader:");
    DisplayValueToLcm(_Version_Cabient_BootLoader_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Kernel Fw:");
    DisplayValueToLcm(_Version_Cabient_Kernel_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","CSU Fw:");
    DisplayValueToLcm(_Version_Cabient_CSU_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Priamry Fw:");
    DisplayValueToLcm(_Version_Cabient_Priamry_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Relay(0) Fw:");
    DisplayValueToLcm(_Version_Cabient_Relay0_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Relay(1) Fw:");
    DisplayValueToLcm(_Version_Cabient_Relay1_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Fan Fw:");
    DisplayValueToLcm(_Version_Cabient_Fan_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","IP Addr:");
    DisplayValueToLcm(_Version_Cabient_IPAddr_Text, (uint8_t *)value, sizeof(value));
    if (ShmDcCommonData->PSU_Number > 0 && ShmDcCommonData->PSU_Number <= 12) {
        sprintf((char *)value,"%s","Primary");
        DisplayValueToLcm(_Version_Cabient_Primary_Text, (uint8_t *)value, sizeof(value));
        sprintf((char *)value,"%s","Secondary");
        DisplayValueToLcm(_Version_Cabient_Secondary_Text, (uint8_t *)value, sizeof(value));
        for(uint8_t i = 0 ; i < ShmDcCommonData->PSU_Number ; i++) {
            sprintf((char *)value,"PSU(%d):",i+1);
            DisplayValueToLcm(_Version_Cabient_PSU1_Text+i*0x10, (uint8_t *)value, sizeof(value));
        }
    }
}
void ShowDispenserVersionDefautlText()
{
    char value[16];
    memset(value, 0x00, sizeof(value));
    sprintf((char *)value,"%s","Dispenser:");
    DisplayValueToLcm(_Version_Dispenser_Name_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","BootLoader:");
    DisplayValueToLcm(_Version_Dispenser_BootLoader_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Kernel Fw:");
    DisplayValueToLcm(_Version_Dispenser_Kernel_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","CSU Fw:");
    DisplayValueToLcm(_Version_Dispenser_CSU_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Relay Fw:");
    DisplayValueToLcm(_Version_Dispenser_Relay_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Fan Fw:");
    DisplayValueToLcm(_Version_Dispenser_Fan_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Prim Fw:");
    DisplayValueToLcm(_Version_Dispenser_Priamry_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","LCM Hw:");
    DisplayValueToLcm(_Version_Dispenser_LCM_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","LED Fw:");
    DisplayValueToLcm(_Version_Dispenser_LED_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Connector(0):");
    DisplayValueToLcm(_Version_Dispenser_Connector0_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","Connector(1):");
    DisplayValueToLcm(_Version_Dispenser_Connector1_Text, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s","IP Addr:");
    DisplayValueToLcm(_Version_Dispenser_IPAddr_Text, (uint8_t *)value, sizeof(value));
}
void ShowCabientVersion()
{
    char value[16];
    memset(value, 0x00, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetModelName);
    DisplayValueToLcm(_Version_Cabient_Model_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetBoolLoaderVersion);
    DisplayValueToLcm(_Version_Cabient_BootLoader_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetKernelVersion);
    DisplayValueToLcm(_Version_Cabient_Kernel_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetRFSystemVersion);
    DisplayValueToLcm(_Version_Cabient_CSU_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetPrimaryVersion);
    DisplayValueToLcm(_Version_Cabient_Priamry_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetRelay0Version);
    DisplayValueToLcm(_Version_Cabient_Relay0_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetRelay1Version);
    DisplayValueToLcm(_Version_Cabient_Relay1_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetFanVersion);
    DisplayValueToLcm(_Version_Cabient_Fan_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",ShmDcCommonData->CabinetIPAddr);
    DisplayValueToLcm(_Version_Cabient_IPAddr_value, (uint8_t *)value, sizeof(value));
    if (ShmDcCommonData->PSU_Number > 0 && ShmDcCommonData->PSU_Number < 12) {
        for(uint8_t i = 0 ; i < ShmDcCommonData->PSU_Number ; i++) {
            if (i>=6) {
                sprintf((char *)value,"%s",ShmDcCommonData->PsuVer[(i-6)].DCVersion);
                DisplayValueToLcm(_Version_Cabient_PSU_Prim7_value+(i-6)*0x10, (uint8_t *)value, sizeof(value));
                sprintf((char *)value,"%s",ShmDcCommonData->PsuVer[(i-6)].FPCVersion);
                DisplayValueToLcm(_Version_Cabient_PSU_Sec7_value+(i-6)*0x10, (uint8_t *)value, sizeof(value));
                continue;
            }
            sprintf((char *)value,"%s",ShmDcCommonData->PsuVer[i].DCVersion);
            DisplayValueToLcm(_Version_Cabient_PSU_Prim1_value+i*0x10, (uint8_t *)value, sizeof(value));
            sprintf((char *)value,"%s",ShmDcCommonData->PsuVer[i].FPCVersion);
            DisplayValueToLcm(_Version_Cabient_PSU_Sec1_value+i*0x10, (uint8_t *)value, sizeof(value));
        }
    }
}
void ShowDispenserVersion()
{
    char value[32];
    memset(value, 0x00, sizeof(value));
    sprintf((char *)value,"%s",pSysConfig->ModelName);
    DisplayValueToLcm(_Version_Dispenser_Model_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->CsuBootLoadFwRev);
    DisplayValueToLcm(_Version_Dispenser_BootLoader_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->CsuKernelFwRev);
    DisplayValueToLcm(_Version_Dispenser_Kernel_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->CsuRootFsFwRev);
    DisplayValueToLcm(_Version_Dispenser_CSU_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->RelayModuleFwRev);
    DisplayValueToLcm(_Version_Dispenser_Relay_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->FanModuleFwRev);
    DisplayValueToLcm(_Version_Dispenser_Fan_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->CsuPrimFwRev);
    DisplayValueToLcm(_Version_Dispenser_Priamry_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->LcmHwRev);
    DisplayValueToLcm(_Version_Dispenser_LCM_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->LedModuleFwRev);
    DisplayValueToLcm(_Version_Dispenser_LED_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->Connector1FwRev);
    DisplayValueToLcm(_Version_Dispenser_Connector0_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysInfo->Connector2FwRev);
    DisplayValueToLcm(_Version_Dispenser_Connector1_value, (uint8_t *)value, sizeof(value));
    sprintf((char *)value,"%s",pSysConfig->Eth0Interface.EthIpAddress);
    DisplayValueToLcm(_Version_Dispenser_IPAddr_value, (uint8_t *)value, sizeof(value));
}
void InformationShow()
{
    is_show = true;
    if (_showInformIndex == 0 ) {
        pSysInfo->PageIndex = __SHOW_CABIENT_VERSION;
    } else {
        pSysInfo->PageIndex = __SHOW_DISPENSER_VERASION;
        ShowDispenserVersion();
    }
}
void DefautLayOut()
{
    ChangeDisplay2Value(_CellPhone_Mode,_ICON_cellphone_mode);
    ChangeDisplay2Value(_Screen_Mode,_ICON_screen_mode);
    ChangeDisplay2Value(_PlugIn_String,_ICON_Plugin);
    ChangeDisplay2Value(_Communication_Status,_ICON_Communication_on);
    ChangeDisplay2Value(_SaftyDetect_Status,_ICON_SaftyDetect_off);
    ChangeDisplay2Value(_PreCharge_Status,_ICON_PrepareCharge_off);
    ChangeDisplay2Value(_Text_Voltage,_ICON_text_Voltage);
    ChangeDisplay2Value(_Text_Current,_ICON_text_Current);
    ChangeDisplay2Value(_Text_Power,_ICON_text_Power);
    ChangeDisplay2Value(_Text_Rate,_ICON_text_Rate);
    ChangeDisplay2Value(_Text_Time,_ICON_text_time);
    ChangeDisplay2Value(_Battery_Progress_0,_ICON_battery_0);
    ChangeDisplay2Value(_Symbol_V,_ICON_symbol_V);
    ChangeDisplay2Value(_Symbol_A,_ICON_symbol_A);
    ChangeDisplay2Value(_Symbol_KW,_ICON_symbol_KW);
    ChangeDisplay2Value(_Symbol_Rate,_ICON_symbol_rate);
    ChangeDisplay2Value(_Symbol_Percent,_ICON_symbol_percent);
    ChangeDisplay2Value(_Text_ChargeRate,_ICON_text_ChargereRate);
    ChangeDisplay2Value(_Text_ChargePay,_ICON_text_ChargePay);
    ChangeDisplay2Value(_Battery_Progress_1,_ICON_text_battery_0);
    ChangeDisplay2Value(_Symbol_Dollar,_ICON_text_dollor);
    ChangeDisplay2Value(_Text_ChargeComplete,_ICON_text_ChargeComplete);
}
int main(void)
{
    bool defaulttext = false;
    if (CreateAllCsuShareMemory() == FAIL) {
        log_error("create share memory error");
        return FAIL;
    }

    MappingGunChargingInfo("LCM Control Task");

    pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
    pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
    pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
    ShmFanModuleData = (struct FanModuleData *)GetShmFanModuleData();;
    ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
    ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
    ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
    struct StatusCodeData *ShmStatusCodeData = (struct StatusCodeData *)GetShmStatusCodeData();;
    struct ChargingInfoData *pDcChargingInfo_0 = (struct ChargingInfoData *)GetDcChargingInfoData(LEFT_GUN_NUM);
    struct ChargingInfoData *pDcChargingInfo_1 = (struct ChargingInfoData *)GetDcChargingInfoData(RIGHT_GUN_NUM);
    _port = CreateCommunicationLcmPort();
    uint8_t changeWarningPriority = 0;
    uint8_t curWarningCount = 255;
    ChangeBackLight(true);
    _totalCount = pSysConfig->TotalConnectorCount;
    _everyPageRollChange = 0;
    //Initialization();

    //printf("_LCM_COMPLETE ");
    //ChangeToOtherPage(_LCM_COMPLETE);
    DefautLayOut();
    //return 0;
    //uint8_t index = 1;
    ShmDcCommonData->PSU_Number = 12;

    CheckLCMPressed();

    while (_port != -1) {
        if (strcmp((char *)pSysInfo->LcmHwRev, moduleName) != 0x00) {
            GetCurrentPage();
            sleep(1);

            ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LcmModuleCommFail = true;

        } else {
            UpdateLcmFunction(ShmDcCommonData,_port);

            // Warning 處理
            if (curWarningCount != pSysWarning->WarningCount) {
                changeWarningPriority = 0;
                pSysWarning->PageIndex = 0;
                curWarningCount = pSysWarning->WarningCount;
                ChangeWarningFunc();
            } else if (pSysWarning->WarningCount > 5 && changeWarningPriority == 0) {
                // 當有兩頁 Warning 則每隔三秒改變一次
                if (pSysWarning->PageIndex == 0) {
                    pSysWarning->PageIndex = 1;
                } else {
                    pSysWarning->PageIndex = 0;
                }

                ChangeWarningFunc();
            }
            /*
            // Show Default Text
            if (!defaulttext) {
                ShowCabientVersionDefaultText();
                ShowDispenserVersionDefautlText();
                defaulttext = true;
            }
            ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LcmModuleCommFail = false;

            // 頁面資訊處理
            ProcessPageInfo();

            // 換頁處理
            //GetCurrentPage(); //DS60-120 add

            if (pSysConfig->ShowInformation == YES && pSysInfo->SystemPage == _PAGE_AUTHORIZE) {
                InformationShow();
                ChangeToOtherPage(pSysInfo->PageIndex);
                usleep(100000);
                continue;
            } else {
                if (is_show)
                    _showInformIndex >= 1 ? _showInformIndex = 0 : _showInformIndex++;
                is_show = false;
            }
            GetCurrentPage(); //DS60-120 add

            ChangeCurPage();
            changeWarningPriority >= 30 ? changeWarningPriority = 0 : changeWarningPriority++;
            usleep(10000); //*/

            ///*
            ProcessPageInfo();
            GetCurrentPage();
            ChangeCurPage();
            usleep(10000);
            //*/
        }
    } //while

    ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LcmModuleCommFail = true;

    log_info("Close LCM Uart Port");
    CloseCommunicationLcmPort();

    return FAIL;
}