#include "myDisplayUnit.h"
#include "myLcd.h"

int8_t uiPageIdAddress = ~(0);
int8_t uiPageCount = 1;
int8_t rx_tx_count;
int8_t rx_tp_count;
int8_t setting_count;
int8_t tx_tp_count;
uiPageUnit_ts uiPageParams[UI_PAGE_ID_MAX];
enterCallback enterCb;
uint32_t buffer_rfBr;
uint32_t rfBr;
uint32_t buffer_channelStep;
uint32_t buffer_freq;
int8_t buffer_rfPower;
char *buffer_type;

void myDisplay_ui_firstUi_setDeviceName(char *name)
{
    myLCD_str8x16(IM_NOMALE, 80, 1, name);
}
void myDisplay_ui_firstUi_setFreq(uint32_t freq)
{
    myLCD_str8x16(IM_NOMALE, 80, 2, "Freq.%6.2fMHz", (float)freq/1000000);
}
void myDisplay_ui_firstUi_setRfPower(int power)
{
    myLCD_str8x16(IM_NOMALE, 95, 3, "Pwr.%ddBm", power);
}
void myDisplay_ui_firstUi_setRfBr(uint32_t br)
{
    myLCD_str8x16(IM_NOMALE, 95, 4, "Br.%.3fkb", (float)br/1000);
}
void myDisplay_ui_firstUi(int agr0, void *agr1_ptr)
{
    if (uiPageIdAddress != UI_PAGE_ID_FIRST_UI)
    {
        myLCD_16x16(IM_INVERSE, 10,  11, "深圳市沃进科技有限公司");
        myLCD_displayImage(IM_NOMALE, 1, 1, IMG_SRC_VOLLGO);
        myLCD_str8x16(IM_NOMALE, 80, 1, "VG------------");
        myLCD_str8x16(IM_NOMALE, 95, 2, "Freq.***");
        myLCD_str8x16(IM_NOMALE, 95, 3, "Pwr.**");
    }
    else
    {
        myLCD_str8x16(agr0 == 0 ? IM_INVERSE : IM_NOMALE, 
                        HORIZONTAL_RIGHT, 5, "ENTER");
    }
  uiPageIdAddress = UI_PAGE_ID_FIRST_UI;
}
void myDisplay_ui_selectMode(int agr0, void *agr1_ptr)
{
  int i;

  myLCD_clearFull();
  for ( i = 0; i <= MAX_PAGE_COUNT; i++)
  {
    myLCD_displayBlock(1, i);
  }
  uiPageIdAddress = UI_PAGE_ID_ITEM_MODE;
  myLCD_str8x16(agr0 == 0 ? IM_INVERSE : IM_NOMALE, 
                10, 1, "Enter RF Transmiter");
  myLCD_str8x16(agr0 == 1 ? IM_INVERSE : IM_NOMALE, 
                10, 2, "Enter RF Receiver");
  myLCD_str8x16(agr0 == 2 ? IM_INVERSE : IM_NOMALE, 
                10, 3, "Enter RF Continuous");
  myLCD_str8x16(agr0 == 3 ? IM_INVERSE : IM_NOMALE, 
                10, 4, "Enter RF Setting");
  myLCD_str8x16(agr0 == 4 ? IM_INVERSE : IM_NOMALE, 
                10, 5, "Enter Device Infor");
}
void myDisplay_ui_rf_tx(uint8_t mode)
{
  int i;

  myLCD_clearFull();
  for ( i = 0; i <= MAX_PAGE_COUNT; i++)
  {
    myLCD_displayBlock(1, i);
  }
  myLCD_str8x16(mode == 0 ? IM_INVERSE : IM_NOMALE, 
                10, 1, "Packet TX");
  myLCD_str8x16(mode == 1 ? IM_INVERSE : IM_NOMALE, 
                10, 2, "Continuous TX");
}
void myDisplay_ui_rf_tx_packet(int agr0, void *agr1_ptr)
{
    int i;
    if (uiPageIdAddress != UI_PAGE_ID_TX_PACKET)
    {
        myLCD_clearFull();
        for ( i = 0; i <= MAX_PAGE_COUNT; i++)
        {
            myLCD_displayBlock(1, i);
        }
        myLCD_str8x16(IM_NOMALE, HORIZONTAL_CENTER, 1, "Packet TX");
        myLCD_str8x16(IM_NOMALE, 5, 2, "Packet:");
    }
    uiPageIdAddress = UI_PAGE_ID_TX_PACKET;

    if (uiPageIdAddress == UI_PAGE_ID_TX_PACKET)
    {
        myLCD_str8x16(agr0 == 0 ? IM_INVERSE : IM_NOMALE, 
                    HORIZONTAL_RIGHT, 6, "START");
    }
}
void myDisplay_ui_rf_tx_packet_buffer(uint8_t *buffer)
{
    if (uiPageIdAddress == UI_PAGE_ID_TX_PACKET)
    {
        myLCD_str8x16(IM_NOMALE, 10, 3, (char *)buffer);
    }
}
void myDisplay_ui_rf_tx_packet_counts(uint32_t rate, uint32_t count)
{
    if (uiPageIdAddress == UI_PAGE_ID_TX_PACKET)
    {
        myLCD_str8x16(IM_NOMALE, HORIZONTAL_RIGHT, 2, "%5.1f%%   %5d",(float)rate/10, count);
    }
}
void myDisplay_ui_rf_tx_packet_consumeTime(uint32_t time)
{
    if (uiPageIdAddress == UI_PAGE_ID_TX_PACKET)
    {
        if (time == ~(uint32_t)0)
        {
            myLCD_str8x16(IM_NOMALE, HORIZONTAL_RIGHT, 1, "   OUT");
        }
        else
        {
            myLCD_str8x16(IM_NOMALE, HORIZONTAL_RIGHT, 1, "%5dms", time);
        }
        
    }
}
void myDisplay_ui_rf_tx_packet_ackRssi(int16_t rssi)
{
    if (uiPageIdAddress == UI_PAGE_ID_TX_PACKET)
    {
        if (rssi > -150 && (rssi < 10))
        {
            myLCD_str8x16(IM_NOMALE, HORIZONTAL_RIGHT, 3, "%04ddBm", rssi%10000);
        }
    }
}
void myDisplay_ui_rf_setting_freq(uint32_t freq)
{
    buffer_freq = freq;
}
void myDisplay_ui_rf_setting_rfPower(int8_t rfPower)
{
    buffer_rfPower = rfPower;
}
void myDisplay_ui_rf_setting_channelStep(uint32_t channelStep)
{
    buffer_channelStep = channelStep;
}
void myDisplay_ui_rf_setting_type(char *type)
{
    buffer_type = type;
}
void myDisplay_ui_rf_setting_rfBr(uint32_t br)
{
    rfBr = br;
}
void myDisplay_ui_rf_setting(int agr0, void *agr1_ptr)
{
  int i;

  if (uiPageIdAddress != UI_PAGE_ID_SETTING)
  {    
    myLCD_clearFull();
    for ( i = 0; i <= MAX_PAGE_COUNT; i++)
    {
      myLCD_displayBlock(1, i);
    }
    myLCD_str8x16(IM_NOMALE, HORIZONTAL_CENTER, 1, "Setting");
    myLCD_str8x16(IM_NOMALE, 10, 2, "Type:");
    myLCD_str8x16(IM_NOMALE, 10, 3, "Chnl:----------MHz");
    myLCD_str8x16(IM_NOMALE, 10, 4, "Step:----MHz");
    myLCD_str8x16(IM_NOMALE, 10, 5, "TxPower:---dBm");
    myLCD_str8x16(IM_NOMALE, 10, 6, "RfBr:----.---kbps");
  }
  uiPageIdAddress = UI_PAGE_ID_SETTING;
  if (uiPageIdAddress == UI_PAGE_ID_SETTING)
  {    
    myLCD_str8x16(agr0 == SET_ITEM_INDEX_TYPE ? IM_INVERSE : IM_NOMALE, 
                  10 + 8 * strlen("Type:"), 2, 
                  "%s", buffer_type);
    myLCD_str8x16(agr0 == SET_ITEM_INDEX_FREQ ? IM_INVERSE : IM_NOMALE, 
                  10 + 8 * strlen("Chnl:"), 3, 
                  "%02d->%6.2f", ((int *)agr1_ptr)[SET_ITEM_INDEX_FREQ], (float)buffer_freq / 1000000);
    myLCD_str8x16(agr0 == SET_ITEM_INDEX_STEP ? IM_INVERSE : IM_NOMALE, 
                  10 + 8 * strlen("Step:"), 4, 
                  "%4.2f", (float)buffer_channelStep / 1000000);
    myLCD_str8x16(agr0 == SET_ITEM_INDEX_TXPOWER ? IM_INVERSE : IM_NOMALE, 
                  10 + 8 * strlen("TxPower:"), 5, 
                  "%03d", buffer_rfPower);
    myLCD_str8x16(agr0 == SET_ITEM_INDEX_RFBAUDRATE ? IM_INVERSE : IM_NOMALE, 
                  10 + 8 * strlen("RfBr:"), 6, 
                  "%8.3f", (float)rfBr/1000);
  }
}
uint8_t ver_buffer;
char *mod_buffer;
void myDisplay_ui_device_infor(int agr0, void *agr1_ptr)
{
  int i;

  if (uiPageIdAddress != UI_PAGE_ID_DEVICE_INFOR)
  {    
    myLCD_clearFull();
    for ( i = 0; i <= MAX_PAGE_COUNT; i++)
    {
      myLCD_displayBlock(1, i);
    }
    myLCD_str8x16(IM_NOMALE, HORIZONTAL_CENTER, 1, "Infor");
    myLCD_str8x16(IM_NOMALE, 10, 2, "TP: %s", mod_buffer);
    myLCD_str8x16(IM_NOMALE, 
                  10, 3, "VER: V%02x", ver_buffer);
    myLCD_str8x16(IM_NOMALE, 10, 4, "MD: VOLLGO");
  }
  uiPageIdAddress = UI_PAGE_ID_DEVICE_INFOR;
}
void myDisplay_ui_deviceInfor_setVer(uint8_t ver)
{
    ver_buffer = ver;
}
void myDisplay_ui_deviceInfor_setModule(char *moduleName)
{
    mod_buffer = moduleName;
}
void myDisplay_ui_rf_rx(uint8_t mode)
{
  int i;

  myLCD_clearFull();
  for ( i = 0; i <= MAX_PAGE_COUNT; i++)
  {
    myLCD_displayBlock(1, i);
  }
  myLCD_str8x16(mode == 0 ? IM_INVERSE : IM_NOMALE, 
                10, 1, "Packet RX");
  myLCD_str8x16(mode == 1 ? IM_INVERSE : IM_NOMALE, 
                10, 2, "Continuous RX");
}
void myDisplay_ui_rf_continuos(int agr0, void *agr1_ptr)
{
  int i;
  if (uiPageIdAddress != UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_clearFull();
    for ( i = 0; i <= MAX_PAGE_COUNT; i++)
    {
      myLCD_displayBlock(1, i);
    }
    myLCD_str8x16(IM_NOMALE, HORIZONTAL_CENTER, 1, "Continuous");
    myDisplay_ui_rf_continuos_rfFreq();
    myDisplay_ui_rf_continuos_rfBr();
    myDisplay_ui_rf_continuos_rfPwr();
  }
  uiPageIdAddress = UI_PAGE_ID_RF_CONTINUOUS;
  if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(agr0 == CNT_ITEM_INDEX_TX ? IM_INVERSE : IM_NOMALE, 
                  HORIZONTAL_CENTER, 2, "TX");
    myLCD_str8x16(agr0 == CNT_ITEM_INDEX_RX ? IM_INVERSE : IM_NOMALE, 
                  HORIZONTAL_CENTER, 3, "RX");
    myLCD_str8x16(agr0 == CNT_ITEM_INDEX_TX_MD ? IM_INVERSE : IM_NOMALE, 
                  110+3, 2, "TX_MD");
  }
}
void myDisplay_ui_rf_continuos_txCurrent(float Current)
{
  if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  10, 4, "C=%6.2fmA", Current);
  }
}
void myDisplay_ui_rf_continuos_rxLen(float rate, uint16_t len)
{
  if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  HORIZONTAL_RIGHT, 4, "%5.1f%% %4d", rate, len);
  }
}
void myDisplay_ui_rf_continuos_rfFreq(void)
{
//   if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  5, 2, 
                  "%6.2fMHz", (float)buffer_freq / 1000000);
  }
}
void myDisplay_ui_rf_continuos_rfBr(void)
{
//   if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  HORIZONTAL_RIGHT, 3, 
                  "%7.3fkbps", (float)rfBr / 1000);
  }
}
void myDisplay_ui_rf_continuos_rfPwr(void)
{
//   if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  5, 3, 
                  "%ddBm", buffer_rfPower);
  }
}
void myDisplay_ui_rf_continuos_rxErrorRate(float rate)
{
  if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  10, 5, "rate=%6.2f%%", rate);
  }
}
void myDisplay_ui_rf_continuos_rxRssi(int16_t rssi)
{
  if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  HORIZONTAL_RIGHT, 2, "%4d", rssi);
  }
}
void myDisplay_ui_rf_continuos_rxContinuousFreq(uint32_t freq)
{
  if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
  {
    myLCD_str8x16(IM_NOMALE, 
                  10, 6, "freq=%6d", freq);
  }
}
void myDisplay_ui_rf_continuos_rxPacketCount(uint8_t status, uint16_t count)
{
    if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
    {
        myLCD_str8x16(IM_NOMALE, 
                    10, 5, "count=%d sta=%d", count, status);
    }
}

