Guest User

Untitled

a guest
Jan 6th, 2016
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // --------------------------------------------------------------------
  28. // This file is part of Adalight.
  29.  
  30. // Adalight is free software: you can redistribute it and/or modify
  31. // it under the terms of the GNU Lesser General Public License as
  32. // published by the Free Software Foundation, either version 3 of
  33. // the License, or (at your option) any later version.
  34.  
  35. // Adalight is distributed in the hope that it will be useful,
  36. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. // GNU Lesser General Public License for more details.
  39.  
  40. // You should have received a copy of the GNU Lesser General Public
  41. // License along with Adalight. If not, see
  42. // <http://www.gnu.org/licenses/>.
  43. // --------------------------------------------------------------------
  44.  
  45. #include <SPI.h>
  46.  
  47. // LED pin for Adafruit 32u4 Breakout Board:
  48. //#define LED_DDR DDRE
  49. //#define LED_PORT PORTE
  50. //#define LED_PIN _BV(PORTE6)
  51. // LED pin for Teensy:
  52. //#define LED_DDR DDRD
  53. //#define LED_PORT PORTD
  54. //#define LED_PIN _BV(PORTD6)
  55. // LED pin for Arduino:
  56. #define LED_DDR DDRB
  57. #define LED_PORT PORTB
  58. #define LED_PIN _BV(PORTB5)
  59.  
  60. // A 'magic word' (along with LED count & checksum) precedes each block
  61. // of LED data; this assists the microcontroller in syncing up with the
  62. // host-side software and properly issuing the latch (host I/O is
  63. // likely buffered, making usleep() unreliable for latch). You may see
  64. // an initial glitchy frame or two until the two come into alignment.
  65. // The magic word can be whatever sequence you like, but each character
  66. // should be unique, and frequent pixel values like 0 and 255 are
  67. // avoided -- fewer false positives. The host software will need to
  68. // generate a compatible header: immediately following the magic word
  69. // are three bytes: a 16-bit count of the number of LEDs (high byte
  70. // first) followed by a simple checksum value (high byte XOR low byte
  71. // XOR 0x55). LED data follows, 3 bytes per LED, in order R, G, B,
  72. // where 0 = off and 255 = max brightness.
  73.  
  74. static const uint8_t magic[] = {'A','d','a'};
  75. #define MAGICSIZE sizeof(magic)
  76. #define HEADERSIZE (MAGICSIZE + 3)
  77.  
  78. #define MODE_HEADER 0
  79. #define MODE_HOLD 1
  80. #define MODE_DATA 2
  81.  
  82. // If no serial data is received for a while, the LEDs are shut off
  83. // automatically. This avoids the annoying "stuck pixel" look when
  84. // quitting LED display programs on the host computer.
  85. static const unsigned long serialTimeout = 15000; // 15 seconds
  86.  
  87. void setup()
  88. {
  89. // Dirty trick: the circular buffer for serial data is 256 bytes,
  90. // and the "in" and "out" indices are unsigned 8-bit types -- this
  91. // much simplifies the cases where in/out need to "wrap around" the
  92. // beginning/end of the buffer. Otherwise there'd be a ton of bit-
  93. // masking and/or conditional code every time one of these indices
  94. // needs to change, slowing things down tremendously.
  95. uint8_t
  96. buffer[256],
  97. indexIn = 0,
  98. indexOut = 0,
  99. mode = MODE_HEADER,
  100. hi, lo, chk, i, spiFlag;
  101. int16_t
  102. bytesBuffered = 0,
  103. hold = 0,
  104. c;
  105. int32_t
  106. bytesRemaining;
  107. unsigned long
  108. startTime,
  109. lastByteTime,
  110. lastAckTime,
  111. t;
  112.  
  113. LED_DDR |= LED_PIN; // Enable output for LED
  114. LED_PORT &= ~LED_PIN; // LED off
  115.  
  116. Serial.begin(115200); // Teensy/32u4 disregards baud rate; is OK!
  117.  
  118. SPI.begin();
  119. SPI.setBitOrder(MSBFIRST);
  120. SPI.setDataMode(SPI_MODE0);
  121. SPI.setClockDivider(SPI_CLOCK_DIV16); // 1 MHz max, else flicker
  122.  
  123. // Issue test pattern to LEDs on startup. This helps verify that
  124. // wiring between the Arduino and LEDs is correct. Not knowing the
  125. // actual number of LEDs connected, this sets all of them (well, up
  126. // to the first 25,000, so as not to be TOO time consuming) to red,
  127. // green, blue, then off. Once you're confident everything is working
  128. // end-to-end, it's OK to comment this out and reprogram the Arduino.
  129. uint8_t testcolor[] = { 0, 0, 0, 255, 0, 0 };
  130. for(char n=3; n>=0; n--) {
  131. for(c=0; c<25000; c++) {
  132. for(i=0; i<3; i++) {
  133. for(SPDR = testcolor[n + i]; !(SPSR & _BV(SPIF)); );
  134. }
  135. }
  136. delay(1); // One millisecond pause = latch
  137. }
  138.  
  139. Serial.print("Ada\n"); // Send ACK string to host
  140.  
  141. startTime = micros();
  142. lastByteTime = lastAckTime = millis();
  143.  
  144. // loop() is avoided as even that small bit of function overhead
  145. // has a measurable impact on this code's overall throughput.
  146.  
  147. for(;;) {
  148.  
  149. // Implementation is a simple finite-state machine.
  150. // Regardless of mode, check for serial input each time:
  151. t = millis();
  152. if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
  153. buffer[indexIn++] = c;
  154. bytesBuffered++;
  155. lastByteTime = lastAckTime = t; // Reset timeout counters
  156. } else {
  157. // No data received. If this persists, send an ACK packet
  158. // to host once every second to alert it to our presence.
  159. if((t - lastAckTime) > 1000) {
  160. Serial.print("Ada\n"); // Send ACK string to host
  161. lastAckTime = t; // Reset counter
  162. }
  163. // If no data received for an extended time, turn off all LEDs.
  164. if((t - lastByteTime) > serialTimeout) {
  165. for(c=0; c<32767; c++) {
  166. for(SPDR=0; !(SPSR & _BV(SPIF)); );
  167. }
  168. delay(1); // One millisecond pause = latch
  169. lastByteTime = t; // Reset counter
  170. }
  171. }
  172.  
  173. switch(mode) {
  174.  
  175. case MODE_HEADER:
  176.  
  177. // In header-seeking mode. Is there enough data to check?
  178. if(bytesBuffered >= HEADERSIZE) {
  179. // Indeed. Check for a 'magic word' match.
  180. for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
  181. if(i == MAGICSIZE) {
  182. // Magic word matches. Now how about the checksum?
  183. hi = buffer[indexOut++];
  184. lo = buffer[indexOut++];
  185. chk = buffer[indexOut++];
  186. if(chk == (hi ^ lo ^ 0x55)) {
  187. // Checksum looks valid. Get 16-bit LED count, add 1
  188. // (# LEDs is always > 0) and multiply by 3 for R,G,B.
  189. bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
  190. bytesBuffered -= 3;
  191. spiFlag = 0; // No data out yet
  192. mode = MODE_HOLD; // Proceed to latch wait mode
  193. } else {
  194. // Checksum didn't match; search resumes after magic word.
  195. indexOut -= 3; // Rewind
  196. }
  197. } // else no header match. Resume at first mismatched byte.
  198. bytesBuffered -= i;
  199. }
  200. break;
  201.  
  202. case MODE_HOLD:
  203.  
  204. // Ostensibly "waiting for the latch from the prior frame
  205. // to complete" mode, but may also revert to this mode when
  206. // underrun prevention necessitates a delay.
  207.  
  208. if((micros() - startTime) < hold) break; // Still holding; keep buffering
  209.  
  210. // Latch/delay complete. Advance to data-issuing mode...
  211. LED_PORT &= ~LED_PIN; // LED off
  212. mode = MODE_DATA; // ...and fall through (no break):
  213.  
  214. case MODE_DATA:
  215.  
  216. while(spiFlag && !(SPSR & _BV(SPIF))); // Wait for prior byte
  217. if(bytesRemaining > 0) {
  218. if(bytesBuffered > 0) {
  219. SPDR = buffer[indexOut++]; // Issue next byte
  220. bytesBuffered--;
  221. bytesRemaining--;
  222. spiFlag = 1;
  223. }
  224. // If serial buffer is threatening to underrun, start
  225. // introducing progressively longer pauses to allow more
  226. // data to arrive (up to a point).
  227. if((bytesBuffered < 32) && (bytesRemaining > bytesBuffered)) {
  228. startTime = micros();
  229. hold = 100 + (32 - bytesBuffered) * 10;
  230. mode = MODE_HOLD;
  231. }
  232. } else {
  233. // End of data -- issue latch:
  234. startTime = micros();
  235. hold = 1000; // Latch duration = 1000 uS
  236. LED_PORT |= LED_PIN; // LED on
  237. mode = MODE_HEADER; // Begin next header search
  238. }
  239. } // end switch
  240. } // end for(;;)
  241. }
  242.  
  243. void loop()
  244. {
  245. // Not used. See note in setup() function.
  246. }
RAW Paste Data