
#include <rgb_lcd.h>
#include <si5351.h>

char version[] = "SSB1";

const long bandStart = 18068000;    
const long bandEnd = 18168000;
const long txcio = 9001200; 

volatile long freq = 18163000;  // HFLINK SEL Channel 18E       
volatile long radix = 100;  
         
volatile long oldfreq = 0;

volatile long txvfo = freq + txcio;

volatile long currentfreq = 0;
volatile int updatedisplay = 0;

// Rotary encoder pins and other inputs
static const int rotAPin = 2;
static const int rotBPin = 3;
static const int pushSwPin = 4;
static const int txrxPin = 5;
static const int txRLA1Pin = 7;
static const int txRLA2Pin = 6;
static const int modeSWPin = 8;
static const int modeRLAPin = 9;

volatile int tx = 0;
volatile int oldtxrx = 0;

volatile int mode = 0;
volatile int oldmode = 0;


// Rotary encoder variables, used by interrupt routines
volatile int rotState = 0;
volatile int rotAval = 1;
volatile int rotBval = 1;

// Instantiate the Objects
rgb_lcd lcd;
Si5351 si5351;

void setup()
{
 
  // Initialize the display
  lcd.begin(16, 2);

  lcd.cursor();

  // Initialize the Si5351
  si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
  si5351.set_correction(-30756, SI5351_PLL_INPUT_XO);
  si5351.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
  si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA);
  //si5351.drive_strength(SI5351_CLK1, SI5351_DRIVE_8MA);
  si5351.drive_strength(SI5351_CLK2, SI5351_DRIVE_8MA);

  
  // Update display and send start frequency
//  UpdateDisplay();
  SendFrequency();
}


void loop()
{

  SendFrequency();
  delay(50);

}




void SendFrequency()
{
  
 
  //si5351.output_enable(SI5351_CLK0, 1);
  //si5351.output_enable(SI5351_CLK2, 1); // leave TX CIO on at all times - keeps balanced modulator "warm" to maintain carrier suppression?
  si5351.set_freq((txvfo * 100ULL), SI5351_CLK0);
  //si5351.set_freq((freq * 100ULL), SI5351_CLK1);
  si5351.set_freq((txcio * 100ULL), SI5351_CLK2);
 
}
