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.

PixelInvaders.ino 6.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * PixelInvaders tpm2.net implementation, Copyright (C) 2013 michael vogt <michu@neophob.com>
  3. *
  4. * If you like this, make sure you check out http://www.pixelinvaders.ch
  5. *
  6. *
  7. * Main pixel controller web site:
  8. * http://pixelinvaders.ch/?page_id=160
  9. *
  10. * Download Pixel controller software from:
  11. * https://code.google.com/p/pixelcontroller/downloads/list
  12. *
  13. * The set up is as follows. In the pixel controller folder go to >Data> folder.
  14. * replace config.properties file with the one provided with this example. Open
  15. * the config file and set the port to the correct name, and set the resolution x
  16. * and y values to your array size. Save the config file as config.properties in
  17. * the data folder and delete the original config file.
  18. *
  19. * In the sketch below, set the led strip length values and the define NUM LEDS value
  20. * to what you have.
  21. *
  22. * Load the sketch and then run the PixelController jar file.
  23. *
  24. * There are a number of config parameters that had to be changed. The pixel controller
  25. * does not auto detect the port in the tpm2serial mode, so this must be done manually
  26. * in the config file.
  27. *
  28. * http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811?p=39147&viewfull=1#post39147
  29. *
  30. *
  31. * This file is part of PixelController.
  32. *
  33. * PixelController is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2, or (at your option)
  36. * any later version.
  37. *
  38. * PixelController is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  46. *
  47. *
  48. *
  49. */
  50. #include <OctoWS2811.h>
  51. const int ledsPerStrip = 32;
  52. DMAMEM int displayMemory[ledsPerStrip*6];
  53. int drawingMemory[ledsPerStrip*6];
  54. const int config = WS2811_GRB | WS2811_800kHz;
  55. OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config);
  56. //#include <FastSPI_LED2.h>
  57. //---- START USER CONFIG ----
  58. #define DEBUG 1
  59. //how many led pixels are connected
  60. #define NUM_LEDS 256
  61. // Teensy 3.0 has the LED on pin 13
  62. #define LED_PIN 13
  63. //---- END USER CONFIG ----
  64. #define BAUD_RATE 115200
  65. //define some tpm constants
  66. #define TPM2NET_HEADER_SIZE 4
  67. #define TPM2NET_HEADER_IDENT 0x9c
  68. #define TPM2NET_CMD_DATAFRAME 0xda
  69. #define TPM2NET_CMD_COMMAND 0xc0
  70. #define TPM2NET_CMD_ANSWER 0xaa
  71. #define TPM2NET_FOOTER_IDENT 0x36
  72. //3 byte per pixel or 24bit (RGB)
  73. #define BPP 3
  74. //package size we expect.
  75. #define MAX_PACKED_SIZE 520
  76. #define PIXELS_PER_PACKET 170
  77. // buffers for receiving and sending data
  78. uint8_t packetBuffer[MAX_PACKED_SIZE]; //buffer to hold incoming packet
  79. uint16_t psize;
  80. uint8_t currentPacket;
  81. uint8_t totalPacket;
  82. //********************************
  83. // SETUP
  84. //********************************
  85. void setup() {
  86. memset(packetBuffer, 0, MAX_PACKED_SIZE);
  87. Serial.begin(BAUD_RATE);
  88. Serial.flush();
  89. Serial.setTimeout(20);
  90. #ifdef DEBUG
  91. Serial.println("HI");
  92. #endif
  93. // pinMode(ledPin, OUTPUT);
  94. // debugBlink(500);
  95. //first blink: init
  96. digitalWrite(LED_PIN, HIGH);
  97. delay(250);
  98. digitalWrite(LED_PIN, LOW);
  99. leds.begin();
  100. leds.show();
  101. //Flickering issues?
  102. //...it turned out that as my PSU got hotter, the voltage was dropping towards the end of the LED strip.
  103. //Tried feeding power to both ends of the strip, only delayed the issue slightly. Changed to a bigger PSU and fault went away
  104. //(dual ends feeding power).
  105. // For safety (to prevent too high of a power draw), the test case defaults to
  106. // setting brightness to 50% brightness
  107. // LEDS.setBrightness(64);
  108. showInitImage(); // display some colors
  109. }
  110. //********************************
  111. // LOOP
  112. //********************************
  113. void loop() {
  114. int16_t res = readCommand();
  115. if (res > 0) {
  116. // leds.setPixel(1,250);
  117. // leds.show();
  118. #ifdef DEBUG
  119. Serial.print("FINE: ");
  120. Serial.print(psize, DEC);
  121. Serial.print("/");
  122. Serial.print(currentPacket, DEC);
  123. #if defined (CORE_TEENSY_SERIAL)
  124. Serial.send_now();
  125. #endif
  126. #endif
  127. digitalWrite(LED_PIN, HIGH);
  128. updatePixels();
  129. digitalWrite(LED_PIN, LOW);
  130. }
  131. #ifdef DEBUG
  132. else {
  133. if (res!=-1) {
  134. Serial.print("ERR: ");
  135. Serial.println(res, DEC);
  136. #if defined (CORE_TEENSY_SERIAL)
  137. Serial.send_now();
  138. #endif
  139. }
  140. }
  141. #endif
  142. }
  143. //********************************
  144. // UPDATE PIXELS
  145. //********************************
  146. void updatePixels() {
  147. uint8_t nrOfPixels = psize/3;
  148. uint16_t ofs=0;
  149. uint16_t ledOffset = PIXELS_PER_PACKET*currentPacket;
  150. for (uint32_t i=0; i<nrOfPixels; i++) {
  151. leds.setPixel(i+ledOffset, Color(packetBuffer[ofs], packetBuffer[ofs+1], packetBuffer[ofs+2]));
  152. ofs += 3;
  153. }
  154. //update only if all data packets recieved
  155. if (currentPacket==totalPacket-1) {
  156. #ifdef DEBUG
  157. Serial.println("DRAW!");
  158. #if defined (CORE_TEENSY_SERIAL)
  159. Serial.send_now();
  160. #endif
  161. #endif
  162. // LEDS.show();
  163. leds.show();
  164. } else {
  165. #ifdef DEBUG
  166. Serial.print("NOTUPDATE: ");
  167. Serial.println(currentPacket, DEC);
  168. #if defined (CORE_TEENSY_SERIAL)
  169. Serial.send_now();
  170. #endif
  171. #endif
  172. }
  173. }
  174. //********************************
  175. // READ SERIAL PORT
  176. //********************************
  177. int16_t readCommand() {
  178. uint8_t startChar = Serial.read();
  179. if (startChar != TPM2NET_HEADER_IDENT) {
  180. return -1;
  181. }
  182. uint8_t dataFrame = Serial.read();
  183. if (dataFrame != TPM2NET_CMD_DATAFRAME) {
  184. return -2;
  185. }
  186. uint8_t s1 = Serial.read();
  187. uint8_t s2 = Serial.read();
  188. psize = (s1<<8) + s2;
  189. if (psize < 6 || psize > MAX_PACKED_SIZE) {
  190. return -3;
  191. }
  192. currentPacket = Serial.read();
  193. totalPacket = Serial.read();
  194. //get remaining bytes
  195. uint16_t recvNr = Serial.readBytes((char *)packetBuffer, psize);
  196. if (recvNr!=psize) {
  197. return -5;
  198. }
  199. uint8_t endChar = Serial.read();
  200. if (endChar != TPM2NET_FOOTER_IDENT) {
  201. return -6;
  202. }
  203. return psize;
  204. }
  205. // --------------------------------------------
  206. // create initial image
  207. // --------------------------------------------
  208. void showInitImage() {
  209. for (int i = 0 ; i < NUM_LEDS; i++ ) {
  210. leds.setPixel(i,Color(i*0.2,i*0.5,i*0.5));
  211. }
  212. leds.show();
  213. }
  214. void debugBlink(uint8_t t) {
  215. digitalWrite(LED_PIN, HIGH);
  216. delay(t);
  217. digitalWrite(LED_PIN, LOW);
  218. }
  219. /* Helper functions */
  220. // Create a 24 bit color value from R,G,B
  221. unsigned int Color(byte r, byte g, byte b)
  222. {
  223. //Take the lowest 8 bits of each value and append them end to end
  224. return( (((unsigned int)b & 0xFF )<<16) | (((unsigned int)r & 0xFF)<<8) | ((unsigned int)g & 0xFF));
  225. }