#include <UIPEthernet.h>
#include <utility/logging.h>
#include <DMD2.h>
#include <fonts/SystemFont5x7.h>
#include <fonts/Arial14.h>
#include <fonts/Arial_black_16.h>
#include <fonts/Droid_Sans_12.h>

#include <SPI.h> //Aqui incluimos la libreria SPI
const int WIDTH = 10;

byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };

byte ip[] = { 192,168,100,15 };
// Server port
EthernetServer server(80);


String HTTP_req;          // stores the HTTP request
byte LED_Val = 128;   // state of LED, off by default

// Declarations
// This sketch uses pins 2 to 9 for the outputs.
int x1 = 0; // flechas posicion 
int y1 = 0;
SoftDMD dmd(WIDTH,1);  // DMD controls the entire display
DMD_TextBox box(dmd);  // "box" provides a text box to automatically write to/scroll the display


void setup() {
// Declare Pin Status
   Serial.begin(9600);
  dmd.setBrightness(20);
  dmd.begin();
// Initialize the server
   Ethernet.begin(mac, ip);
   server.begin();
}

// the loop routine runs over and over again forever:
void loop() {

  read_data(); // Reads incoming client requests.
 
}

void read_data ()
{
  // Check for a connection.
  EthernetClient client = server.available();
  if (client) {
    boolean newLine = true;
    String line = "";
    while (client.connected() && client.available()) {
    char c = client.read();
        if (c == '\n' && newLine) {
          //  Send http header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
                    client.print  (LED_Val); //insert current value
                    Serial.print(HTTP_req);
        }
        
        if (c == '\n') {
          //New line
          newLine = true;
           determine_input_tag(line);
          line = "";
        }
        else if (c != '\r') {
          // 
          newLine = false;
          line += c;
        }
    }

    determine_input_tag(line); 
    delay(1);
    // Terminate the connection.
    client.stop();   
  }
}

// The "determine_input_tag" routine modifies the required
// variable depending on the client request.

void determine_input_tag(String line)
  {
  if (line.startsWith("tag", 0)) {
    String android = line.substring(4, line.length());
    
    if (android == "A") 
     mess1();
    if (android == "B")
    mess2();
      if (android == "C")
    mess3();
    if (android == "D")
    mess4();
       if (android == "E")
    mess5();
       if (android == "F")
    mess6();
       if (android == "off")
    messoff();
  }
}
// MENSAJE 1 
void mess1 (){
 dmd.clearScreen();
 dmd.selectFont(Droid_Sans_12);
 dmd.drawString(0, 3, F("Salida 1 123456789"));*// TEST 1x10
 dmd.drawString(0, 163, F("Salida 2 123456789"));// TEST 1x10
/*    x1 = 140;
    //Fecha arriba
    dmd.drawFilledBox(x1,14,x1+4,9);
    dmd.drawLine(x1-4,8,x1+8,8);
    dmd.drawLine(x1-3,7,x1+7,7);
    dmd.drawLine(x1-2,6,x1+6,6);
    dmd.drawLine(x1-1,5,x1+5,5);
    dmd.drawLine(x1,4,x1+4,4);
    dmd.drawLine(x1+1,3,x1+3,3);
    dmd.drawLine(x1+2,2,x1+2,2);
x1 = 140;
y1 = 28;
        //Fecha arriba
    dmd.drawFilledBox(x1,14,x1+4,y1-5);
    dmd.drawLine(x1-4,y1-5,x1+8,y1-6);
    dmd.drawLine(x1-3,y1-6,x1+7,y1-7);
    dmd.drawLine(x1-2,y1-7,x1+6,y1-8);
    dmd.drawLine(x1-1,y1-9,x1+5,y1-9);
    dmd.drawLine(x1,y1-10,x1+4,y1-10);
    dmd.drawLine(x1+1,y1-11,x1+3,y1-11);
    dmd.drawLine(x1+2,y1-12,x1+2,y1-12);
  */  
}
// MENSAJE !2
void mess2 (){
  dmd.clearScreen();
dmd.selectFont(Arial14);
  dmd.drawString(0, 3, F("Salida 2 123456789"));
     // derecha
 x1 = 140;
    dmd.drawFilledBox(x1,6,x1+9,10);
    dmd.drawLine(x1+6,2,x1+6,14);
    dmd.drawLine(x1+7,3,x1+7,13);
    dmd.drawLine(x1+8,4,x1+8,12);
    dmd.drawLine(x1+9,5,x1+9,11);
    dmd.drawLine(x1+10,6,x1+10,10);
    dmd.drawLine(x1+11,7,x1+11,9);
    dmd.drawLine(x1+12,8,x1+12,8);    
}

// MENSAJE 3
void mess3 (){
  dmd.clearScreen();
dmd.selectFont(Arial14);
  dmd.drawString(0, 3, F("Salida 3 123456789"));

  //izq
x1=140;
    dmd.drawFilledBox(x1,6,x1+5,10);
    dmd.drawLine(x1-1,2,x1-1,14);
    dmd.drawLine(x1-2,3,x1-2,13);
    dmd.drawLine(x1-3,4,x1-3,12);
    dmd.drawLine(x1-4,5,x1-4,11);
    dmd.drawLine(x1-5,6,x1-5,10);
    dmd.drawLine(x1-6,7,x1-6,9);
    dmd.drawLine(x1-7,8,x1-7,8);
}


void mess4 (){
   dmd.clearScreen();
   dmd.selectFont(Arial_Black_16);
   dmd.drawString(25, 0, F("Emergencia 1 "));
//Atencion
   x1=8;
    dmd.drawCircle(x1 ,8,7);
    dmd.drawFilledBox(x1-1,3,x1+1,8);
    dmd.drawFilledBox(x1-1,11,x1+1,12);

    x1=151;
    dmd.drawCircle(x1 ,8,7);
    dmd.drawFilledBox(x1-1,3,x1+1,8);
    dmd.drawFilledBox(x1-1,11,x1+1,12);
}
void mess5 (){
     dmd.clearScreen();
   dmd.selectFont(Arial_Black_16);
   dmd.drawString(25, 0, F("EMERGENCIA 2 "));
//Atencion
   x1=8;
    dmd.drawCircle(x1 ,8,7);
    dmd.drawFilledBox(x1-1,3,x1+1,8);
    dmd.drawFilledBox(x1-1,11,x1+1,12);

    x1=151;
    dmd.drawCircle(x1 ,8,7);
    dmd.drawFilledBox(x1-1,3,x1+1,8);
    dmd.drawFilledBox(x1-1,11,x1+1,12);
}
void mess6 (){
   dmd.clearScreen();
   // derecha
    x1 = 10;
    dmd.drawFilledBox(x1,6,x1+9,10);
    dmd.drawLine(x1+6,2,x1+6,14);
    dmd.drawLine(x1+7,3,x1+7,13);
    dmd.drawLine(x1+8,4,x1+8,12);
    dmd.drawLine(x1+9,5,x1+9,11);
    dmd.drawLine(x1+10,6,x1+10,10);
    dmd.drawLine(x1+11,7,x1+11,9);
    dmd.drawLine(x1+12,8,x1+12,8);

    x1 = 50;
    //Fecha arriba
    dmd.drawFilledBox(x1,14,x1+4,9);
    dmd.drawLine(x1-4,8,x1+8,8);
    dmd.drawLine(x1-3,7,x1+7,7);
    dmd.drawLine(x1-2,6,x1+6,6);
    dmd.drawLine(x1-1,5,x1+5,5);
    dmd.drawLine(x1,4,x1+4,4);
    dmd.drawLine(x1+1,3,x1+3,3);
    dmd.drawLine(x1+2,2,x1+2,2);
    //Flecha Abajo 
x1 = 90;
    dmd.drawFilledBox(x1,2,x1+4,7);
    dmd.drawLine(x1-4,8,x1+9,8);
    dmd.drawLine(x1-3,9,x1+8,9);
    dmd.drawLine(x1-2,10,x1+7,10);
    dmd.drawLine(x1-1,11,x1+6,11);
    dmd.drawLine(x1,12,x1+5,12);
    dmd.drawLine(x1+1,13,x1+4,13);
    dmd.drawLine(x1+2,14,x1+3,14);

      //izq
    x1=110;
    dmd.drawFilledBox(x1,6,x1+5,10);
    dmd.drawLine(x1-1,2,x1-1,14);
    dmd.drawLine(x1-2,3,x1-2,13);
    dmd.drawLine(x1-3,4,x1-3,12);
    dmd.drawLine(x1-4,5,x1-4,11);
    dmd.drawLine(x1-5,6,x1-5,10);
    dmd.drawLine(x1-6,7,x1-6,9);
    dmd.drawLine(x1-7,8,x1-7,8);

   x1=130;
    dmd.drawCircle(x1 ,8,7);
    dmd.drawFilledBox(x1-1,3,x1+1,8);
    dmd.drawFilledBox(x1-1,11,x1+1,12);
}
void messoff (){
    dmd.clearScreen();

}