From efe5fd5cbdf99ee0284bf888a828bb28b8e7bd4b Mon Sep 17 00:00:00 2001 From: Paint Your Dragon Date: Wed, 26 Oct 2011 16:08:23 -0700 Subject: [PATCH] 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. --- Arduino/LEDstream/LEDstream.pde | 18 +++++++++++++++++- Processing/Adalight/Adalight.pde | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Arduino/LEDstream/LEDstream.pde b/Arduino/LEDstream/LEDstream.pde index de1dafd..3067f39 100644 --- a/Arduino/LEDstream/LEDstream.pde +++ b/Arduino/LEDstream/LEDstream.pde @@ -83,7 +83,7 @@ void setup() unsigned long t = 0; - LED_DDR |= LED_PIN; // Enable output for LED + LED_DDR |= LED_PIN; // Enable output for LED LED_PORT &= ~LED_PIN; // LED off Serial.begin(115200); // Teensy/32u4 disregards baud rate; is OK! @@ -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. diff --git a/Processing/Adalight/Adalight.pde b/Processing/Adalight/Adalight.pde index d69c30b..5fcb3af 100644 --- a/Processing/Adalight/Adalight.pde +++ b/Processing/Adalight/Adalight.pde @@ -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); + } +} +