Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Last active October 26, 2015 12:50
Show Gist options
  • Save kasperkamperman/6c7c256f7042a3ca5118 to your computer and use it in GitHub Desktop.
Save kasperkamperman/6c7c256f7042a3ca5118 to your computer and use it in GitHub Desktop.
Use ControlP5 annotations to make a control panel for FastLED. Uses a protocol of 12 bytes.
/* Use ControlP5 annotations to make a control panel for FastLED.
Uses a protocol of 12 bytes.
Protocol description:
https://drive.google.com/file/d/0B3Tn6oRmh71oS3FNQ2hsODVwS1E
Arduino code:
https://gist.github.com/kasperkamperman/7e827bb481dfcdb4cb52
*/
import controlP5.*;
import java.nio.*;
import processing.serial.*;
ControlP5 cp5;
ColorControl cc1;
EffectControl ef1, ef2, ef3;
Serial serialPort;
void setup() {
size(900,600);
cp5 = new ControlP5(this);
cc1 = new ColorControl(0);
ef1 = new EffectControl(1);
//ef2 = new EffectControl(2);
ef3 = new EffectControl(3);
cp5.addControllersFor("color1", cc1)
.setPosition(10, 10, cc1);
cp5.addControllersFor("ef1", ef1)
.setPosition(10, 180, ef1);
//cp5.addControllersFor("ef2", ef2)
//.setPosition(400, 10, ef2);
cp5.addControllersFor("ef3", ef3)
.setPosition(400, 300, ef3);
// List all the available serial ports:
printArray(Serial.list());
serialPort = new Serial(this, Serial.list()[3], 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;
//uint8_t padding byte;
//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);
byte[] byteArray = buffer.array();
//printArray(result);
// check if the values still match
// 0xFFFF is to show it as unsigned.
//println(buffer.getShort(3) & 0xFFFF);
serialPort.write(byteArray);
}
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 = 0, label="noise fader", properties = { "height=15", "width=255", "min=0", "max=255", "value=0"})
void noiseFader(int val) {
int cmdId = 1;
sendCommand(type, typeId, cmdId, val, 0, 0, 0);
}
@ControlElement (x = 0, y = 20, label="noise default", properties = { "height=15", "width=255", "min=0", "max=255", "value=0"})
void noiseDefault(int val) {
int cmdId = 2;
sendCommand(type, typeId, cmdId, val, 0, 0, 0);
}
@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