Advertisement
MrRockchip

super_s_part8266

Jun 9th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 34.60 KB | None | 0 0
  1. //// [000_000000][001_000000][002_000000][003_000000][004_000000][005_000000][006_000000][007_000000][001_FF0000]{
  2. //// [@@@_FFFFFF] [@@@_1B00EA] [@@@_1B00EA]![@@@_1B00EA]"[@@@_1B00EA]#[@@@_1B00EA]!
  3.  
  4. /* Neopixel web server with ESP8266-01
  5.  * with switch to rainbow animation
  6.  *
  7.  * Tom Tobback Sep 2016
  8.  * BuffaloLabs www.cassiopeia.hk/kids
  9. LICENSE:
  10. Copyright (c) 2016 Tom Tobback
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17.  
  18.  * web page esp/rgb returns POST request with 3 RGB parameters
  19.  * web page inspired by https://github.com/dimsumlabs/nodemcu-httpd
  20.  *
  21.  * voltage regulator from 5V to 3.3V
  22.  * neopixel strip 8 LEDs on GPIO2, and Vcc on 3.3V otherwise the data goes wrong sometimes
  23.  *
  24.  * ESP8266 ESP-01 connections
  25.  * Vcc 3.3V
  26.  * GND GND
  27.  * GPIO2 neopixel data pin with 430ohm resistor
  28.  * GPIO3 switch between animation/webserver (only at startup!) with 4K3 pull-down resistor and IN5819 diode to Vcc
  29.  * CH_PD 3.3V (actually Vcc)
  30.  * RST 3.3V (actually Vcc)
  31.  *
  32.  * 3 way switch on 3.3V to choose between webserver and animation
  33.  * GPIO3=RX is connected to switch: HIGH= animation LOW= webserver
  34.  * in HIGH state the power goes from GPIO3 to Vcc via a diode IN5819
  35.  * in LOW state the Vcc power cannot reach GPIO3
  36.  */
  37.  
  38. #define STRIP_NUMPIXELS  8
  39. #define MODE_STATIC_ALL  0
  40. #define MODE_INDIVIDUAL 56
  41. #define NO_SPEED_CHANGE 99
  42.  
  43. #include <ESP8266WiFi.h>
  44. #include <DNSServer.h>
  45. #include <ESP8266WebServer.h>
  46.  
  47. /* Set up your SSID and Password */
  48. const char*     ssid =  "NodeMCU"; // SSID
  49. const char* password = "19216811"; // Password
  50.  
  51. /* Set up your IP addresses */
  52. IPAddress local_ip(192,168,1,1);
  53. IPAddress  gateway(192,168,1,1);
  54. IPAddress subnet(255,255,255,0);
  55.  
  56. const byte DNS_PORT = 53;
  57. DNSServer dnsServer;
  58. ESP8266WebServer webServer(80);
  59.  
  60. boolean animation;
  61.  
  62. String webpageRGB = ""
  63.  " <!DOCTYPE html> "
  64.   " <html style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center;'> "
  65.    " <head> "
  66.     " <title>Neopixel control</title> "
  67.     " <meta name='mobile-web-app-capable' content='yes' /> "
  68.     " <meta name='viewport' content='width=device-width, height=device-height, initial-scale=1.0, user-scalable=yes' /> "
  69.    " </head> "
  70.    " <body style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center; font-family: \"Courier New\", Courier, monospace; font-weight: bold;'> "
  71.     " <script type='text/javascript'> "
  72.      " function toggleDetails() { "
  73.       " var elem = document.getElementById('my_details'); "
  74.       " if (elem.open == true) { "
  75.        " elem.open = false; "
  76.       " } "
  77.       " else { "
  78.        " elem.open = true; "
  79.       " } "
  80.      " } "
  81.          " var stripLocked = false; "
  82.          " function updateStrip(val_r, val_g, val_b, val_m, val_s) { "
  83.           " if (stripLocked) { "
  84.            " return; "
  85.           " } "
  86.           " function colourCorrect(v) { "
  87.            " return Math.round(1023 - ((v * v)/64)); "
  88.           " } "
  89.           " var params = [ "
  90.            " 'r=' + colourCorrect(val_r), "
  91.            " 'g=' + colourCorrect(val_g), "
  92.            " 'b=' + colourCorrect(val_b), "
  93.            " 'm=' + val_m, "
  94.            " 's=' + val_s "
  95.            " ].join('&'); "
  96.           " var req = new XMLHttpRequest(); "
  97.           " req.open('POST', '?' + params, true); "
  98.           " req.send(); "
  99.           " stripLocked = true; "
  100.           " req.onreadystatechange = function() { "
  101.            " if (req.readyState == 4) { "
  102.             " stripLocked = false; "
  103.            " } "
  104.           " } "
  105.          " } "
  106.      " function updateSummaryColor() { "
  107.       " var val_r = document.getElementById('range_r').value; "
  108.       " my_r      =        (parseInt(val_r, 10 )).toString(16).padStart(2, '0'); "
  109.       " my_r_inv  = (255 - (parseInt(val_r, 10))).toString(16).padStart(2, '0'); "
  110.       " var val_g = document.getElementById('range_g').value; "
  111.       " my_g      =        (parseInt(val_g, 10 )).toString(16).padStart(2, '0'); "
  112.       " my_g_inv  = (255 - (parseInt(val_g, 10))).toString(16).padStart(2, '0'); "
  113.       " var val_b = document.getElementById('range_b').value; "
  114.       " my_b      =        (parseInt(val_b, 10 )).toString(16).padStart(2, '0'); "
  115.       " my_b_inv  = (255 - (parseInt(val_b, 10))).toString(16).padStart(2, '0'); "
  116.       " document.getElementById('text_r').value = my_r; "
  117.       " document.getElementById('text_g').value = my_g; "
  118.       " document.getElementById('text_b').value = my_b; "
  119.       " document.getElementById('text_a').value = my_r + my_g + my_b; "
  120.       " var my_details = document.getElementById('my_details'); "
  121.       " my_details.style.background = '#' + my_r     + my_g     + my_b; "
  122.       " my_details.style.color      = '#' + my_r_inv + my_g_inv + my_b_inv; "
  123.       " var val_m = document.getElementById('range_m').value; "
  124.       " my_m      =        (parseInt(val_m, 10 )).toString(10).padStart(2, '0'); "
  125.       " document.getElementById('text_m').value = my_m; "
  126.       " var val_s = document.getElementById('range_s').value; "
  127.       " my_s      =        (parseInt(val_s, 10 )).toString(10).padStart(1, '0'); "
  128.       " document.getElementById('text_s').value = my_s; "
  129.       " updateStrip(parseInt(val_r, 10), parseInt(val_g, 10), parseInt(val_b, 10), parseInt(val_m, 10), parseInt(val_s, 10)); "
  130.      " } "
  131.      " function changedText(range_id, text_id, my_base) { "
  132.       " document.getElementById(range_id).value = parseInt(document.getElementById(text_id).value, my_base); "
  133.       " updateSummaryColor(); "
  134.      " } "
  135.      " function changedText_a() { "
  136.       " var text_a = document.getElementById('text_a'); "
  137.       " document.getElementById('range_r').value = parseInt(text_a.value.substring(0, 2), 16); "
  138.       " document.getElementById('range_g').value = parseInt(text_a.value.substring(2, 4), 16); "
  139.       " document.getElementById('range_b').value = parseInt(text_a.value.substring(4, 6), 16); "
  140.       " updateSummaryColor(); "
  141.      " } "
  142.      " function setMode(val_m) { "
  143.       " document.getElementById('range_m').value = val_m; "
  144.       " updateSummaryColor(); "
  145.      " } "
  146.      " // window.onload = updateSummaryColor; "
  147.     " </script> "
  148.     " <div style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center; display: grid; grid-template-columns: 100%; row-gap: 2%; column-gap: 1%;'> "
  149.      " <div style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center; display: grid; grid-template-columns: 7% 74% 17%; row-gap: 2%; column-gap: 1%;'> "
  150.       " <text id='rgb' name='rgb' style='height: 100%; width: 100%; margin: 0%; text-align: right; font-family: inherit; font-weight: inherit; background: #ffffff; color: #000000;'>RGB</text> "
  151.       " <button id='my_toggler' onclick='toggleDetails()' style='width: 100%; margin: 0%; font-family: inherit; font-weight: inherit;'> "
  152.        " <details id='my_details' onclick='toggleDetails()' style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center; font-family: inherit; font-weight: inherit; background: #ffffff; color: #000000;'> "
  153.         " <summary id='my_summary'>Palette</summary> "
  154.         " <iframe style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center;' srcdoc=\" "
  155.          " <body style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center;'> "
  156.           " <canvas id='colorspace' style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center;'></canvas> "
  157.          " </body> "
  158.          " <script type='text/javascript'> "
  159.           " (function () { "
  160.            " var canvas = document.getElementById('colorspace'); "
  161.            " var ctx = canvas.getContext('2d'); "
  162.            " function drawCanvas() { "
  163.             " var colours = ctx.createLinearGradient(0, 0, window.innerWidth, 0); "
  164.             " for (var i = 0; i <= 360; i += 10) { "
  165.              " colours.addColorStop((i/360), 'hsl(' + i + ', 100%, 50%)'); "
  166.             " } "
  167.             " ctx.fillStyle = colours; "
  168.             " ctx.fillRect(0, 0, window.innerWidth, window.innerHeight); "
  169.             " var luminance = ctx.createLinearGradient(0, 0, 0, ctx.canvas.height); "
  170.             " luminance.addColorStop(0,    '#ffffff'); "
  171.             " luminance.addColorStop(0.05, '#ffffff'); "
  172.             " luminance.addColorStop(0.5,  'rgba(0,0,0,0)'); "
  173.             " luminance.addColorStop(0.95, '#000000'); "
  174.             " luminance.addColorStop(1,    '#000000'); "
  175.             " ctx.fillStyle = luminance; "
  176.             " ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); "
  177.            " } "
  178.            " var eventLocked = false; "
  179.            " function handleEvent(clientX, clientY) { "
  180.             " if (eventLocked) { "
  181.              " return; "
  182.             " } "
  183.             " function colourCorrect(v) { "
  184.              " return Math.round(1023 - ((v * v)/64)); "
  185.             " } "
  186.             " var data = ctx.getImageData(clientX, clientY, 1, 1).data; "
  187.             " my_r     =        (parseInt(data[0], 10 )).toString(16).padStart(2, '0'); "
  188.             " my_r_inv = (255 - (parseInt(data[0], 10))).toString(16).padStart(2, '0'); "
  189.             " my_g     =        (parseInt(data[1], 10 )).toString(16).padStart(2, '0'); "
  190.             " my_g_inv = (255 - (parseInt(data[1], 10))).toString(16).padStart(2, '0'); "
  191.             " my_b     =        (parseInt(data[2], 10 )).toString(16).padStart(2, '0'); "
  192.             " my_b_inv = (255 - (parseInt(data[2], 10))).toString(16).padStart(2, '0'); "
  193.             " function drawCoordinates(x,y) { "
  194.              " var ctx = document.getElementById('colorspace').getContext('2d'); "
  195.              " var pointSize = 3; "
  196.              " ctx.fillStyle = '#' + my_r_inv + my_g_inv + my_b_inv; "
  197.              " ctx.beginPath(); "
  198.              " ctx.arc(x, y, pointSize, 0, Math.PI * 2, true); "
  199.              " ctx.fill(); "
  200.             " } "
  201.             " drawCanvas(); "
  202.             " drawCoordinates(clientX, clientY); "
  203.             " var my_details = parent.document.getElementById('my_details'); "
  204.             " my_details.style.background = '#' + my_r     + my_g     + my_b; "
  205.             " my_details.style.color      = '#' + my_r_inv + my_g_inv + my_b_inv; "
  206.             " function updateSliders(val_r, val_g, val_b) { "
  207.              " parent.document.getElementById('range_r').value = val_r; "
  208.              " parent.document.getElementById('range_g').value = val_g; "
  209.              " parent.document.getElementById('range_b').value = val_b; "
  210.              " parent.document.getElementById('text_r').value = (parseInt(val_r, 10 )).toString(16).padStart(2, '0'); "
  211.              " parent.document.getElementById('text_g').value = (parseInt(val_g, 10 )).toString(16).padStart(2, '0'); "
  212.              " parent.document.getElementById('text_b').value = (parseInt(val_b, 10 )).toString(16).padStart(2, '0'); "
  213.              " parent.document.getElementById('text_a').value = parent.document.getElementById('text_r').value + "
  214.              "                                                  parent.document.getElementById('text_g').value + "
  215.              "                                                  parent.document.getElementById('text_b').value; "
  216.             " } "
  217.             " updateSliders(parseInt(data[0], 10), parseInt(data[1], 10), parseInt(data[2], 10)); "
  218.             " var params = [ "
  219.              " 'r=' + colourCorrect(data[0]), "
  220.              " 'g=' + colourCorrect(data[1]), "
  221.              " 'b=' + colourCorrect(data[2]), "
  222.              " 'm=' + parseInt(parent.document.getElementById('range_m').value, 10), "
  223.              " 's=' + parseInt(parent.document.getElementById('range_s').value, 10) "
  224.              " ].join('&'); "
  225.             " var req = new XMLHttpRequest(); "
  226.             " req.open('POST', '?' + params, true); "
  227.             " req.send(); "
  228.             " eventLocked = true; "
  229.             " req.onreadystatechange = function() { "
  230.              " if (req.readyState == 4) { "
  231.               " eventLocked = false; "
  232.              " } "
  233.             " } "
  234.            " } "
  235.            " canvas.addEventListener('click', function(event) { "
  236.             " var rect = canvas.getBoundingClientRect(); "
  237.             " var my_x = event.clientX - rect.left; "
  238.             " var my_y = event.clientY - rect.top; "
  239.             " handleEvent(my_x, my_y); "
  240.             " }, false); "
  241.            " canvas.addEventListener('touchmove', function(event) { "
  242.             " var rect = canvas.getBoundingClientRect(); "
  243.             " var my_x = event.touches[0].clientX - rect.left; "
  244.             " var my_y = event.touches[0].clientY - rect.top; "
  245.             " handleEvent(my_x, my_y); "
  246.             " }, false); "
  247.            " function resizeCanvas() { "
  248.             " var rect = canvas.getBoundingClientRect(); "
  249.             " canvas.width  = window.innerWidth - rect.left; "
  250.             " canvas.height = window.innerHeight - rect.top - 4; "
  251.             " drawCanvas(); "
  252.            " } "
  253.            " window.addEventListener('resize', resizeCanvas, false); "
  254.            " resizeCanvas(); "
  255.            " document.ontouchmove = function(e) { e.preventDefault() }; "
  256.           " })(); "
  257.          " </script> "
  258.          " \"> "
  259.         " </iframe> "
  260.        " </details> "
  261.       " </button> "
  262.       " <input type='text'  id='text_a'  name='text_a' size='6' maxlength='6' value='ffffff' onchange='changedText_a();'                      style='height:  75%; margin: 0%; text-align: center; font-family: inherit; font-weight: inherit;'> "
  263.       " <text id='r__'    name='r__'    style='height: 100%; width: 100%; margin: 0%; text-align: right; font-family: inherit; font-weight: inherit; background: #ffffff; color: #ff0000;'>RED</text> "
  264.       " <input type='range' id='range_r' name='range_r' min='0' max='255'     value='255'    onchange='updateSummaryColor();'                 style='height: 100%; margin: 0%; display: flex; flex-grow: 1; flex-shrink: 1; flex-basis: auto;'> "
  265.       " <input type='text'  id='text_r'  name='text_r' size='2' maxlength='2' value='ff'     onchange='changedText(\"range_r\", \"text_r\", 16);' style='height:  75%; margin: 0%; text-align: center; font-family: inherit; font-weight: inherit;'> "
  266.       " <text id='_g_'    name='_g_'    style='height: 100%; width: 100%; margin: 0%; text-align: right; font-family: inherit; font-weight: inherit; background: #ffffff; color: #00ff00;'>GRE</text> "
  267.       " <input type='range' id='range_g' name='range_g' min='0' max='255'     value='255'    onchange='updateSummaryColor();'                 style='height: 100%; margin: 0%; display: flex; flex-grow: 1; flex-shrink: 1; flex-basis: auto;'> "
  268.       " <input type='text'  id='text_g'  name='text_g' size='2' maxlength='2' value='ff'     onchange='changedText(\"range_g\", \"text_g\", 16);' style='height:  75%; margin: 0%; text-align: center; font-family: inherit; font-weight: inherit;'> "
  269.       " <text id='__b'    name='__b'    style='height: 100%; width: 100%; margin: 0%; text-align: right; font-family: inherit; font-weight: inherit; background: #ffffff; color: #0000ff;'>BLU</text> "
  270.       " <input type='range' id='range_b' name='range_b' min='0' max='255'     value='255'    onchange='updateSummaryColor();'                 style='height: 100%; margin: 0%; display: flex; flex-grow: 1; flex-shrink: 1; flex-basis: auto;'> "
  271.       " <input type='text'  id='text_b'  name='text_b' size='2' maxlength='2' value='ff'     onchange='changedText(\"range_b\", \"text_b\", 16);' style='height:  75%; margin: 0%; text-align: center; font-family: inherit; font-weight: inherit;'> "
  272.       " <text id='mod'    name='mod'    style='height: 100%; width: 100%; margin: 0%; text-align: right; font-family: inherit; font-weight: inherit; background: #ffffff; color: #000000;'>MOD</text> "
  273.       " <input type='range' id='range_m' name='range_m' min='0' max='55'      value='0'      onchange='updateSummaryColor();'                 style='height: 100%; margin: 0%; display: flex; flex-grow: 1; flex-shrink: 1; flex-basis: auto;'> "
  274.       " <input type='text'  id='text_m'  name='text_m' size='2' maxlength='2' value='00'     onchange='changedText(\"range_m\", \"text_m\", 10);' style='height:  75%; margin: 0%; text-align: center; font-family: inherit; font-weight: inherit;'> "
  275.       " <text id='spd'    name='spd'    style='height: 100%; width: 100%; margin: 0%; text-align: right; font-family: inherit; font-weight: inherit; background: #ffffff; color: #000000;'>SPD</text> "
  276.       " <input type='range' id='range_s' name='range_s' min='0' max='9'       value='4'      onchange='updateSummaryColor();'                 style='height: 100%; margin: 0%; display: flex; flex-grow: 1; flex-shrink: 1; flex-basis: auto;'> "
  277.       " <input type='text'  id='text_s'  name='text_s' size='2' maxlength='1' value='4'      onchange='changedText(\"range_s\", \"text_s\", 10);' style='height:  75%; margin: 0%; text-align: center; font-family: inherit; font-weight: inherit;'> "
  278.       " <div></div> "
  279.       " <div> "
  280.        " <button id='effect_00' onclick='setMode(00)' style='height: 100%; width: 100%; margin: 0%; font-family: inherit; font-weight: inherit;'>00. STATIC</button> "
  281.       " </div> "
  282.       " <div></div> "
  283.       " <div></div> "
  284.       " <div><pre></pre></div> "
  285.       " <div></div> "
  286.      " </div> "
  287.      " <div style='height: 100%; width: 100%; margin: 0%; align-items: center; justify-content: center; display: flex; flex-grow: 1; flex-shrink: 1; flex-basis: auto; flex-direction: column; overflow-y: scroll;'> "
  288.       " <div style='height: 100%; width: 100%; margin: 0%; justify-content: center; display: grid; grid-template-columns: 19% 19% 19% 19% 19%; row-gap: 1%; column-gap: 1%;'> "
  289.        " <button id='effect_01' onclick='setMode(01)' style='font-family: inherit; font-weight: inherit;'>01. Blink</button> "
  290.        " <button id='effect_02' onclick='setMode(02)' style='font-family: inherit; font-weight: inherit;'>02. Breath</button> "
  291.        " <button id='effect_03' onclick='setMode(03)' style='font-family: inherit; font-weight: inherit;'>03. Color Wipe</button> "
  292.        " <button id='effect_04' onclick='setMode(04)' style='font-family: inherit; font-weight: inherit;'>04. Color Wipe Inverse</button> "
  293.        " <button id='effect_05' onclick='setMode(05)' style='font-family: inherit; font-weight: inherit;'>05. Color Wipe Reverse</button> "
  294.        " <button id='effect_06' onclick='setMode(06)' style='font-family: inherit; font-weight: inherit;'>06. Color Wipe IRverse</button> "
  295.        " <button id='effect_07' onclick='setMode(07)' style='font-family: inherit; font-weight: inherit;'>07. Color Wipe Random</button> "
  296.        " <button id='effect_08' onclick='setMode(08)' style='font-family: inherit; font-weight: inherit;'>08. Random Color</button> "
  297.        " <button id='effect_09' onclick='setMode(09)' style='font-family: inherit; font-weight: inherit;'>09. Single Dynamic</button> "
  298.        " <button id='effect_10' onclick='setMode(10)' style='font-family: inherit; font-weight: inherit;'>10. Multi Dynamic</button> "
  299.        " <button id='effect_11' onclick='setMode(11)' style='font-family: inherit; font-weight: inherit;'>11. Rainbow</button> "
  300.        " <button id='effect_12' onclick='setMode(12)' style='font-family: inherit; font-weight: inherit;'>12. Rainbow Cycle</button> "
  301.        " <button id='effect_13' onclick='setMode(13)' style='font-family: inherit; font-weight: inherit;'>13. Scan</button> "
  302.        " <button id='effect_14' onclick='setMode(14)' style='font-family: inherit; font-weight: inherit;'>14. Dual Scan</button> "
  303.        " <button id='effect_15' onclick='setMode(15)' style='font-family: inherit; font-weight: inherit;'>15. Fade</button> "
  304.        " <button id='effect_16' onclick='setMode(16)' style='font-family: inherit; font-weight: inherit;'>16. Theater Chase</button> "
  305.        " <button id='effect_17' onclick='setMode(17)' style='font-family: inherit; font-weight: inherit;'>17. Theater Chase Rainbow</button> "
  306.        " <button id='effect_18' onclick='setMode(18)' style='font-family: inherit; font-weight: inherit;'>18. Running Lights</button> "
  307.        " <button id='effect_19' onclick='setMode(19)' style='font-family: inherit; font-weight: inherit;'>19. Twinkle</button> "
  308.        " <button id='effect_20' onclick='setMode(20)' style='font-family: inherit; font-weight: inherit;'>20. Twinkle Random</button> "
  309.        " <button id='effect_21' onclick='setMode(21)' style='font-family: inherit; font-weight: inherit;'>21. Twinkle Fade</button> "
  310.        " <button id='effect_22' onclick='setMode(22)' style='font-family: inherit; font-weight: inherit;'>22. Twinkle Fade Random</button> "
  311.        " <button id='effect_23' onclick='setMode(23)' style='font-family: inherit; font-weight: inherit;'>23. Sparkle</button> "
  312.        " <button id='effect_24' onclick='setMode(24)' style='font-family: inherit; font-weight: inherit;'>24. Flash Sparkle</button> "
  313.        " <button id='effect_25' onclick='setMode(25)' style='font-family: inherit; font-weight: inherit;'>25. Hyper Sparkle</button> "
  314.        " <button id='effect_26' onclick='setMode(26)' style='font-family: inherit; font-weight: inherit;'>26. Strobe</button> "
  315.        " <button id='effect_27' onclick='setMode(27)' style='font-family: inherit; font-weight: inherit;'>27. Strobe Rainbow</button> "
  316.        " <button id='effect_28' onclick='setMode(28)' style='font-family: inherit; font-weight: inherit;'>28. Multi Strobe</button> "
  317.        " <button id='effect_29' onclick='setMode(29)' style='font-family: inherit; font-weight: inherit;'>29. Blink Rainbow</button> "
  318.        " <button id='effect_30' onclick='setMode(30)' style='font-family: inherit; font-weight: inherit;'>30. Chase White</button> "
  319.        " <button id='effect_31' onclick='setMode(31)' style='font-family: inherit; font-weight: inherit;'>31. Chase Color</button> "
  320.        " <button id='effect_32' onclick='setMode(32)' style='font-family: inherit; font-weight: inherit;'>32. Chase Random</button> "
  321.        " <button id='effect_33' onclick='setMode(33)' style='font-family: inherit; font-weight: inherit;'>33. Chase Rainbow</button> "
  322.        " <button id='effect_34' onclick='setMode(34)' style='font-family: inherit; font-weight: inherit;'>34. Chase Flash</button> "
  323.        " <button id='effect_35' onclick='setMode(35)' style='font-family: inherit; font-weight: inherit;'>35. Chase Flash Random</button> "
  324.        " <button id='effect_36' onclick='setMode(36)' style='font-family: inherit; font-weight: inherit;'>36. Chase Rainbow White</button> "
  325.        " <button id='effect_37' onclick='setMode(37)' style='font-family: inherit; font-weight: inherit;'>37. Chase Blackout</button> "
  326.        " <button id='effect_38' onclick='setMode(38)' style='font-family: inherit; font-weight: inherit;'>38. Chase Blackout Rainbow</button> "
  327.        " <button id='effect_39' onclick='setMode(39)' style='font-family: inherit; font-weight: inherit;'>39. Color Sweep Random</button> "
  328.        " <button id='effect_40' onclick='setMode(40)' style='font-family: inherit; font-weight: inherit;'>40. Running Color</button> "
  329.        " <button id='effect_41' onclick='setMode(41)' style='font-family: inherit; font-weight: inherit;'>41. Running Red Blue</button> "
  330.        " <button id='effect_42' onclick='setMode(42)' style='font-family: inherit; font-weight: inherit;'>42. Running Random</button> "
  331.        " <button id='effect_43' onclick='setMode(43)' style='font-family: inherit; font-weight: inherit;'>43. Larson Scanner</button> "
  332.        " <button id='effect_44' onclick='setMode(44)' style='font-family: inherit; font-weight: inherit;'>44. Comet</button> "
  333.        " <button id='effect_45' onclick='setMode(45)' style='font-family: inherit; font-weight: inherit;'>45. Fireworks</button> "
  334.        " <button id='effect_46' onclick='setMode(46)' style='font-family: inherit; font-weight: inherit;'>46. Fireworks Random</button> "
  335.        " <button id='effect_47' onclick='setMode(47)' style='font-family: inherit; font-weight: inherit;'>47. Merry Christmas</button> "
  336.        " <button id='effect_48' onclick='setMode(48)' style='font-family: inherit; font-weight: inherit;'>48. Fire Flicker</button> "
  337.        " <button id='effect_49' onclick='setMode(49)' style='font-family: inherit; font-weight: inherit;'>49. Fire Flicker Soft</button> "
  338.        " <button id='effect_50' onclick='setMode(50)' style='font-family: inherit; font-weight: inherit;'>50. Fire Flicker Intense</button> "
  339.        " <button id='effect_51' onclick='setMode(51)' style='font-family: inherit; font-weight: inherit;'>51. Circus Combustus</button> "
  340.        " <button id='effect_52' onclick='setMode(52)' style='font-family: inherit; font-weight: inherit;'>52. Halloween</button> "
  341.        " <button id='effect_53' onclick='setMode(53)' style='font-family: inherit; font-weight: inherit;'>53. 2-color Chase</button> "
  342.        " <button id='effect_54' onclick='setMode(54)' style='font-family: inherit; font-weight: inherit;'>54. 3-color Chase</button> "
  343.        " <button id='effect_55' onclick='setMode(55)' style='font-family: inherit; font-weight: inherit;'>55. Twinkle Fox</button> "
  344.       " </div> "
  345.      " </div> "
  346.     " </div> "
  347.    " </body> "
  348.   " </html> ";
  349. //////////////////////////////////////////////////////////////////////////////////////////////////
  350.  
  351. static uint32_t strip_color(uint8_t r, uint8_t g, uint8_t b)
  352. {
  353.   return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
  354. }
  355.  
  356. void serial_printhex(uint32_t num, uint16_t digits)
  357. {
  358.   if ((digits >= 3) && (num < 0x100)) {
  359.     Serial.print("0");
  360.   }
  361.   if ((digits >= 2) && (num < 0x10)) {
  362.     Serial.print("0");
  363.   }
  364.   Serial.print(num, HEX);
  365. }
  366.  
  367. void strip_setPixelColor(uint32_t n, uint8_t r, uint8_t g, uint8_t b)
  368. {
  369.   Serial.print("[");
  370.   serial_printhex(n, 3);
  371.   Serial.print("_");
  372.   serial_printhex(r, 2);
  373.   serial_printhex(g, 2);
  374.   serial_printhex(b, 2);
  375.   Serial.print("]");
  376.   return;
  377. }
  378.  
  379. void strip_setStripColor(uint8_t r, uint8_t g, uint8_t b)
  380. {
  381.   Serial.print("[^^^_");
  382.   serial_printhex(r, 2);
  383.   serial_printhex(g, 2);
  384.   serial_printhex(b, 2);
  385.   Serial.print("]");
  386.   return;
  387. }
  388.  
  389. void strip_show(uint8_t mod, uint8_t spd)
  390. {
  391.   switch(mod)
  392.   {
  393.           /**** ASCII: 32 - 47 *****/
  394.           /***  !"#$%&'()*+,-./ ****/
  395.       case  0 ... 15 :
  396.         Serial.print((char)( 32 + mod));
  397.         break;
  398.           /**** ASCII: 71 - 90 *****/
  399.           /* GHIJKLMNOPQRSTUVWXYZ **/
  400.       case 16 ... 35 :
  401.         Serial.print((char)( 55 + mod));
  402.         break;
  403.           /*** ASCII: 103 - 123 ****/
  404.           /* ghijklmnopqrstuvwxyz{ */
  405.       case 36 ... 56 :
  406.         Serial.print((char)( 67 + mod));
  407.         break;
  408.           /******* ASCII: 32 *******/
  409.           /***********   ***********/
  410.       default:
  411.         Serial.print(" ");
  412.   }
  413.   switch(spd)
  414.   {
  415.           /**** ASCII: 58 - 64 ****/
  416.           /******* :;<=>?@ ********/
  417.       case  0 ... 6 :
  418.         Serial.print((char)( 58 + spd));
  419.         break;
  420.           /*** ASCII: 124 - 126 ***/
  421.           /********* |}~ **********/
  422.       case  7 ... 9 :
  423.         Serial.print((char)(117 + spd));
  424.         break;
  425.       default:
  426.         break;
  427.   }
  428.   return;
  429. }
  430.  
  431. void strip_setPixelColor(uint32_t n, uint32_t c)
  432. {
  433.   uint8_t r = (uint8_t)(c >> 16);
  434.   uint8_t g = (uint8_t)(c >> 8);
  435.   uint8_t b = (uint8_t)c;
  436.   strip_setPixelColor(n, r, g, b);
  437. }
  438.  
  439. void handleRGB()
  440. {
  441.   // Serial.println("handle RGB..");
  442.  
  443.   webServer.send(200, "text/html", webpageRGB);
  444.  
  445.   String red   = webServer.arg(0); // read RGB arguments
  446.   String green = webServer.arg(1);
  447.   String blue  = webServer.arg(2);
  448.  
  449.   uint8_t mod  = webServer.arg(3).toInt();
  450.   uint8_t spd  = webServer.arg(4).toInt();
  451.  
  452.   allRGB(255 - red.toInt() / 4, 255 - green.toInt() / 4, 255 - blue.toInt() / 4, mod, spd);
  453. }
  454.  
  455. //////////////////////////////////////////////////////////////////////////////////////////////////
  456.  //////////////////////////////////////////////////////////////////////////////////////////////////
  457.  //////////////////////////////////////////////////////////////////////////////////////////////////
  458.  
  459. void setup()
  460. {
  461.  /*
  462.   * It is recommended to use the lower baud rate. The higher the baud rate,
  463.   * the higher the bit error rate will be, and the control action might fail.
  464.   */
  465.   Serial.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  466.  
  467.   /* STRIP */// strip.begin();
  468.   strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE); // Initialize all pixels to 'off'
  469.   /* STRIP */// strip.setBrightness(250); // 0-255 range
  470.  
  471.   animation = false;
  472.  
  473.   if (!animation) { // start webserver
  474.     allRGB(0, 0, 0, MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  475.     delay(1000);
  476.     // Serial.println("Starting webserver...");
  477.  
  478.     WiFi.mode(WIFI_AP);
  479.     WiFi.softAPConfig(local_ip, gateway, subnet);
  480.     WiFi.softAP(ssid, password);
  481.  
  482.     // if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS request
  483.     dnsServer.start(DNS_PORT, "esp", local_ip);
  484.     webServer.on("/", handleRGB);
  485.  
  486.     webServer.begin();
  487.  
  488.     introRed(); // knightrider
  489.     // colorWipe(0x859d12, 5);
  490.     // rainbow(5);
  491.     // rainbowCycle(5);
  492.     // theaterChaseRainbow(5);
  493.     // theaterChase(0x7e1ae1, 5);
  494.     // colorWipe(0x123456, 5);
  495.  
  496.     //// strip_setPixelColor(0, 255, 0, 0);
  497.     //// strip_setPixelColor(3, 255, 0, 0);
  498.     //// strip_setPixelColor(4, 255, 0, 0);
  499.     //// strip_setPixelColor(7, 255, 0, 0);
  500.     strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  501. /*
  502.  * CURRENT LOG:
  503.  * CLEAR_ALL, CLEAR_ALL + 0 LED RED, CLEAR_ALL + 1 LED RED, ..., CLEAR_ALL + 7 LED RED,
  504.  * CLEAR_ALL + 6 LED RED, ..., CLEAR_ALL + 0 LED RED, ..., CLEAR_ALL + 7 LED RED, ...,
  505.  * CLEAR_ALL + 1 LED RED, CLEAR_ALL, 8 LED RED (???), 0 + 3 + 4 + 7 LED RED
  506.  */
  507.   }
  508.   else {
  509.     WiFi.disconnect();
  510.     WiFi.softAPdisconnect(true);
  511.     WiFi.mode(WIFI_STA);
  512.     // Serial.println("Starting animation...");
  513.   }
  514. }
  515.  
  516. //////////////////////////////////////////////////////////////////////////////////////////////////
  517.  
  518. void loop()
  519. {
  520.   if (animation) {
  521.     rainbow(20);
  522.     rainbowCycle(20);
  523.     theaterChaseRainbow(50);
  524.   }
  525.   else {
  526.     dnsServer.processNextRequest();
  527.     webServer.handleClient();
  528.   }
  529. }
  530.  //////////////////////////////////////////////////////////////////////////////////////////////////
  531.  /////////////////////// NEOPIXEL FUNCTIONS //////////////////////////////////////////////////
  532.  //////////////////////////////////////////////////////////////////////////////////////////////////
  533.  
  534. // Fill the dots one after the other with a color
  535. void colorWipe(uint32_t c, uint8_t wait)
  536. {
  537.   uint32_t i;
  538.  
  539.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  540.     strip_setPixelColor(i, c);
  541.     strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  542.     delay(wait);
  543.   }
  544. }
  545.  
  546. void rainbow(uint8_t wait)
  547. {
  548.   uint32_t i;
  549.   uint16_t j;
  550.  
  551.   for (j = 0; j < 256; j++) {
  552.     for (i = 0; i < STRIP_NUMPIXELS; i++) {
  553.       strip_setPixelColor(i, Wheel((i + j) & 255));
  554.     }
  555.     strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  556.     delay(wait);
  557.   }
  558. }
  559.  
  560. // Slightly different, this makes the rainbow equally distributed throughout
  561. void rainbowCycle(uint8_t wait)
  562. {
  563.   uint32_t i;
  564.   uint16_t j;
  565.  
  566.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  567.     for (i = 0; i < STRIP_NUMPIXELS; i++) {
  568.       strip_setPixelColor(i, Wheel(((i * 256 / STRIP_NUMPIXELS) + j) & 255));
  569.     }
  570.     strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  571.     delay(wait);
  572.   }
  573. }
  574.  
  575. //Theatre-style crawling lights.
  576. void theaterChase(uint32_t c, uint8_t wait)
  577. {
  578.   uint32_t i;
  579.   uint16_t j, q;
  580.  
  581.   for (j = 0; j < 10; j++) { //do 10 cycles of chasing
  582.     for (q = 0; q < 3; q++) {
  583.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  584.         strip_setPixelColor(i + q, c); //turn every third pixel on
  585.       }
  586.       strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  587.  
  588.       delay(wait);
  589.  
  590.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  591.         strip_setPixelColor(i + q, 0); //turn every third pixel off
  592.       }
  593.     }
  594.   }
  595. }
  596.  
  597. //Theatre-style crawling lights with rainbow effect
  598. void theaterChaseRainbow(uint8_t wait)
  599. {
  600.   uint32_t i;
  601.   uint16_t j, q;
  602.  
  603.   for (j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
  604.     for (q = 0; q < 3; q++) {
  605.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  606.         strip_setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
  607.       }
  608.       strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  609.  
  610.       delay(wait);
  611.  
  612.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  613.         strip_setPixelColor(i + q, 0); //turn every third pixel off
  614.       }
  615.     }
  616.   }
  617. }
  618.  
  619. // Input a value 0 to 255 to get a color value.
  620. // The colours are a transition r - g - b - back to r.
  621. uint32_t Wheel(byte WheelPos)
  622. {
  623.   WheelPos = 255 - WheelPos;
  624.   if (WheelPos < 85) {
  625.     return strip_color(255 - WheelPos * 3, 0, WheelPos * 3);
  626.   }
  627.   if (WheelPos < 170) {
  628.     WheelPos -= 85;
  629.     return strip_color(0, WheelPos * 3, 255 - WheelPos * 3);
  630.   }
  631.   WheelPos -= 170;
  632.   return strip_color(WheelPos * 3, 255 - WheelPos * 3, 0);
  633. }
  634.  
  635. //////////////////////////////// TOM NEOPIXEL //////////////////////////////////////////////////
  636.  
  637. void introRed()
  638. {
  639.   uint32_t i;
  640.   uint16_t u;
  641.  
  642.   for (u = 0; u < 3; u++) {
  643.     for (i = 0; i < STRIP_NUMPIXELS; i++) {
  644.       oneRed(i);
  645.       delay(100);
  646.     }
  647.     for (i = STRIP_NUMPIXELS - 2; i > 0 ; i--) {
  648.       oneRed(i);
  649.       delay(100);
  650.     }
  651.   }
  652.   /* STRIP */// oneRed(STRIP_NUMPIXELS); // switch all off (switch on only 8, but beyond range)
  653.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  654.     strip_setPixelColor(i, 0, 0, 0);
  655.   }
  656. }
  657.  
  658. void oneRed(uint32_t u)
  659. {
  660.   uint32_t i;
  661.  
  662.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  663.     strip_setPixelColor(i, 0, 0, 0);
  664.   }
  665.   strip_setPixelColor(u, 255, 0, 0);
  666.   strip_show(MODE_INDIVIDUAL, NO_SPEED_CHANGE);
  667. }
  668.  
  669. void allRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t mod, uint8_t spd)
  670. {
  671.   uint32_t i;
  672.   /*
  673.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  674.     strip_setPixelColor(i, r, g, b);
  675.   }
  676.   */
  677.   strip_setStripColor(r, g, b);
  678.   strip_show(mod, spd);
  679. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement