Startup test added to LEDstream (Arduino), shutdown added to Adalight (Processing)

Upon initial power-up or reset, and each time the serial port is opened,
the LEDs will briefly flash through red, green and blue, then off. This
can be used to diagnose whether the wiring between Arduino and LEDs is
correct, and to spot any breaks in the strand. Unrelated, Adalight (the
Processing code) now issues an all-off state to the LEDs on most (but
not all) exit cases.
This commit is contained in:
Paint Your Dragon 2011-10-26 16:08:23 -07:00
parent f43a8086e1
commit efe5fd5cbd
2 changed files with 36 additions and 1 deletions

View File

@ -99,6 +99,22 @@ void setup()
// SPI clock line only. Your mileage may vary. Experiment!
// SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz
// Issue test pattern to LEDs on startup. This helps verify that
// wiring between the Arduino and LEDs is correct. Not knowing the
// actual number of LEDs connected, this sets all of them (well, up
// to the first 25,000, so as not to be TOO time consuming) to red,
// green, blue, then off. Once you're confident everything is working
// end-to-end, it's OK to comment this out and reprogram the Arduino.
uint8_t testcolor[] = { 0, 0, 0, 255, 0, 0 };
for(char n=3; n>=0; n--) {
for(c=0; c<25000; c++) {
for(i=0; i<3; i++) {
for(SPDR = testcolor[n + i]; !(SPSR & _BV(SPIF)); );
}
}
delay(1); // One millisecond pause = latch
}
// loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.

View File

@ -36,6 +36,7 @@ GraphicsDevice[] gs;
PImage preview = createImage(arrayWidth, arrayHeight, RGB);
Rectangle bounds;
Serial port;
DisposeHandler dh; // For disabling LEDs on exit
void setup() {
GraphicsEnvironment ge;
@ -43,6 +44,7 @@ void setup() {
int i;
float f;
dh = new DisposeHandler(this);
port = new Serial(this, Serial.list()[0], 115200);
size(arrayWidth * imgScale, arrayHeight * imgScale, JAVA2D);
@ -151,3 +153,20 @@ color block(PImage image, int x, int y) {
return color(r / s2, g / s2, b / s2);
}
// The DisposeHandler is called on program exit (but before the Serial
// library is shutdown), in order to turn off the LEDs (reportedly more
// reliable than stop()). Seems to work for the window close box and
// escape key exit, but not the 'Quit' menu option.
// Thanks to phi.lho in the Processing forums.
public class DisposeHandler {
DisposeHandler(PApplet pa) {
pa.registerDispose(this);
}
public void dispose() {
// Fill buffer (after header) with 0's, and issue to Arduino...
Arrays.fill(buffer, 6, buffer.length, byte(0));
port.write(buffer);
}
}