Skip to content
Open
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
265 changes: 234 additions & 31 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,93 +25,179 @@ const char* password = STAPSK;
uint8_t rgb[3] = {122,122,122};
uint8_t wave = 0;
int mode = 0;
int o1 = 0; //Option 1
int o2 = 0; //Option 2
String modenames[] = {"default", "random", "simple wave", "Multiwave", "insertionsort","bubblesort", "stalinsort","dual mulitwave with options","quicksort"};

ESP8266WebServer server(80);

const int led = LED_BUILTIN;

const String postForms = "<html>\
String postFirstHalf = "<html>\
<head>\
<title>ESP8266 Web Server POST handling</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
body { background-color: #222222; font-family: Arial, Helvetica, Sans-Serif; Color: #cccccc; }\
\
#site {\
height:100%;\
display: grid;\
grid-template-columns: 3fr 4fr 3fr 3fr 4fr 3fr;\
grid-template-rows: 100px 50px 100px auto 50px;\
grid-template-areas:\
\". header header header header .\"\
\". . . . . .\"\
\". mode rgb rgb options .\"\
\". modes rgb rgb options .\"\
\". . . . . .\"\
}\
\
#header {\
grid-area: header;\
/*background-color: #CCCCCC;\
color: #222222;*/\
font-size: 4vw;\
text-align: center;\
}\
\
#rgb {\
grid-area: rgb;\
background-color: #252525;\
text-align: center;\
}\
\
#options {\
grid-area: options;\
background-color: #303030;\
text-align: center;\
}\
\
#mode {\
font-size: 1.5em;\
grid-area: mode;\
background-color: #303030;\
text-align: center;\
}\
\
#modes {\
grid-area: modes;\
background-color: #303030;\
text-align: center;\
}\
</style>\
\
</head>\
<body>\
<h1>POST plain text to r</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/r/\">\
<input type=\"number\" name=\'r\'><br>\
<div id=\"site\">\
<div id=\"header\">\
LEDstrip controller\
</div>\
<div id =\"mode\">\
mode<br>\
<br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/mode/\" id=\"modeform\">\
<input type=\"number\" name=\"mode\" id=\"modevalue\" value=\"world\"><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
</div>\
<div id =\"modes\">\
<hr>\
<h1>modes</h1><br>";

String postSecondHalf = "\
</div>\
\
<div id =\"rgb\">\
<h1>RGB</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/rgb/\">\
<input type=\"number\" name='r'><br><br>\
<input type=\"number\" name=\"g\" value=\"world\"><br><br>\
<input type=\"number\" name=\"b\" value=\"world\"><br><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
<h1>POST form data to g</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/g/\">\
<input type=\"number\" name=\"g\" value=\"world\"><br>\
</div>\
<div id =\"options\">\
<h1>Optional parameters</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/o1/\">\
<input type=\"number\" name=\"o1\" value=\"world\"><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
<h1>POST form data to b</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/b/\">\
<input type=\"number\" name=\"b\" value=\"world\"><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/o2/\">\
<input type=\"number\" name=\"o2\" value=\"world\"><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
<h1>POST form data to mode</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/mode/\">\
<input type=\"number\" name=\"mode\" value=\"world\"><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
</form></div></div>\
<script>\
function submitThis(i) {\
document.getElementById(\"modevalue\").value=i;\
document.getElementById(\"modeform\").submit();\
}\
</script>\
</body>\
</html>";
String postForms = "";

void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/html", postForms);
digitalWrite(led, 0);
}

void handleR() {
void handleRGB() {
if (server.method() != HTTP_POST) {
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
} else {
String temp = server.arg("plain");
temp.remove(0,2);
rgb[0] = temp.toInt();
int index = 0;
while (true) {
int amp = temp.indexOf("&");
int es = temp.indexOf("=");
if (amp != -1) {
rgb[index] = temp.substring(es+1,amp).toInt();
temp.remove(0,amp+1);
} else {
rgb[index] = temp.substring(es+1).toInt();
break;
}
index++;
}
Serial.println(rgb[0]);
Serial.println(temp);
digitalWrite(led, 0);
}
server.send(200, "text/html", postForms);
}

void handleG() {
void handleOption1() {
if (server.method() != HTTP_POST) {
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
} else {
digitalWrite(led, 1);
String temp = server.arg("plain");
temp.remove(0,2);
rgb[1] = temp.toInt();
Serial.println(rgb[1]);
temp.remove(0,3);
o1 = temp.toInt();
Serial.println(o1);
Serial.println(temp);
digitalWrite(led, 0);
}
server.send(200, "text/html", postForms);

}

void handleB() {
void handleOption2() {
if (server.method() != HTTP_POST) {
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
} else {
digitalWrite(led, 1);
String temp = server.arg("plain");
temp.remove(0,2);
rgb[2] = temp.toInt();
Serial.println(rgb[2]);
temp.remove(0,3);
o2 = temp.toInt();
Serial.println(o2);
Serial.println(temp);
digitalWrite(led, 0);
}
Expand Down Expand Up @@ -150,6 +236,11 @@ void handleNotFound() {
}

void setup(void) {
String buttons = "";
for (int i = 0; i<sizeof(modenames)/sizeof(modenames[0]); i++) {
buttons += "<button onclick=\"submitThis(" + String(i) + ")\">"+modenames[i] + "</button><br>";
}
postForms = postFirstHalf + buttons + postSecondHalf;
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Expand All @@ -174,11 +265,11 @@ void setup(void) {

server.on("/", handleRoot);

server.on("/r/", handleR);
server.on("/rgb/", handleRGB);

server.on("/g/", handleG);
server.on("/o1/", handleOption1);

server.on("/b/", handleB);
server.on("/o2/", handleOption2);

server.on("/mode/",handleMode);

Expand All @@ -189,6 +280,81 @@ void setup(void) {
Serial.println("HTTP server started");
}


int partition(int arr[],int low,int high) {
int pivot = arr[high];
int small = low;
for (int i = low; i < high; i++) {
if (arr[i]<pivot) {
int sw = arr[i];
arr[i] = arr[small];
arr[small] = sw;
small++;
pixels.setPixelColor(i,pixels.ColorHSV(arr[i]));
pixels.setPixelColor(small,pixels.ColorHSV(arr[small]));
pixels.show();
server.handleClient();
}
}
int sw = arr[high];
arr[high] = arr[small];
arr[small] = sw;
pixels.setPixelColor(high,pixels.ColorHSV(arr[high]));
pixels.setPixelColor(small,pixels.ColorHSV(arr[small]));
pixels.show();
server.handleClient();
return small;
}


int quicksort(int arr[], int low, int high) {
if (low<high) {
int pi = partition(arr,low,high);

quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);

}
}

void shadertoy(double (*r)(double, double), double (*g)(double, double),double (*b)(double, double)) {
for(uint16_t j = 0; j<2*NUMPIXELS;j++) {

for(uint16_t i = 0; i<NUMPIXELS;i++){
double i2 = double(i)/NUMPIXELS;
double j2 = double(j)/NUMPIXELS;
pixels.setPixelColor(i,int(255*((*r)(i2,j2))),int(255*((*g)(i2,j2))),int(255*((*b)(i2,j2))));
}

pixels.show(); // Send the updated pixel colors to the hardware.

server.handleClient();
}
}

double smod(double x,double m) {
return fmod(m+fmod(x,m),m);
}

double step(double lim, double x) {
return (x > lim) ? 1.:0.;
}
double right_light_pulse(double x,double t) {
double combined = smod(x*30,1.)+t*5;
combined = fabs(1.-2.*smod(combined,1.));
return combined*combined;
}

double empty_light_pulse(double x, double t) {
return 0.;
}

double left_light_pulse(double x, double t) {
double combined = (smod(x*30,1.)-t*5);
combined = fabs(1.-2.*smod(combined,1.));
return combined*combined;
}

void loop(void) {
if(mode == 0){
pixels.clear(); // Set all pixel colors to 'off'
Expand Down Expand Up @@ -354,6 +520,43 @@ void loop(void) {
delay(1000);
server.handleClient();
}
}else if(mode == 7) {
double (*r)(double, double);
double (*g)(double, double);
double (*b)(double, double);
r= &empty_light_pulse;
g = &empty_light_pulse;
b = &empty_light_pulse;
if (o1 == 0) {
r = &right_light_pulse;
} else if (o1 == 1) {
g = &right_light_pulse;
} else if (o1 == 2) {
b = &right_light_pulse;
}
if (o2 == 0) {
r = &left_light_pulse;
} else if (o2 == 1) {
g = &left_light_pulse;
} else if (o2 == 2) {
b = &left_light_pulse;
}
shadertoy(r,g,b);
}else if(mode == 8) {
int list_length = 300;//rgb[0];

int *arr = new int[list_length];

for (int i = 0; i < list_length; i++) {
arr[i] = random(65536);
pixels.setPixelColor(i,pixels.ColorHSV(arr[i]));
pixels.show();
server.handleClient();
}


quicksort(arr,0,list_length-1);
server.handleClient();
}else{
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
Expand Down