esp8266-house-led-rgb.ino 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* Govee LED lights that were purcahsed from Amazon.com 100ft for 15USD.
  2. * 24Vdc 6 LED in series with current limiting resistor every 19.5".
  3. *
  4. * +----B----B----B----B----B----B----2K4-------------+
  5. * | |
  6. * 24Vdc ----+----R----R----R----R----R----R----2K4-----------------+
  7. * | | |
  8. * +----G----G----G----G----G----G----3K3---------------------+
  9. * | | | |
  10. * | | | |
  11. * | | | |
  12. * +---------+----B----B----B----B----B----B----2K4---+ | |
  13. * | | | |
  14. * +----R----R----R----R----R----R----2K4-------+ |
  15. * | | | |
  16. * +----G----G----G----G----G----G----3K3-----------+
  17. * | | | |
  18. * | control bus: blue red green
  19. * | | | |
  20. * ... ... ... ...
  21. *
  22. * We installed about 150ft so about 90 led circuits will need power. ESP8266 can
  23. * do 1KHz PWM so we can use 3 pins if we want. Another idea I had was to use lots
  24. * of pins to control 20mA LED driver ICs. With 15 pins one could make a circuit to
  25. * drive 32 levels of current to each color giving 32,768 colors. Each color would
  26. * have 5 npn controlling 1,2,4,8,16 LED drivers. One could then step through 0..620mA.
  27. *
  28. * Instead we will just use PWM to give us color control. We just need to calculate how
  29. * much current we want to send to our LED strip. Because each circuit runs in parallel
  30. * we need to determine how much current each branch will use. The current limiting
  31. * resistors means we can probably just use a fixed voltage supply. With a 48Vdc supply
  32. * at max brightness green will see 9mA, blue will see 12mA, and red will see 15mA. These
  33. * values are all pretty safe for the LED but the red would blow out a PN2222A with 1080mA.
  34. *
  35. * We should be careful about the inductance of the wire. Assuming 1/4mm diameter wire we
  36. * might have 47uH. We can use a diode on the collector of each color to send the spikes to
  37. * the +48Vdc rail. Each color will use an NPN-BJT, probably 3 parallel PN2222A with
  38. * ballast resitors, as buffers for the PWM signal from the esp8266.
  39. *
  40. * In the end I ended up using optoisolated pwm driving three power mosfets. This ended
  41. * up being a nuisance since the mosfet gates took forever to discharge resulting in
  42. * the LEDs staying lit when the esp8266 pulled the signal down. I did not really fix this
  43. * but I did add a few resistors on the gate to bleed off the gate charge. In effect the
  44. * max brightness is 0xfc instead of 0xff since the led does not turn off on 0xfd. But that
  45. * is not really a deal-breaker, just annoying.
  46. *
  47. * test using:
  48. * curl -X POST http://rain-gutter-rgb.lan.rome7.com/rgb -d "color=010101"
  49. */
  50. #include <ESP8266WiFi.h>
  51. #include <WiFiClient.h>
  52. #include <ESP8266WiFiMulti.h>
  53. #include <ESP8266mDNS.h>
  54. #include <ESP8266WebServer.h>
  55. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  56. uint8_t hexStr[7] = "010101";
  57. /* 131 allows for frame count, 16 colors(3), 16 transistions(1), and a null terminator
  58. * the user provides the input as utf-8 encoded hexidecimal, we need twice
  59. * the storage space, but parsing is a little easier: 2 + (2 * (3 * 16 + 16)) + 1 = 131
  60. * [?] there are 16 transistions because there is an extra transition when we
  61. * reach the end an loop to the begining
  62. * [?] frame count allows user to have an animation that is not the maximum length,
  63. * as of this note, the max frame count is 0x0f, '0f' when using a
  64. */
  65. #define HOW_MANY_ANIMATION_FRAMES 16
  66. #define MAX_ANIMATE_FRAME_COUNT_LEN 2
  67. #define NULL_TERMINATOR_LEN 1
  68. #define FRAME_LEN (3 + 1)
  69. #define ANIMATE_BUFFER_LEN (MAX_ANIMATE_FRAME_COUNT_LEN + (2 * HOW_MANY_ANIMATION_FRAMES * FRAME_LEN) + NULL_TERMINATOR_LEN)
  70. #define MAX_ANIMATE_FRAME_COUNT ((ANIMATE_BUFFER_LEN - 2 - 1) / 8)
  71. uint8_t animateStr[ANIMATE_BUFFER_LEN];
  72. uint8_t animate = 0;
  73. uint8_t red = 1;
  74. uint8_t green = 1;
  75. uint8_t blue = 1;
  76. uint8_t brightness = 50;
  77. #define DISABLE_FASTLED_CORRECTION 0
  78. #define ENABLE_FASTLED_CORRECTION 1
  79. #define DISABLE_GAMMA_CORRECTION 0
  80. #define USE_PHILIP_GAMMA_CORRECTION 1
  81. #define USE_BG100_S_CURVE_GAMMA_CORRECTION 2
  82. uint8_t gammaCorrectionType = DISABLE_GAMMA_CORRECTION;
  83. uint8_t useFastLedCorrection = DISABLE_FASTLED_CORRECTION;
  84. // quick fix for gamma correction from Adafruit's Phillip Burgess
  85. static uint8_t phillip_adafruitGamma8[] = {
  86. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
  89. 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
  90. 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
  91. 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
  92. 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
  93. 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
  94. 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
  95. 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
  96. 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
  97. 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  98. 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  99. 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  100. 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  101. 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
  102. // another implementation of gamma correction produced by bg100: f(x) = 1/(1+EXP(((A2/21)-6)*-1))*255
  103. static uint8_t bg100_sCurveGamma8[] = {
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  105. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  106. 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05,
  107. 0x05, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x09, 0x0A, 0x0A, 0x0B, 0x0B,
  108. 0x0C, 0x0C, 0x0D, 0x0D, 0x0E, 0x0F, 0x0F, 0x10, 0x11, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  109. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1F, 0x20, 0x21, 0x23, 0x24, 0x26, 0x27, 0x29, 0x2B, 0x2C,
  110. 0x2E, 0x30, 0x32, 0x34, 0x36, 0x38, 0x3A, 0x3C, 0x3E, 0x40, 0x43, 0x45, 0x47, 0x4A, 0x4C, 0x4F,
  111. 0x51, 0x54, 0x57, 0x59, 0x5C, 0x5F, 0x62, 0x64, 0x67, 0x6A, 0x6D, 0x70, 0x73, 0x76, 0x79, 0x7C,
  112. 0x7F, 0x82, 0x85, 0x88, 0x8B, 0x8E, 0x91, 0x94, 0x97, 0x9A, 0x9C, 0x9F, 0xA2, 0xA5, 0xA7, 0xAA,
  113. 0xAD, 0xAF, 0xB2, 0xB4, 0xB7, 0xB9, 0xBB, 0xBE, 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
  114. 0xD0, 0xD2, 0xD3, 0xD5, 0xD7, 0xD8, 0xDA, 0xDB, 0xDD, 0xDE, 0xDF, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5,
  115. 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xED, 0xEE, 0xEF, 0xEF, 0xF0, 0xF1, 0xF1, 0xF2,
  116. 0xF2, 0xF3, 0xF3, 0xF4, 0xF4, 0xF5, 0xF5, 0xF6, 0xF6, 0xF6, 0xF7, 0xF7, 0xF7, 0xF8, 0xF8, 0xF8,
  117. 0xF9, 0xF9, 0xF9, 0xF9, 0xFA, 0xFA, 0xFA, 0xFA, 0xFA, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFC,
  118. 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD,
  119. 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF
  120. }
  121. /* Fast-Led typical smd5050 led RGB color correction (0xFFB0F0 red 255, green 176, blue 240)
  122. * measured brightness from typical LED vary by pn junction semiconductor material.
  123. *
  124. * [?] I am not too sure if this is measured by human eye or by device. Human eyes have thee
  125. * issues that need to be accounted for, one is the non-linear brightness that our nerves send
  126. * to our brains. Our eyes are more sensitive to green light. The last problem is not really
  127. * something we can handle in code as it in the realm of psychophysics, Weber–Fechner laws. The
  128. * Weber–Fechner has to do with the way our brains have a big impact on human perception.
  129. *
  130. * I believe that this color correction has to do with mechanical measurements as they are linear
  131. * and I know that the human eye sensitivities are anything but. Otherwise this is very crude.
  132. */
  133. #define FASTLED_RED_CORRECTION = 0x00FF;
  134. #define FASTLED_GREEN_CORRECTION = 0x00B0;
  135. #define FASTLED_BLUE_CORRECTION = 0x00F0;
  136. uint8_t correctColor(uint8_t input, uint16_t factor) {
  137. uint16_t out;
  138. uint16_t in;
  139. in = input;
  140. out = in * factor;
  141. out /= 255;
  142. return ENABLE_FASTLED_CORRECTION == useFastLedCorrection
  143. ? gammaCorrection((uint8_t) out)
  144. : gammaCorrection(input);
  145. }
  146. uint8_t gammaCorrection(uint8_t input) {
  147. switch(gammaCorrectionType) {
  148. case USE_PHILIP_GAMMA_CORRECTION: return phillip_adafruitGamma8[input];
  149. case USE_BG100_S_CURVE_GAMMA_CORRECTION: return bg100_sCurveGamma8[input];
  150. case DISABLE_GAMMA_CORRECTION:
  151. default:
  152. }
  153. return input;
  154. }
  155. ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
  156. void handleRoot(); // function prototypes for HTTP handlers
  157. void handleLogin();
  158. void handleNotFound();
  159. void setup(void){
  160. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  161. delay(10);
  162. Serial.println('\n');
  163. wifiMulti.addAP("n-phone-number-upstairs", "3103229909cedar"); // add Wi-Fi networks you want to connect to
  164. wifiMulti.addAP("n-phone-number", "3103229909cedar");
  165. Serial.println("Connecting ...");
  166. int i = 0;
  167. 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
  168. delay(250);
  169. Serial.print('.');
  170. }
  171. Serial.println('\n');
  172. Serial.print("Connected to ");
  173. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  174. Serial.print("IP address:\t");
  175. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  176. /*
  177. if (MDNS.begin("rain-gutter-rgb")) { // Start the mDNS responder for esp8266.local
  178. Serial.println("mDNS responder started");
  179. } else {
  180. Serial.println("Error setting up MDNS responder!");
  181. }
  182. */
  183. server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
  184. //server.on("/login", HTTP_POST, handleLogin); // Call the 'handleLogin' function when a POST request is made to URI "/login"
  185. server.on("/rgb", HTTP_POST, handleRGB); // Call the 'handleRGB' function when a POST request is made to URI "/login"
  186. server.on("/dec", HTTP_GET, handleGetDec);
  187. server.on("/echo", HTTP_GET, handleEchoHex);
  188. server.on("/animate", HTTP_GET, handleAnimate);
  189. server.on("/cfg", HTTP_GET, handleGetCfg);
  190. server.on("/cfg", HTTP_POST, handleCfg);
  191. server.on("/brightness", HTTP_POST, handleGetBrightness);
  192. server.on("/brightness", HTTP_POST, handleBrightness
  193. server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
  194. server.begin(); // Actually start the server
  195. Serial.println("HTTP server started");
  196. /* esp8266 12-f breakout board has pins mapped a bit odd, and arduino esp8266 libs use GPIO pin number */
  197. #define PIN_5 14 /* GPIO_14 */
  198. #define PIN_6 12 /* GPIO_12 */
  199. #define PIN_7 13 /* GPIO_13 */
  200. /* slowing the PWM frequency decreases the error caused by the power mosfets
  201. * having a slow turn-off
  202. * this comes at a cost of flickering. One might think that LED PWM at
  203. * 1000Hz is not noticeable by the human eye, but one can see the flicker
  204. * appear as multiple spots when moving. It isn't really a flicker but one
  205. * can tell.
  206. */
  207. //analogWriteFreq(1000);
  208. analogWriteFreq(250);
  209. // lower brightness to minimal value
  210. brightness = 0;
  211. updateBrightness ();
  212. // turn on the onboard led for testing brightness
  213. analogWrite(/* esp12f onboard led */ 2, 255);
  214. // setup pins with a test pwm
  215. analogWrite(PIN_5, red);
  216. analogWrite(PIN_6, green);
  217. analogWrite(PIN_7, blue);
  218. }
  219. void loop(void){
  220. // keep track of where we are in the animation
  221. uint8_t frame = 0;
  222. uint8_t frameCount = 0;
  223. #define KEY_FRAMES 10
  224. #define MS_IN_ANIMATION_DELAY 250
  225. #define TEMPORAL_LENGTH (MS_IN_ANIMATION_DELAY / KEY_FRAMES)
  226. uint8_t redSubFrames[KEY_FRAMES];
  227. uint8_t greenSubFrames[KEY_FRAMES];
  228. uint8_t blueSubFrames[KEY_FRAMES];
  229. // keep track of where we are in the subframe
  230. uint8_t subframe = KEY_FRAMES;
  231. /* because subframes occur every quarter second and animation delays can be long (up to 64 seconds)
  232. * we need to guess what the next color should be for the next second based on where we are now
  233. */
  234. uint8_t animationDelayCounter = 0;
  235. uint16_t totalSubframes = 0;
  236. uint8_t frameAnimationDelay = 0;
  237. uint8_t frameStartRed = 0;
  238. uint8_t frameStartGreen = 0;
  239. uint8_t frameStartBlue = 0;
  240. uint8_t frameEndRed = 0;
  241. uint8_t frameEndGreen = 0;
  242. uint8_t frameEndBlue = 0;
  243. // tracking temporal length so that we do not run too fast or slow based on CPU frequency
  244. uint8_t delayCount = 0;
  245. while(true) {
  246. // listen for HTTP requests from clients
  247. server.handleClient();
  248. // check if we are supposed to animate
  249. if(0 == animate) break;
  250. /* we will transition several times a second (technically serveral times per animation delay)
  251. *
  252. * [?] this delay is not really necesarry, we just need a way to not run too fast
  253. *
  254. * [!] this delay also reduces power consumption of the esp8266 significantly
  255. * not something we want necessarily, but worth mentioning in case one questions
  256. * what the heck is happening
  257. */
  258. delay(1);
  259. if(delayCount++ < TEMPORAL_LENGTH) break;
  260. // do we have any subframes to animate?
  261. if(0 == subframe) {
  262. // reset the subframe counter
  263. subframe = KEY_FRAMES;
  264. // are we done with this frame?
  265. if(0 == animationDelayCounter) {
  266. // move to the next frame
  267. if(0 == frame) frame = (ANIMATE_BUFFER_LEN - 1) / 8;
  268. frame--;
  269. // get the next animation delay
  270. frameAnimationDelay = animationDelayCounter =
  271. // is this a stop frame
  272. if(0xff == frameAnimationDelay) { animate = 0; break; }
  273. // is this a loop frame
  274. if(0x00 == frameAnimationDelay) { frame = 0; continue; }
  275. // set our start and end frame colors
  276. frameStartRed = 0;
  277. frameStartGreen = 0;
  278. frameStartBlue = 0;
  279. frameEndRed = 0;
  280. frameEndGreen = 0;
  281. frameEndBlue = 0;
  282. }
  283. // using the start, stop, and animation delay counter, compute subframes
  284. // track progress through animation delay
  285. animationDelayCounter--;
  286. }
  287. // set color
  288. setColor(redSubFrames[subframe -1], greeenSubFrames[subframe -1], blueSubFrames[subframe -1])
  289. // track progress through subframes
  290. subframe--;
  291. }
  292. }
  293. void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
  294. //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>");
  295. server.send(200, "text/html", "rain-gutter-rgb http-post (hex values): /rgb color=\'RRGGBB\'");
  296. }
  297. void handleEchoHex() {
  298. // make sure null terminated, then send as string
  299. hexStr[6] = '\0';
  300. server.send(200, "text/html", (char*) hexStr);
  301. }
  302. void handleAnimate() {
  303. // turn on animate
  304. animate = 1;
  305. }
  306. void handleGetDec() {
  307. String hex = String("");
  308. server.send(200, "text/html", hex + red + "," + green + "," + blue + '\n');
  309. }
  310. void handleGetBrightness() {
  311. String info = "";
  312. server.send(200, "text/html", info + brightness + '\n');
  313. return;
  314. }
  315. /* we are going to cheat the brightness by adjusting pwm resolution
  316. *
  317. * in effect this will change the brightness and we will not need to perform any math
  318. * the way this works is that PWM resolution is 10-bit but when we set the duty-cycle
  319. * we only use values [0..255]. By increasing the resoltion past 255 we reduce the
  320. * brightness. Cool.
  321. *
  322. * [?] because our gate drive voltage source is a weak reverse biased zener
  323. * regulator and we just use resistors to turn off the power mosfets we
  324. * have a hard time with power losses turning on and off the mosfet. When
  325. * we drive all three mosfets on with 100% duty cycle the voltage source
  326. * drops well below the needed drive voltage and the mosfet Rds becomes
  327. * pretty terrible. It works but is really bad. If we run less than
  328. * 50% duty we should be okay. Using 1023 as the starting point means that
  329. * at 0 brightness mosfets are driven at 25% duty (255/1032). At 100
  330. * brightness they are driven at 60% duty 255/432.
  331. */
  332. void updateBrightness () {
  333. // default range is 0..255
  334. analogWriteRange(1023);
  335. // brightness is inverted, technically it is darkness
  336. uint8_t brightnessValue = 6 * (100 - brightness);
  337. // reducing PWM range will increase brightness
  338. analogWriteRange(1023 - brightnessValue);
  339. }
  340. void handleBrightness() {
  341. char buffer[4];
  342. if( ! server.hasArg("brightness")) {
  343. server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
  344. return;
  345. }
  346. // else
  347. server.arg("brightness").getBytes(buffer, 3);
  348. brightness = (nibbler(buffer[0]) << 4) + nibbler(buffer[1]);
  349. // limit brightness to 100
  350. brightness = 100 < brightness ? 100 : brightness;
  351. updateBrightness();
  352. handleGetBrightness();
  353. return;
  354. }
  355. void handleCfg() {
  356. uint8_t valid = 0;
  357. char buf[3];
  358. if(server.hasArg("quickGamma")) {
  359. server.arg("quickGamma").getBytes(buf, 2);
  360. gammaCorrectionType = '1' == buf[0]
  361. ? USE_BG100_S_CURVE_GAMMA_CORRECTION
  362. : DISABLE_GAMMA_CORRECTION;
  363. valid += 1;
  364. }
  365. if(server.hasArg("sCurveGamma")) {
  366. server.arg("sCurveGamma").getBytes(buf, 2);
  367. gammaCorrectionType = '1' == buf[0]
  368. ? USE_PHILIP_GAMMA_CORRECTION
  369. : DISABLE_GAMMA_CORRECTION;
  370. valid += 1;
  371. }
  372. if(server.hasArg("colorCorrection")) {
  373. server.arg("colorCorrection").getBytes(buf, 2);
  374. gammaCorrectionType = '1' == buf[0]
  375. ? ENABLE_FASTLED_CORRECTION
  376. : DISABLE_FASTLED_CORRECTION;
  377. valid += 1;
  378. }
  379. if(valid) handleGetCfg(); else server.send(400, "text/plain", "400: Invalid Request");
  380. return;
  381. }
  382. void handleGetCfg() {
  383. String info = "cfg [quickGamma:";
  384. uint8_t fastled = ENABLE_FASTLED_CORRECTION == useFastLedCorrection;
  385. uint8_t sCurve = USE_PHILIP_GAMMA_CORRECTION == gammaCorrectionType;
  386. uint8_t quick = USE_BG100_S_CURVE_GAMMA_CORRECTION == gammaCorrectionType;
  387. if(valid) server.send(200, "text/html", info + quick + " sCurveGamma:" + sCurve + " colorCorrection:" + fastled + ']' + '\n');
  388. return;
  389. }
  390. void handleRGB() { // If a POST request is made to URI /login
  391. if( ! server.hasArg("color")) {
  392. server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
  393. return;
  394. }
  395. // turn of animate
  396. animate = 1;
  397. // kinda lame but parsing is not super easy here
  398. server.arg("color").getBytes(hexStr, 7);
  399. setColor(
  400. /* red */ (nibbler(hexStr[0]) << 4) + nibbler(hexStr[1]),
  401. /* green */ (nibbler(hexStr[2]) << 4) + nibbler(hexStr[3]),
  402. /* blue */ (nibbler(hexStr[4]) << 4) + nibbler(hexStr[5])
  403. )
  404. String info = "degub r:";
  405. server.send(200, "text/html", info + red + " g:" + green + " b:" + blue + '\n');
  406. }
  407. void setColor(uint8_t r, uint8_t g, uint8_t b) {
  408. // store colors for API
  409. red = r;
  410. green = g;
  411. blue = b;
  412. /* [!] it looks like we always correct color, this is not the case
  413. * the correctColor() routine, and the chained gammaCorrection() routine
  414. * check configuration settings before setting the LED color.
  415. *
  416. * the fast-led correction tones down green significantly and blue a bit.
  417. * Both linearly, I assume this is not to adjust for human eye sensitivity
  418. * but just for the junction efficiency
  419. */
  420. analogWrite(PIN_5, correctColor(red, FASTLED_RED_CORRECTION));
  421. analogWrite(PIN_6, correctColor(green, FASTLED_GREEN_CORRECTION));
  422. analogWrite(PIN_7, correctColor(blue, FASTLED_BLUE_CORRECTION));
  423. }
  424. // converts ASCII utf8 chars to integer values, [0..F], otherwise zero
  425. uint8_t nibbler(uint8_t v) {
  426. switch (v) {
  427. case 'f': case 'F': return 15;
  428. case 'e': case 'E': return 14;
  429. case 'd': case 'D': return 13;
  430. case 'c': case 'C': return 12;
  431. case 'b': case 'B': return 11;
  432. case 'a': case 'A': return 10;
  433. case '9': return 9;
  434. case '8': return 8;
  435. case '7': return 7;
  436. case '6': return 6;
  437. case '5': return 5;
  438. case '4': return 4;
  439. case '3': return 3;
  440. case '2': return 2;
  441. case '1': return 1;
  442. default: return 0;
  443. }
  444. }
  445. uint8_t hexStrToInt(char buf[]) {
  446. return (int) strtol(buf, 0, 16);
  447. }
  448. void handleLogin() { // If a POST request is made to URI /login
  449. if( ! server.hasArg("username") || ! server.hasArg("password")
  450. || server.arg("username") == NULL || server.arg("password") == NULL) { // If the POST request doesn't have username and password data
  451. server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
  452. return;
  453. }
  454. if(server.arg("username") == "John Doe" && server.arg("password") == "password123") { // If both the username and the password are correct
  455. server.send(200, "text/html", "<h1>Welcome, " + server.arg("username") + "!</h1><p>Login successful</p>");
  456. } else { // Username and password don't match
  457. server.send(401, "text/plain", "401: Unauthorized");
  458. }
  459. }
  460. void handleNotFound(){
  461. 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
  462. }