#include <ESP8266WiFi.h>
#include <Wire.h>
char prog;
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "led"; //Set your password

/////////////////////
// Pin Definitions //
/////////////////////
  int val = -1;
WiFiServer server(80);

void setup() 
{
  setupWiFi();
  server.begin();
  Wire.begin();
  Serial.begin(9600);
}

void loop() 
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  if (req.indexOf("/home") != -1)
    val = 0; //Displays instruction and address codes for patterns
  else if (req.indexOf("/test") != -1)
    val = 1; //Cycle through adafruit test pattern
  else if (req.indexOf("/rainbow") != -1)
    val = 2; //Mood rainbow
  else if (req.indexOf("/blink/r") != -1)
    val = 3; // Blinky/flashy in red
  else if (req.indexOf("/blink/g") != -1)
    val = 4;// Blinky/flashy in green
  else if (req.indexOf("/blink/b") != -1)
    val = 5;// Blinky/flashy in blue
  else if (req.indexOf("/blink/rgb") != -1)
    val = 6; // Blinky/flashy rainbow
  else if (req.indexOf("/text") != -1)
    val = 7; 
  else if (req.indexOf("/off") != -1)
    val = 8; 
  else if (req.indexOf("/boxes") != -1)
    val = 9; 
  else if (req.indexOf("/arrow") != -1)
    val = 10;
  else if (req.indexOf("/ghost") != -1)
    val = 11; 
  else if (req.indexOf("/pacman") != -1)
    val = 12;
  else if (req.indexOf("/spaceinvader") != -1)
    val = 13; 
  else if (req.indexOf("/info") != -1)
    val = 14; 

  client.flush();

 // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  // If we're setting the LED, print out a message saying we did
   if (val == 0)
  {
    s += "Welcome to LED Matrix Control! ";
    s += "<br>";
    s += "<br>";
    s += "To choose your desired pattern, type 192.168.4.1 followed by the listed codes below"; 
    s += "<br>";
    s += "For example 192.169.4.1/home will display this page"; 
    s += "<br>";
    s += "<br>";
    s += "/home - Will show this page";
    s += "<br>";
    s += "/test - Will go through all LED programs";
    s += "<br>";
    s += "/rainbow - Displays a dynamic rainbow";
    s += "<br>";
    s += "/blink/r - Displays a blinky pattern in red";
    s += "<br>";
    s += "/blink/g - Displays a blinky pattern in green";
    s += "<br>";
    s += "/blink/b - Displays a blinky pattern in blue";
    s += "<br>";
    s += "/blink/rgb - Displays a rainbow blinky pattern";
    s += "<br>";
    s += "/text - Displays a message";
    s += "<br>";
    s += "/off - Clears Matrix";
    s += "<br>";
    s += "/boxes - Displays a rectangular pattern";
    s += "<br>";
    s += "/arrow - Displays a rotating arrow";
    s += "<br>";
    s += "/ghost - Displays the Pac Man Ghost 'blinky'";
    s += "<br>";
    s += "/pacman - Displays Pac Man";
    s += "<br>";
    s += "/spaceinvaders - Displays a Space Invader Animation";
    s += "<br>";
    s += "/info Displays matrix info";
    s += "<br>";
   
  }
  if (val == 1)
  {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
   s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Test' selected";
  }
  else if (val == 2)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Rainbow' selected"; 
  }
    else if (val == 3)
  {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Red blink' selected"; 
  }
    else if (val == 4)
  {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Green Blink' selected"; 
  }
    else if (val == 5)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Blue Blink' selected";
  }
   else if (val == 6)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Rainbow Blink' selected"; 
  }
  else if (val == 7)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'text' selected"; 
  }
  else if (val == 8)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "Display Cleared"; 
  }
  else if (val == 9)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Boxes' selected"; 
  }
  else if (val == 10)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Arrow' selected"; 
  }
  else if (val == 11)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'Ghost' selected"; 
  }
  else if (val == 12)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'PacMan' selected"; 
  }
    else if (val == 13)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "'spaceinvader' selected"; 
  }
  else if (val == 14)
  { 
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(val);              // sends one byte
  Wire.endTransmission();    // stop transmitting
    s += "Welcome to LED Matrix Control!";
    s += "<br>";
    s += "<br>";
    s += "Designed, built, programmed and tested by DIYTronics"; 
     s += "<br>";
    s += "Libary's Used:"; 
     s += "<br>";
    s += "Adafruit Neo Pixel";
     s += "<br>";
    s += "Adafruit Neo Matrix";
     s += "<br>";
    s += "Adafruit GFX"; 
     s += "<br>";
    s += "Fast LED";
    s += "<br>";
    s += "Wire";
     s += "<br>";
      s += "<br>";
    s += "Hardware used: ";
    s += "<br>";
    s += "Arduino DUE";
    s += "<br>";
    s += "Link Sprite ESP8266 UNO compatible board";
    s += "<br>";
    s += "121x WS8212B LED's ";
    s += "<br>";
    s += "Hardware used: ";
    s += "<br>";
    s += "<br>";
    s += "Thanks to all contibuters/creaters of the above!";
  }
  else
  {
     s += "Welcome to LED Matrix Control! ";
    s += "<br>";
    s += "<br>";
    s += "To choose your desired pattern, type 192.168.4.1 followed by the listed codes below"; 
    s += "<br>";
    s += "For example 192.169.4.1/home will display this page"; 
    s += "<br>";
    s += "<br>";
    s += "/home - Will show this page";
    s += "<br>";
    s += "/off - Clears Matrix";
    s += "<br>";
    s += "/test - Will go through all LED programs";
    s += "<br>";
    s += "/rainbow - Displays a dynamic rainbow";
    s += "<br>";
    s += "/blink/r - Displays a blinky pattern in red";
    s += "<br>";
    s += "/blink/g - Displays a blinky pattern in green";
    s += "<br>";
    s += "/blink/b - Displays a blinky pattern in blue";
    s += "<br>";
    s += "/blink/rgb - Displays a rainbow blinky pattern";
    s += "<br>";
    s += "/text - Displays a message";
    s += "<br>";
    s += "/boxes - Displays a rectangular pattern";
    s += "<br>";
    s += "/arrow - Displays a rotating arrow";
    s += "<br>";
    s += "/ghost - Displays the Pac Man Ghost 'blinky'";
    s += "<br>";
    s += "/pacman - Displays Pac Man";
    s += "<br>";
    s += "/spaceinvaders - Displays a Space Invader Animation";
    s += "<br>";
    s += "/info - Displays matrix info";
    s += "<br>";
  }
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);

  // Do a little work to get a unique-ish name. Append the
  // last two bytes of the MAC (HEX'd) to "Thing-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "ESP8266 Thing " + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i=0; i<AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
}





