 #include <SPI.h>
 #include <Adafruit_ILI9341.h>
 #include <XPT2046_Touchscreen.h>
+#include "ArialBold24pt7b.h"
+#include "events.h"
 
 #define MAJOR_VERSION   "1"
 #define MINOR_VERSION   "0"

#define TFT_CS      10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);  

// touchscreen stuff
int ts_rotation = 0;
int ts_width = tft.width();
int ts_height = tft.height();


void ts_setRotation(uint8_t rot)
{
  ts_rotation = rot % 4;

  }
}

//-----------------------------------------------
//-----------------------------------------------
bool ts_read(int *x, int *y)
{
  // See if there's any touch data for us
  if (ts.bufferEmpty())
  {
    return false;
  }

  // Retrieve a point  
  TS_Point p = ts.getPoint();

  if (p.z == 0)
  {
    return false;
  }

  // Scale from ~0->4000 to tft.width using the calibration #'s
  *x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  *y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  Serial.print("New x="); Serial.print(*x);
  Serial.print(", y="); Serial.println(*y);

  return true;
}

//-----------------------------------------------
// Draw the frequency byte buffer to the screen.
// Updates all digits on the screen.  Skips leading zeros.
//-----------------------------------------------

void display_frequency(void)
{
  bool leading_space = true;

  for (int i = 0; i < NUM_F_CHAR; ++i)
  {
    tft.fillRect(freq_display[i]-1, FREQ_OFFSET_Y-CHAR_HEIGHT-1,
                 CHAR_WIDTH+2, CHAR_HEIGHT+2, FREQ_BG);
    if ((freq_display[i] != '0') || !leading_space)
    {
      tft.drawChar(char_x_offset[i], FREQ_OFFSET_Y, freq_display[i], FREQ_FG, FREQ_BG, 1);
      leading_space = false;
    }
  }
}

//-----------------------------------------------
//-----------------------------------------------
void setup(void)
{
  Serial.begin(115200);
  Serial.printf("PixelVFO %s.%s\n", MAJOR_VERSION, MINOR_VERSION);
  
  // initialize the touchscreen pins, mode and level
//  pinMode(TS_CS, OUTPUT);
//  digitalWrite(TS_CS, HIGH);
//  pinMode(TS_IRQ, INPUT_PULLUP);

  // link the TS_IRQ pin to its interrupt handler
  //attachInterrupt(digitalPinToInterrupt(TS_IRQ), touch_irq, CHANGE);
  
  // set up the VFO frequency data structures
  frequency = 1000000L;
  freq_to_buff(freq_display, 1000000L);

  // initialize 'char_x_offset' array
  int x_offset = FREQ_OFFSET_X;
  for (int i = 0; i <= NUM_F_CHAR; ++i)
  {
    char_x_offset[i] = x_offset;
    x_offset += CHAR_WIDTH;
  }

  SPI.begin();
  
  tft.begin();
  tft.setFont(&ArialBold24pt7b);
  tft.setRotation(1);

  ts.begin();
  ts_setRotation(1);
  
//  tft.fillScreen(ILI9341_BLACK);
  // draw the basic screen
  draw_screen();
  
  // draw the 'thousands' markers
  draw_thousands();
  
  // draw the 'edge of digits' markers
  for (int i = 0; i < NUM_F_CHAR; ++i)
  { 
    uint16_t x = char_x_offset[i];
    
    tft.drawFastVLine(x, 44, 6, ILI9341_RED);
  }
  
  // show the frequency
  display_frequency();
}

//-----------------------------------------------
//-----------------------------------------------
void loop()
{
  int x = 0;
  int y = 0;
  
  if (ts_read(&x, &y))
    tft.fillCircle(x, y, 5, ILI9341_RED);
}
