Guest User

ta daaaaa

a guest
Oct 23rd, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 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. #include <EEPROM.h>
  30. #include <stdlib.h>
  31.  
  32. //get the lib here: https://github.com/neophob/WS2801-Library
  33. #include <FastLED.h>
  34.  
  35. //define some tpm constants
  36. #define TPM2NET_LISTENING_PORT 65506
  37. #define TPM2NET_HEADER_SIZE 5
  38. #define TPM2NET_HEADER_IDENT 0x9c
  39. #define TPM2NET_CMD_DATAFRAME 0xda
  40. #define TPM2NET_CMD_COMMAND 0xc0
  41. #define TPM2NET_CMD_ANSWER 0xaa
  42. #define TPM2NET_FOOTER_IDENT 0x36
  43.  
  44. #define NR_OF_PANELS 1
  45. #define PIXELS_PER_PANEL 520
  46. #define NUM_LEDS PIXELS_PER_PANEL
  47.  
  48. CRGB leds[PIXELS_PER_PANEL];
  49.  
  50.  
  51. //3 byte per pixel or 24bit (RGB)
  52. #define BPP 3
  53.  
  54. //package size we expect. the footer byte is not included here!
  55. #define EXPECTED_PACKED_SIZE (PIXELS_PER_PANEL*BPP+TPM2NET_HEADER_SIZE)
  56.  
  57. //use softspi as the spi line is used by the ethernet lib
  58. #define DATA_PIN 3
  59.  
  60.  
  61. //as the arduino ethernet has only 2kb ram
  62. //we must limit the maximal udp packet size
  63. //a 64 pixel matrix needs 192 bytes data
  64. #define UDP_PACKET_SIZE 1900
  65.  
  66.  
  67. //some santiy checks here
  68. #if EXPECTED_PACKED_SIZE > UDP_PACKET_SIZE
  69. #error EXPECTED PACKED SIZE is bigger than UDP BUFFER! increase the buffer
  70. #endif
  71.  
  72. //#if UDP_TX_PACKET_MAX_SIZE < EXPECTED_PACKED_SIZE
  73. //#error UDP packet size to small - modify UDP_TX_PACKET_MAX_SIZE in the file EthernetUdp.h and set buffers to 64 bytes
  74. //#endif
  75.  
  76.  
  77. // buffers for receiving and sending data
  78. uint8_t packetBuffer[UDP_PACKET_SIZE]; //buffer to hold incoming packet,
  79.  
  80. //initialize pixels
  81. //Neophob_LPD6803 strip = Neophob_LPD6803(PIXELS_PER_PANEL*NR_OF_PANELS);
  82. //WS2801 strip = WS2801(PIXELS_PER_PANEL*NR_OF_PANELS, DATA_PIN, CLOCK_PIN);
  83.  
  84. //network stuff, TODO: add dhcp/bonjour support
  85. byte mac[6] = { 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x00 };
  86. char macstr[18];
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. EthernetUDP Udp;
  94.  
  95.  
  96.  
  97. void setup() {
  98. //Serial.begin(115200);
  99. //Serial.println("Hello!");
  100.  
  101. // start the Ethernet and UDP:
  102.  
  103.  
  104. //---------------------------Random MAC address stored in EEPROM---------------------------//
  105.  
  106. if (EEPROM.read(1) == '#') {
  107. for (int i = 2; i < 6; i++) {
  108. mac[i] = EEPROM.read(i);
  109. }
  110. } else {
  111. randomSeed(analogRead(3));
  112. for (int i = 2; i < 6; i++) {
  113. mac[i] = random(0, 255);
  114. EEPROM.write(i, mac[i]);
  115. }
  116. EEPROM.write(1, '#');
  117. }
  118. snprintf(macstr, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  119. //-----------------------------------------------------------------------------------------//
  120.  
  121.  
  122.  
  123.  
  124. //-------------------strt netorking and UDP-----------------------//
  125. Ethernet.begin(mac);
  126.  
  127.  
  128.  
  129.  
  130. Udp.begin(TPM2NET_LISTENING_PORT);
  131. memset(packetBuffer, 0, UDP_PACKET_SIZE);
  132.  
  133. FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  134.  
  135. //Serial.println("Setup done");
  136. }
  137.  
  138.  
  139. void loop() {
  140. // if there's data available, read a packet
  141. int packetSize = Udp.parsePacket();
  142. //Serial.print("Received packet of size ");
  143. //Serial.println(packetSize);
  144.  
  145. //tpm2 header size is 5 bytes
  146. if (packetSize>EXPECTED_PACKED_SIZE) {
  147.  
  148. // read the packet into packetBufffer
  149. Udp.read(packetBuffer, UDP_PACKET_SIZE);
  150.  
  151. // -- Header check
  152.  
  153. //check header byte
  154. if (packetBuffer[0]!=TPM2NET_HEADER_IDENT) {
  155. Serial.print("Invalid header ident ");
  156. Serial.println(packetBuffer[0], HEX);
  157. return;
  158. }
  159.  
  160. //check command
  161. if (packetBuffer[1]!=TPM2NET_CMD_DATAFRAME) {
  162. Serial.print("Invalid block type ");
  163. Serial.println(packetBuffer[1], HEX);
  164. return;
  165. }
  166.  
  167. uint16_t frameSize = packetBuffer[2];
  168. frameSize = (frameSize << 8) + packetBuffer[3];
  169. Serial.print("Framesize ");
  170. Serial.println(frameSize, HEX);
  171.  
  172. //use packetNumber to calculate offset
  173. uint8_t packetNumber = packetBuffer[4];
  174. Serial.print("packetNumber ");
  175. Serial.println(packetNumber, HEX);
  176.  
  177. //check footer
  178. if (packetBuffer[frameSize+TPM2NET_HEADER_SIZE]!=TPM2NET_FOOTER_IDENT) {
  179. Serial.print("Invalid footer ident ");
  180. Serial.println(packetBuffer[frameSize+TPM2NET_HEADER_SIZE], HEX);
  181. return;
  182. }
  183.  
  184. //calculate offset
  185. uint16_t currentLed = packetNumber*PIXELS_PER_PANEL;
  186. int x=TPM2NET_HEADER_SIZE;
  187. memcpy(leds + (currentLed*3), packetBuffer + TPM2NET_HEADER_SIZE, frameSize * 3);
  188.  
  189. FastLED.show();
  190.  
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment