Advertisement
Guest User

Arduino mega + Ethernet shield + FastSPI library

a guest
Aug 27th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <SPI.h>        
  2. #include <Ethernet.h>
  3. #include <EthernetUdp.h>
  4. #include <FastSPI_LED.h>
  5.  
  6. #define NUM_LEDS 160
  7. struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
  8. struct CRGB *leds;
  9. #define PIN 51
  10.  
  11. int i = 0;
  12. long r;
  13. long g;
  14. long b;
  15.  
  16. // Enter a MAC address and IP address for your controller below.
  17. // The IP address will be dependent on your local network:
  18. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0xEB };
  19. IPAddress ip(192, 168, 100, 40);
  20. unsigned int localPort = 41181;      // local port to listen on
  21.  
  22. // buffers for receiving and sending data
  23. char packetBuffer[250]; //buffer to hold incoming packet,
  24. char ReplyBuffer[] = "k";       // a string to send back to keep it synchronized
  25.  
  26. // An EthernetUDP instance to let us send and receive packets over UDP
  27. EthernetUDP Udp;
  28.  
  29. void setup() {
  30.   Serial.begin(115200);
  31.  
  32.   // Leds:
  33.   FastSPI_LED.setLeds(NUM_LEDS);
  34.   FastSPI_LED.setChipset(CFastSPI_LED::SPI_WS2801);
  35.   FastSPI_LED.setPin(PIN);
  36.   FastSPI_LED.setDataRate(5);
  37.   FastSPI_LED.init();
  38.   FastSPI_LED.start();
  39.   leds = (struct CRGB*)FastSPI_LED.getRGBData();
  40.  
  41.   // start the Ethernet and UDP:
  42.   Ethernet.begin(mac,ip);
  43.   Udp.begin(localPort);
  44.  
  45.   while (Udp.parsePacket()) {
  46.     Udp.flush();
  47.   }
  48. }
  49.  
  50. void loop() {
  51.   long packetSize = Udp.parsePacket();
  52.  
  53.   if(Udp.available() && packetSize > 0)
  54.   {
  55.       Serial.println("got package!");
  56.       Udp.read(packetBuffer,250);
  57.      
  58.       memset(leds, 0, NUM_LEDS * 3);
  59.      
  60.       for(i = 0 ; i < NUM_LEDS; i++ ) {
  61.           leds[i].r = 0;
  62.           leds[i].g = 0;
  63.           leds[i].b = 255;
  64.       }
  65.       noInterrupts();
  66.       FastSPI_LED.show();
  67.       delay(10);
  68.       interrupts();
  69.      
  70.       Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  71.       Udp.write(ReplyBuffer);
  72.       Udp.endPacket();
  73.   }
  74.   else
  75.   {
  76.     Serial.println("no no no!");
  77.     memset(leds, 0, NUM_LEDS * 3);    
  78.     for (i = 0; i < NUM_LEDS; i++) {
  79.       leds[i].r = 0;
  80.       leds[i].g = 0;
  81.       leds[i].b = 0;
  82.     }
  83.    
  84.     noInterrupts();
  85.     FastSPI_LED.show();
  86.     delay(10);
  87.     interrupts();
  88.   }
  89.   delay(100);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement