BlinkenLights.ino 9.86 KB
Newer Older
Mitchell Pomery's avatar
Mitchell Pomery committed
1 2 3
/*
 BlinkenLights
 
Mitchell Pomery's avatar
Mitchell Pomery committed
4 5 6 7 8 9
 Code by Mitchell Pomery [BG3] with help from Andrew Adamson [BOB]
 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
Mitchell Pomery's avatar
Mitchell Pomery committed
10 11 12 13 14 15
 
 */

///TODO: Vary light pattern randomly
///TODO: Make run when no network is present
///TODO: Recover when network reappears
Mitchell Pomery's avatar
Mitchell Pomery committed
16
///TODO: Make everything use posts. maybe
Mitchell Pomery's avatar
Mitchell Pomery committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

#include <Ethernet.h> // Needed For Reasons
#include <WebServer.h> // Lets us easily do web requests
#include <SPI.h> // Needed to control Lights
#include <Adafruit_NeoPixel.h> // 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

// Used to store the LED's values
struct led {
  char red;
  char green;
  char blue;
};

Mitchell Pomery's avatar
Mitchell Pomery committed
39 40 41 42 43 44 45
// For controlling the lights
int position = 0; // How far through the cycle we are
struct led ledArray[STRIPLENGTH]; // led array
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
Mitchell Pomery's avatar
Mitchell Pomery committed
46 47

static uint8_t mac[] = { 0xC0, 0xCA, 0xC0, 0x1A, 0x19, 0x82 }; // C0CA C01A 1982
Mitchell Pomery's avatar
Mitchell Pomery committed
48
IPAddress ip(130,95,13,96); // 130.95.13.96 (Can we forcefully take .82?
Mitchell Pomery's avatar
Mitchell Pomery committed
49 50 51 52 53 54 55


// Set up our light strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPLENGTH, PIN, NEO_GRB + NEO_KHZ800);
// Set up our webserver
WebServer webserver(PREFIX, 80);

Mitchell Pomery's avatar
Mitchell Pomery committed
56 57 58
/**
 * Change the lights to our updated array
 */
Mitchell Pomery's avatar
Mitchell Pomery committed
59 60 61 62 63 64 65
void showArray() {
  for (int i = 0; i < STRIPLENGTH; i ++) {
    strip.setPixelColor(i, (int) ledArray[i].red, (int) ledArray[i].green, (int) ledArray[i].blue);
  }
  strip.show();
}

Mitchell Pomery's avatar
Mitchell Pomery committed
66 67 68 69 70 71 72
/**
 * 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
 */
Mitchell Pomery's avatar
Mitchell Pomery committed
73 74 75 76 77 78
void setLED(int pos, int red, int green, int blue) {
  ledArray[pos].red = red;
  ledArray[pos].green = green;
  ledArray[pos].blue = blue;
}

