#include "Nextion.h"

char L_nowdisp = -1;          //Sended nowdisp
char L_vfoActive;             //vfoActive
unsigned long L_vfoCurr;      //vfoA
byte L_vfoCurr_mode;          //vfoA_mode
unsigned long L_vfoA;         //vfoA
byte L_vfoA_mode;             //vfoA_mode
unsigned long L_vfoB;         //vfoB
byte L_vfoB_mode;             //vfoB_mode
char L_ritOn;
unsigned long L_ritTxFrequency; //ritTxFrequency
char L_inTx;
byte L_isDialLock;            //byte isDialLock
byte  L_Split;            //isTxType
byte  L_TXStop;           //isTxType
byte L_tuneStepIndex;     //byte tuneStepIndex
byte L_scaledSMeter;      //scaledSMeter
unsigned long L_sideTone; //sideTone
byte L_cwKeyType;          //L_cwKeyType 0: straight, 1 : iambica, 2: iambicb
unsigned int L_cwSpeed;   //cwSpeed
byte L_cwDelayTime;       //cwDelayTime
byte L_delayBeforeCWStartTime;  //byte delayBeforeCWStartTime
byte L_attLevel;
byte L_isIFShift;             //1 = ifShift, 2 extend
int L_ifShiftValue;
byte L_sdrModeOn;
byte scaledSMeter = 0;
float calcVSWR = 0.0;
float L_calcVSWR = 0.0;
byte scaledVSWR = 0;
byte L_scaledVSWR = 0;
float fwdPower = 0;
float L_fwdPower = 0;
float revPower = 0;
float L_revPower = 0;


//Control must have prefix 'v' or 's'
char softSTRHeader[11] = {'p', 'm', '.', 's', '0', '.', 't', 'x', 't', '=', '\"'};
char softINTHeader[10] = {'p', 'm', '.', 'v', '0', '.', 'v', 'a', 'l', '='};

char softTemp[20];

/*!
    @brief  Send a string or numeric variable to the Nextion LCD.
    @param  varType
            The type of the variable being sent to the Nextion LCD.
    @param  varIndex
            The index (ID) of the variable being sent to the Nextion LCD.
 */
void sendHeader(char varType, char varIndex)
{
  if (varType == SWS_HEADER_STR_TYPE)
  {
    softSTRHeader[4] = varIndex;
    for (unsigned i = 0; i < sizeof(softSTRHeader)/sizeof(softSTRHeader[0]); i++)
      Serial1.write(softSTRHeader[i]);
  }
  else
  {
    softINTHeader[4] = varIndex;
    for (unsigned i = 0; i < sizeof(softINTHeader)/sizeof(softINTHeader[0]); i++)
      Serial1.write(softINTHeader[i]);
  }
}

/*!
    @brief  Send an unsigned long variable to the Nextion LCD.
    @param  varIndex
            The index (ID) of the variable being sent to the Nextion LCD.
    @param  sendValue
            The value of the variable being sent to the Nextion LCD.
 */
void sendCommandUL(char varIndex, unsigned long sendValue)
{
  sendHeader(SWS_HEADER_INT_TYPE, varIndex);

  memset(softTemp, 0, 20);
  ultoa(sendValue, softTemp, DEC);
  Serial1.print(softTemp);
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);  
}

/*!
    @brief  Send a (signed) long variable to the Nextion LCD.
    @param  varIndex
            The index (ID) of the variable being sent to the Nextion LCD.
    @param  sendValue
            The value of the variable being sent to the Nextion LCD.
 */
void sendCommandL(char varIndex, long sendValue)
{
  sendHeader(SWS_HEADER_INT_TYPE, varIndex);

  memset(softTemp, 0, 20);
  ltoa(sendValue, softTemp, DEC);
  Serial1.print(softTemp);
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);  
}

/*!
    @brief  Send a string variable to the Nextion LCD.
    @param  varIndex
            The index (ID) of the variable being sent to the Nextion LCD.
    @param  sendValue
            The value of the variable being sent to the Nextion LCD.
 */
void sendCommandStr(char varIndex, const char* sendValue)
{
  sendHeader(SWS_HEADER_STR_TYPE, varIndex);
  
  Serial1.print(sendValue);
  Serial1.write('\"');
  Serial1.write(0xFF);
  Serial1.write(0xFF);
  Serial1.write(0xFF);
}

unsigned char softBuff1Num[14] = {'p', 'm', '.', 'c', '0', '.', 'v', 'a', 'l', '=', 0, 0xFF, 0xFF, 0xFF};

/*!
    @brief  Send a single digit variable to the Nextion LCD.
    @param  varIndex
            The index (ID) of the variable being sent to the Nextion LCD.
            Values 0~9 are: Mode, nowDisp, ActiveVFO, IsDialLock, IsTxtType, IsSplitType.
    @param  sendValue
            The value of the variable being sent to the Nextion LCD.
 */
void sendCommand1Num(char varIndex, char sendValue)
{
  softBuff1Num[4] = varIndex;
  softBuff1Num[10] = sendValue + 0x30;  // convert to character digit

  for (unsigned i = 0; i < sizeof(softBuff1Num)/sizeof(softBuff1Num[0]); i++)
    Serial1.write(softBuff1Num[i]);
}
