Skip to content

Instantly share code, notes, and snippets.

@internetjanitor
Created April 26, 2013 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save internetjanitor/5469052 to your computer and use it in GitHub Desktop.
Save internetjanitor/5469052 to your computer and use it in GitHub Desktop.
Sample code for remote control of Arduino and WS2811 LED strip
#!/usr/bin/python
import socket
from time import sleep
from random import randint
# addressing information of target
IPADDR = '2.0.0.20'
PORTNUM = 6000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
s.connect((IPADDR, PORTNUM))
s.send(bytes(2) + "\n")
s.close()
def f7(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if x not in seen and not seen_add(x)]
try:
for i in range(30000):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
s.connect((IPADDR, PORTNUM))
cmd = chr(1) + chr(randint(1,154)) + chr(randint(1,154)) + chr(randint(1,154))
cmdlist = []
for j in range(200):
#cmd = cmd + chr(randint(1,240))
#cmd = cmd + chr(j)
cmdlist.append(randint(1,240))
print cmdlist
for j in sorted(f7(cmdlist)):
cmd = cmd + chr(j)
#print cmd
s.send(cmd)
s.close()
#sleep(0.61)
#print i
except:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
s.connect((IPADDR, PORTNUM))
s.send(chr(2))
s.close()
#include <FastSPI_LED.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define NUM_LEDS 240
uint8_t mac [6] = { 1, 2, 3, 0, 0, 20}; // the mac address of node
uint8_t localIp [4] = { 2, 0, 0, 20};
unsigned int localPort = 6000;
// Sometimes chipsets wire in a backwards sort of way
struct CRGB { unsigned char b; unsigned char r; unsigned char g; };
// struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB *leds;
#define PIN 4
//const int MAX_BUFFER_UDP = 1650;
//volatile uint8_t packetBuffer [MAX_BUFFER_UDP];
//unsigned char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
unsigned char packetBuffer[1600];
EthernetUDP Udp;
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void setup()
{
Serial.begin(115200);
Ethernet.begin(mac, localIp);
Udp.begin(localPort);
FastSPI_LED.setLeds(NUM_LEDS);
FastSPI_LED.setChipset(CFastSPI_LED::SPI_WS2811);
FastSPI_LED.setPin(PIN);
FastSPI_LED.init();
FastSPI_LED.start();
leds = (struct CRGB*)FastSPI_LED.getRGBData();
}
void loop() {
long packetSize = Udp.parsePacket();
if( Udp.available() )
handle_packet(packetSize);
//Serial.print("freeMemory()=");
//Serial.println(freeRam());
}
void handle_packet(long packetSize) {
// Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Udp.read(packetBuffer,1600);
//Serial.println("Contents:");
//Serial.println(packetBuffer);
//packetBuffer[packetSize+1] = '\0';
int offset = 0;
if (packetBuffer[offset] == 1) {
offset++;
int r = packetBuffer[offset]; offset++;
int g = packetBuffer[offset]; offset++;
int b = packetBuffer[offset]; offset++;
if (r > 255) r=255;
if (g > 255) g=255;
if (b > 255) b=255;
char rgb[20];
sprintf(rgb, "RGB %d %d %d", r, g, b);
Serial.println(rgb);
//Serial.println("RGB:" + itoa(r) + itoa(g) + itoa(b));
memset(leds, 0, NUM_LEDS * 3);
int lastl = 0;
for (int i = offset; i < (packetSize-1); i++) {
int l = packetBuffer[i];
//Serial.print(l); Serial.print(",");
if (l > lastl + 1) {
for (int j=lastl; j < l; j++) {
leds[l].r = 0;
leds[l].g = 0;
leds[l].b = 0;
}
}
leds[l].r = r;
leds[l].g = g;
leds[l].b = b;
}
Serial.println("");
} else if (int(packetBuffer[offset]) == 2) {
memset(leds, 0, NUM_LEDS * 3);
//Serial.println("ALL OFF");
} else {
Serial.println(packetBuffer[offset]);
}
noInterrupts();
FastSPI_LED.show();
delay(10);
interrupts();
//Serial.println("show\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment