Merge pull request #7 from dmadison/whitespace

Reworked whitespace
This commit is contained in:
David Madison 2017-03-27 17:58:11 -04:00 committed by GitHub
commit 672f401a4d
1 changed files with 129 additions and 128 deletions

View File

@ -9,9 +9,9 @@
// --- General Settings // --- General Settings
static const uint8_t static const uint8_t
Num_Leds = 80, // strip length Num_Leds = 80, // strip length
Led_Pin = 6, // Arduino data output pin Led_Pin = 6, // Arduino data output pin
Brightness = 255; // maximum brightness Brightness = 255; // maximum brightness
// --- FastLED Setings // --- FastLED Setings
#define LED_TYPE WS2812B // led strip type for FastLED #define LED_TYPE WS2812B // led strip type for FastLED
@ -19,10 +19,10 @@ static const uint8_t
// --- Serial Settings // --- Serial Settings
static const unsigned long static const unsigned long
SerialSpeed = 115200, // serial port speed, max available SerialSpeed = 115200, // serial port speed, max available
SerialTimeout = 150000; // time before LEDs are shut off, if no data SerialTimeout = 150000; // time before LEDs are shut off, if no data
// (150 seconds) // (150 seconds)
// --- Optional Settings (uncomment to add) // --- Optional Settings (uncomment to add)
//#define CLEAR_ON_START // LEDs are cleared on reset //#define CLEAR_ON_START // LEDs are cleared on reset
//#define GROUND_PIN 10 // additional grounding pin (optional) //#define GROUND_PIN 10 // additional grounding pin (optional)
@ -38,19 +38,19 @@ uint8_t * ledsRaw = (uint8_t *)leds;
// A 'magic word' (along with LED count & checksum) precedes each block // A 'magic word' (along with LED count & checksum) precedes each block
// of LED data; this assists the microcontroller in syncing up with the // of LED data; this assists the microcontroller in syncing up with the
// host-side software and properly issuing the latch (host I/O is // host-side software and properly issuing the latch (host I/O is
// likely buffered, making usleep() unreliable for latch). You may see // likely buffered, making usleep() unreliable for latch). You may see
// an initial glitchy frame or two until the two come into alignment. // an initial glitchy frame or two until the two come into alignment.
// The magic word can be whatever sequence you like, but each character // The magic word can be whatever sequence you like, but each character
// should be unique, and frequent pixel values like 0 and 255 are // should be unique, and frequent pixel values like 0 and 255 are
// avoided -- fewer false positives. The host software will need to // avoided -- fewer false positives. The host software will need to
// generate a compatible header: immediately following the magic word // generate a compatible header: immediately following the magic word
// are three bytes: a 16-bit count of the number of LEDs (high byte // are three bytes: a 16-bit count of the number of LEDs (high byte
// first) followed by a simple checksum value (high byte XOR low byte // first) followed by a simple checksum value (high byte XOR low byte
// XOR 0x55). LED data follows, 3 bytes per LED, in order R, G, B, // XOR 0x55). LED data follows, 3 bytes per LED, in order R, G, B,
// where 0 = off and 255 = max brightness. // where 0 = off and 255 = max brightness.
static const uint8_t magic[] = { static const uint8_t magic[] = {
'A','d','a'}; 'A','d','a'};
#define MAGICSIZE sizeof(magic) #define MAGICSIZE sizeof(magic)
#define HEADERSIZE (MAGICSIZE + 3) #define HEADERSIZE (MAGICSIZE + 3)
@ -58,143 +58,144 @@ static const uint8_t magic[] = {
#define MODE_DATA 2 #define MODE_DATA 2
void setup(){ void setup(){
#ifdef GROUND_PIN #ifdef GROUND_PIN
pinMode(GROUND_PIN, OUTPUT); pinMode(GROUND_PIN, OUTPUT);
digitalWrite(GROUND_PIN, LOW); digitalWrite(GROUND_PIN, LOW);
#endif #endif
FastLED.addLeds<LED_TYPE, Led_Pin, COLOR_ORDER>(leds, Num_Leds); FastLED.addLeds<LED_TYPE, Led_Pin, COLOR_ORDER>(leds, Num_Leds);
FastLED.setBrightness(Brightness); FastLED.setBrightness(Brightness);
#ifdef CLEAR_ON_START #ifdef CLEAR_ON_START
FastLED.show(); FastLED.show();
#endif #endif
Serial.begin(SerialSpeed); Serial.begin(SerialSpeed);
adalight(); adalight();
} }
void adalight(){ void adalight(){
// Dirty trick: the circular buffer for serial data is 256 bytes, // Dirty trick: the circular buffer for serial data is 256 bytes,
// and the "in" and "out" indices are unsigned 8-bit types -- this // and the "in" and "out" indices are unsigned 8-bit types -- this
// much simplifies the cases where in/out need to "wrap around" the // much simplifies the cases where in/out need to "wrap around" the
// beginning/end of the buffer. Otherwise there'd be a ton of bit- // beginning/end of the buffer. Otherwise there'd be a ton of bit-
// masking and/or conditional code every time one of these indices // masking and/or conditional code every time one of these indices
// needs to change, slowing things down tremendously. // needs to change, slowing things down tremendously.
uint8_t
buffer[256],
indexIn = 0,
indexOut = 0,
mode = MODE_HEADER,
hi, lo, chk, i;
int16_t
c;
uint16_t
bytesBuffered = 0;
uint32_t
bytesRemaining,
outPos;
unsigned long
lastByteTime,
lastAckTime,
t;
Serial.print("Ada\n"); // Send ACK string to host uint8_t
buffer[256],
indexIn = 0,
indexOut = 0,
mode = MODE_HEADER,
hi, lo, chk, i;
int16_t
c;
uint16_t
bytesBuffered = 0;
uint32_t
bytesRemaining,
outPos;
unsigned long
lastByteTime,
lastAckTime,
t;
lastByteTime = lastAckTime = millis(); Serial.print("Ada\n"); // Send ACK string to host
// loop() is avoided as even that small bit of function overhead lastByteTime = lastAckTime = millis();
// has a measurable impact on this code's overall throughput.
for(;;) { // loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.
// Implementation is a simple finite-state machine. for(;;) {
// Regardless of mode, check for serial input each time:
t = millis(); // Implementation is a simple finite-state machine.
if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) { // Regardless of mode, check for serial input each time:
buffer[indexIn++] = c; t = millis();
bytesBuffered++; if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
lastByteTime = lastAckTime = t; // Reset timeout counters buffer[indexIn++] = c;
} bytesBuffered++;
else { lastByteTime = lastAckTime = t; // Reset timeout counters
// No data received. If this persists, send an ACK packet }
// to host once every second to alert it to our presence. else {
if((t - lastAckTime) > 1000) { // No data received. If this persists, send an ACK packet
Serial.print("Ada\n"); // Send ACK string to host // to host once every second to alert it to our presence.
lastAckTime = t; // Reset counter if((t - lastAckTime) > 1000) {
} Serial.print("Ada\n"); // Send ACK string to host
// If no data received for an extended time, turn off all LEDs. lastAckTime = t; // Reset counter
if((t - lastByteTime) > SerialTimeout) { }
memset(leds, 0, Num_Leds * sizeof(struct CRGB)); //filling Led array by zeroes // If no data received for an extended time, turn off all LEDs.
FastLED.show(); if((t - lastByteTime) > SerialTimeout) {
lastByteTime = t; // Reset counter memset(leds, 0, Num_Leds * sizeof(struct CRGB)); //filling Led array by zeroes
} FastLED.show();
} lastByteTime = t; // Reset counter
}
}
switch(mode) { switch(mode) {
case MODE_HEADER: case MODE_HEADER:
// In header-seeking mode. Is there enough data to check? // In header-seeking mode. Is there enough data to check?
if(bytesBuffered >= HEADERSIZE) { if(bytesBuffered >= HEADERSIZE) {
// Indeed. Check for a 'magic word' match. // Indeed. Check for a 'magic word' match.
for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]);); for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
if(i == MAGICSIZE) { if(i == MAGICSIZE) {
// Magic word matches. Now how about the checksum? // Magic word matches. Now how about the checksum?
hi = buffer[indexOut++]; hi = buffer[indexOut++];
lo = buffer[indexOut++]; lo = buffer[indexOut++];
chk = buffer[indexOut++]; chk = buffer[indexOut++];
if(chk == (hi ^ lo ^ 0x55)) { if(chk == (hi ^ lo ^ 0x55)) {
// Checksum looks valid. Get 16-bit LED count, add 1 // Checksum looks valid. Get 16-bit LED count, add 1
// (# LEDs is always > 0) and multiply by 3 for R,G,B. // (# LEDs is always > 0) and multiply by 3 for R,G,B.
bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L); bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
bytesBuffered -= 3; bytesBuffered -= 3;
outPos = 0; outPos = 0;
memset(leds, 0, Num_Leds * sizeof(struct CRGB)); memset(leds, 0, Num_Leds * sizeof(struct CRGB));
mode = MODE_DATA; // Proceed to latch wait mode mode = MODE_DATA; // Proceed to latch wait mode
} }
else { else {
// Checksum didn't match; search resumes after magic word. // Checksum didn't match; search resumes after magic word.
indexOut -= 3; // Rewind indexOut -= 3; // Rewind
} }
} // else no header match. Resume at first mismatched byte. } // else no header match. Resume at first mismatched byte.
bytesBuffered -= i; bytesBuffered -= i;
} }
break; break;
case MODE_DATA: case MODE_DATA:
if(bytesRemaining > 0) { if(bytesRemaining > 0) {
if(bytesBuffered > 0) { if(bytesBuffered > 0) {
if (outPos < sizeof(leds)){ if (outPos < sizeof(leds)){
#ifdef CALIBRATE #ifdef CALIBRATE
if(outPos < 3) if(outPos < 3)
ledsRaw[outPos++] = buffer[indexOut++]; ledsRaw[outPos++] = buffer[indexOut++];
else{ else{
ledsRaw[outPos] = ledsRaw[outPos%3]; // Sets RGB data to first LED color ledsRaw[outPos] = ledsRaw[outPos%3]; // Sets RGB data to first LED color
outPos++; outPos++;
indexOut++; indexOut++;
} }
#else #else
ledsRaw[outPos++] = buffer[indexOut++]; // Issue next byte ledsRaw[outPos++] = buffer[indexOut++]; // Issue next byte
#endif #endif
} }
bytesBuffered--; bytesBuffered--;
bytesRemaining--; bytesRemaining--;
} }
} }
else { else {
// End of data -- issue latch: // End of data -- issue latch:
mode = MODE_HEADER; // Begin next header search mode = MODE_HEADER; // Begin next header search
FastLED.show(); FastLED.show();
} }
} // end switch } // end switch
} // end for(;;) } // end for(;;)
} }
void loop() void loop()
{ {
// Not used. See note in adalight() function. // loop() is avoided as even that small bit of function overhead
// has a measurable impact on this code's overall throughput.
} }