Guest User

Pixelcontroller

a guest
Oct 23rd, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. /*
  2. * PixelInvaders tpm2.net implementation, Copyright (C) 2013 michael vogt <[email protected]>
  3. *
  4. * If you like this, make sure you check out http://www.pixelinvaders.ch
  5. *
  6. * This file is part of PixelController.
  7. *
  8. * PixelController is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * PixelController is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. *
  23. *
  24. */
  25.  
  26. #include <SPI.h>
  27. #include <Ethernet.h>
  28. #include <EthernetUdp.h>
  29.  
  30. //get the lib here: https://github.com/neophob/WS2801-Library
  31. #include <WS2801.h>
  32.  
  33. //define some tpm constants
  34. #define TPM2NET_LISTENING_PORT 65506
  35. #define TPM2NET_HEADER_SIZE 5
  36. #define TPM2NET_HEADER_IDENT 0x9c
  37. #define TPM2NET_CMD_DATAFRAME 0xda
  38. #define TPM2NET_CMD_COMMAND 0xc0
  39. #define TPM2NET_CMD_ANSWER 0xaa
  40. #define TPM2NET_FOOTER_IDENT 0x36
  41.  
  42. #define NR_OF_PANELS 1
  43. #define PIXELS_PER_PANEL 64
  44.  
  45. //3 byte per pixel or 24bit (RGB)
  46. #define BPP 3
  47.  
  48. //package size we expect. the footer byte is not included here!
  49. #define EXPECTED_PACKED_SIZE (PIXELS_PER_PANEL*BPP+TPM2NET_HEADER_SIZE)
  50.  
  51. //use softspi as the spi line is used by the ethernet lib
  52. #define DATA_PIN 3
  53. #define CLOCK_PIN 2
  54.  
  55. //as the arduino ethernet has only 2kb ram
  56. //we must limit the maximal udp packet size
  57. //a 64 pixel matrix needs 192 bytes data
  58. #define UDP_PACKET_SIZE 512
  59.  
  60.  
  61. //some santiy checks here
  62. #if EXPECTED_PACKED_SIZE > UDP_PACKET_SIZE
  63. #error EXPECTED PACKED SIZE is bigger than UDP BUFFER! increase the buffer
  64. #endif
  65.  
  66. //#if UDP_TX_PACKET_MAX_SIZE < EXPECTED_PACKED_SIZE
  67. //#error UDP packet size to small - modify UDP_TX_PACKET_MAX_SIZE in the file EthernetUdp.h and set buffers to 64 bytes
  68. //#endif
  69.  
  70.  
  71. // buffers for receiving and sending data
  72. uint8_t packetBuffer[UDP_PACKET_SIZE]; //buffer to hold incoming packet,
  73.  
  74. //initialize pixels
  75. //Neophob_LPD6803 strip = Neophob_LPD6803(PIXELS_PER_PANEL*NR_OF_PANELS);
  76. WS2801 strip = WS2801(PIXELS_PER_PANEL*NR_OF_PANELS, DATA_PIN, CLOCK_PIN);
  77.  
  78. //network stuff, TODO: add dhcp/bonjour support
  79. byte mac[] = { 0xBE, 0x00, 0xBE, 0x00, 0xBE, 0x01 };
  80. IPAddress ip(192, 168, 111, 177);
  81. EthernetUDP Udp;
  82.  
  83.  
  84.  
  85. void setup() {
  86. Serial.begin(115200);
  87. Serial.println("Hello!");
  88.  
  89. // start the Ethernet and UDP:
  90. Ethernet.begin(mac,ip);
  91. Udp.begin(TPM2NET_LISTENING_PORT);
  92. memset(packetBuffer, 0, UDP_PACKET_SIZE);
  93.  
  94. strip.begin();
  95. showInitImage(); // display some colors
  96.  
  97. Serial.println("Setup done");
  98. }
  99.  
  100.  
  101. void loop() {
  102. // if there's data available, read a packet
  103. int packetSize = Udp.parsePacket();
  104. Serial.print("Received packet of size ");
  105. Serial.println(packetSize);
  106.  
  107. //tpm2 header size is 5 bytes
  108. if (packetSize>EXPECTED_PACKED_SIZE) {
  109.  
  110. // read the packet into packetBufffer
  111. Udp.read(packetBuffer, UDP_PACKET_SIZE);
  112.  
  113. // -- Header check
  114.  
  115. //check header byte
  116. if (packetBuffer[0]!=TPM2NET_HEADER_IDENT) {
  117. Serial.print("Invalid header ident ");
  118. Serial.println(packetBuffer[0], HEX);
  119. return;
  120. }
  121.  
  122. //check command
  123. if (packetBuffer[1]!=TPM2NET_CMD_DATAFRAME) {
  124. Serial.print("Invalid block type ");
  125. Serial.println(packetBuffer[1], HEX);
  126. return;
  127. }
  128.  
  129. uint16_t frameSize = packetBuffer[2];
  130. frameSize = (frameSize << 8) + packetBuffer[3];
  131. Serial.print("Framesize ");
  132. Serial.println(frameSize, HEX);
  133.  
  134. //use packetNumber to calculate offset
  135. uint8_t packetNumber = packetBuffer[4];
  136. Serial.print("packetNumber ");
  137. Serial.println(packetNumber, HEX);
  138.  
  139. //check footer
  140. if (packetBuffer[frameSize+TPM2NET_HEADER_SIZE]!=TPM2NET_FOOTER_IDENT) {
  141. Serial.print("Invalid footer ident ");
  142. Serial.println(packetBuffer[frameSize+TPM2NET_HEADER_SIZE], HEX);
  143. return;
  144. }
  145.  
  146. //calculate offset
  147. uint16_t currentLed = packetNumber*PIXELS_PER_PANEL;
  148. int x=TPM2NET_HEADER_SIZE;
  149. for (byte i=0; i < frameSize; i++) {
  150. strip.setPixelColor(currentLed++, packetBuffer[x], packetBuffer[x+1], packetBuffer[x+2]);
  151. x+=3;
  152. }
  153.  
  154. //TODO maybe update leds only if we got all pixeldata?
  155. strip.show(); // write all the pixels out
  156. }
  157. }
  158.  
  159. // --------------------------------------------
  160. // create initial image
  161. // --------------------------------------------
  162. void showInitImage() {
  163. //just create some boring colors
  164. for (int i=0; i < strip.numPixels(); i++) {
  165. strip.setPixelColor(i, (i)%255, (i*2)%255, (i*4)%255);
  166. }
  167. // Update the strip
  168. strip.show();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment