#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);


unsigned long micros1;
unsigned long micros2;
unsigned long micros3;

uint16_t code[22] = {0};
volatile uint8_t code_cnt = 0;

volatile uint32_t lastMicros = 0;


void isrFunction() {
  if (micros() - lastMicros > 20000) code_cnt = 0;
  if (code_cnt == 22) return;
  if (code_cnt == 0) lastMicros = micros();
  else {
    code[code_cnt - 1]  = micros() - lastMicros;
    lastMicros = micros();
  }
  code_cnt++;
  //if(code_cnt==20) code_cnt=0;
}


uint8_t printed = false;
void setup() {
  Serial.begin(9600);
  Serial.println("Serial code view start");


  Serial.begin(9600);

  tft.begin();

  // read diagnostics (optional but can help debug problems)
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);

  delay(10);
  tft.setRotation (1);
  tft.fillScreen(ILI9341_BLACK);
  tft.drawLine(0, 0, 320, 80, ILI9341_YELLOW);
  delay(300);
  tft.fillScreen(ILI9341_BLACK);
  attachInterrupt(digitalPinToInterrupt(2), isrFunction, CHANGE ) ;
  Serial.println("Type one character at serial console and pres enter");
}

#define sigdiv 7
int x1 = 0;
int x2 = 0;
int sHigh = 10;
int sLow = 40;

void loop(void) {

  int y_offset = 60;

  cli();
  uint32_t lm = lastMicros;
  sei();

  if (code_cnt > 0 && (micros() - lm > 20000))
  {
    tft.fillScreen(ILI9341_BLACK);
    x1 = 0;
    x2 = 0;
    for (int i = 0; i < code_cnt; i++) {
      if (i % 2 == 1)
      {
        x2 = x1 + code[i] / sigdiv;
        tft.drawLine(x1, y_offset + sLow, x1, y_offset + sHigh, ILI9341_YELLOW);
        tft.drawLine(x1, y_offset + sHigh, x2, y_offset + sHigh, ILI9341_YELLOW);
        x1 = x2;
      }
      if (i % 2 == 0)
      {

        x2 = x1 + code[i] / sigdiv;
        tft.drawLine(x1, y_offset + sHigh, x1, y_offset + sLow, ILI9341_YELLOW);
        tft.drawLine(x1, y_offset + sLow, x2, y_offset + sLow, ILI9341_YELLOW);
        x1 = x2;
      }
    }

    for (int i = 0; i < 20; i++)
    {

      tft.drawLine(i * 102 / 7, y_offset + sHigh + 40, i * 102 / 7, y_offset + sHigh + 50, ILI9341_YELLOW);
      if(i%10==0) tft.drawLine(i * 102 / 7+5, y_offset + sHigh + 42, i * 102 / 7+5, y_offset + sHigh + 45, ILI9341_RED);
    }

    code_cnt = 0;
    for (int i = 0; i < 20; i++) {
      Serial.print(code[i]);
      Serial.print(", ");
    }
    Serial.println();
  }



  //if (code_cnt = 0) tft.fillScreen(ILI9341_BLACK);

}