/*  Govee LED lights that were purcahsed from Amazon.com 100ft for 15USD.
 *  24Vdc 6 LED in series with current limiting resistor every 19.5".
 *  
 *            +----B----B----B----B----B----B----2K4-------------+
 *            |                                                  |
 *  24Vdc ----+----R----R----R----R----R----R----2K4-----------------+
 *            |                                                  |   |
 *            +----G----G----G----G----G----G----3K3---------------------+
 *            |                                                  |   |   |
 *            |                                                  |   |   |
 *            |                                                  |   |   |
 *            +---------+----B----B----B----B----B----B----2K4---+   |   |
 *                      |                                        |   |   |
 *                      +----R----R----R----R----R----R----2K4-------+   |
 *                      |                                        |   |   |
 *                      +----G----G----G----G----G----G----3K3-----------+
 *                      |                                        |   |   |
 *                      |                control bus:         blue  red  green
 *                      |                                        |   |   |
 *                      ...                                    ...  ...  ...
 *
 *  We installed about 150ft so about 90 led circuits will need power.  ESP8266 can
 *  do 1KHz PWM so we can use 3 pins if we want.  Another idea I had was to use lots
 *  of pins to control 20mA LED driver ICs.  With 15 pins one could make a circuit to
 *  drive 32 levels of current to each color giving 32,768 colors.  Each color would
 *  have 5 npn controlling 1,2,4,8,16 LED drivers.  One could then step through 0..620mA.
 *  
 *  Instead we will just use PWM to give us color control.  We just need to calculate how
 *  much current we want to send to our LED strip.  Because each circuit runs in parallel
 *  we need to determine how much current each branch will use.  The current limiting
 *  resistors means we can probably just use a fixed voltage supply.  With a 48Vdc supply
 *  at max brightness green will see 9mA, blue will see 12mA, and red will see 15mA.  These
 *  values are all pretty safe for the LED but the red would blow out a PN2222A with 1080mA.
 *
 *  We should be careful about the inductance of the wire.  Assuming 1/4mm diameter wire we
 *  might have 47uH.  We can use a diode on the collector of each color to send the spikes to
 *  the +48Vdc rail.  Each color will use an NPN-BJT, probably 3 parallel PN2222A with
 *  ballast resitors, as buffers for the PWM signal from the esp8266.
 *
 *  In the end I ended up using optoisolated pwm driving three power mosfets.  This ended
 *  up being a nuisance since the mosfet gates took forever to discharge resulting in
 *  the LEDs staying lit when the esp8266 pulled the signal down.  I did not really fix this
 *  but I did add a few resistors on the gate to bleed off the gate charge.  In effect the
 *  max brightness is 0xfc instead of 0xff since the led does not turn off on 0xfd.  But that
 *  is not really a deal-breaker, just annoying.
 *
 *  test using:
 *  curl -X POST http://rain-gutter-rgb.lan.rome7.com/rgb -d "color=010101"
 */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h> 
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>

ESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'

uint8_t hexStr[7] = "010101";

/*  131 allows for frame count, 16 colors(3), 16 transistions(1), and a null terminator
 *  the user provides the input as utf-8 encoded hexidecimal, we need twice
 *  the storage space, but parsing is a little easier: 2 + (2 * (3 * 16 + 16)) + 1 = 131
 *  [?] there are 16 transistions because there is an extra transition when we
 *      reach the end an loop to the begining
 *  [?] frame count allows user to have an animation that is not the maximum length,
 *      as of this note, the max frame count is 0x0f, '0f' when using a
 */
#define HOW_MANY_ANIMATION_FRAMES 16
#define MAX_ANIMATE_FRAME_COUNT_LEN 2
#define NULL_TERMINATOR_LEN 1
#define FRAME_LEN (3 + 1)
#define ANIMATE_BUFFER_LEN (MAX_ANIMATE_FRAME_COUNT_LEN + (2 * HOW_MANY_ANIMATION_FRAMES * FRAME_LEN) + NULL_TERMINATOR_LEN)
#define MAX_ANIMATE_FRAME_COUNT ((ANIMATE_BUFFER_LEN - 2 - 1) / 8)
uint8_t animateStr[ANIMATE_BUFFER_LEN];
uint8_t animate = 0;

uint8_t red = 1;
uint8_t green = 1;
uint8_t blue = 1;

ESP8266WebServer server(80);    // Create a webserver object that listens for HTTP request on port 80

