PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

228 lines
6.8KB

  1. /* OctoWS2811 VideoSDcard.ino - Video on LEDs, played from SD Card
  2. http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
  3. Copyright (c) 2014 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 programs to prepare the SD card video file have moved to "extras"
  20. https://github.com/PaulStoffregen/OctoWS2811/tree/master/extras
  21. The recommended hardware for SD card playing is:
  22. Teensy 3.1: http://www.pjrc.com/store/teensy31.html
  23. Octo28 Apaptor: http://www.pjrc.com/store/octo28_adaptor.html
  24. SD Adaptor: http://www.pjrc.com/store/wiz820_sd_adaptor.html
  25. Long Pins: http://www.digikey.com/product-search/en?keywords=S1082E-36-ND
  26. See the included "hardware.jpg" image for suggested pin connections,
  27. with 2 cuts and 1 solder bridge needed for the SD card pin 3 chip select.
  28. Required Connections
  29. --------------------
  30. pin 2: LED Strip #1 OctoWS2811 drives 8 LED Strips.
  31. pin 14: LED strip #2 All 8 are the same length.
  32. pin 7: LED strip #3
  33. pin 8: LED strip #4 A 100 to 220 ohm resistor should used
  34. pin 6: LED strip #5 between each Teensy pin and the
  35. pin 20: LED strip #6 wire to the LED strip, to minimize
  36. pin 21: LED strip #7 high frequency ringining & noise.
  37. pin 5: LED strip #8
  38. pin 15 & 16 - Connect together, but do not use
  39. pin 4: Do not use
  40. pin 3: SD Card, CS
  41. pin 11: SD Card, MOSI
  42. pin 12: SD Card, MISO
  43. pin 13: SD Card, SCLK
  44. */
  45. #include <OctoWS2811.h>
  46. #include <SPI.h>
  47. #include <SD.h>
  48. #include <Audio.h>
  49. #include <Wire.h>
  50. #define LED_WIDTH 60 // number of LEDs horizontally
  51. #define LED_HEIGHT 32 // number of LEDs vertically (must be multiple of 8)
  52. #define FILENAME "VIDEO.BIN"
  53. const int ledsPerStrip = LED_WIDTH * LED_HEIGHT / 8;
  54. DMAMEM int displayMemory[ledsPerStrip*6];
  55. int drawingMemory[ledsPerStrip*6];
  56. elapsedMicros elapsedSinceLastFrame = 0;
  57. bool playing = false;
  58. OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, WS2811_800kHz);
  59. File videofile;
  60. AudioPlayQueue audio;
  61. AudioOutputAnalog dac;
  62. AudioConnection patchCord1(audio, dac);
  63. void setup() {
  64. AudioMemory(40);
  65. //while (!Serial) ;
  66. delay(50);
  67. Serial.println("VideoSDcard");
  68. leds.begin();
  69. leds.show();
  70. if (!SD.begin(3)) stopWithErrorMessage("Could not access SD card");
  71. Serial.println("SD card ok");
  72. videofile = SD.open(FILENAME, FILE_READ);
  73. if (!videofile) stopWithErrorMessage("Could not read " FILENAME);
  74. Serial.println("File opened");
  75. playing = true;
  76. elapsedSinceLastFrame = 0;
  77. }
  78. // read from the SD card, true=ok, false=unable to read
  79. // the SD library is much faster if all reads are 512 bytes
  80. // this function lets us easily read any size, but always
  81. // requests data from the SD library in 512 byte blocks.
  82. //
  83. bool sd_card_read(void *ptr, unsigned int len)
  84. {
  85. static unsigned char buffer[512];
  86. static unsigned int bufpos = 0;
  87. static unsigned int buflen = 0;
  88. unsigned char *dest = (unsigned char *)ptr;
  89. unsigned int n;
  90. while (len > 0) {
  91. if (buflen == 0) {
  92. n = videofile.read(buffer, 512);
  93. if (n == 0) return false;
  94. buflen = n;
  95. bufpos = 0;
  96. }
  97. unsigned int n = buflen;
  98. if (n > len) n = len;
  99. memcpy(dest, buffer + bufpos, n);
  100. dest += n;
  101. bufpos += n;
  102. buflen -= n;
  103. len -= n;
  104. }
  105. return true;
  106. }
  107. // skip past data from the SD card
  108. void sd_card_skip(unsigned int len)
  109. {
  110. unsigned char buf[256];
  111. while (len > 0) {
  112. unsigned int n = len;
  113. if (n > sizeof(buf)) n = sizeof(buf);
  114. sd_card_read(buf, n);
  115. len -= n;
  116. }
  117. }
  118. void loop()
  119. {
  120. unsigned char header[5];
  121. if (playing) {
  122. if (sd_card_read(header, 5)) {
  123. if (header[0] == '*') {
  124. // found an image frame
  125. unsigned int size = (header[1] | (header[2] << 8)) * 3;
  126. unsigned int usec = header[3] | (header[4] << 8);
  127. unsigned int readsize = size;
  128. // Serial.printf("v: %u %u", size, usec);
  129. if (readsize > sizeof(drawingMemory)) {
  130. readsize = sizeof(drawingMemory);
  131. }
  132. if (sd_card_read(drawingMemory, readsize)) {
  133. // Serial.printf(", us = %u", (unsigned int)elapsedSinceLastFrame);
  134. // Serial.println();
  135. while (elapsedSinceLastFrame < usec) ; // wait
  136. elapsedSinceLastFrame -= usec;
  137. leds.show();
  138. } else {
  139. error("unable to read video frame data");
  140. return;
  141. }
  142. if (readsize < size) {
  143. sd_card_skip(size - readsize);
  144. }
  145. } else if (header[0] == '%') {
  146. // found a chunk of audio data
  147. unsigned int size = (header[1] | (header[2] << 8)) * 2;
  148. // Serial.printf("a: %u", size);
  149. // Serial.println();
  150. while (size > 0) {
  151. unsigned int len = size;
  152. if (len > 256) len = 256;
  153. int16_t *p = audio.getBuffer();
  154. if (!sd_card_read(p, len)) {
  155. error("unable to read audio frame data");
  156. return;
  157. }
  158. if (len < 256) {
  159. for (int i=len; i < 256; i++) {
  160. *((char *)p + i) = 0; // fill rest of buffer with zero
  161. }
  162. }
  163. audio.playBuffer();
  164. size -= len;
  165. }
  166. } else {
  167. error("unknown header");
  168. return;
  169. }
  170. } else {
  171. error("unable to read 5-byte header");
  172. return;
  173. }
  174. } else {
  175. delay(2000);
  176. videofile = SD.open(FILENAME, FILE_READ);
  177. if (videofile) {
  178. Serial.println("File opened");
  179. playing = true;
  180. elapsedSinceLastFrame = 0;
  181. }
  182. }
  183. }
  184. // when any error happens during playback, close the file and restart
  185. void error(const char *str)
  186. {
  187. Serial.print("error: ");
  188. Serial.println(str);
  189. videofile.close();
  190. playing = false;
  191. }
  192. // when an error happens during setup, give up and print a message
  193. // to the serial monitor.
  194. void stopWithErrorMessage(const char *str)
  195. {
  196. while (1) {
  197. Serial.println(str);
  198. delay(1000);
  199. }
  200. }