Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <string>
- #include <atomic>
- #include <stdlib.h>
- #include <WiFi.h>
- #include <FastLED.h>
- #define NUM_LEDS 300
- #define DATA_PIN 5
- #define LED_TYPE WS2812B
- #define COLOUR_ORDER GRB
- #define BRIGHTNESS 64
- int j = NUM_LEDS-1;
- int CHECK = 0;
- int Q = NUM_LEDS/4;
- int H = NUM_LEDS/2;
- int T = 3*Q;
- CRGB leds[NUM_LEDS];
- const char* ssid = "TheDnBCave";
- const char* password = "*************";
- WiFiServer server(80);
- String redString = "0";
- String greenString = "0";
- String blueString = "0";
- String intensityString = "0";
- String checkString = "0";
- int pos1 = 0;
- int pos2 = 0;
- int pos3 = 0;
- int pos4 = 0;
- int pos5 = 0;
- String header;
- std::atomic_int R(0);
- std::atomic_int G(0);
- std::atomic_int B(0);
- std::atomic_int I(0);
- std::atomic_int C(0);
- int r_holder;
- int g_holder;
- int b_holder;
- int i_holder;
- int c_holder;
- std::atomic_int Ri(0);
- std::atomic_int Gi(0);
- std::atomic_int Bi(0);
- std::atomic_int Ii(0);
- std::atomic_int Ci(0);
- TaskHandle_t Serv;
- // Current time
- unsigned long currentTime = millis();
- // Previous time
- unsigned long previousTime = 0;
- // Define timeout time in milliseconds (example: 2000ms = 2s)
- const long timeoutTime = 2000;
- void setup() {
- FastLED.addLeds<LED_TYPE, DATA_PIN, COLOUR_ORDER>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- Serial.begin(115200);
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- server.begin();
- xTaskCreatePinnedToCore(
- Server, /* Function to implement the task */
- "Server", /* Name of the task */
- 50000, /* Stack size in words */
- NULL, /* Task input parameter */
- 0, /* Priority of the task */
- &Serv, /* Task handle. */
- 0); /* Core where the task should run */
- leds[0].r = 255;
- }
- bool hasChanged(){
- if(R.load(std::memory_order_acquire) != Ri.load(std::memory_order_acquire)
- || G.load(std::memory_order_acquire) != Gi.load(std::memory_order_acquire)
- || B.load(std::memory_order_acquire) != Bi.load(std::memory_order_acquire)
- || I.load(std::memory_order_acquire) != Ii.load(std::memory_order_acquire)
- || C.load(std::memory_order_acquire) != Ci.load(std::memory_order_acquire))
- {
- Ri.store(R.load(std::memory_order_acquire), std::memory_order_release);
- Gi.store(G.load(std::memory_order_acquire), std::memory_order_release);
- Bi.store(B.load(std::memory_order_acquire), std::memory_order_release);
- Ii.store(I.load(std::memory_order_acquire), std::memory_order_release);
- Ci.store(C.load(std::memory_order_acquire), std::memory_order_release);
- return true;
- }
- else{
- return false;
- }
- }
- void Server(void * pvParameters){
- for(;;){
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) { // If a new client connects,
- currentTime = millis();
- previousTime = currentTime;
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = ""; // make a String to hold incoming data from the client
- while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
- currentTime = millis();
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0) {
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- // Display the HTML web page
- client.println("<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width =device-width, initial-scale=1\"><link rel=\"icon\" href=\"data:,\"><link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\"></head><body><div class=\"container\"><div class=\"row\"><h1>ESP Color Picker</h1></div><a class=\"btn btn-primary btn-lg\" href=\"#\" id=\"change_color\" role=\"button\">Change Color</a><input id=\"rgb\" type=\"text\" value=\"7F7F7F\" style=\"background:#7f7f7f\"><div><input id=\"r\" type=\"range\" min=\"0\" max=\"255\" onchange=\"updateColour()\"/><label for=\"r\">Red</label><input id=\"g\" type=\"range\" min=\"0\" max=\"255\" onchange=\"updateColour()\" /><label for=\"g\">Green</label><input id=\"b\" type=\"range\" min=\"0\" max=\"255\" onchange=\"updateColour()\" /><label for=\"b\">Blue</label><input id=\"i\" type=\"range\" min=\"0\" max=\"64\" onchange=\"updateColour()\" /><label for=\"i\">Intensity</label></div><div><a class=\"btn btn-primary btn-lg\" href=\"#\" id=\"change_mode\" role=\"button\">Change Mode</a><input id=\"r1\" type=\"radio\" name=\"mode-type\" onchange=\"updateMode()\"/><label for=\"r1\">Mode 1</label><input id=\"r2\" type=\"radio\" name=\"mode-type\" onchange=\"updateMode()\"/><label for=\"r2\">Mode 2</label></div><script>function updateColour() {var r = parseInt( document.getElementById(\"r\").value, 10);var g = parseInt( document.getElementById(\"g\").value, 10);var b = parseInt(document.getElementById(\"b\").value, 10); var i = document.getElementById(\"i\").value;var hex = r.toString(16) + g.toString(16) + b.toString(16);document.getElementById('rgb').value = hex;document.getElementById('rgb').style.background = \"#\" + hex;document.getElementById(\"change_color\").href = \"?r\" + r + \"g\" + g + \"b\" + b + \"i\" + i + \"&\";}function updateMode(){var r;if(document.getElementById('r1').checked){r = 1}else if(document.getElementById('r2').checked){r = 2;}document.getElementById(\"change_mode\").href = \"?m\"+ r + \"&\";}</script></body></html>");
- // The HTTP response ends with another blank line
- client.println();
- // Request sample: /?r201g32b255&
- // Red = 201 | Green = 32 | Blue = 255
- if(header.indexOf("GET /?r") >= 0) {
- Serial.println("Static colour selected - calculating RGB Values...");
- pos1 = header.indexOf('r');
- pos2 = header.indexOf('g');
- pos3 = header.indexOf('b');
- pos4 = header.indexOf('i');
- pos4 = header.indexOf('&');
- redString = header.substring(pos1+1, pos2);
- greenString = header.substring(pos2+1, pos3);
- blueString = header.substring(pos3+1, pos4);
- intensityString = header.substring(pos4+1, pos5);
- R.store(redString.toInt(), std::memory_order_release);
- G.store(greenString.toInt(), std::memory_order_release);
- B.store(blueString.toInt(), std::memory_order_release);
- I.store(intensityString.toInt(), std::memory_order_release);
- Serial.println(R.load(std::memory_order_acquire));
- Serial.println(G.load(std::memory_order_acquire));
- Serial.println(B.load(std::memory_order_acquire));
- }
- else if(header.indexOf("GET /?m") >= 0) {
- Serial.println("Looping mode selected - calculating mode selection...");
- pos1 = header.indexOf('m');
- pos2 = header.indexOf('&');
- checkString = header.substring(pos1+1, pos2);
- C.store(checkString.toInt(), std::memory_order_release);
- Serial.println(C.load(std::memory_order_acquire));
- }
- // Break out of the while loop
- break;
- } else { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- } else if (c != '\r') { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- vTaskDelay(3);
- }
- }
- }
- /*void Display(void * pvParameters){
- for (;;){
- }
- }*/
- void loop() {
- if (hasChanged())
- {
- Serial.println("Change detected!");
- c_holder = C.load(std::memory_order_acquire);
- if(c_holder == 0)
- {
- Serial.println("I am in the static display block");
- r_holder = R.load(std::memory_order_acquire);
- g_holder = G.load(std::memory_order_acquire);
- b_holder = B.load(std::memory_order_acquire);
- i_holder = I.load(std::memory_order_acquire);
- FastLED.setBrightness(i_holder);
- Serial.println("Now updating all elements with the following values: ");
- Serial.println(r_holder);
- Serial.println(g_holder);
- Serial.println(b_holder);
- //for(int i=0; i<NUM_LEDS/2; i++)
- for (int i=0; i<NUM_LEDS; i++)
- {
- leds[i].setRGB(r_holder, g_holder, b_holder);
- Serial.print("[");
- Serial.print(i);
- Serial.print("].R = ");
- Serial.println(leds[i].r);
- //leds[j].setRGB(r_holder, g_holder, b_holder);
- //FastLED.show();
- //j--;
- }
- Serial.println("All elements updated - showing");
- FastLED.show();
- //j=NUM_LEDS-1;
- }
- else if(c_holder == 1)
- {
- Serial.println("I am in mode 1");
- for(int i=0; i<50; i++)
- {
- if(i==49)
- {
- for(int j=49; j>=0; j--)
- {
- leds[j] = CRGB::Red;
- leds[j+50] = CRGB::Orange;
- leds[j+100] = CRGB::Yellow;
- leds[j+150] = CRGB::Green;
- leds[j+200] = CRGB::Blue;
- leds[j+250] = CRGB::Purple;
- FastLED.show();
- leds[j] = CRGB::Black;
- leds[j+50] = CRGB::Black;
- leds[j+100] = CRGB::Black;
- leds[j+150] = CRGB::Black;
- leds[j+200] = CRGB::Black;
- leds[j+250] = CRGB::Black;
- }
- }
- else
- {
- leds[i] = CRGB::Red;
- leds[i+50] = CRGB::Orange;
- leds[i+100] = CRGB::Yellow;
- leds[i+150] = CRGB::Green;
- leds[i+200] = CRGB::Blue;
- leds[i+250] = CRGB::Purple;
- FastLED.show();
- }
- }
- }
- else if(c_holder == 2)
- {
- Serial.println("I am in mode 2");
- for(int i=0; i<NUM_LEDS; i++)
- {
- leds[i] = CRGB::Red;
- leds[(i+Q) % NUM_LEDS] = CRGB::Green;
- leds[(i+H) % NUM_LEDS] = CRGB::Blue;
- leds[(i+T) % NUM_LEDS] = CRGB::Yellow;
- FastLED.show();
- delay(20);
- for(int i=0; i<NUM_LEDS; i++)
- {
- leds[i] = CRGB::Black;
- }
- }
- }
- }
- vTaskDelay(3);
- }
Add Comment
Please, Sign In to add comment