Skip to content

Commit

Permalink
Multi-monitor support, speed improvements, LED auto-off and other goo…
Browse files Browse the repository at this point in the history
…d stuff
  • Loading branch information
PaintYourDragon committed Nov 20, 2011
1 parent fb1c294 commit db2ff23
Show file tree
Hide file tree
Showing 2 changed files with 342 additions and 125 deletions.
32 changes: 31 additions & 1 deletion Arduino/LEDstream/LEDstream.pde
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ static const uint8_t magic[] = {'A','d','a'};
#define MODE_HOLD 1
#define MODE_DATA 2

// If no serial data is received for a while, the LEDs are shut off
// automatically. This avoids the annoying "stuck pixel" look when
// quitting LED display programs on the host computer.
static const unsigned long serialTimeout = 15000; // 15 seconds

void setup()
{
// Dirty trick: the circular buffer for serial data is 256 bytes,
Expand All @@ -82,7 +87,10 @@ void setup()
int32_t
bytesRemaining;
unsigned long
startTime = micros();
startTime,
lastByteTime,
lastAckTime,
t;

LED_DDR |= LED_PIN; // Enable output for LED
LED_PORT &= ~LED_PIN; // LED off
Expand Down Expand Up @@ -116,16 +124,38 @@ void setup()
delay(1); // One millisecond pause = latch
}

Serial.print("Ada\n"); // Send ACK string to host

startTime = micros();
lastByteTime = lastAckTime = millis();

// loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.

for(;;) {

// Implementation is a simple finite-state machine.
// Regardless of mode, check for serial input each time:
t = millis();
if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
buffer[indexIn++] = c;
bytesBuffered++;
lastByteTime = lastAckTime = t; // Reset timeout counters
} else {
// No data received. If this persists, send an ACK packet
// to host once every second to alert it to our presence.
if((t - lastAckTime) > 1000) {
Serial.print("Ada\n"); // Send ACK string to host
lastAckTime = t; // Reset counter
}
// If no data received for an extended time, turn off all LEDs.
if((t - lastByteTime) > serialTimeout) {
for(c=0; c<32767; c++) {
for(SPDR=0; !(SPSR & _BV(SPIF)); );
}
delay(1); // One millisecond pause = latch
lastByteTime = t; // Reset counter
}
}

switch(mode) {
Expand Down
Loading

0 comments on commit db2ff23

Please sign in to comment.