esp8266-house-led-rgb.ino 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 = 0x07;
  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 handleRGB();
  160. void handleGetDec();
  161. void handleEchoHex();
  162. void handleAnimate();
  163. void handleGetCfg();
  164. void handleCfg();
  165. void handleGetBrightness();
  166. void handleBrightness();
  167. // pwm.h - https://github.com/StefanBruens/ESP8266_new_pwm
  168. extern "C" void pwm_start();
  169. extern "C" void pwm_init(uint32_t period, uint32_t *duty, uint32_t pwm_channel_num, uint32_t (*pin_info_list)[3]);
  170. extern "C" void pwm_set_duty(uint32_t duty, uint8_t channel);
  171. /* esp8266 12-f breakout board has pins mapped a bit odd, and arduino esp8266 libs use GPIO pin number */
  172. #define PIN_5 14 /* GPIO_14 */
  173. #define PIN_6 12 /* GPIO_12 */
  174. #define PIN_7 13 /* GPIO_13 */
  175. #define PIN_LED 2 /* GPIO_2 active low */
  176. // new pwm
  177. #define PWM_RED 0
  178. #define PWM_GREEN 1
  179. #define PWM_BLUE 2
  180. #define PWM_LED 3
  181. void setup(void){
  182. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  183. delay(10);
  184. Serial.println('\n');
  185. wifiMulti.addAP("n-phone-number-upstairs", "3103229909cedar"); // add Wi-Fi networks you want to connect to
  186. wifiMulti.addAP("n-phone-number", "3103229909cedar");
  187. Serial.println("Connecting ...");
  188. int i = 0;
  189. 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
  190. delay(250);
  191. Serial.print('.');
  192. }
  193. Serial.println('\n');
  194. Serial.print("Connected to ");
  195. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  196. Serial.print("IP address:\t");
  197. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  198. /*
  199. if (MDNS.begin("rain-gutter-rgb")) { // Start the mDNS responder for esp8266.local
  200. Serial.println("mDNS responder started");
  201. } else {
  202. Serial.println("Error setting up MDNS responder!");
  203. }
  204. */
  205. server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
  206. //server.on("/login", HTTP_POST, handleLogin); // Call the 'handleLogin' function when a POST request is made to URI "/login"
  207. server.on("/rgb", HTTP_POST, handleRGB); // Call the 'handleRGB' function when a POST request is made to URI "/login"
  208. server.on("/dec", HTTP_GET, handleGetDec);
  209. server.on("/echo", HTTP_GET, handleEchoHex);
  210. server.on("/cfg", HTTP_GET, handleGetCfg);
  211. server.on("/cfg", HTTP_POST, handleCfg);
  212. server.on("/brightness", HTTP_GET, handleGetBrightness);
  213. server.on("/brightness", HTTP_POST, handleBrightness);
  214. //server.on("/animate", HTTP_GET, handleAnimate);
  215. server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
  216. server.begin(); // Actually start the server
  217. Serial.println("HTTP server started");
  218. /* slowing the PWM frequency decreases the error caused by the power mosfets
  219. * having a slow turn-off
  220. * this comes at a cost of flickering. One might think that LED PWM at
  221. * 1000Hz is not noticeable by the human eye, but one can see the flicker
  222. * appear as multiple spots when moving. It isn't really a flicker but one
  223. * can tell.
  224. */
  225. //analogWriteFreq(1000);
  226. //analogWriteFreq(250);
  227. // default range is 0..255
  228. //analogWriteRange(1023);
  229. // start code from StefanBruens/ESP8266_new_pwm
  230. #define PWM_CHANNELS 4
  231. const uint32_t period = 5000; // * 200ns ^= 1 kHz
  232. uint32_t io_info[PWM_CHANNELS][3] = {
  233. // MUX, FUNC, PIN
  234. {PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, PIN_6},
  235. {PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13, PIN_7},
  236. {PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14, PIN_5},
  237. {PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2 , PIN_LED}
  238. };
  239. // initial duty: all at 1%
  240. uint32_t pwm_duty_init[PWM_CHANNELS] = {50, 50, 50, 50};
  241. pwm_init(period, pwm_duty_init, PWM_CHANNELS, io_info);
  242. pwm_start();
  243. pinMode(PIN_5, OUTPUT);
  244. pinMode(PIN_6, OUTPUT);
  245. pinMode(PIN_7, OUTPUT);
  246. if(false) {
  247. // turn on the onboard led for testing brightness
  248. analogWrite(/* esp12f onboard led */ 2, 0);
  249. // setup pins with a test pwm
  250. analogWrite(PIN_5, red);
  251. analogWrite(PIN_6, green);
  252. analogWrite(PIN_7, blue);
  253. }
  254. }
  255. void loop(void){
  256. // keep track of where we are in the animation
  257. uint8_t frame = 0;
  258. uint8_t frameCount = 0;
  259. #define KEY_FRAMES 10
  260. #define MS_IN_ANIMATION_DELAY 250
  261. #define TEMPORAL_LENGTH (MS_IN_ANIMATION_DELAY / KEY_FRAMES)
  262. uint8_t redSubFrames[KEY_FRAMES];
  263. uint8_t greenSubFrames[KEY_FRAMES];
  264. uint8_t blueSubFrames[KEY_FRAMES];
  265. // keep track of where we are in the subframe
  266. uint8_t subframe = KEY_FRAMES;
  267. /* because subframes occur every quarter second and animation delays can be long (up to 64 seconds)
  268. * we need to guess what the next color should be for the next second based on where we are now
  269. */
  270. uint8_t animationDelayCounter = 0;
  271. uint16_t totalSubframes = 0;
  272. uint8_t frameAnimationDelay = 0;
  273. uint8_t frameStartRed = 0;
  274. uint8_t frameStartGreen = 0;
  275. uint8_t frameStartBlue = 0;
  276. uint8_t frameEndRed = 0;
  277. uint8_t frameEndGreen = 0;
  278. uint8_t frameEndBlue = 0;
  279. // tracking temporal length so that we do not run too fast or slow based on CPU frequency
  280. uint8_t delayCount = 0;
  281. while(true) {
  282. // listen for HTTP requests from clients
  283. server.handleClient();
  284. // check if we are supposed to animate
  285. if(0 == animate) break;
  286. /* we will transition several times a second (technically serveral times per animation delay)
  287. *
  288. * [?] this delay is not really necesarry, we just need a way to not run too fast
  289. *
  290. * [!] this delay also reduces power consumption of the esp8266 significantly
  291. * not something we want necessarily, but worth mentioning in case one questions
  292. * what the heck is happening
  293. */
  294. //delay(1);
  295. if(delayCount++ < TEMPORAL_LENGTH) break;
  296. // do we have any subframes to animate?
  297. if(0 == subframe) {
  298. // reset the subframe counter
  299. subframe = KEY_FRAMES;
  300. // are we done with this frame?
  301. if(0 == animationDelayCounter) {
  302. // move to the next frame
  303. if(0 == frame) frame = (ANIMATE_BUFFER_LEN - 1) / 8;
  304. frame--;
  305. // get the next animation delay
  306. // is this a stop frame
  307. if(0xff == frameAnimationDelay) { animate = 0; break; }
  308. // is this a loop frame
  309. if(0x00 == frameAnimationDelay) { frame = 0; continue; }
  310. // set our start and end frame colors
  311. frameStartRed = 0;
  312. frameStartGreen = 0;
  313. frameStartBlue = 0;
  314. frameEndRed = 0;
  315. frameEndGreen = 0;
  316. frameEndBlue = 0;
  317. }
  318. // using the start, stop, and animation delay counter, compute subframes
  319. // track progress through animation delay
  320. animationDelayCounter--;
  321. }
  322. // set color
  323. setColor(redSubFrames[subframe -1], greenSubFrames[subframe -1], blueSubFrames[subframe -1]);
  324. // track progress through subframes
  325. subframe--;
  326. }
  327. }
  328. void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
  329. //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>");
  330. server.send(200, "text/html", "rain-gutter-rgb http-post (hex values): /rgb color=\'RRGGBB\'");
  331. }
  332. void handleEchoHex() {
  333. // make sure null terminated, then send as string
  334. hexStr[6] = '\0';
  335. server.send(200, "text/html", (char*) hexStr);
  336. }
  337. void handleAnimate() {
  338. // turn on animate
  339. animate = 1;
  340. }
  341. void handleGetDec() {
  342. String hex = String("");
  343. server.send(200, "text/html", hex + red + "," + green + "," + blue + '\n');
  344. }
  345. void handleGetBrightness() {
  346. String info = String("");
  347. server.send(200, "text/html", info + brightness + '\n');
  348. }
  349. /* we are going to cheat the brightness by adjusting pwm resolution
  350. *
  351. * in effect this will change the brightness and we will not need to perform any math
  352. * the way this works is that PWM resolution is 10-bit but when we set the duty-cycle
  353. * we only use values [0..255]. By increasing the resoltion past 255 we reduce the
  354. * brightness. Cool.
  355. *
  356. * [?] because our gate drive voltage source is a weak reverse biased zener
  357. * regulator and we just use resistors to turn off the power mosfets we
  358. * have a hard time with power losses turning on and off the mosfet. When
  359. * we drive all three mosfets on with 100% duty cycle the voltage source
  360. * drops well below the needed drive voltage and the mosfet Rds becomes
  361. * pretty terrible. It works but is really bad. If we run less than
  362. * 50% duty we should be okay. Using 1023 as the starting point means that
  363. * at 0 brightness mosfets are driven at 25% duty (255/1032). At 100
  364. * brightness they are driven at 60% duty 255/432.
  365. */
  366. void updateBrightness () {
  367. setColor(red, green, blue);
  368. return;
  369. // brightness is inverted, technically it is darkness
  370. uint8_t brightnessValue = 6 * (100 - brightness);
  371. // reducing PWM range will increase brightness
  372. //analogWriteRange(1023 - brightnessValue);
  373. // this works, but only gives 3 levels, 8,9,10
  374. //if(false) analogWriteResolution(resolution++ % 2 ?10 :8);
  375. //analogWriteFreq(250);
  376. // all colors need to be re-written after adjusting brightness
  377. //setColor(red, green, blue);
  378. // turn on the onboard led for testing brightness
  379. //analogWrite(/* esp12f onboard led */ 2, 100);
  380. }
  381. void handleBrightness() {
  382. uint8_t buffer[4];
  383. if( ! server.hasArg("brightness")) {
  384. server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
  385. return;
  386. }
  387. // else
  388. server.arg("brightness").getBytes(buffer, 3);
  389. brightness = (nibbler(buffer[0]) << 4) + nibbler(buffer[1]);
  390. // limit brightness to 15
  391. brightness = 0x0f < brightness ? 0x0f : brightness;
  392. updateBrightness();
  393. handleGetBrightness();
  394. }
  395. void handleCfg() {
  396. uint8_t valid = 0;
  397. uint8_t buf[3];
  398. if(server.hasArg("quickGamma")) {
  399. server.arg("quickGamma").getBytes(buf, 2);
  400. gammaCorrectionType = '1' == buf[0]
  401. ? USE_BG100_S_CURVE_GAMMA_CORRECTION
  402. : DISABLE_GAMMA_CORRECTION;
  403. valid += 1;
  404. }
  405. if(server.hasArg("sCurveGamma")) {
  406. server.arg("sCurveGamma").getBytes(buf, 2);
  407. gammaCorrectionType = '1' == buf[0]
  408. ? USE_PHILIP_GAMMA_CORRECTION
  409. : DISABLE_GAMMA_CORRECTION;
  410. valid += 1;
  411. }
  412. if(server.hasArg("colorCorrection")) {
  413. server.arg("colorCorrection").getBytes(buf, 2);
  414. useFastLedCorrection = '1' == buf[0]
  415. ? ENABLE_FASTLED_CORRECTION
  416. : DISABLE_FASTLED_CORRECTION;
  417. valid += 1;
  418. }
  419. if(valid) handleGetCfg(); else server.send(400, "text/plain", "400: Invalid Request");
  420. return;
  421. }
  422. void handleGetCfg() {
  423. String info = String("cfg [quickGamma:");
  424. uint8_t fastled = ENABLE_FASTLED_CORRECTION == useFastLedCorrection;
  425. uint8_t sCurve = USE_PHILIP_GAMMA_CORRECTION == gammaCorrectionType;
  426. uint8_t quick = USE_BG100_S_CURVE_GAMMA_CORRECTION == gammaCorrectionType;
  427. server.send(200, "text/html", info + quick + " sCurveGamma:" + sCurve + " colorCorrection:" + fastled + ']' + '\n');
  428. }
  429. void handleRGB() { // If a POST request is made to URI /login
  430. if( ! server.hasArg("color")) {
  431. server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
  432. return;
  433. }
  434. // turn of animate
  435. animate = 1;
  436. // kinda lame but parsing is not super easy here
  437. server.arg("color").getBytes(hexStr, 7);
  438. setColor(
  439. /* red */ (nibbler(hexStr[0]) << 4) + nibbler(hexStr[1]),
  440. /* green */ (nibbler(hexStr[2]) << 4) + nibbler(hexStr[3]),
  441. /* blue */ (nibbler(hexStr[4]) << 4) + nibbler(hexStr[5])
  442. );
  443. String info = String("degub r:");
  444. server.send(200, "text/html", info + red + " g:" + green + " b:" + blue + '\n');
  445. }
  446. void setColor(uint8_t r, uint8_t g, uint8_t b) {
  447. // store colors for API
  448. red = r;
  449. green = g;
  450. blue = b;
  451. pwm_set_duty((uint16_t) brightness * correctColor(red, FASTLED_RED_CORRECTION), PWM_RED);
  452. pwm_set_duty((uint16_t) brightness * correctColor(green, FASTLED_RED_CORRECTION), PWM_GREEN);
  453. pwm_set_duty((uint16_t) brightness * correctColor(blue, FASTLED_RED_CORRECTION), PWM_BLUE);
  454. pwm_set_duty((uint16_t) brightness * 255, PWM_LED);
  455. pwm_start();
  456. return;
  457. /* [!] it looks like we always correct color, this is not the case
  458. * the correctColor() routine, and the chained gammaCorrection() routine
  459. * check configuration settings before setting the LED color.
  460. *
  461. * the fast-led correction tones down green significantly and blue a bit.
  462. * Both linearly, I assume this is not to adjust for human eye sensitivity
  463. * but just for the junction efficiency
  464. */
  465. analogWrite(PIN_5, correctColor(red, FASTLED_RED_CORRECTION));
  466. analogWrite(PIN_6, correctColor(green, FASTLED_GREEN_CORRECTION));
  467. analogWrite(PIN_7, correctColor(blue, FASTLED_BLUE_CORRECTION));
  468. }
  469. // converts ASCII utf8 chars to integer values, [0..F], otherwise zero
  470. uint8_t nibbler(uint8_t v) {
  471. switch (v) {
  472. case 'f': case 'F': return 15;
  473. case 'e': case 'E': return 14;
  474. case 'd': case 'D': return 13;
  475. case 'c': case 'C': return 12;
  476. case 'b': case 'B': return 11;
  477. case 'a': case 'A': return 10;
  478. case '9': return 9;
  479. case '8': return 8;
  480. case '7': return 7;
  481. case '6': return 6;
  482. case '5': return 5;
  483. case '4': return 4;
  484. case '3': return 3;
  485. case '2': return 2;
  486. case '1': return 1;
  487. default: return 0;
  488. }
  489. }
  490. void handleLogin() { // If a POST request is made to URI /login
  491. if( ! server.hasArg("username") || ! server.hasArg("password")
  492. || server.arg("username") == NULL || server.arg("password") == NULL) { // If the POST request doesn't have username and password data
  493. server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
  494. return;
  495. }
  496. if(server.arg("username") == "John Doe" && server.arg("password") == "password123") { // If both the username and the password are correct
  497. server.send(200, "text/html", "<h1>Welcome, " + server.arg("username") + "!</h1><p>Login successful</p>");
  498. } else { // Username and password don't match
  499. server.send(401, "text/plain", "401: Unauthorized");
  500. }
  501. }
  502. void handleNotFound(){
  503. 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
  504. }