#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); 

#define IRpin 4

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

#define number_of_codes 3
uint16_t code[number_of_codes][128]={ };
uint8_t code_cnt=0;
uint8_t signal_cnt=0;

void setup() {
  Serial.begin(57600);
  Serial.println("IR code view start"); 
 
  pinMode(IRpin,INPUT);
  Serial.begin(57600); 

  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);
}

void loop(void) {
  while( digitalRead(IRpin) == 1 ) ;
  
   // IR signal prisutan
   micros1 = micros();
      do
      {
         // petlja se vrti dok imamo IR signal
         while( digitalRead(IRpin) == 0 ) ; 

         micros2 = micros(); // signal je završio, spremi vrijeme

         // pošalji koliko je trajao PULS
         //Serial.println(micros2-micros1);
         code[code_cnt][signal_cnt++] = (uint16_t)(micros2-micros1);
         
         // nema signala
         micros3 = micros2;
         
         // čekamo novi signal ili timeout
         while( digitalRead(IRpin) == 1 && (micros3-micros2)<10000) micros3 = micros(); 
         // novi IR signal
         
         //Serial.println(micros3-micros2);
         code[code_cnt][signal_cnt++] = (uint16_t)(micros3-micros2);
         micros1=micros3;
        
      } while(micros3-micros2<10000); // 10000us se smatra da više nema signala
      
      
      int sHigh=10;
      int sLow=20;
      int x1=0;
      int x2=0;
      
      #define sigdiv 110
      
      
      if(code_cnt==0) tft.fillScreen(ILI9341_BLACK); 
      int y_offset = code_cnt * 60;
      
      for(int i=0;code[code_cnt][i]<10000;i++) 
      {
           
           Serial.print(code[code_cnt][i]);
           Serial.print(", ");
           if(i%2==0)
           {
             x2=x1+code[code_cnt][i]/sigdiv;
             if(x2>=320) { y_offset+=20; x2=code[code_cnt][i]/sigdiv; x1=0 ;}
             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==1)
           {
            
             x2=x1+code[code_cnt][i]/sigdiv;
             if(x2>=320) { y_offset+=20; x2=code[code_cnt][i]/sigdiv; x1=0 ;}
             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;
           }
           
      }
      code_cnt++;
      if(code_cnt==number_of_codes) code_cnt=0;
      signal_cnt=0;
      Serial.println("\n----");


}