void myDisplay_ui_rf_continuos_rxPacket(uint8_t *buf, uint16_t len)
{
    int i = 0;
    if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
    {
        myLCD_str8x16(IM_NOMALE, 
                    10, 6, "                        ");
        if (len)
        {
            myLCD_str8x16(IM_NOMALE, 
                        10, 6, "packet=");
            for ( i = 0; i < len; i++)
            {
                myLCD_str8x16(IM_NOMALE, 
                            10, 6 + strlen("packet=") + i,
                             "%x", *buf);
                buf ++;
            }
            
        }
        else
        {
            myLCD_str8x16(IM_NOMALE, 
                        10, 6, "packet=%s", buf);
        }
    }
}
void myDisplay_ui_rf_continuos_rxPacketGetCount(uint32_t count)
{
    if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
    {
        myLCD_str8x16(IM_NOMALE, 
                    10, 6, "rx count=%d", count);
    }
}

void myDisplay_ui_rf_rx_packet(int agr0, void *agr1_ptr)
{
    int i;
    if (uiPageIdAddress != UI_PAGE_ID_RX_PACKET)
    {
        myLCD_clearFull();
        for ( i = 0; i <= MAX_PAGE_COUNT; i++)
        {
            myLCD_displayBlock(1, i);
        }
        myLCD_str8x16(IM_NOMALE, HORIZONTAL_CENTER, 1, "Packet RX");
        myLCD_str8x16(IM_NOMALE, 
                    5, 3, "Packet:");
    }
    uiPageIdAddress = UI_PAGE_ID_RX_PACKET;

    if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
    {
        myLCD_str8x16(agr0 == 0 ? IM_INVERSE : IM_NOMALE, 
                    HORIZONTAL_RIGHT, 6, "START");
    }
}
void myDisplay_ui_rf_rxPacket_rxCurrent(float current)
{
  if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
  {
    myLCD_str8x16(IM_NOMALE, 
                  5, 2, "%-6.2fmA", current);
  }
}
void myDisplay_ui_rf_rxPacket_rssi(int16_t rssi)
{
    if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
    {
        if (rssi > -150 && (rssi < 10))
        {
            myLCD_str8x16(IM_NOMALE, 
                        HORIZONTAL_RIGHT, 2, "%04ddBm", rssi%10000);
        }
    }
}
void myDisplay_ui_rf_rxPacket_count(uint32_t count)
{
    if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
    {
        myLCD_str8x16(IM_NOMALE, 
                    HORIZONTAL_CENTER, 3, "%06d", count);
    }
}
void myDisplay_ui_rf_rxPacket_rate(float rate, int count, int frame)
{
    if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
    {
            myLCD_str8x16(IM_NOMALE, 
                        10, 3, "rssi=%3.0f%%,cnt=%3d %1d", rate, count, frame & 0x0f);
    }
}
void myDisplay_ui_rf_rxPacket_buffer(uint8_t *buf, uint16_t len)
{
    int i = 0;
    if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
    {
        myLCD_str8x16(IM_NOMALE, 
                    10, 5, "                        ");
        if (len)
        {
            myLCD_str8x16(IM_NOMALE, 
                        10, 5, "packet=");
            for ( i = 0; i < len; i++)
            {
                myLCD_str8x16(IM_NOMALE, 
                            10, 5 + strlen("packet=") + i,
                             "%x", *buf);
                buf ++;
            }
            
        }
        else
        {
            myLCD_str8x16(IM_NOMALE, 
                        10, 5, "packet=%-15s", buf);
        }
    }
}
uint8_t displayBuffer[3][20];
void loadDisplayBuffer(uint8_t *buf, uint16_t len)
{
    
    memcpy((char *)displayBuffer[0], (char *)displayBuffer[1], 20);
    memcpy((char *)displayBuffer[1], (char *)displayBuffer[2], 20);
    memset(displayBuffer[2], 0, 20);
    memcpy((char *)displayBuffer[2], (char *)buf, 20);
}
void myDisplay_ui_rf_rxPacket_scroll_buffer(uint8_t *buf, uint16_t len)
{
    int i = 0;
    if (uiPageIdAddress == UI_PAGE_ID_RX_PACKET)
    {

        if (len)
        {
            for ( i = 0; i < len; i++)
            {
                myLCD_str8x16(IM_NOMALE, 
                            10 + i, 5,
                             "%x", *buf);
                buf ++;
            }
            loadDisplayBuffer(buf, len);
        }
        else
        {
        }
        loadDisplayBuffer(buf, len);
        myLCD_str8x16(IM_NOMALE, 18, 4, 
                        "%-15s", displayBuffer[0]);
        myLCD_str8x16(IM_NOMALE, 18, 5, 
                        "%-15s", displayBuffer[1]);
        myLCD_str8x16(IM_NOMALE, 18, 6, 
                        "%-15s", displayBuffer[2]);
    }
}
void loadDisplayBufferContinue(uint8_t *buf, uint16_t len)
{
    
    memcpy((char *)displayBuffer[0], (char *)displayBuffer[1], 20);
    memset(displayBuffer[1], 0, 20);
    memcpy((char *)displayBuffer[1], (char *)buf, 20);
}
void myDisplay_ui_rf_rxContinue_scroll_buffer(uint8_t *buf, uint16_t len)
{
    int i = 0;
    if (uiPageIdAddress == UI_PAGE_ID_RF_CONTINUOUS)
    {
        loadDisplayBufferContinue(buf, len);
        myLCD_str8x16(IM_NOMALE, 18, 5, 
                        "%-15s", displayBuffer[0]);
        myLCD_str8x16(IM_NOMALE, 18, 6, 
                        "%-15s", displayBuffer[1]);
    }
}
/**
 * 光标控制
 * direct: 方向控制
 *          =1，向上移动
 *          =0，向下移动
 * **/
void myDisplay_change(uint8_t direct)
{
    if (uiPageParams[uiPageCount - 1].cursorCount)
    {
        //设置加减控制
      if (uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting])
      {
        if (direct)
        {
          uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting] ++;
          if (uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting] >=
              uiPageParams[uiPageCount - 1].itemMaxValueTab[uiPageParams[uiPageCount - 1].cursorCounting])
          {
            uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting] = uiPageParams[uiPageCount - 1].itemMinValueTab[uiPageParams[uiPageCount - 1].cursorCounting];
          }
        }
        else
        {
          uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting] --;
          if (uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting] < uiPageParams[uiPageCount - 1].itemMinValueTab[uiPageParams[uiPageCount - 1].cursorCounting])
          {
            uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting] = 
            uiPageParams[uiPageCount - 1].itemMaxValueTab[uiPageParams[uiPageCount - 1].cursorCounting] - 1;
          }
        }
        if (enterCb)
        {
          enterCb(uiPageCount, 
                uiPageParams[uiPageCount - 1].cursorCounting, 
                uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting],
                uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting]
                );
        }
      }
      else
      {
          //光标移动控制
        if (direct)
        {
          uiPageParams[uiPageCount - 1].cursorCounting --;
          if (uiPageParams[uiPageCount - 1].cursorCounting < 0)
          {
            uiPageParams[uiPageCount - 1].cursorCounting = uiPageParams[uiPageCount - 1].cursorCount - 1;
          }
        }
        else
        {
          uiPageParams[uiPageCount - 1].cursorCounting ++;
          if (uiPageParams[uiPageCount - 1].cursorCounting >= uiPageParams[uiPageCount - 1].cursorCount)
          {
            uiPageParams[uiPageCount - 1].cursorCounting = 0;
          }
        }
      }
      
      uiPageParams[uiPageCount - 1].uiDriver(uiPageParams[uiPageCount - 1].cursorCounting, 
                                            uiPageParams[uiPageCount - 1].itemValueTab);
      
    }
}
/***
 * 按下确定键
 *  进入下一个界面或者进入设置状态
*/
void myDisplay_enter(uint8_t direct)
{
  if (direct == ENTER_NEXT_PAGE)
  {
      //进入设置状态
    if (uiPageParams[uiPageCount - 1].itemTypeTab[uiPageParams[uiPageCount - 1].cursorCounting] == TYPE_WRITE_TEXT)
    {
        //切换设置状态
      uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting] = 
          !uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting];
      uiPageParams[uiPageCount - 1].uiDriver(uiPageParams[uiPageCount - 1].cursorCounting, 
                                            uiPageParams[uiPageCount - 1].itemValueTab);
      if (enterCb)
      {
        enterCb(uiPageParams[uiPageCount - 1].id, 
              uiPageParams[uiPageCount - 1].cursorCounting, 
              uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting],
              uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting]
              );
      }
      
    }
    //切换到下一个界面
    if (uiPageParams[uiPageCount - 1].itemTypeTab[uiPageParams[uiPageCount - 1].cursorCounting] == TYPE_NEXT_LINK)
    {
      uiPageCount = uiPageParams[uiPageCount - 1].nextPageIdTab[uiPageParams[uiPageCount - 1].cursorCounting];
      uiPageParams[uiPageCount - 1].uiDriver(uiPageParams[uiPageCount - 1].cursorCounting, 
                                            uiPageParams[uiPageCount - 1].itemValueTab);
    }
  }
  //切换到上一个界面
  if (direct == ENTER_LAST_PAGE)
  {
    if (uiPageParams[uiPageCount - 1].lastPageIdTab[0])
    {    
      memset(uiPageParams[uiPageCount - 1].writeStaTab, 0, sizeof(uiPageParams[uiPageCount - 1].writeStaTab));
      if (enterCb)
      {
        enterCb(uiPageParams[uiPageCount - 1].id, 
              uiPageParams[uiPageCount - 1].cursorCounting, 
              uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting],
              uiPageParams[uiPageCount - 1].itemValueTab[uiPageParams[uiPageCount - 1].cursorCounting]
              );
      }
      uiPageCount = uiPageParams[uiPageCount - 1].lastPageIdTab[0];
      uiPageParams[uiPageCount - 1].uiDriver(uiPageParams[uiPageCount - 1].cursorCounting, 
                                            uiPageParams[uiPageCount - 1].itemValueTab);
    }
  }
}
int8_t myDisplay_getPageId(void)
{
  return uiPageIdAddress;
}
void uiTimerFlash_callBack(void)
{
    if (uiPageParams[uiPageCount - 1].writeStaTab[uiPageParams[uiPageCount - 1].cursorCounting])
    {
        uiPageParams[uiPageCount - 1].writeStatusTab[uiPageParams[uiPageCount - 1].cursorCounting] = 
            !uiPageParams[uiPageCount - 1].writeStatusTab[uiPageParams[uiPageCount - 1].cursorCounting];
        if (uiPageParams[uiPageCount - 1].writeStatusTab[uiPageParams[uiPageCount - 1].cursorCounting])
        {
            uiPageParams[uiPageCount - 1].uiDriver(uiPageParams[uiPageCount - 1].cursorCounting, 
                                                    uiPageParams[uiPageCount - 1].itemValueTab);
        }
        else
        {
            uiPageParams[uiPageCount - 1].uiDriver((~0), uiPageParams[uiPageCount - 1].itemValueTab);
        }
    }
}
void myDisplay_init(enterCallback cb)
{
    myLCD_init();

  uiPageParams[UI_PAGE_ID_FIRST_UI].id = UI_PAGE_ID_FIRST_UI + 1;
  uiPageParams[UI_PAGE_ID_FIRST_UI].uiDriver = myDisplay_ui_firstUi;
  uiPageParams[UI_PAGE_ID_FIRST_UI].lastPageIdTab[0] = 0;
  uiPageParams[UI_PAGE_ID_FIRST_UI].nextPageIdTab[0] = 2;
  uiPageParams[UI_PAGE_ID_FIRST_UI].itemTypeTab[0] = TYPE_NEXT_LINK;
  uiPageParams[UI_PAGE_ID_FIRST_UI].writeStaTab[0] = 1;
  uiPageParams[UI_PAGE_ID_FIRST_UI].cursorCounting = 0;
  uiPageParams[UI_PAGE_ID_FIRST_UI].cursorCount = 2;

  uiPageParams[UI_PAGE_ID_ITEM_MODE].id = UI_PAGE_ID_ITEM_MODE + 1;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].uiDriver = myDisplay_ui_selectMode;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].lastPageIdTab[0] = 0;

  uiPageParams[UI_PAGE_ID_ITEM_MODE].nextPageIdTab[0] = UI_PAGE_ID_TX_PACKET + 1;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].itemTypeTab[0] = TYPE_NEXT_LINK;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].nextPageIdTab[1] = UI_PAGE_ID_RX_PACKET + 1;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].itemTypeTab[1] = TYPE_NEXT_LINK;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].nextPageIdTab[2] = UI_PAGE_ID_RF_CONTINUOUS + 1;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].itemTypeTab[2] = TYPE_NEXT_LINK;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].nextPageIdTab[3] = UI_PAGE_ID_SETTING + 1;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].itemTypeTab[3] = TYPE_NEXT_LINK;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].nextPageIdTab[4] = UI_PAGE_ID_DEVICE_INFOR + 1;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].itemTypeTab[4] = TYPE_NEXT_LINK;

  uiPageParams[UI_PAGE_ID_ITEM_MODE].cursorCounting = 0;
  uiPageParams[UI_PAGE_ID_ITEM_MODE].cursorCount = 5;

  uiPageParams[UI_PAGE_ID_TX_PACKET].id = UI_PAGE_ID_TX_PACKET + 1;
  uiPageParams[UI_PAGE_ID_TX_PACKET].uiDriver = myDisplay_ui_rf_tx_packet;
  uiPageParams[UI_PAGE_ID_TX_PACKET].lastPageIdTab[0] = UI_PAGE_ID_ITEM_MODE + 1;
  uiPageParams[UI_PAGE_ID_TX_PACKET].nextPageIdTab[0] = 0;
  uiPageParams[UI_PAGE_ID_TX_PACKET].itemTypeTab[0] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_TX_PACKET].writeStaTab[0] = 0;
  uiPageParams[UI_PAGE_ID_TX_PACKET].cursorCounting = 0;
  uiPageParams[UI_PAGE_ID_TX_PACKET].cursorCount = 0;

  uiPageParams[UI_PAGE_ID_DEVICE_INFOR].id = UI_PAGE_ID_DEVICE_INFOR + 1;
  uiPageParams[UI_PAGE_ID_DEVICE_INFOR].uiDriver = myDisplay_ui_device_infor;
  uiPageParams[UI_PAGE_ID_DEVICE_INFOR].lastPageIdTab[0] = UI_PAGE_ID_ITEM_MODE + 1;
  uiPageParams[UI_PAGE_ID_DEVICE_INFOR].nextPageIdTab[0] = 0;
  uiPageParams[UI_PAGE_ID_DEVICE_INFOR].cursorCount = 0;

  uiPageParams[UI_PAGE_ID_RX_PACKET].id = UI_PAGE_ID_RX_PACKET + 1;
  uiPageParams[UI_PAGE_ID_RX_PACKET].uiDriver = myDisplay_ui_rf_rx_packet;
  uiPageParams[UI_PAGE_ID_RX_PACKET].lastPageIdTab[0] = UI_PAGE_ID_ITEM_MODE + 1;
  uiPageParams[UI_PAGE_ID_RX_PACKET].nextPageIdTab[0] = 0;
  uiPageParams[UI_PAGE_ID_RX_PACKET].itemTypeTab[0] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_RX_PACKET].writeStaTab[0] = 0;
  uiPageParams[UI_PAGE_ID_RX_PACKET].cursorCounting = 0;
  uiPageParams[UI_PAGE_ID_RX_PACKET].cursorCount = 0;

  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].id = UI_PAGE_ID_RF_CONTINUOUS + 1;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].uiDriver = myDisplay_ui_rf_continuos;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].lastPageIdTab[CNT_ITEM_INDEX_TX] = UI_PAGE_ID_ITEM_MODE + 1;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].itemTypeTab[CNT_ITEM_INDEX_TX] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].writeStaTab[CNT_ITEM_INDEX_TX] = 0;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].itemTypeTab[CNT_ITEM_INDEX_RX] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].writeStaTab[CNT_ITEM_INDEX_RX] = 0;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].itemTypeTab[CNT_ITEM_INDEX_TX_MD] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].writeStaTab[CNT_ITEM_INDEX_TX_MD] = 0;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].cursorCounting = 0;
  uiPageParams[UI_PAGE_ID_RF_CONTINUOUS].cursorCount = CNT_ITEM_INDEX_MAX_COUNT;

  uiPageParams[UI_PAGE_ID_SETTING].id = UI_PAGE_ID_SETTING + 1;
  uiPageParams[UI_PAGE_ID_SETTING].uiDriver = myDisplay_ui_rf_setting;
  uiPageParams[UI_PAGE_ID_SETTING].lastPageIdTab[SET_ITEM_INDEX_TYPE] = UI_PAGE_ID_ITEM_MODE + 1;
  uiPageParams[UI_PAGE_ID_SETTING].itemTypeTab[SET_ITEM_INDEX_TYPE] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_SETTING].itemMaxValueTab[SET_ITEM_INDEX_TYPE] = 4;

  uiPageParams[UI_PAGE_ID_SETTING].itemTypeTab[SET_ITEM_INDEX_FREQ] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_SETTING].itemMaxValueTab[SET_ITEM_INDEX_FREQ] = 32;

  uiPageParams[UI_PAGE_ID_SETTING].itemTypeTab[SET_ITEM_INDEX_STEP] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_SETTING].itemMaxValueTab[SET_ITEM_INDEX_STEP] = 200;

  uiPageParams[UI_PAGE_ID_SETTING].itemTypeTab[SET_ITEM_INDEX_TXPOWER] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_SETTING].itemMinValueTab[SET_ITEM_INDEX_TXPOWER] = -9;
  uiPageParams[UI_PAGE_ID_SETTING].itemMaxValueTab[SET_ITEM_INDEX_TXPOWER] = 20+1;

  uiPageParams[UI_PAGE_ID_SETTING].itemTypeTab[SET_ITEM_INDEX_RFBAUDRATE] = TYPE_WRITE_TEXT;
  uiPageParams[UI_PAGE_ID_SETTING].itemMinValueTab[SET_ITEM_INDEX_TXPOWER] = 1;
  uiPageParams[UI_PAGE_ID_SETTING].itemMaxValueTab[SET_ITEM_INDEX_RFBAUDRATE] = 7;

  uiPageParams[UI_PAGE_ID_SETTING].cursorCounting = SET_ITEM_INDEX_TYPE;//默认设置光标在第一个
  uiPageParams[UI_PAGE_ID_SETTING].cursorCount = SET_ITEM_INDEX_MAX_COUNT;

  enterCb = cb;
  uiPageParams[uiPageCount - 1].uiDriver(0, 0);
}
void myDisplay_setSettingParamsProfile(uint8_t index, int value, int min, int max)
{
    uiPageParams[UI_PAGE_ID_SETTING].itemValueTab[index] = value;
    uiPageParams[UI_PAGE_ID_SETTING].itemMinValueTab[index] = min;
    uiPageParams[UI_PAGE_ID_SETTING].itemMaxValueTab[index] = max;
}
void myDisplay_setSettingParams(int chipType, 
                                int rfChannel, 
                                int channelStep, 
                                int txPower, 
                                int rfBaudrate)
{
    uiPageParams[UI_PAGE_ID_SETTING].itemValueTab[SET_ITEM_INDEX_TYPE] = chipType;
    uiPageParams[UI_PAGE_ID_SETTING].itemValueTab[SET_ITEM_INDEX_FREQ] = rfChannel;
    uiPageParams[UI_PAGE_ID_SETTING].itemValueTab[SET_ITEM_INDEX_STEP] = channelStep;
    uiPageParams[UI_PAGE_ID_SETTING].itemValueTab[SET_ITEM_INDEX_TXPOWER] = txPower;
    uiPageParams[UI_PAGE_ID_SETTING].itemValueTab[SET_ITEM_INDEX_RFBAUDRATE] = rfBaudrate;
}
