Commit a4425082 authored by Mitchell Pomery's avatar Mitchell Pomery

Code Clean Up

Making wrapper functions for setting Lights in our array, accounting for
the way the strip runs down the board
parent efc4148d
/* /*
BlinkenLights BlinkenLights
by Mitchell Pomery [BG3] with help from Andrew Adamson [BOB] Code by Mitchell Pomery [BG3] with help from Andrew Adamson [BOB]
modified 20 Jan 2014 Hardware by Andrew Adamson [BOB] with help from Mitchell Pomery [BG3]
Get the dates from the git commits.
The LED strip should be wired from the top left corner and zig zag down
*/ */
///TODO: Vary light pattern randomly ///TODO: Vary light pattern randomly
///TODO: Make run when no network is present ///TODO: Make run when no network is present
///TODO: Recover when network reappears ///TODO: Recover when network reappears
///TODO: Make everything use posts. maybe
#include <Ethernet.h> // Needed For Reasons #include <Ethernet.h> // Needed For Reasons
#include <WebServer.h> // Lets us easily do web requests #include <WebServer.h> // Lets us easily do web requests
...@@ -31,25 +36,26 @@ struct led { ...@@ -31,25 +36,26 @@ struct led {
char blue; char blue;
}; };
int position = 0; // For controlling the lights
int position = 0; // How far through the cycle we are
// led array struct led ledArray[STRIPLENGTH]; // led array
struct led ledArray[STRIPLENGTH]; char brightness = (char) 128; //Brightness of the LEDs
char brightness = (char) 128; // We seem to have issues at the moment putting this up to the maximum (255)
// Most likely due to the fact the LED strip is underpowered
int lightOption = 0; // Which predefined light sequence we are running
static uint8_t mac[] = { 0xC0, 0xCA, 0xC0, 0x1A, 0x19, 0x82 }; // C0CA C01A 1982 static uint8_t mac[] = { 0xC0, 0xCA, 0xC0, 0x1A, 0x19, 0x82 }; // C0CA C01A 1982
IPAddress ip(130,95,13,96); IPAddress ip(130,95,13,96); // 130.95.13.96 (Can we forcefully take .82?
int lightOption = 0;
// Set up our light strip // Set up our light strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPLENGTH, PIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPLENGTH, PIN, NEO_GRB + NEO_KHZ800);
// Set up our webserver // Set up our webserver
WebServer webserver(PREFIX, 80); WebServer webserver(PREFIX, 80);
// showArray /**
* Change the lights to our updated array
*/
void showArray() { void showArray() {
for (int i = 0; i < STRIPLENGTH; i ++) { for (int i = 0; i < STRIPLENGTH; i ++) {
strip.setPixelColor(i, (int) ledArray[i].red, (int) ledArray[i].green, (int) ledArray[i].blue); strip.setPixelColor(i, (int) ledArray[i].red, (int) ledArray[i].green, (int) ledArray[i].blue);
...@@ -57,13 +63,52 @@ void showArray() { ...@@ -57,13 +63,52 @@ void showArray() {
strip.show(); strip.show();
} }
// setLED /**
* Set the LED at pos in the strip to the defined color
* @param pos position in the array
* @param red red portion of color
* @param green green portion of color
* @param blue blue portion of color
*/
void setLED(int pos, int red, int green, int blue) { void setLED(int pos, int red, int green, int blue) {
ledArray[pos].red = red; ledArray[pos].red = red;
ledArray[pos].green = green; ledArray[pos].green = green;
ledArray[pos].blue = blue; ledArray[pos].blue = blue;
} }
/**
* Set the LED at pos in the strip to the defined color
* @param pos position in the array
* @param red red portion of color
* @param green green portion of color
* @param blue blue portion of color
*/
void setLED(int xpos, int yxpos, int red, int green, int blue) {
///TODO: Test this function
int pos = 0;
if (ypos % 2 == 0) { // if we are on an even line, add y
pos = ypos * WIDTH + xpos;
}
else { // on an odd line, take y
if (WIDTH % 2 == 0) { // even width
pos = ypos * WIDTH + HEIGHT - xpos;
}
else { // odd width
pos = ypos * WIDTH + HEIGHT - xpos -1;
}
}
setLED(pos, red, green, blue);
}
/**
* Get the color of the LED at pos in the strip
* @param pos position in the array
* @return color of LED at pos
*/
struct led getLED(int pos) {
return ledArray[pos];
}
// Sets the light sequence to one that is predefined // Sets the light sequence to one that is predefined
void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
URLPARAM_RESULT rc; URLPARAM_RESULT rc;
...@@ -75,24 +120,11 @@ void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url ...@@ -75,24 +120,11 @@ void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url
if (type == WebServer::HEAD) { if (type == WebServer::HEAD) {
return; return;
} }
else if (type == WebServer::POST) { else if (type == WebServer::GET) {
while (server.readPOSTparam(name, NAMELEN, value, VALUELEN))
{
//Serial.print(name);
//Serial.print(" = ");
//Serial.println(value);
//if (name == "seq") {
// lightOption = value;
//}
}
}
else if (type == WebServer::GET) { //WebServer::POST
if (strlen(url_tail)) { if (strlen(url_tail)) {
while (strlen(url_tail)) { while (strlen(url_tail)) {
rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN); rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
if (rc != URLPARAM_EOS) { if (rc != URLPARAM_EOS) {
//Serial.println(name);
//Serial.println(value);
if (String(name).equals("seq")) { if (String(name).equals("seq")) {
lightOption = atoi(value); lightOption = atoi(value);
server.print(lightOption); server.print(lightOption);
...@@ -100,11 +132,9 @@ void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url ...@@ -100,11 +132,9 @@ void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url
} }
} }
} }
///TODO: Get ?seq=X and set lightOption to X
} }
else { else {
server.print("Unknown"); server.print("Unknown Request");
} }
} }
...@@ -112,7 +142,6 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail ...@@ -112,7 +142,6 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail
URLPARAM_RESULT rc; URLPARAM_RESULT rc;
char name[NAMELEN]; char name[NAMELEN];
char value[VALUELEN]; char value[VALUELEN];
int xPos = -1; int xPos = -1;
int yPos = -1; int yPos = -1;
int r = -1; int r = -1;
...@@ -124,24 +153,11 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail ...@@ -124,24 +153,11 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail
if (type == WebServer::HEAD) { if (type == WebServer::HEAD) {
return; return;
} }
else if (type == WebServer::POST) { else if (type == WebServer::GET) {
while (server.readPOSTparam(name, NAMELEN, value, VALUELEN))
{
//Serial.print(name);
//Serial.print(" = ");
//Serial.println(value);
//if (name == "seq") {
// lightOption = value;
//}
}
}
else if (type == WebServer::GET) { //WebServer::POST
if (strlen(url_tail)) { if (strlen(url_tail)) {
while (strlen(url_tail)) { while (strlen(url_tail)) {
rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN); rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
if (rc != URLPARAM_EOS) { if (rc != URLPARAM_EOS) {
//Serial.println(name);
//Serial.println(value);
if (String(name).equals("x")) { if (String(name).equals("x")) {
xPos = atoi(value); xPos = atoi(value);
} }
...@@ -165,11 +181,11 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail ...@@ -165,11 +181,11 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail
server.print("Unknown"); server.print("Unknown");
} }
//Serial.println(xPos); //server.print(xPos);
//Serial.println(yPos); //server.print(yPos);
//Serial.println(r); //server.print(r);
//Serial.println(g); //server.print(g);
//Serial.println(b); //server.print(b);
if (xPos != -1 && yPos != -1 && r != -1 && g != -1 && b != -1) { if (xPos != -1 && yPos != -1 && r != -1 && g != -1 && b != -1) {
setLED(xPos, r, g, b); setLED(xPos, r, g, b);
...@@ -181,28 +197,11 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u ...@@ -181,28 +197,11 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u
char name[NAMELEN]; char name[NAMELEN];
char value[VALUELEN]; char value[VALUELEN];
int xPos = -1;
int yPos = -1;
int r = -1;
int g = -1;
int b = -1;
server.httpSuccess(); server.httpSuccess();
// Kill the connection before doing anything if all they want is the head // Kill the connection before doing anything if all they want is the head
if (type == WebServer::HEAD) { if (type == WebServer::HEAD) {
return; return;
} }
else if (type == WebServer::POST) {
while (server.readPOSTparam(name, NAMELEN, value, VALUELEN))
{
//Serial.print(name);
//Serial.print(" = ");
//Serial.println(value);
//if (name == "seq") {
// lightOption = value;
//}
}
}
else if (type == WebServer::GET) { //WebServer::POST else if (type == WebServer::GET) { //WebServer::POST
if (strlen(url_tail)) { if (strlen(url_tail)) {
while (strlen(url_tail)) { while (strlen(url_tail)) {
...@@ -210,8 +209,10 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u ...@@ -210,8 +209,10 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u
if (rc != URLPARAM_EOS) { if (rc != URLPARAM_EOS) {
//Serial.println(name); //Serial.println(name);
//Serial.println(value); //Serial.println(value);
if (String(name).equals("brightness")) { if (String(name).equals("bright")) {
strip.setBrightness(atoi(value)); strip.setBrightness(atoi(value));
server.print(atoi(value));
strip.show();
} }
} }
} }
...@@ -339,7 +340,7 @@ void setup() { ...@@ -339,7 +340,7 @@ void setup() {
// Start Lights // Start Lights
strip.begin(); strip.begin();
//strip.show(); //strip.show();
strip.setBrightness(32); strip.setBrightness(128);
showArray(); showArray();
} }
...@@ -350,42 +351,18 @@ void loop() ...@@ -350,42 +351,18 @@ void loop()
int len = 64; int len = 64;
webserver.processConnection(buff, &len); webserver.processConnection(buff, &len);
position = position % 256; position = position % 256;
rainbow(10); //rainbow(10);
// Run our light sequence after checking for we requests // Run our light sequence after checking for we requests
/*switch (lightOption) { switch (lightOption) {
case 0: case 0: // Don't change the lights at all
break; break;
case 1: case 1: // Wipe the LED's to Red
colorWipe(strip.Color(255, 0, 0), 50); // Red colorWipe(strip.Color(255, 0, 0), 50); // Red
break; 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(100);
break;
case 8:
rainbowCycle(20);
break;
case 9:
theaterChaseRainbow(50);
break;
default: default:
lightOption = 7; lightOption = 1;
break; break;
}*/ }
showArray(); showArray();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment