Advertisement
GyroGearloose

ESP_Color_Palette_and_Noise_NOW_w_SPEED

Feb 29th, 2016
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.77 KB | None | 0 0
  1. /* Garrett Durland
  2.    https://www.youtube.com/watch?time_continue=2&v=3zQ8qC1KQgU
  3.  
  4.    Additions:
  5.    - Added SPEED slider
  6.    - Took out long variable names [FUNTCION1ON] are now [F_1] to avoid serial character overflow
  7.    - Cleaned up the code for better readability
  8.    - Gyro Gearloose, Feb. 29, 2016
  9.  
  10. */
  11.  
  12. #include "FastLED.h"
  13. FASTLED_USING_NAMESPACE
  14.  
  15. #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
  16. #warning "Requires FastLED 3.1 or later; check github for latest code."
  17. #endif
  18.  
  19. #define LED_PIN     4
  20. #define CLOCK_PIN   5
  21. #define LED_TYPE    APA102
  22. #define COLOR_ORDER BGR
  23. #define MILLI_AMPERE      2000    // IMPORTANT: set here the max milli-Amps of your power supply 5V 2A = 2000
  24. int FRAMES_PER_SECOND   = 100;    // here you can control the speed.
  25. int ledMode = 4;                  // this is the starting palette
  26. const uint8_t kMatrixWidth  = 40;  
  27. const uint8_t kMatrixHeight =  9;  
  28. const bool    kMatrixSerpentineLayout = true;
  29. int BRIGHTNESS =           128;   // this is half brightness
  30. int new_BRIGHTNESS =       128;   // shall be initially the same as brightness
  31. int SPEED          =       100;   // around half speed
  32. int new_SPEED      =       100;
  33.  
  34. #include <ESP8266WiFi.h>
  35. // comes with Huzzah installation. Enter in Arduino settings:
  36. // http://arduino.esp8266.com/package_esp8266com_index.json
  37.  
  38. //!!!!!!!!!!!!!!!!!
  39. //#define USE_AP //   <- select here by commenting/uncommenting
  40. //!!!!!!!!!!!!!!!!!
  41.  
  42. #ifdef USE_AP
  43. // const char* ssid = "ESP_FastLED_Access_Point";
  44. // const char* password = "";  // set to "" for open access point w/o password; or any other pw (min length = 8 characters)
  45.  
  46. #else
  47.  const char* ssid = "your WLAN Name here";
  48.  const char* password = "Your password here";
  49. #endif
  50.  
  51. unsigned long ulReqcount;
  52. unsigned long ulReconncount;
  53.  
  54. WiFiServer server(80);  // Create an instance of the server on Port 80
  55.  
  56. // This example combines two features of FastLED to produce a remarkable range of
  57. // effects from a relatively small amount of code.  This example combines FastLED's
  58. // color palette lookup functions with FastLED's Perlin/simplex noise generator, and
  59. // the combination is extremely powerful.
  60. //
  61. // You might want to look at the "ColorPalette" and "Noise" examples separately
  62. // if this example code seems daunting.
  63. //
  64. //
  65. // The basic setup here is that for each frame, we generate a new array of
  66. // 'noise' data, and then map it onto the LED matrix through a color palette.
  67. //
  68. // Periodically, the color palette is changed, and new noise-generation parameters
  69. // are chosen at the same time.  In this example, specific noise-generation
  70. // values have been selected to match the given color palettes; some are faster,
  71. // or slower, or larger, or smaller than others, but there's no reason these
  72. // parameters can't be freely mixed-and-matched.
  73. //
  74. // In addition, this example includes some fast automatic 'data smoothing' at
  75. // lower noise speeds to help produce smoother animations in those cases.
  76. //
  77. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  78. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  79. // palettes.
  80.  
  81.  
  82. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  83. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  84.  
  85. // The leds
  86. CRGB leds[kMatrixWidth * kMatrixHeight];
  87.  
  88. // The 16 bit version of our coordinates
  89. static uint16_t x;
  90. static uint16_t y;
  91. static uint16_t z;
  92.  
  93. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  94. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  95. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  96. // water.
  97. uint16_t speed = 20; // speed is set dynamically once we've started up
  98.  
  99. // Scale determines how far apart the pixels in our noise matrix are.  Try
  100. // changing these values around to see how it affects the motion of the display.  The
  101. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  102. // of 1 will be so zoomed in, you'll mostly see solid colors.
  103. uint16_t scale = 30; // scale is set dynamically once we've started up
  104.  
  105. // This is the array that we keep our computed noise values in
  106. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  107.  CRGBPalette16 currentPalette( CRGB::Black );
  108.  
  109.  CRGBPalette16 targetPalette( CRGB::Black );
  110. uint8_t       colorLoop = 1;
  111.  
  112. void setup() {
  113.   ulReqcount=0;         // setup globals for Webserver
  114.   ulReconncount=0;
  115.   // Initialize our coordinates to some random values
  116.   x = random16();
  117.   y = random16();
  118.   z = random16();
  119.   Serial.begin(9600);
  120.   delay(1);
  121.   // inital connect
  122.  
  123. #ifdef USE_AP
  124.   // AP mode
  125.   WiFi.mode(WIFI_AP);
  126.   // WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));          // if you want to configure another IP address
  127.   WiFi.softAP(ssid, password);
  128.   server.begin();
  129. // end ACCESS-Point setup ---------------------------------------------------------------------------------------------------
  130. #else
  131. // WEB SERVER setup ---------------------------------------------------------------------------------------------------------
  132.   // inital connect
  133.   WiFi.mode(WIFI_STA);
  134.   WiFiStart();
  135. // end WEB SERVER setup -----------------------------------------------------------------------------------------------------
  136. #endif
  137.  
  138. delay(1000); //safety delay
  139.   LEDS.addLeds<LED_TYPE,LED_PIN,CLOCK_PIN,COLOR_ORDER>(leds,NUM_LEDS).setCorrection(DirectSunlight);
  140.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, LED_COUNT).setCorrection(DirectSunlight);
  141.   FastLED.setBrightness(BRIGHTNESS);
  142.   set_max_power_in_volts_and_milliamps(5, MILLI_AMPERE);
  143. }
  144.  
  145. void WiFiStart()
  146. {
  147.  
  148.   // Connect to WiFi network
  149.   Serial.println();
  150.   Serial.println();
  151.   Serial.print("Connecting to ");
  152.   Serial.println(ssid);
  153.  
  154.   WiFi.begin(ssid, password);
  155.  
  156.   while (WiFi.status() != WL_CONNECTED) {
  157.     delay(500);
  158.     Serial.print(".");
  159.   }
  160.   Serial.println("");
  161.   Serial.println("WiFi connected");
  162.  
  163.   // Start the server
  164.   server.begin();
  165.   Serial.println("Server started");
  166.  
  167.   // Print the IP address
  168.   Serial.println(WiFi.localIP());
  169. }
  170.  
  171. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  172. void fillnoise8() {
  173.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  174.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  175.   // The amount of data smoothing we're doing depends on "speed".
  176.   uint8_t dataSmoothing = 0;
  177.   if( speed < 50) {
  178.     dataSmoothing = 200 - (speed * 4);
  179.   }
  180.  
  181.   for(int i = 0; i < MAX_DIMENSION; i++) {
  182.     int ioffset = scale * i;
  183.     for(int j = 0; j < MAX_DIMENSION; j++) {
  184.       int joffset = scale * j;
  185.      
  186.       uint8_t data = inoise8(x + ioffset,y + joffset,z);
  187.  
  188.       // The range of the inoise8 function is roughly 16-238.
  189.       // These two operations expand those values out to roughly 0..255
  190.       // You can comment them out if you want the raw noise data.
  191.       data = qsub8(data,16);
  192.       data = qadd8(data,scale8(data,39));
  193.  
  194.       if( dataSmoothing ) {
  195.         uint8_t olddata = noise[i][j];
  196.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  197.         data = newdata;
  198.       }
  199.      
  200.       noise[i][j] = data;
  201.     }
  202.   }
  203.  
  204.   z += speed;
  205.  
  206.   // apply slow drift to X and Y, just for visual variation.
  207.   x += speed / 8;
  208.   y -= speed / 16;
  209. }
  210.  
  211. void mapNoiseToLEDsUsingPalette()
  212. {
  213.   static uint8_t ihue=0;
  214.  
  215.   for(int i = 0; i < kMatrixWidth; i++) {
  216.     for(int j = 0; j < kMatrixHeight; j++) {
  217.       // We use the value at the (i,j) coordinate in the noise
  218.       // array for our brightness, and the flipped value from (j,i)
  219.       // for our pixel's index into the color palette.
  220.  
  221.       uint8_t index = noise[j][i];
  222.       uint8_t bri =   noise[i][j];
  223.  
  224.       // if this palette is a 'loop', add a slowly-changing base value
  225.       if( colorLoop) {
  226.         index += ihue;
  227.       }
  228.  
  229.       // brighten up, as the color palette itself often contains the
  230.       // light/dark dynamic range desired
  231.       if( bri > 127 ) {
  232.         bri = 255;
  233.       } else {
  234.         bri = dim8_raw( bri * 2);
  235.       }
  236.  
  237.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  238.       leds[XY(i,j)] = color;
  239.     }
  240.   }
  241.  
  242.   ihue+=1;
  243. }
  244.  
  245. void loop() {
  246.     webserver();
  247.   // Periodically choose a new palette, speed, and scale
  248.   ChangePaletteAndSettingsPeriodically();
  249.       // Crossfade current palette slowly toward the target palette
  250.     //
  251.     // Each time that nblendPaletteTowardPalette is called, small changes
  252.     // are made to currentPalette to bring it closer to matching targetPalette.
  253.     // You can control how many changes are made in each call:
  254.     // - the default of 24 is a good balance
  255.     // - meaningful values are 1-48. 1=veeeeeeeery slow, 48=quickest
  256.     // - "0" means do not change the currentPalette at all; freeze
  257. //  uint8_t maxChanges = 7;
  258. //  nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
  259.  
  260.   // generate noise data
  261.   fillnoise8();
  262.  
  263.   // convert the noise data to colors in the LED array
  264.   // using the current palette
  265.   mapNoiseToLEDsUsingPalette();
  266.  
  267.   show_at_max_brightness_for_power();
  268.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  269. }
  270.  
  271. void webserver() {   /// complete web server (same for access point) ////////////////////////////////////////////////////////
  272.   // Check if a client has connected
  273.   WiFiClient client = server.available();
  274.   if (!client)
  275.   {
  276.     return;
  277.   }
  278.  
  279.   // Wait until the client sends some data
  280.   Serial.println("new client");
  281.   unsigned long ultimeout = millis()+250;
  282.   while(!client.available() && (millis()<ultimeout) )
  283.   {
  284.     delay(1);
  285.   }
  286.   if(millis()>ultimeout)
  287.   {
  288.     Serial.println("client connection time-out!");
  289.     return;
  290.   }
  291.  
  292.   // Read the first line of the request
  293.  
  294.   String sRequest = client.readStringUntil('\r');
  295.   Serial.println(sRequest);
  296.   client.flush();
  297.  
  298.   // stop client, if request is empty
  299.   if(sRequest=="")
  300.   {
  301.     Serial.println("empty request! - stopping client");
  302.     client.stop();
  303.     return;
  304.   }
  305.  
  306.   // get path; end of path is either space or ?
  307.   // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
  308.   String sPath="",sParam="", sCmd="";
  309.   String sGetstart="GET ";
  310.   int iStart,iEndSpace,iEndQuest;
  311.   iStart = sRequest.indexOf(sGetstart);
  312.  
  313.  
  314.   if (iStart>=0)
  315.   {
  316.     iStart+=+sGetstart.length();
  317.     iEndSpace = sRequest.indexOf(" ",iStart);
  318.     iEndQuest = sRequest.indexOf("?",iStart);
  319.  
  320.     // are there parameters?
  321.     if(iEndSpace>0)
  322.     {
  323.       if(iEndQuest>0)
  324.       {
  325.         // there are parameters
  326.         sPath  = sRequest.substring(iStart,iEndQuest);
  327.         sParam = sRequest.substring(iEndQuest,iEndSpace);
  328.       }
  329.       else
  330.       {
  331.         // NO parameters
  332.         sPath  = sRequest.substring(iStart,iEndSpace);
  333.       }
  334.     }
  335.   }
  336.  
  337.   ///////////////////////////////////////////////////////////////////////////////
  338.   // output parameters to serial, you may connect e.g. an Arduino and react on it
  339.   ///////////////////////////////////////////////////////////////////////////////
  340.     if(sParam.length()>0)
  341.   { // output parameters
  342.     int iEqu=sParam.indexOf("=");
  343.     if(iEqu>=0)
  344.     {  // is there a message?
  345.       sCmd = sParam.substring(iEqu+1,sParam.length());
  346. //      Serial.print("We are in output Parameters, value is: ");
  347.  
  348.       int iEqu_bright=sParam.indexOf("200");
  349.       if (iEqu_bright>=0)
  350.        {
  351.         sCmd = sParam.substring(iEqu+1,sParam.length());         // BRIGHTNESS
  352.       Serial.println(sCmd);
  353.       char carray[4];                                // values 0..255 = 3 digits; array = digits + 1
  354.       sCmd.toCharArray(carray, sizeof(carray));      // convert char to the array
  355.       new_BRIGHTNESS = atoi(carray);                 // atoi() converts an ascii character array to an integer
  356.       if (new_BRIGHTNESS == 0) {new_BRIGHTNESS = BRIGHTNESS; }
  357.       BRIGHTNESS = new_BRIGHTNESS;
  358.          FastLED.setBrightness(new_BRIGHTNESS);
  359.       Serial.print("new Brightness: ");
  360.       Serial.println(new_BRIGHTNESS);
  361.     }
  362.  
  363.  // space for RGB (300, 400, 500)
  364.  
  365.        int iEqu_speed=sParam.indexOf("600");
  366.       if (iEqu_speed>=0)
  367.        {
  368.       sCmd = sParam.substring(iEqu+1,sParam.length());           // speed
  369. //      Serial.print("We are in SPEED, value is: ");
  370.       Serial.println(sCmd);
  371.       char carray[4];                                // values 0..255 = 3 digits; array = digits + 1
  372.       sCmd.toCharArray(carray, sizeof(carray));      // convert char to the array
  373.       new_SPEED = atoi(carray);                      // atoi() converts an ascii character array to an integer
  374.       if (new_SPEED == 0) {new_SPEED = SPEED; }      // if something else is selected (no change in brightness)
  375.       SPEED = new_SPEED;                
  376.       FRAMES_PER_SECOND = SPEED;
  377. //     FastLED.setMaxRefreshRate(new_SPEED);         // thought this would work  :(
  378.  
  379.       Serial.print("new FPS: ");
  380.       Serial.println(SPEED);
  381.  
  382.        }
  383.  
  384.     } // end is there a message?
  385.   }   // end output Parameters  
  386.  
  387.  
  388.   //////////////////////////////
  389.   // format the html response //
  390.   //////////////////////////////
  391.   String sResponse,sHeader;
  392.  
  393.   ///////////////////////////////
  394.   // 404 for non-matching path //
  395.   ///////////////////////////////
  396.   if(sPath!="/")
  397.   {
  398.     sResponse="<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>";
  399.    
  400.     sHeader  = "HTTP/1.1 404 Not found\r\n";
  401.     sHeader += "Content-Length: ";
  402.     sHeader += sResponse.length();
  403.     sHeader += "\r\n";
  404.     sHeader += "Content-Type: text/html\r\n";
  405.     sHeader += "Connection: close\r\n";
  406.     sHeader += "\r\n";
  407.   }
  408.   //////////////////////////
  409.   // format the html page //
  410.   //////////////////////////
  411.   else
  412.   {
  413.     ulReqcount++;
  414.     sResponse  = "<html><head><title>ESP_FastLED_Access_Point</title></head><body>";
  415. //    sResponse += "<font color=\"#FFFFF0\"><body bgclror=\"#000000\">";  
  416.     sResponse += "<font color=\"#FFFFF0\"><body bgcolor=\"#151B54\">";  
  417.     sResponse += "<FONT SIZE=-1>";
  418.     sResponse += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">";
  419.     sResponse += "<h1>ESP FastLED Noise Plus Palette</h1><br>";
  420.     sResponse += " <h2>Light Controller</h2>";
  421.  
  422. /*  this creates a list with ON / OFF buttons
  423.     // &nbsp is a non-breaking space; moves next character over
  424.     sResponse += "<p>Rainbow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION1ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION1OFF\"><button>--OFF--</button></a><br>";
  425.     sResponse += "<p>Rainbow Glitter<a href=\"?pin=FUNCTION2ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION2OFF\"><button>--OFF--</button></a><br>";
  426.     sResponse += "<p>Confetti &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION3ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION3OFF\"><button>--OFF--</button></a><br>";
  427.     sResponse += "<p>Sinelon &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION4ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION4OFF\"><button>--OFF--</button></a><br>";
  428.     sResponse += "<p>Juggle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION5ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION5OFF\"><button>--OFF--</button></a></p>";
  429.     sResponse += "<p>BPM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION6ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION6OFF\"><button>--OFF--</button></a></p>";
  430.     sResponse += "<p>Function 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION7ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION7OFF\"><button>--OFF--</button></a></p><br>";
  431. */
  432. //  This is a nice drop down menue
  433.     sResponse += "<FONT SIZE=+1>";
  434.     sResponse += "<form>";
  435. //    sResponse += "Select Animation<br>";
  436.     sResponse += "<p>Select:</p>";
  437.     sResponse += "<select name=\"sCmd\" size=\"10\" >";
  438.     sResponse += "<option value=\"F_0\">All OFF</option>";
  439.     sResponse += "<option value=\"F_1\">2 Random Colors</option>";
  440.     sResponse += "<option value=\"F_2\">3 Random Colors</option>";
  441.     sResponse += "<option value=\"F_3\">Purple & Green</option>";
  442.     sResponse += "<option value=\"F_4\"selected>Black & White</option>";
  443.     sResponse += "<option value=\"F_5\">Forest Colors</option>";
  444.     sResponse += "<option value=\"F_6\">Cloud Colors</option>";
  445.     sResponse += "<option value=\"F_7\">Lava Colors</option>";
  446.     sResponse += "<option value=\"F_8\">Ocean Colors</option>";
  447.     sResponse += "<option value=\"F_9\">Party Colors</option><br>";
  448.     sResponse += "</select>";
  449.     sResponse += "<br><br>";
  450.     sResponse += "<input type= submit value=\"Show the MAGIC !\">";
  451.     sResponse += "</form>";
  452.     sResponse += "<br><br>";
  453. //    sResponse += "<FONT SIZE=-1>";
  454.  
  455. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  456. // Slider BRIGHTNESS        
  457. sResponse += "</p>";
  458. sResponse += "<form action=\"?sCmd\" >";    // ?sCmd forced the '?' at the right spot  
  459. sResponse += "<input type=\"submit\" value=\"SET\">";
  460. sResponse += "&nbsp BRIGHTNESS &nbsp;&nbsp";  // perhaps we can show here the current value
  461. sResponse += round(new_BRIGHTNESS /2.5);    // this is just a scale depending on the max value; round for better readability
  462. sResponse += " %";
  463. sResponse += "<BR>";
  464. sResponse += "<input style=\"width:250px; height:50px\" type=\"range\" name=\"=F_200\" id=\"cmd\" value=\"";   // '=' in front of F__200 forced the = at the right spot
  465. sResponse += BRIGHTNESS;
  466. sResponse += "\" min=10 max=250 step=10 onchange=\"showValue(points)\" />";
  467. sResponse += "</form>";
  468. sResponse += "<p>";
  469. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  470. // Slider SPEED  
  471. sResponse += "</p>";
  472. sResponse += "<form action=\"?sCmd\" >";    // ?sCmd forced the '?' at the right spot  
  473. sResponse += "<input type=\"submit\" value=\"SET\">";
  474. sResponse += "&nbsp <font color=yellow><b>SPEED &nbsp";  // perhaps we can show here the current value
  475. sResponse += round(new_SPEED /2);    // this is just a scale depending on the max value; round for better readability
  476. sResponse += " %";
  477. sResponse += "<BR>";
  478. sResponse += "<input style=\"width:250px; height:50px\" type=\"range\" name=\"=F_600\" id=\"cmd\" value=\"";   // '=' in front of F__200 forced the = at the right spot
  479. sResponse += SPEED;
  480. sResponse += "\" min=20 max=400 step=20 onchange=\"showValue(points)\" />";
  481. sResponse += "</font></b>";
  482. sResponse += "</form>";
  483. sResponse += "<p>";
  484. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  485.  
  486.  sResponse += "<FONT SIZE=-1>";
  487.  
  488.     //////////////////////
  489.     // react on parameters
  490.     //////////////////////
  491.     if (sCmd.length()>0)
  492.     {
  493.       if(sCmd.indexOf("F_0")>=0)  { ledMode =  0; }
  494.       if(sCmd.indexOf("F_1")>=0)  { ledMode =  1; }
  495.       if(sCmd.indexOf("F_2")>=0)  { ledMode =  2; }
  496.       if(sCmd.indexOf("F_3")>=0)  { ledMode =  3; }
  497.       if(sCmd.indexOf("F_4")>=0)  { ledMode =  4; }
  498.       if(sCmd.indexOf("F_5")>=0)  { ledMode =  5; }
  499.       if(sCmd.indexOf("F_6")>=0)  { ledMode =  6; }
  500.       if(sCmd.indexOf("F_7")>=0)  { ledMode =  7; }
  501.       if(sCmd.indexOf("F_8")>=0)  { ledMode =  8; }
  502.       if(sCmd.indexOf("F_9")>=0)  { ledMode =  9; }
  503.  
  504.  
  505. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  506. // oh well, Gyro Gearloose was a bit frustrated. He came up with the
  507. // idea to make 10 digits increments and let the URL (not) react on it.
  508. // However, he was able to assign a new_BRIGHTNESS value;
  509. // what after all serves the purpose. Maybe someone comes up with
  510. // a more ellegant way - HOPEFULLY
  511.  
  512. // do not call a new page when the slider is moved, but assign the new value
  513. // to BRIGHTNESS (this is done in "output parameters to serial", line 314
  514.  
  515.       if(sCmd.indexOf("F_200=20")>=0)  { }
  516.       if(sCmd.indexOf("F_200=30")>=0)  { }
  517.       if(sCmd.indexOf("F_200=40")>=0)  { }
  518.       if(sCmd.indexOf("F_200=50")>=0)  { }
  519.       if(sCmd.indexOf("F_200=60")>=0)  { }
  520.       if(sCmd.indexOf("F_200=70")>=0)  { }
  521.       if(sCmd.indexOf("F_200=80")>=0)  { }
  522.       if(sCmd.indexOf("F_200=90")>=0)  { }
  523.       if(sCmd.indexOf("F_200=100")>=0) { }
  524.       if(sCmd.indexOf("F_200=110")>=0) { }
  525.       if(sCmd.indexOf("F_200=120")>=0) { }
  526.       if(sCmd.indexOf("F_200=130")>=0) { }
  527.       if(sCmd.indexOf("F_200=140")>=0) { }
  528.       if(sCmd.indexOf("F_200=150")>=0) { }
  529.       if(sCmd.indexOf("F_200=160")>=0) { }
  530.       if(sCmd.indexOf("F_200=170")>=0) { }
  531.       if(sCmd.indexOf("F_200=180")>=0) { }
  532.       if(sCmd.indexOf("F_200=190")>=0) { }
  533.       if(sCmd.indexOf("F_200=200")>=0) { }
  534.       if(sCmd.indexOf("F_200=210")>=0) { }
  535.       if(sCmd.indexOf("F_200=220")>=0) { }
  536.       if(sCmd.indexOf("F_200=230")>=0) { }
  537.       if(sCmd.indexOf("F_200=240")>=0) { }
  538.       if(sCmd.indexOf("F_200=250")>=0) { }
  539. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  540.  
  541.       if(sCmd.indexOf("F_600=20")>=0)  {  }  
  542.       if(sCmd.indexOf("F_600=40")>=0)  {  }
  543.       if(sCmd.indexOf("F_600=60")>=0)  {  }
  544.       if(sCmd.indexOf("F_600=80")>=0)  {  }
  545.       if(sCmd.indexOf("F_600=100")>=0) {  }
  546.       if(sCmd.indexOf("F_600=120")>=0) {  }
  547.       if(sCmd.indexOf("F_600=140")>=0) {  }
  548.       if(sCmd.indexOf("F_600=160")>=0) {  }
  549.       if(sCmd.indexOf("F_600=180")>=0) {  }
  550.       if(sCmd.indexOf("F_600=200")>=0) {  }
  551.       if(sCmd.indexOf("F_600=220")>=0) {  }
  552.       if(sCmd.indexOf("F_600=240")>=0) {  }
  553.       if(sCmd.indexOf("F_600=260")>=0) {  }
  554.       if(sCmd.indexOf("F_600=280")>=0) {  }
  555.       if(sCmd.indexOf("F_600=300")>=0) {  }
  556.       if(sCmd.indexOf("F_600=320")>=0) {  }
  557.       if(sCmd.indexOf("F_600=340")>=0) {  }
  558.       if(sCmd.indexOf("F_600=360")>=0) {  }
  559.       if(sCmd.indexOf("F_600=380")>=0) {  }
  560.       if(sCmd.indexOf("F_600=400")>=0) {  }
  561.  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  562.  
  563.  
  564.     } // end sCmd.length()>0
  565.    
  566.    
  567.     sResponse += "<BR>";
  568.     sResponse += "<BR>";
  569.     sResponse += "Powered by FastLED<BR><BR>";
  570.     sResponse += "<FONT SIZE=-2>";
  571.     sResponse += "<font color=\"#FFDE00\">";
  572.     sResponse += "Noise Plus Palette by Mark Kriegsman<BR>";
  573.     sResponse += "Webserver by Stefan Thesen<BR>";
  574.     sResponse += "<font color=\"#FFFFF0\">";
  575.     sResponse += "Gyro Gearloose &nbsp;&nbsp;Feb 2016<BR>";
  576.     sResponse += "</body></html>";
  577.     sHeader  = "HTTP/1.1 200 OK\r\n";
  578.     sHeader += "Content-Length: ";
  579.     sHeader += sResponse.length();
  580.     sHeader += "\r\n";
  581.     sHeader += "Content-Type: text/html\r\n";
  582.     sHeader += "Connection: close\r\n";
  583.     sHeader += "\r\n";
  584.  
  585.   }  // end Format HTML page
  586.  
  587.   // Send the response to the client
  588.   client.print(sHeader);
  589.   client.print(sResponse);
  590.  
  591.  
  592.   // and stop the client
  593.   client.stop();
  594.   Serial.println("Client disonnected");  
  595.   }  // end of web server
  596.  
  597.  
  598. // There are several different palettes of colors demonstrated here.
  599. //
  600. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  601. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  602. //
  603. // Additionally, you can manually define your own color palettes, or you can write
  604. // code that creates color palettes on the fly.
  605.  
  606. // 1 = 5 sec per palette
  607. // 2 = 10 sec per palette
  608. // etc
  609. #define HOLD_PALETTES_X_TIMES_AS_LONG 2
  610.  
  611. void ChangePaletteAndSettingsPeriodically()
  612. {
  613.   uint8_t maxChanges = 10;
  614.   nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
  615.   //uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60; //not used with webserver
  616.   //static uint8_t lastSecond = 99;                                                 //not used with webserver
  617.  
  618.     if (ledMode != 999) {
  619.       switch (ledMode) {
  620.       case  0: all_off(); break;
  621.       case  1: SetupRandomPalette(); speed = 3; scale = 25; colorLoop = 1; break; //2-color palette
  622.       case  2: SetupRandomPalette_g(); speed = 3; scale = 25; colorLoop = 1; break; //3-color palette
  623.       case  3: SetupPurpleAndGreenPalette(); speed = 6; scale = 20; colorLoop = 1; break;
  624.       case  4: SetupBlackAndWhiteStripedPalette(); speed = 4; scale = 20; colorLoop = 1; ; break;
  625.       case  5: targetPalette = ForestColors_p; speed = 3; scale = 20; colorLoop = 0; break;
  626.       case  6: targetPalette = CloudColors_p; speed =  4; scale = 20; colorLoop = 0; break;
  627.       case  7: targetPalette = LavaColors_p;  speed =  8; scale = 19; colorLoop = 0; break;
  628.       case  8: targetPalette = OceanColors_p; speed = 6; scale = 25; colorLoop = 0;  break;
  629.       case  9: targetPalette = PartyColors_p; speed = 3; scale = 20; colorLoop = 1; break;
  630.       }
  631.   }
  632. }
  633.  
  634. // This function generates a random palette that's a gradient
  635. // between four different colors.  The first is a dim hue, the second is
  636. // a bright hue, the third is a bright pastel, and the last is
  637. // another bright hue.  This gives some visual bright/dark variation
  638. // which is more interesting than just a gradient of different hues.
  639.  
  640. // LED animations ###############################################################################
  641. void all_off() {  fill_solid( targetPalette, 16, CRGB::Black);}
  642. void SetupRandomPalette()//two colors
  643. {
  644.   EVERY_N_MILLISECONDS( 8000 ){ //new random palette every 8 seconds. Might have to wait for the first one to show up
  645.   CRGB black  = CRGB::Black;
  646.   CRGB random1 = CHSV( random8(), 255, 255);
  647.   CRGB random2 = CHSV( random8(), 255, 255);
  648.   targetPalette = CRGBPalette16(
  649. //                      CRGB( random8(), 255, 32),
  650. //                      CHSV( random8(), 255, 255),
  651.                       random1,random1,black, black,
  652.                       random2,random2,black, black,
  653.                       random1,random1,black, black,
  654.                       random2,random2,black, black);
  655. //                      CHSV( random8(), 128, 255),
  656. //                      CHSV( random8(), 255, 255), );
  657. }
  658. }
  659. void SetupRandomPalette_g()//three colors
  660. {
  661.   EVERY_N_MILLISECONDS( 8000 ){ //new random palette every 8 seconds
  662.   CRGB black  = CRGB::Black;
  663.   CRGB random1 = CHSV( random8(), 255, 255);
  664.   CRGB random2 = CHSV( random8(), 200, 100);
  665.   CRGB random3 = CHSV( random8(), 150, 200);
  666.   targetPalette = CRGBPalette16(
  667. //                      CRGB( random8(), 255, 32),
  668. //                      CHSV( random8(), 255, 255),
  669.                       random1,random1,black, black,
  670.                       random2,random2,black, random3,
  671.                       random1,random1,black, black,
  672.                       random2,random2,black, random3);
  673. //                      CHSV( random8(), 128, 255),
  674. //                      CHSV( random8(), 255, 255), );
  675. }
  676. }
  677. // This function sets up a palette of purple and green stripes.
  678. void SetupPurpleAndGreenPalette()
  679. {
  680.   CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  681.   CRGB green  = CHSV( HUE_GREEN, 255, 255);
  682.   CRGB black  = CRGB::Black;
  683.  
  684.   targetPalette = CRGBPalette16(
  685.    green,  green,  black,  black,
  686.    purple, purple, black,  black,
  687.    green,  green,  black,  black,
  688.    purple, purple, black,  black );
  689. }
  690. // This function sets up a palette of black and white stripes,
  691. // using code.  Since the palette is effectively an array of
  692. // sixteen CRGB colors, the various fill_* functions can be used
  693. // to set them up.
  694. void SetupBlackAndWhiteStripedPalette()
  695. {
  696.   // 'black out' all 16 palette entries...
  697.   fill_solid( targetPalette, 16, CRGB::Black);
  698.   // and set every eighth one to white.
  699.   currentPalette[0] = CRGB::White;
  700.  // currentPalette[4] = CRGB::White;
  701.   currentPalette[8] = CRGB::White;
  702. //  currentPalette[12] = CRGB::White;
  703. }
  704. //
  705. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  706. //
  707. uint16_t XY( uint8_t x, uint8_t y)
  708. {
  709.   uint16_t i;
  710.   if( kMatrixSerpentineLayout == false) {
  711.     i = (y * kMatrixWidth) + x;
  712.   }
  713.   if( kMatrixSerpentineLayout == true) {
  714.     if( y & 0x01) {
  715.       // Odd rows run backwards
  716.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  717.       i = (y * kMatrixWidth) + reverseX;
  718.     } else {
  719.       // Even rows run forwards
  720.       i = (y * kMatrixWidth) + x;
  721.     }
  722.   }
  723.   return i;
  724. }
  725. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  726. {
  727.   uint8_t brightness = 255;
  728.  
  729.   for( int i = 0; i < NUM_LEDS; i++) {
  730.     leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);
  731.     colorIndex += 3;
  732.   }
  733. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement