Last active
March 11, 2016 14:35
-
-
Save kasperkamperman/1f185758519fa3131ea0 to your computer and use it in GitHub Desktop.
Control panel for FastLED to send command data over TCP. Modify the protocol for your own needs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Use ControlP5 annotations to make a control panel for FastLED. | |
Uses a protocol of 12 bytes. | |
This sends data over TCP to your Photon: | |
Protocol description: | |
https://drive.google.com/file/d/0B3Tn6oRmh71oS3FNQ2hsODVwS1E | |
Particle Photon code: | |
https://gist.github.com/kasperkamperman/89f67f049a2ec45a5c9c | |
Arduino code serial: | |
https://gist.github.com/kasperkamperman/7e827bb481dfcdb4cb52 | |
ControlP5 library: | |
http://www.sojamo.de/libraries/controlP5/ | |
*/ | |
import controlP5.*; | |
import java.nio.*; | |
import processing.serial.*; | |
ControlP5 cp5; | |
ColorControl cc1; | |
EffectControl ef1; | |
import processing.net.*; | |
Serial serialPort; | |
Client client; | |
String PhotonIPAddress = "192.168.1.78"; | |
// we use communication of TCP when true. Otherwise over Serial. | |
boolean isNetwork = true; | |
// make it false if you only want to send data on mouseReleased | |
boolean sendDirectlyFlag = true; | |
byte[] commandByteArray; | |
void setup() { | |
size(900,600); | |
cp5 = new ControlP5(this); | |
cc1 = new ColorControl(0); | |
ef1 = new EffectControl(1); | |
cp5.addControllersFor("color1", cc1) | |
.setPosition(10, 10, cc1); | |
cp5.addControllersFor("ef1", ef1) | |
.setPosition(10, 180, ef1); | |
// List all the available serial ports: | |
printArray(Serial.list()); | |
if(isNetwork) { | |
client = new Client(this, PhotonIPAddress, 12345); | |
} | |
//else { | |
printArray(Serial.list()); | |
// change 7 to the right Serial port number. | |
serialPort = new Serial(this, Serial.list()[7], 57600); | |
serialPort.bufferUntil(10); | |
//} | |
} | |
void draw() { | |
background(128); | |
} | |
void serialEvent(Serial p) { | |
println("from serial: "+p.readString()); | |
} | |
void sendCommand(int type, int typeId, int cmdId, int value0, int value1, int value2, int value3) { | |
println(type+" "+typeId+" "+cmdId+" "+value0+" "+value1+" "+value2+" "+value3); | |
//uint8_t type; | |
//uint8_t typeId; | |
//uint8_t cmdId; | |
//uint16_t cmdValue0; | |
//uint16_t cmdValue1; // future use | |
//uint16_t cmdValue2; // future use | |
//uint16_t cmdValue3; // future use | |
ByteBuffer buffer = ByteBuffer.allocate(12); | |
buffer.order(ByteOrder.LITTLE_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN. | |
buffer.put((byte) type); | |
buffer.put((byte) typeId); | |
buffer.put((byte) cmdId); | |
buffer.put((byte) 0); // paddingbyte | |
buffer.putShort((short) value0); | |
buffer.putShort((short) value1); | |
buffer.putShort((short) value2); | |
buffer.putShort((short) value3); | |
commandByteArray = buffer.array(); | |
if(sendDirectlyFlag) sendData(); | |
// check if the values still match | |
// 0xFFFF is to show it as unsigned. | |
//println(buffer.getShort(3) & 0xFFFF); | |
} | |
void sendData() { | |
if(isNetwork) client.write(commandByteArray); | |
else serialPort.write(commandByteArray); | |
println(">>>>>> SEND >>>>>>>>>>"); | |
} | |
void mouseReleased() { | |
if(!sendDirectlyFlag) sendData(); | |
} | |
public class ColorControl { | |
private int typeId; | |
private int type = 1; | |
ColorControl(int colorId) { | |
typeId = colorId; | |
} | |
@ControlElement (x = 0, y = 0, label="color hue", properties = { "id=0", "height=15", "width=255", "min=0", "max=255", "value=0"}) | |
void colorHue(int val) { | |
int cmdId = 1; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 20, label="color max saturation", properties = { "id=1", "height=15", "width=255", "min=0", "max=255", "value=255"}) | |
void colorMaxSaturation(int val) { | |
int cmdId = 2; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 40, label="color min saturation", properties = { "id=2", "height=15", "width=255", "min=0", "max=255", "value=0"}) | |
void colorMinSat(int val) { | |
int cmdId = 3; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 60, label="color max brightness", properties = { "id=3", "height=15", "width=255", "min=0", "max=255", "value=255"}) | |
void colorMaxBri(int val) { | |
int cmdId = 4; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 80, label="color min brightness", properties = { "id=4", "height=15", "width=255", "min=0", "max=255", "value=0"}) | |
void colorMinBri(int val) { | |
int cmdId = 5; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 100, label="color master brightness", properties = { "id=5", "height=15", "width=255", "min=0", "max=255", "value=255"}) | |
void colorMasterBri(int val) { | |
int cmdId = 6; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
} | |
public class EffectControl { | |
private int typeId; | |
private int type = 3; | |
EffectControl(int effectId) { | |
typeId = effectId; | |
} | |
@ControlElement (x = 0, y = 100, label="noise octave", properties = { "height=15", "width=255", "min=0", "max=255", "value=0"}) | |
void noiseOctave(int val) { | |
int cmdId = 6; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 120, label="noise speed", properties = { "height=15", "width=255", "min=0", "max=65535", "value=0"}) | |
void noiseSpeed(int val) { | |
int cmdId = 7; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 140, label="noise scale", properties = { "height=15", "width=255", "min=0", "max=65535", "value=0"}) | |
void noiseScale(int val) { | |
int cmdId = 8; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 160, label="noise startX", properties = { "height=15", "width=255", "min=0", "max=65535", "value=0"}) | |
void noiseStartX(int val) { | |
int cmdId = 9; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 180, label="noise startY", properties = { "height=15", "width=255", "min=0", "max=65535", "value=0"}) | |
void noiseStartY(int val) { | |
int cmdId = 10; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
@ControlElement (x = 0, y = 200, label="noise start time", properties = { "height=15", "width=255", "min=0", "max=65535", "value=0"}) | |
void noiseStartT(int val) { | |
int cmdId = 11; | |
sendCommand(type, typeId, cmdId, val, 0, 0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment