Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.40 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. // --------------------------------------------------------------------
  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 numberd 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. #define DATA_LED A5
  83. #define SPI_LED A3
  84.  
  85. // If no serial data is received for a while, the LEDs are shut off
  86. // automatically. This avoids the annoying "stuck pixel" look when
  87. // quitting LED display programs on the host computer.
  88. static const unsigned long serialTimeout = 15000; // 15 seconds
  89.  
  90. void setup()
  91. {
  92. // Dirty trick: the circular buffer for serial data is 256 bytes,
  93. // and the "in" and "out" indices are unsigned 8-bit types -- this
  94. // much simplifies the cases where in/out need to "wrap around" the
  95. // beginning/end of the buffer. Otherwise there'd be a ton of bit-
  96. // masking and/or conditional code every time one of these indices
  97. // needs to change, slowing things down tremendously.
  98. uint8_t
  99. buffer[256],
  100. indexIn = 0,
  101. indexOut = 0,
  102. mode = MODE_HEADER,
  103. hi, lo, chk, i, spiFlag;
  104. int16_t
  105. bytesBuffered = 0,
  106. hold = 0,
  107. c;
  108. int32_t
  109. bytesRemaining;
  110. unsigned long
  111. startTime,
  112. lastByteTime,
  113. lastAckTime,
  114. t;
  115. bool
  116. data_in_led = false,
  117. spi_out_led = false;
  118.  
  119. LED_DDR |= LED_PIN; // Enable output for LED
  120. LED_PORT &= ~LED_PIN; // LED off
  121. pinMode(DATA_LED, OUTPUT); //data in led
  122. pinMode(SPI_LED, OUTPUT); //data out led
  123.  
  124. Serial.begin(115200); // Teensy/32u4 disregards baud rate; is OK!
  125.  
  126. SPI.begin();
  127. SPI.setBitOrder(MSBFIRST);
  128. SPI.setDataMode(SPI_MODE0);
  129. SPI.setClockDivider(SPI_CLOCK_DIV16); // 1 MHz max, else flicker
  130.  
  131. // Issue test pattern to LEDs on startup. This helps verify that
  132. // wiring between the Arduino and LEDs is correct. Not knowing the
  133. // actual number of LEDs connected, this sets all of them (well, up
  134. // to the first 25,000, so as not to be TOO time consuming) to red,
  135. // green, blue, then off. Once you're confident everything is working
  136. // end-to-end, it's OK to comment this out and reprogram the Arduino.
  137. uint8_t testcolor[] = { 0, 0, 0, 255, 0, 0 };
  138. for(int i=0; i<5; i++){
  139. for(SPDR = 0x00; !(SPSR & _BV(SPIF)); );
  140. }
  141. for(char n=3; n>=0; n--) {
  142. for(c=0; c<25000; c++) {
  143. for(i=0; i<3; i++) {
  144. for(SPDR = testcolor[n + i]; !(SPSR & _BV(SPIF)); );
  145. }
  146. for(i=0; i<1; i++) {
  147. for(SPDR = 0xFF; !(SPSR & _BV(SPIF)); );
  148. }
  149. }
  150. for(int i=0; i<16; i++){
  151. for(SPDR = 0x00; !(SPSR & _BV(SPIF)); );
  152. }
  153. delay(1); // One millisecond pause = latch
  154. digitalWrite(SPI_LED, spi_out_led = !spi_out_led);
  155. }
  156.  
  157. Serial.print("Ada\n"); // Send ACK string to host
  158.  
  159. startTime = micros();
  160. lastByteTime = lastAckTime = millis();
  161.  
  162. // loop() is avoided as even that small bit of function overhead
  163. // has a measurable impact on this code's overall throughput.
  164.  
  165. for(;;) {
  166. digitalWrite(DATA_LED, LOW);
  167. digitalWrite(SPI_LED, LOW);
  168. // Implementation is a simple finite-state machine.
  169. // Regardless of mode, check for serial input each time:
  170. t = millis();
  171. if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
  172. buffer[indexIn++] = c;
  173. bytesBuffered++;
  174. lastByteTime = lastAckTime = t; // Reset timeout counters
  175. } else {
  176. // No data received. If this persists, send an ACK packet
  177. // to host once every second to alert it to our presence.
  178. if((t - lastAckTime) > 1000) {
  179. Serial.print("Ada\n"); // Send ACK string to host
  180. lastAckTime = t; // Reset counter
  181. }
  182. // If no data received for an extended time, turn off all LEDs.
  183. if((t - lastByteTime) > serialTimeout) {
  184. for(c=0; c<25000; c++) {
  185. for(i=0; i<3; i++) {
  186. for(SPDR = 0x00; !(SPSR & _BV(SPIF)); );
  187. }
  188. for(i=0; i<1; i++) {
  189. for(SPDR = 0xFF; !(SPSR & _BV(SPIF)); );
  190. }
  191. }
  192. delay(1); // One millisecond pause = latch
  193. lastByteTime = t; // Reset counter
  194. }
  195. }
  196.  
  197. switch(mode) {
  198.  
  199. case MODE_HEADER:
  200.  
  201. // In header-seeking mode. Is there enough data to check?
  202. if(bytesBuffered >= HEADERSIZE) {
  203. // Indeed. Check for a 'magic word' match.
  204. for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
  205. if(i == MAGICSIZE) {
  206. // Magic word matches. Now how about the checksum?
  207. hi = buffer[indexOut++];
  208. lo = buffer[indexOut++];
  209. chk = buffer[indexOut++];
  210. if(chk == (hi ^ lo ^ 0x55)) {
  211. // Checksum looks valid. Get 16-bit LED count, add 1
  212. // (# LEDs is always > 0) and multiply by 3 for R,G,B.
  213. bytesRemaining = 4L * (256L * (long)hi + (long)lo) +4L + (256L *(long)hi + (long)lo +15)/16;
  214. bytesBuffered -= 3;
  215. spiFlag = 0; // No data out yet
  216. mode = MODE_HOLD; // Proceed to latch wait mode
  217. digitalWrite(DATA_LED, data_in_led = !data_in_led);
  218. } else {
  219. // Checksum didn't match; search resumes after magic word.
  220. indexOut -= 3; // Rewind
  221. }
  222. } // else no header match. Resume at first mismatched byte.
  223. bytesBuffered -= i;
  224. }
  225. break;
  226.  
  227. case MODE_HOLD:
  228.  
  229. // Ostensibly "waiting for the latch from the prior frame
  230. // to complete" mode, but may also revert to this mode when
  231. // underrun prevention necessitates a delay.
  232.  
  233. if((micros() - startTime) < hold) break; // Still holding; keep buffering
  234.  
  235. // Latch/delay complete. Advance to data-issuing mode...
  236. LED_PORT &= ~LED_PIN; // LED off
  237. mode = MODE_DATA; // ...and fall through (no break):
  238.  
  239. case MODE_DATA:
  240. digitalWrite(SPI_LED, spi_out_led = !spi_out_led);
  241. while(spiFlag && !(SPSR & _BV(SPIF))); // Wait for prior byte
  242. if(bytesRemaining > 0) {
  243. if(bytesBuffered > 0) {
  244. SPDR = buffer[indexOut++]; // Issue next byte
  245. bytesBuffered--;
  246. bytesRemaining--;
  247. spiFlag = 1;
  248. }
  249. // If serial buffer is threatening to underrun, start
  250. // introducing progressively longer pauses to allow more
  251. // data to arrive (up to a point).
  252. // if((bytesBuffered < 32) && (bytesRemaining > bytesBuffered)) {
  253. // startTime = micros();
  254. // hold = 100 + (32 - bytesBuffered) * 10;
  255. // mode = MODE_HOLD;
  256. //}
  257. } else {
  258. // End of data -- issue latch:
  259. startTime = micros();
  260. hold = 1000; // Latch duration = 1000 uS
  261. LED_PORT |= LED_PIN; // LED on
  262. mode = MODE_HEADER; // Begin next header search
  263. }
  264. } // end switch
  265. } // end for(;;)
  266. }
  267.  
  268. void loop()
  269. {
  270. // Not used. See note in setup() function.
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement