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.

Adalight.ino 4.7KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* OctoWS2811 Adalight.ino - Receive Adalight and display on WS2811 LEDs
  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. This example is meant to work with Adafruit's Adalight software,
  20. allowing NeoPixel (WS2812B) LEDs to be used.
  21. https://learn.adafruit.com/adalight-diy-ambient-tv-lighting
  22. For good performance, you may need to use AmbiBox. Running with
  23. Processing has been reported to give only 7 frames/sec. Details here:
  24. https://forums.adafruit.com/viewtopic.php?f=8&t=68444&p=434808#p434808
  25. Options for connecting the LEDs is fewer than 8 sections:
  26. https://forum.pjrc.com/threads/53974-Ambilight-help
  27. Required Connections http://www.pjrc.com/store/octo28_adaptor.html
  28. --------------------
  29. pin 2: LED Strip #1 OctoWS2811 drives 8 LED Strips.
  30. pin 14: LED strip #2 All 8 are the same length.
  31. pin 7: LED strip #3
  32. pin 8: LED strip #4 A 100 ohm resistor should used
  33. pin 6: LED strip #5 between each Teensy pin and the
  34. pin 20: LED strip #6 wire to the LED strip, to minimize
  35. pin 21: LED strip #7 high frequency ringining & noise.
  36. pin 5: LED strip #8
  37. pin 15 & 16 - Connect together, but do not use
  38. pin 4 - Do not use
  39. pin 3 - Do not use as PWM. Normal use is ok.
  40. */
  41. #include <OctoWS2811.h>
  42. const int ledsPerStrip = 60;
  43. DMAMEM int displayMemory[ledsPerStrip*6];
  44. int drawingMemory[ledsPerStrip*6];
  45. const int config = WS2811_GRB | WS2811_800kHz;
  46. OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config);
  47. void setup() {
  48. Serial.begin(115200);
  49. Serial.setTimeout(2000);
  50. leds.begin();
  51. allColor(0xFF0000); // flash all LEDs red
  52. delay(800);
  53. allColor(0x00FF00); // then green
  54. delay(800);
  55. allColor(0x0000FF); // then blue
  56. delay(800);
  57. allColor(0x000000); // then off (published startup diagnostic)
  58. }
  59. void allColor(unsigned int c) {
  60. for (int i=0; i < ledsPerStrip*8; i++) {
  61. leds.setPixel(i, c);
  62. }
  63. leds.show();
  64. }
  65. unsigned int state;
  66. elapsedMillis lastByteTime;
  67. elapsedMillis lastAckTime;
  68. unsigned int count=0;
  69. unsigned int total=0;
  70. void loop() {
  71. unsigned char buf[3];
  72. if (!Serial) {
  73. while (!Serial) /* wait */ ;
  74. delay(20);
  75. Serial.print("Ada\n");
  76. lastByteTime = 0;
  77. lastAckTime = 0;
  78. state = 0;
  79. count = 0;
  80. }
  81. if (state == 0) {
  82. if (Serial.available() == 0) goto wait;
  83. state = (Serial.read() == 'A') ? 1 : 0;
  84. } else if (state == 1) {
  85. if (Serial.available() == 0) goto wait;
  86. state = (Serial.read() == 'd') ? 2 : 0;
  87. } else if (state == 2) {
  88. if (Serial.available() == 0) goto wait;
  89. state = (Serial.read() == 'a') ? 3 : 0;
  90. } else if (state == 3) {
  91. if (Serial.available() < 3) goto wait;
  92. Serial.readBytes((char *)buf, 3);
  93. if ((buf[0] ^ buf[1] ^ 0x55) == buf[2]) {
  94. count = 0;
  95. total = buf[0] * 256 + buf[1] + 1;
  96. state = 4;
  97. } else if (buf[0] == 'A' && buf[1] == 'd' && buf[2] == 'a') {
  98. state = 3;
  99. } else if (buf[1] == 'A' && buf[2] == 'd') {
  100. state = 2;
  101. } else if (buf[2] == 'A') {
  102. state = 1;
  103. } else {
  104. state = 0;
  105. }
  106. } else if (state == 4) {
  107. if (Serial.available() < 3) goto wait;
  108. Serial.readBytes((char *)buf, 3);
  109. if (count < ledsPerStrip*8) {
  110. leds.setPixel(count, (buf[0] << 16) | (buf[1] << 8) | buf[2]);
  111. }
  112. count++;
  113. if (count >= total) {
  114. leds.show();
  115. state = 0;
  116. }
  117. } else {
  118. wait:
  119. if (lastAckTime > 1000) {
  120. lastAckTime = 0;
  121. while (Serial.available()) Serial.read();
  122. Serial.print("Ada\n");
  123. state = 0;
  124. }
  125. if (lastByteTime > 15000) {
  126. lastByteTime = 0;
  127. allColor(0);
  128. }
  129. return;
  130. }
  131. lastAckTime = 0;
  132. lastByteTime = 0;
  133. }