Guest User

nearly there

a guest
Oct 23rd, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 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 <FastLED.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. #define NUM_LEDS PIXELS_PER_PANEL
  45.  
  46.  
  47.  
  48.  
  49. //3 byte per pixel or 24bit (RGB)
  50. #define BPP 3
  51.  
  52. //package size we expect. the footer byte is not included here!
  53. #define EXPECTED_PACKED_SIZE (PIXELS_PER_PANEL*BPP+TPM2NET_HEADER_SIZE)
  54.  
  55. //use softspi as the spi line is used by the ethernet lib
  56. #define DATA_PIN 3
  57.  
  58.  
  59. //as the arduino ethernet has only 2kb ram
  60. //we must limit the maximal udp packet size
  61. //a 64 pixel matrix needs 192 bytes data
  62. #define UDP_PACKET_SIZE 2000
  63.  
  64.  
  65. //some santiy checks here
  66. #if EXPECTED_PACKED_SIZE > UDP_PACKET_SIZE
  67. #error EXPECTED PACKED SIZE is bigger than UDP BUFFER! increase the buffer
  68. #endif
  69.  
  70. //#if UDP_TX_PACKET_MAX_SIZE < EXPECTED_PACKED_SIZE
  71. //#error UDP packet size to small - modify UDP_TX_PACKET_MAX_SIZE in the file EthernetUdp.h and set buffers to 64 bytes
  72. //#endif
  73.  
  74.  
  75. // buffers for receiving and sending data
  76. uint8_t packetBuffer[UDP_PACKET_SIZE]; //buffer to hold incoming packet,
  77.  
  78. //initialize pixels
  79. //Neophob_LPD6803 strip = Neophob_LPD6803(PIXELS_PER_PANEL*NR_OF_PANELS);
  80. //WS2801 strip = WS2801(PIXELS_PER_PANEL*NR_OF_PANELS, DATA_PIN, CLOCK_PIN);
  81.  
  82. //network stuff, TODO: add dhcp/bonjour support
  83. byte mac[] = { 0xBE, 0x00, 0xBE, 0x00, 0xBE, 0x01 };
  84. IPAddress ip(192, 168, 111, 177);
  85. EthernetUDP Udp;
  86.  
  87.  
  88.  
  89. void setup() {
  90. Serial.begin(115200);
  91. Serial.println("Hello!");
  92.  
  93. // start the Ethernet and UDP:
  94. Ethernet.begin(mac,ip);
  95. Udp.begin(TPM2NET_LISTENING_PORT);
  96. memset(packetBuffer, 0, UDP_PACKET_SIZE);
  97.  
  98. FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  99.  
  100. Serial.println("Setup done");
  101. }
  102.  
  103.  
  104. void loop() {
  105. // if there's data available, read a packet
  106. int packetSize = Udp.parsePacket();
  107. Serial.print("Received packet of size ");
  108. Serial.println(packetSize);
  109.  
  110. //tpm2 header size is 5 bytes
  111. if (packetSize>EXPECTED_PACKED_SIZE) {
  112.  
  113. // read the packet into packetBufffer
  114. Udp.read(packetBuffer, UDP_PACKET_SIZE);
  115.  
  116. // -- Header check
  117.  
  118. //check header byte
  119. if (packetBuffer[0]!=TPM2NET_HEADER_IDENT) {
  120. Serial.print("Invalid header ident ");
  121. Serial.println(packetBuffer[0], HEX);
  122. return;
  123. }
  124.  
  125. //check command
  126. if (packetBuffer[1]!=TPM2NET_CMD_DATAFRAME) {
  127. Serial.print("Invalid block type ");
  128. Serial.println(packetBuffer[1], HEX);
  129. return;
  130. }
  131.  
  132. uint16_t frameSize = packetBuffer[2];
  133. frameSize = (frameSize << 8) + packetBuffer[3];
  134. Serial.print("Framesize ");
  135. Serial.println(frameSize, HEX);
  136.  
  137. //use packetNumber to calculate offset
  138. uint8_t packetNumber = packetBuffer[4];
  139. Serial.print("packetNumber ");
  140. Serial.println(packetNumber, HEX);
  141.  
  142. //check footer
  143. if (packetBuffer[frameSize+TPM2NET_HEADER_SIZE]!=TPM2NET_FOOTER_IDENT) {
  144. Serial.print("Invalid footer ident ");
  145. Serial.println(packetBuffer[frameSize+TPM2NET_HEADER_SIZE], HEX);
  146. return;
  147. }
  148.  
  149. //calculate offset
  150. uint16_t currentLed = packetNumber*PIXELS_PER_PANEL;
  151. int x=TPM2NET_HEADER_SIZE;
  152. memcpy(leds + (currentLed*3), packetBuffer + TPM2NET_HEADER_SIZE, frameSize * 3);
  153.  
  154. FastLED.show();
  155.  
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment