//////////////////////////////////////////////////////////////////////////////
///// Name : SPick - El Almacén Inteligente
///// Proposition : Touch screen ILI9341
//////////////////////////////////////////////////////////////////////////////
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>      // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <Fonts/FreeSansBold24pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeMono9pt7b.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
//------------------------------
// Set the legs of the TFT screen.
#define TFT_DC 15
#define TFT_CS 5
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BOXSIZE 40
//-------------------------------------------------
// Time
unsigned long previousMillis = 0;
unsigned long previousMillis_Thingspeak = 0;
const long interval = 1000;
const long interval_Thingspeak = 20000;
//-------------------------------------------------
int led = 17;
int Count = 0;
//-------------------------------------------------
int cantSol;
char cantChar[7];
String cantidadSol; 
int tamBoton = 60;
int tamBotonCantSol = 180;
int largoCantSol;
int xCantidadSol;
int cantSurt;
int pedido;
int partida;
int partidas;
int tipoUM;
int idCod;
String Cod;
String Desc;
String Ubic;
String UM;
String tituloPantalla = "SPick - EL ALMACEN INTELIGENTE";
//--------------------------------
// LGB bar
const int ledCount = 10;    // the number of LEDs in the bar graph
int ledPins[] = {
  1, 2, 42, 41, 40, 35, 0, 45, 48, 47
};   // an array of pin numbers to which LEDs are attached
//--------------------------------
void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo native USB port only
  }
  
  pinMode(led, OUTPUT);
  
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
  //Wire.setPins(10, 8);
  tft.begin();
  if (! ctp.begin(40)) {  // pass in 'sensitivity' coefficient
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1);
  }
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);
//  tft.drawRect(0,0,319,239, ILI9341_WHITE);
  tft.fillRoundRect(0,   0,                 319,20,2,ILI9341_BLUE);
  tft.setFont(&FreeSans9pt7b);
  tft.setCursor(3,12);   // as per Adafruit convention custom fonts draw up from line so move cursor
  tft.setTextSize(1);    
  tft.setTextColor(ILI9341_WHITE);
  tft.println(tituloPantalla);
// --- Botón Surtido Cero
  tft.setFont(&FreeMonoBold12pt7b);  // choose font
  tft.fillRoundRect(250, 22,tamBoton , tamBoton,5,ILI9341_RED);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(272,58);   // as per Adafruit convention custom fonts draw up from line so move cursor
  tft.println("0");
// --- Botón Surtido Parcial
  tft.setFont(&FreeMonoBold12pt7b);  // choose font
  tft.fillRoundRect(250, 24 + tamBoton ,    tamBoton, tamBoton,5,ILI9341_YELLOW);
  tft.setTextColor(ILI9341_BLACK);
  tft.setCursor(260,120);   // as per Adafruit convention custom fonts draw up from line so move cursor
  tft.println("< >");
// --- Botón Menú
  tft.setFont(&FreeSans9pt7b);  // choose font
  tft.fillRoundRect(250, 26 + 2* tamBoton , tamBoton, tamBoton ,5, ILI9341_WHITE);
  tft.setTextColor(ILI9341_BLACK);
  tft.setCursor(254,180);   // as per Adafruit convention custom fonts draw up from line so move cursor
  tft.println("MENU");