void handleRoot();              // function prototypes for HTTP handlers
void handleLogin();
void handleNotFound();

void setup(void){
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');

  wifiMulti.addAP("n-phone-number-upstairs", "3103229909cedar");   // add Wi-Fi networks you want to connect to
  wifiMulti.addAP("n-phone-number", "3103229909cedar");

  Serial.println("Connecting ...");
  int i = 0;
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print('.');
  }
  Serial.println('\n');
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());               // Tell us what network we're connected to
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());            // Send the IP address of the ESP8266 to the computer
  /*
  if (MDNS.begin("rain-gutter-rgb")) {              // Start the mDNS responder for esp8266.local
    Serial.println("mDNS responder started");
  } else {
    Serial.println("Error setting up MDNS responder!");
  }
  */
  server.on("/", HTTP_GET, handleRoot);        // Call the 'handleRoot' function when a client requests URI "/"
  //server.on("/login", HTTP_POST, handleLogin); // Call the 'handleLogin' function when a POST request is made to URI "/login"
  server.on("/rgb", HTTP_POST, handleRGB); // Call the 'handleRGB' function when a POST request is made to URI "/login"
  server.on("/dec", HTTP_GET, handleGetDec);
  server.on("/echo", HTTP_GET, handleEchoHex);
  server.on("/animate", HTTP_GET, handleAnimate);

  server.onNotFound(handleNotFound);           // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"

  server.begin();                            // Actually start the server
  Serial.println("HTTP server started");

  /* esp8266 12-f breakout board has pins mapped a bit odd, and arduino esp8266 libs use GPIO pin number */
  #define PIN_5 14 /* GPIO_14 */
  #define PIN_6 12 /* GPIO_12 */
  #define PIN_7 13 /* GPIO_13 */

  //analogWriteFreq(1000);
  analogWriteFreq(250);

  // setup pins with a test pwm
  analogWrite(PIN_5, red);
  analogWrite(PIN_6, green);
  analogWrite(PIN_7, blue);
}

void loop(void){
  // keep track of where we are in the animation
  uint8_t frame = 0;
  uint8_t frameCount = 0;
  #define KEY_FRAMES 10
  #define MS_IN_ANIMATION_DELAY 250
  #define TEMPORAL_LENGTH (MS_IN_ANIMATION_DELAY / KEY_FRAMES)
  uint8_t redSubFrames[KEY_FRAMES];
  uint8_t greenSubFrames[KEY_FRAMES];
  uint8_t blueSubFrames[KEY_FRAMES];
  // keep track of where we are in the subframe
  uint8_t subframe = KEY_FRAMES;
  /*  because subframes occur every quarter second and animation delays can be long (up to 64 seconds)
   *  we need to guess what the next color should be for the next second based on where we are now
   */
  uint8_t animationDelayCounter = 0;
  uint16_t totalSubframes = 0;

  uint8_t frameAnimationDelay = 0;
  uint8_t frameStartRed = 0;
  uint8_t frameStartGreen = 0;
  uint8_t frameStartBlue = 0;
  uint8_t frameEndRed = 0;
  uint8_t frameEndGreen = 0;
  uint8_t frameEndBlue = 0;

  // tracking temporal length so that we do not run too fast or slow based on CPU frequency
  uint8_t delayCount = 0;

  while(true) {
    // listen for HTTP requests from clients
    server.handleClient();
    // check if we are supposed to animate
    if(0 == animate) break;
    /*  we will transition several times a second (technically serveral times per animation delay)
     *
     *  [?] this delay is not really necesarry, we just need a way to not run too fast
     *
     *  [!] this delay also reduces power consumption of the esp8266 significantly
     *      not something we want necessarily, but worth mentioning in case one questions
     *      what the heck is happening
     */
    delay(1);

    if(delayCount++ < TEMPORAL_LENGTH) break;
    // do we have any subframes to animate?
    if(0 == subframe) {
      // reset the subframe counter
      subframe = KEY_FRAMES;
      // are we done with this frame?
      if(0 == animationDelayCounter) {
        // move to the next frame
        if(0 == frame) frame = (ANIMATE_BUFFER_LEN - 1) / 8;
        frame--;
        // get the next animation delay
        frameAnimationDelay = animationDelayCounter = 
        // is this a stop frame
        if(0xff == frameAnimationDelay) { animate = 0; break; }
        // is this a loop frame
        if(0x00 == frameAnimationDelay) { frame = 0; continue; }
        // set our start and end frame colors
        frameStartRed = 0;
        frameStartGreen = 0;
        frameStartBlue = 0;
        frameEndRed = 0;
        frameEndGreen = 0;
        frameEndBlue = 0;
      }
      // using the start, stop, and animation delay counter, compute subframes
      // track progress through animation delay
      animationDelayCounter--;
    }
    // set color
    setColor(redSubFrames[subframe -1], greeenSubFrames[subframe -1], blueSubFrames[subframe -1])
    // track progress through subframes
    subframe--;
  }
}

