#include <UTFT.h>
#include <stdlib.h>

// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

#define baudrate 57600

#define LED_PIN 13
#define SERIAL_DATA_SIZE 8


#define FETCH_DATA_COMMAND 0x80
#define PRINT_DATA_COMMAND 0x81
#define PRINT_DATA_LCD 0x82

UTFT myGLCD(SSD1289, 38, 39, 40, 41);


void setup()
{
  // Setup the LCD
  myGLCD.InitLCD();
  myGLCD.setFont(BigFont);
  myGLCD.clrScr();

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  // Open serial communications and wait for port to open:
  Serial.begin(baudrate);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  Serial1.begin(baudrate);
  Serial1.println("dofile(\"googleTime.lua\");");
}


void loop()
{
  static byte data_arr[4];
  static int fetch_data = 0;
  static int print_data = 0;
  static int serial_data = 1;
  int outgoingByte = 0;   // for incoming serial data
  int incomingByte = 0;   // for incoming serial data
  int i;
  static int x = 1;
  static int y = 1;

  if (Serial.available())
  {
    if (fetch_data == 0)
    {
      outgoingByte = Serial.read();
      Serial1.write(outgoingByte);
    }
  }

  if (Serial1.available())
  {
    incomingByte = Serial1.read();
    switch (incomingByte)
    {
      case FETCH_DATA_COMMAND:
        if (fetch_data == 0)
        {
          fetch_data = 1;
          serial_data = 0;
        }
        break;
      case PRINT_DATA_COMMAND:
        if (serial_data == 1)
        {
          for (i = 0; i < sizeof(data_arr) / sizeof(byte); i++)
          {
            Serial.print(" dt[");
            Serial.print(i);
            Serial.print("] = ");
            Serial.print(data_arr[i], HEX);
          }
          Serial.println("");
        }
        break;
      case PRINT_DATA_LCD:
        if (print_data == 1)
        {
          print_data = 0;
          serial_data = 1;
        }
        else if (print_data == 0)
        {
          print_data = 1;
          serial_data = 0;
        }
        break;
      default:
        if (serial_data == 1)
        {
          Serial.print((char)incomingByte);
        }
        if (fetch_data != 0)
        {
          *((byte *)data_arr + (byte)fetch_data - 1) = (byte)incomingByte;
          fetch_data++;
        }
        if (fetch_data > sizeof(data_arr) / sizeof(byte))
        {
          int get_x, get_y;
          get_x = (data_arr[0] << 8) + data_arr[1];
          get_y = (data_arr[2] << 8) + data_arr[3];
          x = get_x;
          y = get_y;
          fetch_data = 0;
          serial_data = 1;
        }
        if (print_data == 1)
        {
          myGLCD.print(String((char)incomingByte), x, y);
          x += 16;
          if (x >= 305)
          {
            x = 1;
            y += 16;
          }
          if (y >= 225)
          {
            y = 1;
          }
        }
        break;
    }
  }
}
