Guest User

Untitled

a guest
May 2nd, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. // Arduino "bridge" code between host computer and WS2801-based digital
  2. // RGB LED pixels (e.g. Adafruit product ID #322). Intended for use
  3. // with USB-native boards such as Teensy or Adafruit 32u4 Breakout;
  4. // works on normal serial Arduinos, but throughput is severely limited.
  5. // LED data is streamed, not buffered, making this suitable for larger
  6. // installations (e.g. video wall, etc.) than could otherwise be held
  7. // in the Arduino's limited RAM.
  8.  
  9. // Some effort is put into avoiding buffer underruns (where the output
  10. // side becomes starved of data). The WS2801 latch protocol, being
  11. // delay-based, could be inadvertently triggered if the USB bus or CPU
  12. // is swamped with other tasks. This code buffers incoming serial data
  13. // and introduces intentional pauses if there's a threat of the buffer
  14. // draining prematurely. The cost of this complexity is somewhat
  15. // reduced throughput, the gain is that most visual glitches are
  16. // avoided (though ultimately a function of the load on the USB bus and
  17. // host CPU, and out of our control).
  18.  
  19. // LED data and clock lines are connected to the Arduino's SPI output.
  20. // On traditional Arduino boards, SPI data out is digital pin 11 and
  21. // clock is digital pin 13. On both Teensy and the 32u4 Breakout,
  22. // data out is pin B2, clock is B1. LEDs should be externally
  23. // powered -- trying to run any more than just a few off the Arduino's
  24. // 5V line is generally a Bad Idea. LED ground should also be
  25. // connected to Arduino ground.
  26.  
  27. #include <SPI.h>
  28.  
  29. // LED pin for Adafruit 32u4 Breakout Board:
  30. //#define LED_DDR DDRE
  31. //#define LED_PORT PORTE
  32. //#define LED_PIN _BV(PORTE6)
  33. // LED pin for Teensy:
  34. //#define LED_DDR DDRD
  35. //#define LED_PORT PORTD
  36. //#define LED_PIN _BV(PORTD6)
  37. // LED pin for Arduino:
  38. #define LED_DDR DDRB
  39. #define LED_PORT PORTB
  40. #define LED_PIN _BV(PORTB5)
  41.  
  42. // A 'magic word' (along with LED count & checksum) precedes each block
  43. // of LED data; this assists the microcontroller in syncing up with the
  44. // host-side software and properly issuing the latch (host I/O is
  45. // likely buffered, making usleep() unreliable for latch). You may see
  46. // an initial glitchy frame or two until the two come into alignment.
  47. // The magic word can be whatever sequence you like, but each character
  48. // should be unique, and frequent pixel values like 0 and 255 are
  49. // avoided -- fewer false positives. The host software will need to
  50. // generate a compatible header: immediately following the magic word
  51. // are three bytes: a 16-bit count of the number of LEDs (high byte
  52. // first) followed by a simple checksum value (high byte XOR low byte
  53. // XOR 0x55). LED data follows, 3 bytes per LED, in order R, G, B,
  54. // where 0 = off and 255 = max brightness.
  55.  
  56. static const uint8_t magic[] = {'A','d','a'};
  57. #define MAGICSIZE sizeof(magic)
  58. #define HEADERSIZE (MAGICSIZE + 3)
  59.  
  60. #define MODE_HEADER 0
  61. #define MODE_HOLD 1
  62. #define MODE_DATA 2
  63.  
  64. // If no serial data is received for a while, the LEDs are shut off
  65. // automatically. This avoids the annoying "stuck pixel" look when
  66. // quitting LED display programs on the host computer.
  67. static const unsigned long serialTimeout = 15000; // 15 seconds
  68.  
  69. void setup()
  70. {
  71. // Dirty trick: the circular buffer for serial data is 256 bytes,
  72. // and the "in" and "out" indices are unsigned 8-bit types -- this
  73. // much simplifies the cases where in/out need to "wrap around" the
  74. // beginning/end of the buffer. Otherwise there'd be a ton of bit-
  75. // masking and/or conditional code every time one of these indices
  76. // needs to change, slowing things down tremendously.
  77. uint8_t
  78. buffer[256],
  79. indexIn = 0,
  80. indexOut = 0,
  81. mode = MODE_HEADER,
  82. hi, lo, chk, i, spiFlag;
  83. int16_t
  84. bytesBuffered = 0,
  85. hold = 0,
  86. c;
  87. int32_t
  88. bytesRemaining;
  89. unsigned long
  90. startTime,
  91. lastByteTime,
  92. lastAckTime,
  93. t;
  94.  
  95. LED_DDR |= LED_PIN; // Enable output for LED
  96. LED_PORT &= ~LED_PIN; // LED off
  97.  
  98. Serial.begin(115200); // Teensy/32u4 disregards baud rate; is OK!
  99.  
  100. SPI.begin();
  101. SPI.setBitOrder(MSBFIRST);
  102. SPI.setDataMode(SPI_MODE0);
  103. SPI.setClockDivider(SPI_CLOCK_DIV16); // 1 MHz max, else flicker
  104.  
  105. // Issue test pattern to LEDs on startup. This helps verify that
  106. // wiring between the Arduino and LEDs is correct. Not knowing the
  107. // actual number of LEDs connected, this sets all of them (well, up
  108. // to the first 25,000, so as not to be TOO time consuming) to red,
  109. // green, blue, then off. Once you're confident everything is working
  110. // end-to-end, it's OK to comment this out and reprogram the Arduino.
  111. // uint8_t testcolor[] = { 0, 0, 0, 255, 0, 0 };
  112. // for(char n=3; n>=0; n--) {
  113. // for(c=0; c<25000; c++) {
  114. // for(i=0; i<3; i++) {
  115. // for(SPDR = testcolor[n + i]; !(SPSR & _BV(SPIF)); );
  116. // }
  117. // }
  118. // delay(1); // One millisecond pause = latch
  119. // }
  120.  
  121. Serial.print("Ada\n"); // Send ACK string to host
  122.  
  123. startTime = micros();
  124. lastByteTime = lastAckTime = millis();
  125.  
  126. // loop() is avoided as even that small bit of function overhead
  127. // has a measurable impact on this code's overall throughput.
  128.  
  129. for(;;) {
  130.  
  131. // Implementation is a simple finite-state machine.
  132. // Regardless of mode, check for serial input each time:
  133. t = millis();
  134. if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
  135. buffer[indexIn++] = c;
  136. bytesBuffered++;
  137. lastByteTime = lastAckTime = t; // Reset timeout counters
  138. } else {
  139. // No data received. If this persists, send an ACK packet
  140. // to host once every second to alert it to our presence.
  141. if((t - lastAckTime) > 1000) {
  142. Serial.print("Ada\n"); // Send ACK string to host
  143. lastAckTime = t; // Reset counter
  144. }
  145. // If no data received for an extended time, turn off all LEDs.
  146. if((t - lastByteTime) > serialTimeout) {
  147. for(c=0; c<32767; c++) {
  148. for(SPDR=0; !(SPSR & _BV(SPIF)); );
  149. }
  150. delay(1); // One millisecond pause = latch
  151. lastByteTime = t; // Reset counter
  152. }
  153. }
  154.  
  155. switch(mode) {
  156.  
  157. case MODE_HEADER:
  158.  
  159. // In header-seeking mode. Is there enough data to check?
  160. if(bytesBuffered >= HEADERSIZE) {
  161. // Indeed. Check for a 'magic word' match.
  162. for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
  163. if(i == MAGICSIZE) {
  164. // Magic word matches. Now how about the checksum?
  165. hi = buffer[indexOut++];
  166. lo = buffer[indexOut++];
  167. chk = buffer[indexOut++];
  168. if(chk == (hi ^ lo ^ 0x55)) {
  169. // Checksum looks valid. Get 16-bit LED count, add 1
  170. // (# LEDs is always > 0) and multiply by 3 for R,G,B.
  171. bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
  172. bytesBuffered -= 3;
  173. spiFlag = 0; // No data out yet
  174. mode = MODE_HOLD; // Proceed to latch wait mode
  175. } else {
  176. // Checksum didn't match; search resumes after magic word.
  177. indexOut -= 3; // Rewind
  178. }
  179. } // else no header match. Resume at first mismatched byte.
  180. bytesBuffered -= i;
  181. }
  182. break;
  183.  
  184. case MODE_HOLD:
  185.  
  186. // Ostensibly "waiting for the latch from the prior frame
  187. // to complete" mode, but may also revert to this mode when
  188. // underrun prevention necessitates a delay.
  189.  
  190. if((micros() - startTime) < hold) break; // Still holding; keep buffering
  191.  
  192. // Latch/delay complete. Advance to data-issuing mode...
  193. LED_PORT &= ~LED_PIN; // LED off
  194. mode = MODE_DATA; // ...and fall through (no break):
  195.  
  196. case MODE_DATA:
  197.  
  198. while(spiFlag && !(SPSR & _BV(SPIF))); // Wait for prior byte
  199. if(bytesRemaining > 0) {
  200. if(bytesBuffered > 0) {
  201. SPDR = buffer[indexOut++]; // Issue next byte
  202. bytesBuffered--;
  203. bytesRemaining--;
  204. spiFlag = 1;
  205. }
  206. // If serial buffer is threatening to underrun, start
  207. // introducing progressively longer pauses to allow more
  208. // data to arrive (up to a point).
  209. if((bytesBuffered < 32) && (bytesRemaining > bytesBuffered)) {
  210. startTime = micros();
  211. hold = 100 + (32 - bytesBuffered) * 10;
  212. mode = MODE_HOLD;
  213. }
  214. } else {
  215. // End of data -- issue latch:
  216. startTime = micros();
  217. hold = 1000; // Latch duration = 1000 uS
  218. LED_PORT |= LED_PIN; // LED on
  219. mode = MODE_HEADER; // Begin next header search
  220. }
  221. } // end switch
  222. } // end for(;;)
  223. }
  224.  
  225. void loop()
  226. {
  227. // Not used. See note in setup() function.
  228. }
Advertisement
Add Comment
Please, Sign In to add comment