Added Color set forms and EEprom save

This commit is contained in:
Lukas Bachschwell 2016-07-31 11:42:21 +02:00
parent 161a4d92f8
commit fdc4eff25a
1 changed files with 171 additions and 183 deletions

View File

@ -3,6 +3,8 @@
#include <avr/power.h>
#endif
#include <EEPROM.h>
#define PIN D6
#define NUMPIXELS 28
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
@ -16,59 +18,142 @@ IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);
unsigned long previousMillis = 0;
unsigned long interval = 0;
int mode = 0;
int element = 0;
bool state = false;
unsigned int colorval1 = 0;
unsigned int colorval2 = 0;
unsigned int colorval3 = 0;
//for fading
float val = 0;
unsigned int primary_r = 0;
unsigned int primary_g = 0;
unsigned int luma = 0;
unsigned int color_r = 0;
unsigned int color_g = 0;
unsigned int color_b = 0;
unsigned int primary_r = 50;
unsigned int primary_g = 255;
unsigned int primary_b = 0;
unsigned int second_r = 0;
unsigned int second_g = 0;
unsigned int second_b = 0;
unsigned int second_b = 255;
String responseHTML = ""
"<!DOCTYPE html><html><head><title>LedControl</title></head><body>"
"<h1>Control</h1><br> "
"<a style='font-size:100px;' href='http://192.168.1.1/on'>on</a><br><br><a style='font-size:100px;'href='http://192.168.1.1/off'>off</a>"
"<br><a style='font-size:100px;' href='http://192.168.1.1/1'>run</a></body><br><a style='font-size:100px;' href='http://192.168.1.1/3'>run single</a><br><a style='font-size:100px;' href='http://192.168.1.1/2'>freaky</a></body></html>"
"<br><a style='font-size:100px;' href='http://192.168.1.1/show?num=0'>0</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=1'>Run (once)</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=2'>Freaky breathe</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=3'>Run single</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=4'>Mode 4</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=5'>5</a><br>";
String responseHTML() {
String response = "<!DOCTYPE html><html><head><title>LedControl</title></head><body>";
response += "<h1>Control</h1><br>";
response += "<p ";
response += "style='background-color: rgb(";
response += primary_r;
response += ",";
response += primary_g;
response += ",";
response += primary_b;
response += ");'>Primary</p><br>";
response += "<p ";
response += "style='background-color: rgb(";
response += second_r;
response += ",";
response += second_g;
response += ",";
response += second_b;
response += ");'>Secondary</p><br>";
response += "<br><a style='font-size:100px;' href='http://192.168.1.1/on'>On</a><br><br><a style='font-size:100px;'href='http://192.168.1.1/off'>Off</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=1'>TwoColorSwipe</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=2'>Freaky breathe</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=3'>Run single</a><br>"
"<a style='font-size:100px;' href='http://192.168.1.1/show?num=4'>ToggleColors</a><br><br>"
"<h1>Set primary:</h1>"
"<form method='GET' action='prime'>"
"R: <input type='number'name='r' min='0' max='255'> G: <input type='number'name='g' min='0' max='255'> B: <input type='number'name='b' min='0' max='255'><br><input type='submit' value='Set'></form><br>"
"<h1>Set secondary:</h1>"
"<form method='GET' action='second'>"
"R: <input type='number'name='r' min='0' max='255'> G: <input type='number'name='g' min='0' max='255'> B: <input type='number'name='b' min='0' max='255'><br><input type='submit' value='Set'></form><br>"
"<br></body></html>";
return response;
}
void handleOn() {
mode = 0;
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(200, 50, 0)); // Moderately bright green color.
strip.setPixelColor(i, strip.Color(primary_g, primary_r, primary_b));
}
strip.show();
webServer.send(200, "text/html", responseHTML);
webServer.send(200, "text/html", responseHTML());
}
void handle1() {
mode = 1;
Serial.println("LOL");
webServer.send(200, "text/html", responseHTML);
}
void handle2() {
mode = 2;
Serial.println("LOL2");
webServer.send(200, "text/html", responseHTML);
void handlePrime() {
for ( uint8_t i = 0; i < webServer.args(); i++ ) {
if (webServer.argName( i ) == "r") {
char buf[3];
webServer.arg( i ).toCharArray(buf, 4);
int num = atoi(buf);
if ((0 <= num) && (num < 256)) {
primary_r = num;
EEPROM.write(0, num);
}
} else if (webServer.argName( i ) == "g") {
char buf[3];
webServer.arg( i ).toCharArray(buf, 4);
int num = atoi(buf);
if ((0 <= num) && (num < 256)) {
primary_g = num;
EEPROM.write(1, num);
}
} else if (webServer.argName( i ) == "b") {
char buf[3];
webServer.arg( i ).toCharArray(buf, 4);
int num = atoi(buf);
if ((0 <= num) && (num < 256)) {
primary_b = num;
EEPROM.write(2, num);
}
}
}
EEPROM.commit();
Serial.println("Setting prime color");
webServer.send(200, "text/html", responseHTML());
}
void handle3() {
mode = 3;
Serial.println("LOL3");
webServer.send(200, "text/html", responseHTML);
void handleSecond() {
for ( uint8_t i = 0; i < webServer.args(); i++ ) {
if (webServer.argName( i ) == "r") {
char buf[3];
webServer.arg( i ).toCharArray(buf, 4);
int num = atoi(buf);
if ((0 <= num) && (num < 256)) {
second_r = num;
EEPROM.write(3, num);
}
} else if (webServer.argName( i ) == "g") {
char buf[3];
webServer.arg( i ).toCharArray(buf, 4);
int num = atoi(buf);
if ((0 <= num) && (num < 256)) {
second_g = num;
EEPROM.write(4, num);
}
} else if (webServer.argName( i ) == "b") {
char buf[3];
webServer.arg( i ).toCharArray(buf, 4);
int num = atoi(buf);
if ((0 <= num) && (num < 256)) {
second_b = num;
EEPROM.write(5, num);
}
}
}
EEPROM.commit();
Serial.println("Setting second color");
webServer.send(200, "text/html", responseHTML());
}
@ -79,7 +164,7 @@ void handleOff() {
strip.setPixelColor(i, strip.Color(0, 0, 0)); // off
}
strip.show();
webServer.send(200, "text/html", responseHTML);
webServer.send(200, "text/html", responseHTML());
}
void handleShow() {
for ( uint8_t i = 0; i < webServer.args(); i++ ) {
@ -89,9 +174,9 @@ void handleShow() {
int num = atoi(buf);
switch (num) {
case 0:
mode = 0;
Serial.println("leds should be off here");
break;
mode = 0;
Serial.println("leds should be off here");
break;
case 1:
mode = 1;
Serial.println("mode 1 run");
@ -112,16 +197,28 @@ void handleShow() {
Serial.println(num);
}
}
webServer.send(200, "text/html", responseHTML);
webServer.send(200, "text/html", responseHTML());
}
void setup() {
strip.begin();
Serial.begin(115200);
Serial.println("Ready");
EEPROM.begin(512);
//read colors from eeprom
primary_r = EEPROM.read(0);
primary_g = EEPROM.read(1);
primary_b = EEPROM.read(2);
second_r = EEPROM.read(3);
second_g = EEPROM.read(4);
second_b = EEPROM.read(5);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("Scary?");
WiFi.softAP("LedControl", "wien60pioneers");
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
@ -132,9 +229,12 @@ void setup() {
webServer.on("/show", handleShow);
webServer.on("/prime", handlePrime);
webServer.on("/second", handleSecond);
webServer.onNotFound([]() {
webServer.send(200, "text/html", responseHTML);
webServer.send(200, "text/html", responseHTML());
});
webServer.begin();
@ -153,174 +253,62 @@ void loop() {
switch (mode) {
case 1:
case 1: //2colorchange
interval = 100;
if (element < NUMPIXELS) {
element++;
}
else {
} else {
element = 0;
colorval1 = ((colorval1 == 50) ? 0 : 50);
state = !state; //toggle colors
}
strip.setPixelColor(element, strip.Color(255, colorval1, 0)); // Moderately bright green color.
if (state) {
strip.setPixelColor(element, strip.Color(primary_g, primary_r, primary_b));
} else {
strip.setPixelColor(element, strip.Color(second_g, second_r, second_b));
}
strip.show();
break;
case 2:
case 2: // fading
interval = 10;
if (colorval2 == 0)colorval1--;
else colorval1++;
if (colorval1 > 150) colorval2 = 0;
if (colorval1 < 30) colorval2 = 1;
val = ((exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0) + 1;
if (val > 255) val = 255;
if (val < 50) val = 50;
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(colorval1, colorval1, colorval1));
strip.setPixelColor(i, strip.Color(round((primary_g / 255)*val), round((primary_r / 255)*val), round((primary_b / 255)*val)));
//strip.setPixelColor(i, strip.Color(val, val, val));
}
strip.show();
if (val > 80)strip.show();
break;
case 3:
interval = 100;
//run single
interval = 150;
if (element < NUMPIXELS)
element++;
else
element = 0;
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(((i == element) ? 100 : 200), ((i == element) ? 100 : 50), ((i == element) ? 100 : 0))); // Moderately bright green color.
strip.setPixelColor(i, strip.Color(((i == element) ? second_g : primary_g), ((i == element) ? second_r : primary_r), ((i == element) ? second_b : primary_b)));
}
strip.show();
break;
case 4:
//switch every second
interval = 400;
for (int i = 0; i < NUMPIXELS; i = i + 2) {
strip.setPixelColor(i, strip.Color(((state) ? second_g : primary_g), ((state) ? second_r : primary_r), ((state) ? second_b : primary_b)));
}
for (int i = 1; i < NUMPIXELS; i = i + 2) {
strip.setPixelColor(i, strip.Color(((!state) ? second_g : primary_g), ((!state) ? second_r : primary_r), ((!state) ? second_b : primary_b)));
}
state = !state;
strip.show();
break;
default:
// nothing
break;
}
previousMillis = millis();
}
}
void startShow(int i) {
mode = 0;
switch (i) {
case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
break;
case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red
break;
case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green
break;
case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue
break;
case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
break;
case 5: theaterChase(strip.Color(127, 0, 0), 50); // Red
break;
case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue
break;
case 7: rainbow(20);
break;
case 8: rainbowCycle(20);
break;
case 9: theaterChaseRainbow(50);
break;
}
}
//####### Neopixeltricks
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}