//################# LIBRARIES ##########################
#include 
#include 
#include      // https://github.com/tzapu/WiFiManager
#include 
#include 
#include 
#include 
#include 
//################# DISPLAY CONNECTIONS ################
// LED Matrix Pin -> ESP8266 Pin
// Vcc            -> 3v  (3V on NodeMCU 3V3 on WEMOS)
// Gnd            -> Gnd (G on NodeMCU)
// DIN            -> D7  (Same Pin for WEMOS)
// CS             -> D4  (Same Pin for WEMOS)
// CLK            -> D5  (Same Pin for WEMOS)
//################ PROGRAM SETTINGS ####################
String version = "0.1b";
int PORT = 80;
int wait   = 50;
int spacer = 1;
int width  = 5 + spacer;
String SITE_WIDTH =  "1000";
int timezone = 1;
int dstOffset = 1;
const char* ntpServer = "fr.pool.ntp.org";
int brightness = 2;
int pinCS = D4;
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays   = 1;
//################## INITIALIZING ######################
char time_value[20];
String message, webpage;
ESP8266WebServer server(PORT); 
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
void setup() {
  Serial.begin(115200); // initialize serial communications
  WiFiManager wifiManager;
  wifiManager.setTimeout(180);
  if(!wifiManager.autoConnect("LibreMetric")) {
    Serial.println(F("failed to connect and timeout occurred"));
    delay(6000);
    ESP.reset(); //reset and try again
    delay(180000);
  }
  // At this stage the WiFi manager will have successfully connected to a network, or if not will try again in 180-seconds
  Serial.println(F("WiFi connected..."));
  Serial.println(WiFi.localIP());
  configTime(timezone * 3600, dstOffset * 3600, ntpServer); // First sync of time with (S)NTP
  
//----------------------------------------------------------------------
  server.begin();
  Serial.println(F("Webserver started..."));
  matrix.setIntensity(brightness);
  brightness=16;
  Serial.print(numberOfHorizontalDisplays);
  Serial.println(" displays configurated...");
  
  for (int i=0; i= 0 && letter >= 0 ) {
      if ( letter < message.length() ) {
        matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background OFF, reverse these to invert the display!
      }
      letter--;
      x -= width;
    }
    matrix.write(); // Send bitmap to display
    delay(wait);
  }
}
void GetMessage() {
  webpage = "";
  append_page_header();
  String IPaddress = WiFi.localIP().toString();
  webpage += "
";
    webpage += "
";
  append_page_footer();
  if (server.args() > 0 ) { // Arguments were received
    for ( uint8_t i = 0; i  <= server.args(); i++ ) {
      String Argument_Name   = server.argName(i);
      String client_response = server.arg(i);
      if (Argument_Name == "message") message = client_response;
      if (Argument_Name == "brightness") brightness = client_response.toInt();
    }
  }
  server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults
}
void append_page_header() {
  webpage  = F("");
  webpage += "";
  webpage += F("Envoi de messages - LibreMetricLibreMetric ");
  webpage += version+"
";
}
void append_page_footer(){ // Saves repeating many lines of code for HTML page footers
  webpage += F("");
  webpage += F("");
}