From 649af4b600aa31ee0b4e1f0f7965594fc3eb0b07 Mon Sep 17 00:00:00 2001 From: Mitchell Pomery Date: Tue, 28 Jan 2014 21:06:33 +0800 Subject: [PATCH] Comments Everywhere And a couple of helper functions --- BlinkenLights.ino | 150 ++++++++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 65 deletions(-) diff --git a/BlinkenLights.ino b/BlinkenLights.ino index b2f922f..3241bd8 100644 --- a/BlinkenLights.ino +++ b/BlinkenLights.ino @@ -14,20 +14,22 @@ ///TODO: Make run when no network is present ///TODO: Recover when network reappears ///TODO: Make everything use posts. maybe +///TODO: Sanity checks. Everywhere. Until I'm Insane. +///TODO: Figure out memory usage. If we max memory we will see weird behaviour. #include // Needed For Reasons #include // Lets us easily do web requests #include // Needed to control Lights #include // Controls the Lights -#define STRIPLENGTH 42 // Number of LED's in light strip #define PIN 6 // Pin that the LED strip is attached to #define PREFIX "" // Document root for our pages #define PORT 80 // Web Server Port #define NAMELEN 8 // Max variable length in request #define VALUELEN 256 // Max value from request -#define WIDTH // How many LEDs wide our array is -#define HEIGHT // How many LEDs high our array is +#define WIDTH 7 // How many LEDs wide our array is +#define HEIGHT 6 // How many LEDs high our array is +#define STRIPLENGTH 42 // Number of LED's in light strip // Used to store the LED's values struct led { @@ -36,33 +38,61 @@ struct led { char blue; }; -// For controlling the lights +// For controlling the lights - Should only be changed by the functions int position = 0; // How far through the cycle we are struct led ledArray[STRIPLENGTH]; // led array +int lightOption = 0; // Which predefined light sequence we are running char brightness = (char) 128; //Brightness of the LEDs // 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 +// Network Settings static uint8_t mac[] = { 0xC0, 0xCA, 0xC0, 0x1A, 0x19, 0x82 }; // C0CA C01A 1982 IPAddress ip(130,95,13,96); // 130.95.13.96 (Can we forcefully take .82? - +// If we remove the ip address, we will automatically use DHCP // Set up our light strip Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPLENGTH, PIN, NEO_GRB + NEO_KHZ800); // Set up our webserver WebServer webserver(PREFIX, 80); +// ============================ LIGHT MANIPULATION ============================ + /** * Change the lights to our updated array + * We should probably get rid of this soonish */ void showArray() { + ///TODO: Rename this to something better? for (int i = 0; i < STRIPLENGTH; i ++) { strip.setPixelColor(i, (int) ledArray[i].red, (int) ledArray[i].green, (int) ledArray[i].blue); } strip.show(); } +/** + * Set the LED at pos in the strip to the defined color + * @param xpos x coordinate + * @param ypos y cordinate + * @return position in the array + */ +int coordToPos(int xpos, int ypos) { + ///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; + } + } + return pos; +} + /** * Set the LED at pos in the strip to the defined color * @param pos position in the array @@ -77,38 +107,45 @@ void setLED(int pos, int red, int green, int blue) { } /** - * Set the LED at pos in the strip to the defined color - * @param pos position in the array + * Set the LED at (xpos, ypos) in the strip to the defined color + * @param xpos x coordinate + * @param ypos y coordinate * @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; - } - } +void setLED(int xpos, int ypos, int red, int green, int blue) { + int pos = coordToPos(xpos, ypos); setLED(pos, red, green, blue); } /** - * Get the color of the LED at pos in the strip - * @param pos position in the array + * Get the colour of the LED in pos position in the strip + * @param pos position in the strip * @return color of LED at pos */ struct led getLED(int pos) { return ledArray[pos]; } +/** + * Get the colour of the LED at (xpos, ypos) + * @param xpos x coordinate + * @param ypos y coordinate + * @return color of LED at (xpos, ypos) + */ +struct led getLED(int xpos, int ypos) { + int pos = coordToPos(xpos, ypos); + return ledArray[pos]; +} + +// ================================ WEB PAGES ================================ + +///TODO: Comment this stuff +///TODO: Reduce code reuse in the web page functions +///TODO: Start using post requests +///TODO: Make a function to set all the lights at once + // Sets the light sequence to one that is predefined void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { URLPARAM_RESULT rc; @@ -116,7 +153,7 @@ void webSetSequence(WebServer &server, WebServer::ConnectionType type, char *url char value[VALUELEN]; 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 head if (type == WebServer::HEAD) { return; } @@ -149,7 +186,7 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail int b = -1; 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 head if (type == WebServer::HEAD) { return; } @@ -180,13 +217,6 @@ void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail else { server.print("Unknown"); } - - //server.print(xPos); - //server.print(yPos); - //server.print(r); - //server.print(g); - //server.print(b); - if (xPos != -1 && yPos != -1 && r != -1 && g != -1 && b != -1) { setLED(xPos, r, g, b); } @@ -198,7 +228,7 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u char value[VALUELEN]; 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 head if (type == WebServer::HEAD) { return; } @@ -207,8 +237,6 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u while (strlen(url_tail)) { rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN); if (rc != URLPARAM_EOS) { - //Serial.println(name); - //Serial.println(value); if (String(name).equals("bright")) { strip.setBrightness(atoi(value)); server.print(atoi(value)); @@ -223,6 +251,11 @@ void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *u } } +// ============================== LIGHT DISPLAYS ============================== + +///TODO: Comment this stuff +///TODO: Make sure these functions don't block. Otherwise web requests are slow + // Fill the dots one after the other with a color void colorWipe(uint32_t c, uint8_t wait) { for(uint16_t i=0; i