 #define NEXT_FREE   (0)
 
 // address for Frequency 'frequency'
-const int AddressFreq = NEXT_FREE;
-#define NEXT_FREE   (AddressFreq + sizeof(Frequency))
+const int EepromFreq = NEXT_FREE;
+#define NEXT_FREE   (EepromFreq + sizeof(Frequency))
 
 // address for int 'selected digit'
-const int AddressSelDigit = NEXT_FREE;
-#define NEXT_FREE   (AddressSelDigit + sizeof(SelOffset))
+const int EepromSelDigit = NEXT_FREE;
+#define NEXT_FREE   (EepromSelDigit + sizeof(SelOffset))
 
 // address for 'VfoClockOffset' calibration
-const int AddressVfoClockOffset = NEXT_FREE;
-#define NEXT_FREE   (AddressVfoClockOffset + sizeof(VfoClockOffset))
+const int EepromVfoOffset = NEXT_FREE;
+#define NEXT_FREE   (EepromVfoOffset + sizeof(VfoClockOffset))
 
 // address for byte 'contrast'
-const int AddressContrast = NEXT_FREE;
-#define NEXT_FREE   (AddressContrast + sizeof(LcdContrast))
+const int EepromContrast = NEXT_FREE;
+#define NEXT_FREE   (EepromContrast + sizeof(LcdContrast))
 
 // address for byte 'brightness'
-const int AddressBrightness = NEXT_FREE;
-#define NEXT_FREE   (AddressBrightness + sizeof(LcdBrightness))
+const int EepromBrightness = NEXT_FREE;
+#define NEXT_FREE   (EepromBrightness + sizeof(LcdBrightness))
 
 // address for int 'hold click time'
-const int AddressHoldClickTime = NEXT_FREE;
-#define NEXT_FREE   (AddressHoldClickTime + sizeof(ReHoldClickTime))
+const int EepromHoldClickTime = NEXT_FREE;
+#define NEXT_FREE   (EepromHoldClickTime + sizeof(ReHoldClickTime))
 
 // address for int 'double click time'
-const int AddressDClickTime = NEXT_FREE;
-#define NEXT_FREE   (AddressDClickTime + sizeof(ReDClickTime))
+const int EepromDClickTime = NEXT_FREE;
+#define NEXT_FREE   (EepromDClickTime + sizeof(ReDClickTime))
 
 // number of frequency save slots in EEPROM
 const int NumSaveSlots = 10;
 
-const int SaveFreqBase = NEXT_FREE;
-#define NEXT_FREE   (SaveFreqBase + NumSaveSlots * sizeof(Frequency))
+const int EepromSaveFreqBase = NEXT_FREE;
+#define NEXT_FREE   (EepromSaveFreqBase + NumSaveSlots * sizeof(Frequency))
 
 //also save the offset for each frequency
-const int SaveOffsetBase = NEXT_FREE;
-#define NEXT_FREE   (SaveOffsetBase + NumSaveSlots * sizeof(SelOffset);
+const int EepromSaveOffsetBase = NEXT_FREE;
+#define NEXT_FREE   (EepromSaveOffsetBase + NumSaveSlots * sizeof(SelOffset);
 
 // additional EEPROM saved items go here
 

}

//----------------------------------------
// Cursor commands:
//     CSn;  set cursor index to 'n'
//     CG;   get cursor index
//----------------------------------------

const char * xcmd_cursor(char *answer, char *cmd)
{
  switch (cmd[1])
  {
    case 'G':
      // return cursor index
      if (strlen(cmd) == 3)
      {
        answer[0] = VfoSelectDigit + '0';
        answer[1] = '\0';
        return answer;
      }
      break;
    case 'S':
      if (strlen(cmd) == 4)
      {
        char *result_ptr = cmd + 2;

        int new_index = *result_ptr - '0';
        if (new_index <= 7)
        {
          VfoSelectDigit = new_index;
          display_sel_value(VfoFrequency, VfoSelectDigit,
                            NumFreqChars, NumCols - NumFreqChars - 2, 0);
          return "OK";
        }
      }
      break;
  }
  
  return "ERROR";
}

//----------------------------------------
// Digit commands:
//     DS+n;        increment frequency digit by 'n'
//     DS-n;        decrement frequency digit by 'n'
//----------------------------------------

const char * xcmd_digit(char *answer, char *cmd)
{
  switch (cmd[1])
  {
    case 'S':
      if (strlen(cmd) == 5)
      {
        char sign = *(cmd + 2);

        if (sign == '+' || sign == '-')
        {
          int inc = *(cmd + 3) - '0';

          if (inc >= 0 && inc <= 9)
          {
            if (sign == '+')
            {
              while (inc--)
              {
                VfoFrequency += offset2bump[VfoSelectDigit];
              }
              if (VfoFrequency > MaxFreq)
                VfoFrequency = MaxFreq;
              display_sel_value(VfoFrequency, VfoSelectDigit,
                                NumFreqChars, NumCols - NumFreqChars - 2, 0);
            }
            else
            {
              while (inc--)
              {
                VfoFrequency -= offset2bump[VfoSelectDigit];
              }
              if (VfoFrequency < MinFreq)
                VfoFrequency = MinFreq;
              display_sel_value(VfoFrequency, VfoSelectDigit,
                                NumFreqChars, NumCols - NumFreqChars - 2, 0);
            }
            return "OK";
          }
        }
      }
      break;
  }
  
  return "ERROR";
}

//----------------------------------------
// Convert a brightness 'index' to a brightness value.
//
//     index  brightness index in [1, 16]
//
// Returns the brightness value in range [15, 255], step 16.
//----------------------------------------
int brightness_index2value(int index)
{
  // clamp 'index' to range [1, 16]
  if (index < 1) index = 1;
  if (index > 16) index = 16;

  // return the appropriate value
  return index * 16 - 1;
}

//----------------------------------------
// Convert a brightness value to a brightness 'index'.
//
//     value  brightness value in the range [15, 255], step 16
//
// Returns the brightness index in range [1, 16].
//----------------------------------------
int brightness_value2index(int value)
{
  // clamp 'value' to canonical value in range [15, 255], step 10
  if (value < 15) value = 15;
  if (value > 255) value = 255;
  value = ((value + 1) / 16) * 16 - 1;
  
  return (value + 16) / 16;
}

//----------------------------------------
// Convert a contrast 'index' to a contrast value.
//
//     index  contrast index in [0, 15]
//
// Returns the contrast value in range [0, 150], step 10.
//----------------------------------------
int contrast_index2value(int index)
{
  // clamp 'index' to range [0, 15]
  if (index < 0) index = 0;
  if (index > 15) index = 15;

  // return the appropriate value
  return index * 10;
}

//----------------------------------------
// Convert a contrast value to a contrast 'index'.
//
//     value  contrast value in the range [0, 150], step 10
//
// Returns the contrast index in range [0, 15].
//----------------------------------------
int contrast_value2index(int value)
{
  // clamp 'value' to canonical value in range [0, 150], step 10
  if (value < 0) value = 0;
  if (value > 150) value = 150;
  value = (value / 10) * 10;
  
  return value / 10;
}

//----------------------------------------
// Display:
//     PBSn;  presentation brightness set to N [1, 16]
//     PBG;   get presentation brightness
//     PCSn;  presentation contrast set to N [0, 15]
//     PCG;   get presentation contrast
//----------------------------------------

const char * xcmd_presentation(char *answer, char *cmd)
{
  switch (cmd[1])
  {
    case 'B':
      // brightness
      switch (cmd[2])
      {
        case 'S':
        {
            // set brightness
            char *ch_ptr = cmd + 3;
  
            while (*ch_ptr != ';')
            {
              char ch = *ch_ptr++;
    
              if (ch < '0' || ch > '9')
              {
                return "ERROR";
              }
            }
            *ch_ptr = '\0';    // remove terminating ';'
            int b_index = (int) str2ulong(cmd+3);
            if ((b_index < 1) || (b_index > 16))
              return "ERROR";
            LcdBrightness = brightness_index2value(b_index);
            analogWrite(mc_Brightness, LcdBrightness);
            return "OK";
        }

        case 'G':
          // get brightness
          int b_index = brightness_value2index(LcdBrightness);
          Serial.printf(F("PBG: LcdBrightness=%d, b_index=%d\n"), LcdBrightness, b_index);
          if (b_index > 9)
          {
            ulong2str(answer, 2, (int) b_index);
            answer[2] = '\0';
          }
          else
          {
            ulong2str(answer, 1, (int) b_index);
            answer[1] = '\0';
          }
          return answer;
      }

    case 'C':
      // contrast
      switch (cmd[2])
      {
        case 'S':
          {
            // set contrast
            char *ch_ptr = cmd + 3;
  
            while (*ch_ptr != ';')
            {
              char ch = *ch_ptr++;
    
              if (ch < '0' || ch > '9')
              {
                return "ERROR";
              }
            }
            *ch_ptr = '\0';    // remove terminating ';'
            int c_index = (int) str2ulong(cmd+3);
            if ((c_index < 0) || (c_index > 15))
              return "ERROR";
            LcdContrast = contrast_index2value(c_index);
            analogWrite(mc_Contrast, LcdContrast);
            return "OK";
          }
          
        case 'G':
          // get contrast
          int c_index = contrast_value2index(LcdContrast);
          if (c_index > 9)
          {
            ulong2str(answer, 2, (int) c_index);
            answer[2] = '\0';
          }
          else
          {
            ulong2str(answer, 1, (int) c_index);
            answer[1] = '\0';
          }
          return answer;      
      }
  }
  
  return "ERROR";
}

//----------------------------------------
// Convert a positive float < 10.0 to a string with 2 decimal places.
//     buff         address of buffer to fill
//     value        the float value to convert


void save_to_eeprom(void)
{
  EEPROM.put(EepromFreq, VfoFrequency);
  EEPROM.put(EepromSelDigit, VfoSelectDigit);
  EEPROM.put(EepromVfoOffset, VfoClockOffset);
  EEPROM.put(EepromBrightness, LcdBrightness);
  EEPROM.put(EepromContrast, LcdContrast);
  EEPROM.put(EepromHoldClickTime, ReHoldClickTime);
  EEPROM.put(EepromDClickTime, ReDClickTime);
}

//----------------------------------------
