PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoDisplay.ino 10KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* OctoWS2811 VideoDisplay.ino - Video on LEDs, from a PC, Mac, Raspberry Pi
  2. http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
  3. Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. Update: The movie2serial program which transmit data has moved to "extras"
  20. https://github.com/PaulStoffregen/OctoWS2811/tree/master/extras
  21. Required Connections
  22. --------------------
  23. pin 2: LED Strip #1 OctoWS2811 drives 8 LED Strips.
  24. pin 14: LED strip #2 All 8 are the same length.
  25. pin 7: LED strip #3
  26. pin 8: LED strip #4 A 100 to 220 ohm resistor should used
  27. pin 6: LED strip #5 between each Teensy pin and the
  28. pin 20: LED strip #6 wire to the LED strip, to minimize
  29. pin 21: LED strip #7 high frequency ringining & noise.
  30. pin 5: LED strip #8
  31. pin 15 & 16 - Connect together, but do not use
  32. pin 4: Do not use
  33. pin 3: Do not use as PWM. Normal use is ok.
  34. pin 12: Frame Sync
  35. When using more than 1 Teensy to display a video image, connect
  36. the Frame Sync signal between every board. All boards will
  37. synchronize their WS2811 update using this signal.
  38. Beware of image distortion from long LED strip lengths. During
  39. the WS2811 update, the LEDs update in sequence, not all at the
  40. same instant! The first pixel updates after 30 microseconds,
  41. the second pixel after 60 us, and so on. A strip of 120 LEDs
  42. updates in 3.6 ms, which is 10.8% of a 30 Hz video frame time.
  43. Doubling the strip length to 240 LEDs increases the lag to 21.6%
  44. of a video frame. For best results, use shorter length strips.
  45. Multiple boards linked by the frame sync signal provides superior
  46. video timing accuracy.
  47. A Multi-TT USB hub should be used if 2 or more Teensy boards
  48. are connected. The Multi-TT feature allows proper USB bandwidth
  49. allocation. Single-TT hubs, or direct connection to multiple
  50. ports on the same motherboard, may give poor performance.
  51. */
  52. #include <OctoWS2811.h>
  53. // The actual arrangement of the LEDs connected to this Teensy 3.0 board.
  54. // LED_HEIGHT *must* be a multiple of 8. When 16, 24, 32 are used, each
  55. // strip spans 2, 3, 4 rows. LED_LAYOUT indicates the direction the strips
  56. // are arranged. If 0, each strip begins on the left for its first row,
  57. // then goes right to left for its second row, then left to right,
  58. // zig-zagging for each successive row.
  59. #define LED_WIDTH 60 // number of LEDs horizontally
  60. #define LED_HEIGHT 16 // number of LEDs vertically (must be multiple of 8)
  61. #define LED_LAYOUT 0 // 0 = even rows left->right, 1 = even rows right->left
  62. // The portion of the video image to show on this set of LEDs. All 4 numbers
  63. // are percentages, from 0 to 100. For a large LED installation with many
  64. // Teensy 3.0 boards driving groups of LEDs, these parameters allow you to
  65. // program each Teensy to tell the video application which portion of the
  66. // video it displays. By reading these numbers, the video application can
  67. // automatically configure itself, regardless of which serial port COM number
  68. // or device names are assigned to each Teensy 3.0 by your operating system.
  69. #define VIDEO_XOFFSET 0
  70. #define VIDEO_YOFFSET 0 // display entire image
  71. #define VIDEO_WIDTH 100
  72. #define VIDEO_HEIGHT 100
  73. //#define VIDEO_XOFFSET 0
  74. //#define VIDEO_YOFFSET 0 // display upper half
  75. //#define VIDEO_WIDTH 100
  76. //#define VIDEO_HEIGHT 50
  77. //#define VIDEO_XOFFSET 0
  78. //#define VIDEO_YOFFSET 50 // display lower half
  79. //#define VIDEO_WIDTH 100
  80. //#define VIDEO_HEIGHT 50
  81. const int ledsPerStrip = LED_WIDTH * LED_HEIGHT / 8;
  82. DMAMEM int displayMemory[ledsPerStrip*6];
  83. int drawingMemory[ledsPerStrip*6];
  84. elapsedMicros elapsedUsecSinceLastFrameSync = 0;
  85. const int config = WS2811_800kHz; // color config is on the PC side
  86. OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config);
  87. void setup() {
  88. pinMode(12, INPUT_PULLUP); // Frame Sync
  89. Serial.setTimeout(50);
  90. leds.begin();
  91. leds.show();
  92. }
  93. void loop() {
  94. //
  95. // wait for a Start-Of-Message character:
  96. //
  97. // '*' = Frame of image data, with frame sync pulse to be sent
  98. // a specified number of microseconds after reception of
  99. // the first byte (typically at 75% of the frame time, to
  100. // allow other boards to fully receive their data).
  101. // Normally '*' is used when the sender controls the pace
  102. // of playback by transmitting each frame as it should
  103. // appear.
  104. //
  105. // '$' = Frame of image data, with frame sync pulse to be sent
  106. // a specified number of microseconds after the previous
  107. // frame sync. Normally this is used when the sender
  108. // transmits each frame as quickly as possible, and we
  109. // control the pacing of video playback by updating the
  110. // LEDs based on time elapsed from the previous frame.
  111. //
  112. // '%' = Frame of image data, to be displayed with a frame sync
  113. // pulse is received from another board. In a multi-board
  114. // system, the sender would normally transmit one '*' or '$'
  115. // message and '%' messages to all other boards, so every
  116. // Teensy 3.0 updates at the exact same moment.
  117. //
  118. // '@' = Reset the elapsed time, used for '$' messages. This
  119. // should be sent before the first '$' message, so many
  120. // frames are not played quickly if time as elapsed since
  121. // startup or prior video playing.
  122. //
  123. // '?' = Query LED and Video parameters. Teensy 3.0 responds
  124. // with a comma delimited list of information.
  125. //
  126. int startChar = Serial.read();
  127. if (startChar == '*') {
  128. // receive a "master" frame - we send the frame sync to other boards
  129. // the sender is controlling the video pace. The 16 bit number is
  130. // how far into this frame to send the sync to other boards.
  131. unsigned int startAt = micros();
  132. unsigned int usecUntilFrameSync = 0;
  133. int count = Serial.readBytes((char *)&usecUntilFrameSync, 2);
  134. if (count != 2) return;
  135. count = Serial.readBytes((char *)drawingMemory, sizeof(drawingMemory));
  136. if (count == sizeof(drawingMemory)) {
  137. unsigned int endAt = micros();
  138. unsigned int usToWaitBeforeSyncOutput = 100;
  139. if (endAt - startAt < usecUntilFrameSync) {
  140. usToWaitBeforeSyncOutput = usecUntilFrameSync - (endAt - startAt);
  141. }
  142. digitalWrite(12, HIGH);
  143. pinMode(12, OUTPUT);
  144. delayMicroseconds(usToWaitBeforeSyncOutput);
  145. digitalWrite(12, LOW);
  146. // WS2811 update begins immediately after falling edge of frame sync
  147. digitalWrite(13, HIGH);
  148. leds.show();
  149. digitalWrite(13, LOW);
  150. }
  151. } else if (startChar == '$') {
  152. // receive a "master" frame - we send the frame sync to other boards
  153. // we are controlling the video pace. The 16 bit number is how long
  154. // after the prior frame sync to wait until showing this frame
  155. unsigned int usecUntilFrameSync = 0;
  156. int count = Serial.readBytes((char *)&usecUntilFrameSync, 2);
  157. if (count != 2) return;
  158. count = Serial.readBytes((char *)drawingMemory, sizeof(drawingMemory));
  159. if (count == sizeof(drawingMemory)) {
  160. digitalWrite(12, HIGH);
  161. pinMode(12, OUTPUT);
  162. while (elapsedUsecSinceLastFrameSync < usecUntilFrameSync) /* wait */ ;
  163. elapsedUsecSinceLastFrameSync -= usecUntilFrameSync;
  164. digitalWrite(12, LOW);
  165. // WS2811 update begins immediately after falling edge of frame sync
  166. digitalWrite(13, HIGH);
  167. leds.show();
  168. digitalWrite(13, LOW);
  169. }
  170. } else if (startChar == '%') {
  171. // receive a "slave" frame - wait to show it until the frame sync arrives
  172. pinMode(12, INPUT_PULLUP);
  173. unsigned int unusedField = 0;
  174. int count = Serial.readBytes((char *)&unusedField, 2);
  175. if (count != 2) return;
  176. count = Serial.readBytes((char *)drawingMemory, sizeof(drawingMemory));
  177. if (count == sizeof(drawingMemory)) {
  178. elapsedMillis wait = 0;
  179. while (digitalRead(12) != HIGH && wait < 30) ; // wait for sync high
  180. while (digitalRead(12) != LOW && wait < 30) ; // wait for sync high->low
  181. // WS2811 update begins immediately after falling edge of frame sync
  182. if (wait < 30) {
  183. digitalWrite(13, HIGH);
  184. leds.show();
  185. digitalWrite(13, LOW);
  186. }
  187. }
  188. } else if (startChar == '@') {
  189. // reset the elapsed frame time, for startup of '$' message playing
  190. elapsedUsecSinceLastFrameSync = 0;
  191. } else if (startChar == '?') {
  192. // when the video application asks, give it all our info
  193. // for easy and automatic configuration
  194. Serial.print(LED_WIDTH);
  195. Serial.write(',');
  196. Serial.print(LED_HEIGHT);
  197. Serial.write(',');
  198. Serial.print(LED_LAYOUT);
  199. Serial.write(',');
  200. Serial.print(0);
  201. Serial.write(',');
  202. Serial.print(0);
  203. Serial.write(',');
  204. Serial.print(VIDEO_XOFFSET);
  205. Serial.write(',');
  206. Serial.print(VIDEO_YOFFSET);
  207. Serial.write(',');
  208. Serial.print(VIDEO_WIDTH);
  209. Serial.write(',');
  210. Serial.print(VIDEO_HEIGHT);
  211. Serial.write(',');
  212. Serial.print(0);
  213. Serial.write(',');
  214. Serial.print(0);
  215. Serial.write(',');
  216. Serial.print(0);
  217. Serial.println();
  218. } else if (startChar >= 0) {
  219. // discard unknown characters
  220. }
  221. }