PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 satır
9.2KB

  1. /* OctoWS2811 movie2serial.pde - Transmit video data to 1 or more
  2. Teensy 3.0 boards running OctoWS2811 VideoDisplay.ino
  3. http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
  4. Copyright (c) 2018 Paul Stoffregen, PJRC.COM, LLC
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. // Linux systems (including Raspberry Pi) require 49-teensy.rules in
  22. // /etc/udev/rules.d/, and gstreamer compatible with Processing's
  23. // video library.
  24. // To configure this program, edit the following sections:
  25. //
  26. // 1: change myMovie to open a video file of your choice ;-)
  27. //
  28. // 2: edit the serialConfigure() lines in setup() for your
  29. // serial device names (Mac, Linux) or COM ports (Windows)
  30. //
  31. // 3: if your LED strips have unusual color configuration,
  32. // edit colorWiring(). Nearly all strips have GRB wiring,
  33. // so normally you can leave this as-is.
  34. //
  35. // 4: if playing 50 or 60 Hz progressive video (or faster),
  36. // edit framerate in movieEvent().
  37. import processing.video.*;
  38. import processing.serial.*;
  39. import java.awt.Rectangle;
  40. Movie myMovie;
  41. float gamma = 1.7;
  42. int numPorts=0; // the number of serial ports in use
  43. int maxPorts=24; // maximum number of serial ports
  44. Serial[] ledSerial = new Serial[maxPorts]; // each port's actual Serial port
  45. Rectangle[] ledArea = new Rectangle[maxPorts]; // the area of the movie each port gets, in % (0-100)
  46. boolean[] ledLayout = new boolean[maxPorts]; // layout of rows, true = even is left->right
  47. PImage[] ledImage = new PImage[maxPorts]; // image sent to each port
  48. int[] gammatable = new int[256];
  49. int errorCount=0;
  50. float framerate=0;
  51. void settings() {
  52. size(480, 400); // create the window
  53. }
  54. void setup() {
  55. String[] list = Serial.list();
  56. delay(20);
  57. println("Serial Ports List:");
  58. println(list);
  59. serialConfigure("/dev/ttyACM0"); // change these to your port names
  60. serialConfigure("/dev/ttyACM1");
  61. if (errorCount > 0) exit();
  62. for (int i=0; i < 256; i++) {
  63. gammatable[i] = (int)(pow((float)i / 255.0, gamma) * 255.0 + 0.5);
  64. }
  65. myMovie = new Movie(this, "/tmp/Toy_Story.avi");
  66. myMovie.loop(); // start the movie :-)
  67. }
  68. // movieEvent runs for each new frame of movie data
  69. void movieEvent(Movie m) {
  70. println("movieEvent");
  71. // read the movie's next frame
  72. m.read();
  73. //if (framerate == 0) framerate = m.getSourceFrameRate();
  74. framerate = 30.0; // TODO, how to read the frame rate???
  75. for (int i=0; i < numPorts; i++) {
  76. // copy a portion of the movie's image to the LED image
  77. int xoffset = percentage(m.width, ledArea[i].x);
  78. int yoffset = percentage(m.height, ledArea[i].y);
  79. int xwidth = percentage(m.width, ledArea[i].width);
  80. int yheight = percentage(m.height, ledArea[i].height);
  81. ledImage[i].copy(m, xoffset, yoffset, xwidth, yheight,
  82. 0, 0, ledImage[i].width, ledImage[i].height);
  83. // convert the LED image to raw data
  84. byte[] ledData = new byte[(ledImage[i].width * ledImage[i].height * 3) + 3];
  85. image2data(ledImage[i], ledData, ledLayout[i]);
  86. if (i == 0) {
  87. ledData[0] = '*'; // first Teensy is the frame sync master
  88. int usec = (int)((1000000.0 / framerate) * 0.75);
  89. ledData[1] = (byte)(usec); // request the frame sync pulse
  90. ledData[2] = (byte)(usec >> 8); // at 75% of the frame time
  91. } else {
  92. ledData[0] = '%'; // others sync to the master board
  93. ledData[1] = 0;
  94. ledData[2] = 0;
  95. }
  96. // send the raw data to the LEDs :-)
  97. ledSerial[i].write(ledData);
  98. }
  99. }
  100. // image2data converts an image to OctoWS2811's raw data format.
  101. // The number of vertical pixels in the image must be a multiple
  102. // of 8. The data array must be the proper size for the image.
  103. void image2data(PImage image, byte[] data, boolean layout) {
  104. int offset = 3;
  105. int x, y, xbegin, xend, xinc, mask;
  106. int linesPerPin = image.height / 8;
  107. int pixel[] = new int[8];
  108. for (y = 0; y < linesPerPin; y++) {
  109. if ((y & 1) == (layout ? 0 : 1)) {
  110. // even numbered rows are left to right
  111. xbegin = 0;
  112. xend = image.width;
  113. xinc = 1;
  114. } else {
  115. // odd numbered rows are right to left
  116. xbegin = image.width - 1;
  117. xend = -1;
  118. xinc = -1;
  119. }
  120. for (x = xbegin; x != xend; x += xinc) {
  121. for (int i=0; i < 8; i++) {
  122. // fetch 8 pixels from the image, 1 for each pin
  123. pixel[i] = image.pixels[x + (y + linesPerPin * i) * image.width];
  124. pixel[i] = colorWiring(pixel[i]);
  125. }
  126. // convert 8 pixels to 24 bytes
  127. for (mask = 0x800000; mask != 0; mask >>= 1) {
  128. byte b = 0;
  129. for (int i=0; i < 8; i++) {
  130. if ((pixel[i] & mask) != 0) b |= (1 << i);
  131. }
  132. data[offset++] = b;
  133. }
  134. }
  135. }
  136. }
  137. // translate the 24 bit color from RGB to the actual
  138. // order used by the LED wiring. GRB is the most common.
  139. int colorWiring(int c) {
  140. int red = (c & 0xFF0000) >> 16;
  141. int green = (c & 0x00FF00) >> 8;
  142. int blue = (c & 0x0000FF);
  143. red = gammatable[red];
  144. green = gammatable[green];
  145. blue = gammatable[blue];
  146. return (green << 16) | (red << 8) | (blue); // GRB - most common wiring
  147. }
  148. // ask a Teensy board for its LED configuration, and set up the info for it.
  149. void serialConfigure(String portName) {
  150. if (numPorts >= maxPorts) {
  151. println("too many serial ports, please increase maxPorts");
  152. errorCount++;
  153. return;
  154. }
  155. try {
  156. ledSerial[numPorts] = new Serial(this, portName);
  157. if (ledSerial[numPorts] == null) throw new NullPointerException();
  158. ledSerial[numPorts].write('?');
  159. } catch (Throwable e) {
  160. println("Serial port " + portName + " does not exist or is non-functional");
  161. errorCount++;
  162. return;
  163. }
  164. delay(50);
  165. String line = ledSerial[numPorts].readStringUntil(10);
  166. if (line == null) {
  167. println("Serial port " + portName + " is not responding.");
  168. println("Is it really a Teensy 3.0 running VideoDisplay?");
  169. errorCount++;
  170. return;
  171. }
  172. String param[] = line.split(",");
  173. if (param.length != 12) {
  174. println("Error: port " + portName + " did not respond to LED config query");
  175. errorCount++;
  176. return;
  177. }
  178. // only store the info and increase numPorts if Teensy responds properly
  179. ledImage[numPorts] = new PImage(Integer.parseInt(param[0]), Integer.parseInt(param[1]), RGB);
  180. ledArea[numPorts] = new Rectangle(Integer.parseInt(param[5]), Integer.parseInt(param[6]),
  181. Integer.parseInt(param[7]), Integer.parseInt(param[8]));
  182. ledLayout[numPorts] = (Integer.parseInt(param[5]) == 0);
  183. numPorts++;
  184. }
  185. // draw runs every time the screen is redrawn - show the movie...
  186. void draw() {
  187. //println("draw");
  188. // show the original video
  189. image(myMovie, 0, 80);
  190. // then try to show what was most recently sent to the LEDs
  191. // by displaying all the images for each port.
  192. for (int i=0; i < numPorts; i++) {
  193. // compute the intended size of the entire LED array
  194. int xsize = percentageInverse(ledImage[i].width, ledArea[i].width);
  195. int ysize = percentageInverse(ledImage[i].height, ledArea[i].height);
  196. // computer this image's position within it
  197. int xloc = percentage(xsize, ledArea[i].x);
  198. int yloc = percentage(ysize, ledArea[i].y);
  199. // show what should appear on the LEDs
  200. image(ledImage[i], 240 - xsize / 2 + xloc, 10 + yloc);
  201. }
  202. }
  203. // respond to mouse clicks as pause/play
  204. boolean isPlaying = true;
  205. void mousePressed() {
  206. if (isPlaying) {
  207. myMovie.pause();
  208. isPlaying = false;
  209. } else {
  210. myMovie.play();
  211. isPlaying = true;
  212. }
  213. }
  214. // scale a number by a percentage, from 0 to 100
  215. int percentage(int num, int percent) {
  216. double mult = percentageFloat(percent);
  217. double output = num * mult;
  218. return (int)output;
  219. }
  220. // scale a number by the inverse of a percentage, from 0 to 100
  221. int percentageInverse(int num, int percent) {
  222. double div = percentageFloat(percent);
  223. double output = num / div;
  224. return (int)output;
  225. }
  226. // convert an integer from 0 to 100 to a float percentage
  227. // from 0.0 to 1.0. Special cases for 1/3, 1/6, 1/7, etc
  228. // are handled automatically to fix integer rounding.
  229. double percentageFloat(int percent) {
  230. if (percent == 33) return 1.0 / 3.0;
  231. if (percent == 17) return 1.0 / 6.0;
  232. if (percent == 14) return 1.0 / 7.0;
  233. if (percent == 13) return 1.0 / 8.0;
  234. if (percent == 11) return 1.0 / 9.0;
  235. if (percent == 9) return 1.0 / 11.0;
  236. if (percent == 8) return 1.0 / 12.0;
  237. return (double)percent / 100.0;
  238. }