PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

186 行
5.9KB

  1. /* OctoWS2811 movie2sdcard.pde - Convert video for SD card playing, with
  2. Teensy 3.1 running OctoWS2811 VideoSDcard.ino
  3. http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
  4. Copyright (c) 2014 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. // To configure this program, edit the following:
  22. //
  23. // 1: Change myMovie to open a video file of your choice ;-)
  24. // Also change the output file name.
  25. //
  26. // 2: Edit ledWidth, ledHeight, ledLayout for your LEDs
  27. //
  28. // 3: Edit framerate. This configures the speed VideoSDcard
  29. // will play your video data. It's also critical for merging
  30. // audio to be played by Teensy 3.1's digital to analog output.
  31. //
  32. import processing.video.*;
  33. import processing.serial.*;
  34. import java.io.*;
  35. int ledWidth = 60; // size of LED panel
  36. int ledHeight = 32;
  37. boolean ledLayout = true; // layout of rows, true = even is left->right
  38. double framerate = 23.98; // You MUST set this to the movie's frame rate
  39. // Processing does not seem to have a way to detect it.
  40. Movie myMovie = new Movie(this, "/Users/paul/myvideo.mov");
  41. FileOutputStream myFile; // edit output filename below...
  42. float gamma = 1.8;
  43. PImage ledImage;
  44. int[] gammatable = new int[256];
  45. long elapsed_picoseconds=0L;
  46. long elapsed_microseconds=0L;
  47. long picoseconds_per_frame = (long)(1e12 / framerate + 0.5);
  48. boolean fileopen=true;
  49. void setup() {
  50. for (int i=0; i < 256; i++) {
  51. gammatable[i] = (int)(pow((float)i / 255.0, gamma) * 255.0 + 0.5);
  52. }
  53. try {
  54. myFile = new FileOutputStream("/Users/paul/myvideo.bin");
  55. } catch (Exception e) {
  56. exit();
  57. }
  58. ledImage = createImage(ledWidth, ledHeight, RGB);
  59. size(720, 560); // create the window
  60. myMovie.play(); // start the movie :-)
  61. }
  62. // movieEvent runs for each new frame of movie data
  63. void movieEvent(Movie m) {
  64. // read the movie's next frame
  65. m.read();
  66. elapsed_picoseconds += picoseconds_per_frame;
  67. int usec = (int)((elapsed_picoseconds / 1000000L) - elapsed_microseconds);
  68. elapsed_microseconds += (long)usec;
  69. println("usec = " + usec);
  70. // copy the movie's image to the LED image
  71. ledImage.copy(m, 0, 0, m.width, m.height, 0, 0, ledWidth, ledHeight);
  72. // convert the LED image to raw data
  73. byte[] ledData = new byte[(ledWidth * ledHeight * 3) + 5];
  74. image2data(ledImage, ledData, ledLayout);
  75. ledData[0] = '*'; // first Teensy is the frame sync master
  76. ledData[1] = (byte)(ledWidth * ledHeight);
  77. ledData[2] = (byte)((ledWidth * ledHeight) >> 8);
  78. ledData[3] = (byte)(usec); // request the frame sync pulse
  79. ledData[4] = (byte)(usec >> 8); // at 75% of the frame time
  80. // send the raw data to the LEDs :-)
  81. //ledSerial[i].write(ledData);
  82. try {
  83. myFile.write(ledData);
  84. } catch (Exception e) {
  85. exit();
  86. }
  87. }
  88. // image2data converts an image to OctoWS2811's raw data format.
  89. // The number of vertical pixels in the image must be a multiple
  90. // of 8. The data array must be the proper size for the image.
  91. void image2data(PImage image, byte[] data, boolean layout) {
  92. int offset = 5;
  93. int x, y, xbegin, xend, xinc, mask;
  94. int linesPerPin = image.height / 8;
  95. int pixel[] = new int[8];
  96. for (y = 0; y < linesPerPin; y++) {
  97. if ((y & 1) == (layout ? 0 : 1)) {
  98. // even numbered rows are left to right
  99. xbegin = 0;
  100. xend = image.width;
  101. xinc = 1;
  102. } else {
  103. // odd numbered rows are right to left
  104. xbegin = image.width - 1;
  105. xend = -1;
  106. xinc = -1;
  107. }
  108. for (x = xbegin; x != xend; x += xinc) {
  109. for (int i=0; i < 8; i++) {
  110. // fetch 8 pixels from the image, 1 for each pin
  111. pixel[i] = image.pixels[x + (y + linesPerPin * i) * image.width];
  112. pixel[i] = colorWiring(pixel[i]);
  113. }
  114. // convert 8 pixels to 24 bytes
  115. for (mask = 0x800000; mask != 0; mask >>= 1) {
  116. byte b = 0;
  117. for (int i=0; i < 8; i++) {
  118. if ((pixel[i] & mask) != 0) b |= (1 << i);
  119. }
  120. data[offset++] = b;
  121. }
  122. }
  123. }
  124. }
  125. // translate the 24 bit color from RGB to the actual
  126. // order used by the LED wiring. GRB is the most common.
  127. int colorWiring(int c) {
  128. int red = (c & 0xFF0000) >> 16;
  129. int green = (c & 0x00FF00) >> 8;
  130. int blue = (c & 0x0000FF);
  131. red = gammatable[red];
  132. green = gammatable[green];
  133. blue = gammatable[blue];
  134. return (green << 16) | (red << 8) | (blue); // GRB - most common wiring
  135. }
  136. // draw runs every time the screen is redrawn - show the movie...
  137. void draw() {
  138. if (myMovie.time() < myMovie.duration()) {
  139. image(myMovie, 0, 80);
  140. image(ledImage, 240 - ledWidth / 2, 10);
  141. } else {
  142. if (fileopen) {
  143. println("movie stop, closing output file");
  144. try {
  145. myFile.close();
  146. } catch (Exception e) {
  147. exit();
  148. }
  149. fileopen = false;
  150. }
  151. }
  152. }
  153. // respond to mouse clicks as pause/play
  154. boolean isPlaying = true;
  155. void mousePressed() {
  156. if (isPlaying) {
  157. myMovie.pause();
  158. isPlaying = false;
  159. } else {
  160. myMovie.play();
  161. isPlaying = true;
  162. }
  163. }