void handleRoot() {                          // When URI / is requested, send a web page with a button to toggle the LED
  //server.send(200, "text/html", "<form action=\"/login\" method=\"POST\"><input type=\"text\" name=\"username\" placeholder=\"Username\"></br><input type=\"password\" name=\"password\" placeholder=\"Password\"></br><input type=\"submit\" value=\"Login\"></form><p>Try 'John Doe' and 'password123' ...</p>");
  server.send(200, "text/html", "rain-gutter-rgb http-post (hex values): /rgb  color=\'RRGGBB\'");
}

void handleEchoHex() {
  // make sure null terminated, then send as string
  hexStr[6] = '\0';
  server.send(200, "text/html", (char*) hexStr);
}

void handleAnimate() {
  // turn on animate
  animate = 1;
}

void handleGetDec() {
  String hex =  String("");
  server.send(200, "text/html", hex + red + "," + green + "," + blue + '\n');
}

void handleRGB() {                         // If a POST request is made to URI /login
  if( ! server.hasArg("color")) {
    server.send(400, "text/plain", "400: Invalid Request");         // The request is invalid, so send HTTP status 400
    return;
  }

  // turn of animate
  animate = 1;

  // kinda lame but parsing is not super easy here
  server.arg("color").getBytes(hexStr, 7);
  setColor(
    /* red */ (nibbler(hexStr[0]) << 4) + nibbler(hexStr[1]),
    /* green */ (nibbler(hexStr[2]) << 4) + nibbler(hexStr[3]),
    /* blue  */ (nibbler(hexStr[4]) << 4) + nibbler(hexStr[5])
  )

  /*
  server.arg("color").substring(0, 2).toCharArray(hexStr, 2);
  r = hexStrToInt(hexStr);
  server.arg("color").substring(2, 4).toCharArray(hexStr, 2);
  g = hexStrToInt(hexStr);
  server.arg("color").substring(4, 6).toCharArray(hexStr, 2);
  b = hexStrToInt(hexStr);
  */
  String info = "degub r:";
  server.send(200, "text/html", info + red + " g:" + green + " b:" + blue + '\n');
  //server.send(200, "text/html", "okay");
}

void setColor(uint8_t r, uint8_t g, uint8_t b) {
  // store colors for API
  red   = r;
  green = g;
  blue  = b;
  analogWrite(PIN_5, red);
  analogWrite(PIN_6, green);
  analogWrite(PIN_7, blue);
}
// converts ASCII utf8 chars to integer values, [0..F], otherwise zero
uint8_t nibbler(uint8_t v) {
  switch (v) {
    case 'f': case 'F': return 15;
    case 'e': case 'E': return 14;
    case 'd': case 'D': return 13;
    case 'c': case 'C': return 12;
    case 'b': case 'B': return 11;
    case 'a': case 'A': return 10;
    case '9':           return 9;
    case '8':           return 8;
    case '7':           return 7;
    case '6':           return 6;
    case '5':           return 5;
    case '4':           return 4;
    case '3':           return 3;
    case '2':           return 2;
    case '1':           return 1;
    default:            return 0;
  }
}

uint8_t hexStrToInt(char buf[]) {
  return (int) strtol(buf, 0, 16);
}

void handleLogin() {                         // If a POST request is made to URI /login
  if( ! server.hasArg("username") || ! server.hasArg("password") 
      || server.arg("username") == NULL || server.arg("password") == NULL) { // If the POST request doesn't have username and password data
    server.send(400, "text/plain", "400: Invalid Request");         // The request is invalid, so send HTTP status 400
    return;
  }
  if(server.arg("username") == "John Doe" && server.arg("password") == "password123") { // If both the username and the password are correct
    server.send(200, "text/html", "<h1>Welcome, " + server.arg("username") + "!</h1><p>Login successful</p>");
  } else {                                                                              // Username and password don't match
    server.send(401, "text/plain", "401: Unauthorized");
  }
}

void handleNotFound(){
  server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}