Mitchell Pomery's avatar
Mitchell Pomery committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/**
 * 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];
}

Mitchell Pomery's avatar
Mitchell Pomery committed
112 113 114 115 116 117 118 119 120 121 122
// 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;
  char name[NAMELEN];
  char value[VALUELEN];
  
  server.httpSuccess();
  // Kill the connection before doing anything if all they want is the head
  if (type == WebServer::HEAD) {
    return;
  }
Mitchell Pomery's avatar
Mitchell Pomery committed
123
  else if (type == WebServer::GET) {
Mitchell Pomery's avatar
Mitchell Pomery committed
124 125 126 127 128 129 130 131 132 133 134 135 136
    if (strlen(url_tail)) {
      while (strlen(url_tail)) {
        rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
        if (rc != URLPARAM_EOS) {
          if (String(name).equals("seq")) {
            lightOption = atoi(value);
            server.print(lightOption);
          }
        }
      }
    }
  }
  else {
Mitchell Pomery's avatar
Mitchell Pomery committed
137
    server.print("Unknown Request");
Mitchell Pomery's avatar
Mitchell Pomery committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  }
}

void webSetLED(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  URLPARAM_RESULT rc;
  char name[NAMELEN];
  char value[VALUELEN];
  int xPos = -1;
  int yPos = -1;
  int r = -1;
  int g = -1;
  int b = -1;
  
  server.httpSuccess();
  // Kill the connection before doing anything if all they want is the head
  if (type == WebServer::HEAD) {
    return;
  }
Mitchell Pomery's avatar
Mitchell Pomery committed
156
  else if (type == WebServer::GET) {
Mitchell Pomery's avatar
Mitchell Pomery committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    if (strlen(url_tail)) {
      while (strlen(url_tail)) {
        rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
        if (rc != URLPARAM_EOS) {
          if (String(name).equals("x")) {
            xPos = atoi(value);
          }
          else if (String(name).equals("y")) {
            yPos = atoi(value);
          }
          else if (String(name).equals("r")) {
            r = atoi(value);
          }
          else if (String(name).equals("g")) {
            g = atoi(value);
          }
          else if (String(name).equals("b")) {
            b = atoi(value);
          }
        }
      }
    }
  }
  else {
    server.print("Unknown");
  }
  
Mitchell Pomery's avatar
Mitchell Pomery committed
184 185 186 187 188
  //server.print(xPos);
  //server.print(yPos);
  //server.print(r);
  //server.print(g);
  //server.print(b);
Mitchell Pomery's avatar
Mitchell Pomery committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  
  if (xPos != -1 && yPos != -1 && r != -1 && g != -1 && b != -1) {
    setLED(xPos, r, g, b);
  }
}

void webSetBrightness(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  URLPARAM_RESULT rc;
  char name[NAMELEN];
  char value[VALUELEN];
  
  server.httpSuccess();
  // Kill the connection before doing anything if all they want is the head
  if (type == WebServer::HEAD) {
    return;
  }
  else if (type == WebServer::GET) { //WebServer::POST
    if (strlen(url_tail)) {
      while (strlen(url_tail)) {
        rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN);
        if (rc != URLPARAM_EOS) {
          //Serial.println(name);
          //Serial.println(value);
Mitchell Pomery's avatar
Mitchell Pomery committed
212
          if (String(name).equals("bright")) {
Mitchell Pomery's avatar
Mitchell Pomery committed
213
            strip.setBrightness(atoi(value));
Mitchell Pomery's avatar
Mitchell Pomery committed
214 215
            server.print(atoi(value));
            strip.show();
Mitchell Pomery's avatar
Mitchell Pomery committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
          }
        }
      }
    }
  }
  else {
    server.print("Unknown");
  }
}

// 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(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, Wheel((i+position) & 255));
  }
  strip.show();
  delay(wait);
  position++;
}

// 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) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

void setup() {
  // Open //Serial communications and wait for port to open:
  //Serial.begin(9600);
   //while (!//Serial) {
    //; // wait for //Serial port to connect. Needed for Leonardo only
  //}
  //Serial.println("//Serial Port Opened");
  
  // start the Ethernet connection and the server:
  //Serial.println("Starting Ethernet");
  Ethernet.begin(mac, ip);
  
  //Serial.println("Setting Up Server");
  webserver.setDefaultCommand(&webSetSequence);
  webserver.addCommand("custom", &webSetSequence);
  webserver.addCommand("individual", &webSetLED);
  webserver.addCommand("brightness", &webSetBrightness);
  
  //Serial.println("Starting Server");
  webserver.begin();
  //Serial.print("server is at ");
  //Serial.println(Ethernet.localIP());
  
  // Initialize our LED Array
  
  for (int i = 0; i < STRIPLENGTH; i ++) {
    ledArray[i].red = (char) 128;
    ledArray[i].green = (char) 128;
    ledArray[i].blue = (char) 128;
  }
  
  // Start Lights
  strip.begin();
  //strip.show();
Mitchell Pomery's avatar
Mitchell Pomery committed
343
  strip.setBrightness(128);
Mitchell Pomery's avatar
Mitchell Pomery committed
344 345 346 347 348 349 350 351 352 353
  showArray();
}

void loop()
{
  // process incoming connections one at a time forever
  char buff[64];
  int len = 64;
  webserver.processConnection(buff, &len);
  position = position % 256;
Mitchell Pomery's avatar
Mitchell Pomery committed
354
  //rainbow(10);
Mitchell Pomery's avatar
Mitchell Pomery committed
355
  // Run our light sequence after checking for we requests
Mitchell Pomery's avatar
Mitchell Pomery committed
356 357
  switch (lightOption) {
    case 0: // Don't change the lights at all
Mitchell Pomery's avatar
Mitchell Pomery committed
358
      break;
Mitchell Pomery's avatar
Mitchell Pomery committed
359
    case 1: // Wipe the LED's to Red
Mitchell Pomery's avatar
Mitchell Pomery committed
360 361 362
      colorWipe(strip.Color(255, 0, 0), 50); // Red
      break;
    default:
Mitchell Pomery's avatar
Mitchell Pomery committed
363
      lightOption = 1;
Mitchell Pomery's avatar
Mitchell Pomery committed
364
      break;
Mitchell Pomery's avatar
Mitchell Pomery committed
365
  }
Mitchell Pomery's avatar
Mitchell Pomery committed
366 367 368
  
  showArray();
}