// --- Inicializa pedido y partida
  pedido = 176271;
  partida = 1;
  partidas = 0;
}
void loop() {  
if (partida < partidas){
  partida++;
}
else{
  partida = 1;
  partidas = random(15);
  pedido++;
}
// ------------- Calcula y posicion UM en pantalla ------------
  idCod = random(1,16);
  tft.setFont(&FreeSansBold9pt7b);  // choose font
  switch (idCod) {
    case 1:
      Cod = "Cod_1";
      Desc = "Descripcion Cod_1";
      Ubic = "A2-3-5";
      UM = "pz";
      break;
    case 2:
      Cod = "Cod_2";
      Desc = "Descripcion Cod_2";
      Ubic = "A1-4-1";
      UM = "par";
      break;
    case 3:
      Cod = "Cod_3";
      Desc = "Descripcion Cod_3";
      Ubic = "B4-1-1";
      UM = "metro lineal";
      break;
    case 4:
      Cod = "Cod_4";
      Desc = "Descripcion Cod_4";
      Ubic = "A4-4-4";
      UM = "pz";
      break;
    case 5:
      Cod = "Cod_5";
      Desc = "Descripcion Cod_5";
      Ubic = "A5-5-5";
      UM = "oz";
      break;
    case 6:
      Cod = "Cod_6";
      Desc = "Descripcion Cod_6";
      Ubic = "A6-6-6";
      UM = "lb";
      break;
    case 7:
      Cod = "Cod_7";
      Desc = "Descripcion Cod_7";
      Ubic = "A7-7-7";
      UM = "kg";
      break;
    case 8:
      Cod = "Cod_8";
      Desc = "Descripcion Cod_8";
      Ubic = "A8-8-8";
      UM = "jgo";
      break;
    case 9:
      Cod = "Cod_9";
      Desc = "Descripcion Cod_9";
      Ubic = "A9-9-9";
      UM = "par";
      break;
    case 10:
      Cod = "Cod_10";
      Desc = "Descripcion Cod_10";
      Ubic = "A10-10-10";
      UM = "pz";
      break;
    case 11:
      Cod = "Cod_11";
      Desc = "Descripcion Cod_11";
      Ubic = "A11-11-11";
      UM = "par";
      break;
    case 12:
      Cod = "Cod_12";
      Desc = "Descripcion Cod_12";
      Ubic = "A12-12-12";
      UM = "m3";
      break;
    case 13:
      Cod = "Cod_13";
      Desc = "Descripcion Cod_13";
      Ubic = "A13-13-13";
      UM = "yd";
      break;
    case 14:
      Cod = "Cod_14";
      Desc = "Descripcion Cod_14";
      Ubic = "A14-14-14";
      UM = "pz";
      break;
    case 15:
      Cod = "Cod_15";
      Desc = "Descripcion Cod_15";
      Ubic = "A15-15-15";
      UM = "pie";
      break;
    case 16:
      Cod = "Cod_16";
      Desc = "Descripcion Cod_16";
      Ubic = "A16-16-16";
      UM = "pz";
      break;
    default:
      Cod = "Cod_Desc";
      Desc = "Descripcion Cod_Desc";
      Ubic = "A2-3-5";
      UM = "jgo";
  }
  tft.fillRoundRect(192,210,248,238,5,ILI9341_BLACK);
  tft.setCursor(196,230);
  tft.setTextColor(ILI9341_GREEN);
  tft.println(UM);
 
// ---------- Muestra el Código, Descripción y Ubicación ------------
  tft.fillRoundRect(0,22,248,140,5,ILI9341_BLACK);
// Muestra el Código
  tft.setFont(&FreeSansBold18pt7b);
  tft.setTextColor(ILI9341_CYAN);
  tft.setCursor(4,80);  
  tft.println(Cod);  
// Muestra la Descripción
  tft.setFont(&FreeMono9pt7b);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(4,100);   
  tft.println(Desc);  
// Muestra la Ubicación
  tft.setFont(&FreeSans9pt7b);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(4,124);   
  tft.println("Ubic: "+Ubic);  
  // Muestra el pedido y la partida
  tft.setFont(&FreeSans9pt7b);
  tft.setTextColor(ILI9341_YELLOW);
  tft.setCursor(4,40);   
  tft.println("Ped: "+String(pedido) + "  part: " + String(partida) + " de " +  partidas); 
// ---------- Calcula y posición CantSol en pantalla ------------
  cantSol = random(200);
  cantidadSol = String(cantSol);
  largoCantSol = cantidadSol.length() * 24;
  xCantidadSol = (tamBotonCantSol - largoCantSol) / 2;
  // Conversion de string a char
  cantidadSol.toCharArray(cantChar , cantidadSol.length() + 1);
  tft.setFont(&FreeSansBold24pt7b);  // choose font
  tft.fillRoundRect(2,186,tamBotonCantSol ,52, 5,ILI9341_GREEN);
  
  tft.setTextSize(1);    
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextWrap(false);
  tft.setCursor(xCantidadSol,228);
  tft.println(cantidadSol);
  
  Touch();
    // for (int i = 0; i <= 9; i++){
    //   digitalWrite(ledPins[i], LOW);
    // }
    delay(1000);
}
void Touch(){
  delay(10);
  // Wait for a touch
  if (! ctp.touched()) {
    return;
  }
  TS_Point p = ctp.getPoint();
  p.x = map(p.x, 0, 240, 240, 0);
  p.y = map(p.y, 0, 320, 320, 0);
  Serial.print("("); Serial.print(p.x);
  Serial.print(", "); Serial.print(p.y);
  Serial.println(")");
  if (p.y < BOXSIZE){
    if (p.x < 40){
      digitalWrite(led, HIGH);
    }
    else if (p.x < 40*2){
      digitalWrite(led, LOW);
    }
    else if (p.x < 40*3 || p.x < 40*4){
      Count++;
      if(Count > 1)
        Count = 0;
    }
  }
}