Skip to content

Instantly share code, notes, and snippets.

@orgicus
Created May 18, 2016
Embed
What would you like to do?
FadeCandy strip test
var numFadeCandies = 5;
var packet = new Uint8ClampedArray(4 + (numFadeCandies * 512 * 3));
var socket;
var value;
var brightnessSlider;
var ledSliders = [];
var ledLabels = [];
//devices reported back by fcserver
var devices = [];
//most recent message received from server
var message;
function setup() {
socket = new WebSocket('ws://localhost:7890');
socket.onclose = onFCClose;
socket.onopen = onFCOpen;
socket.onmessage = onFCMessage;
createElement("br");
createElement("label","brightness").position(10,10);
brightnessSlider = createSlider(0, 255, 0);
brightnessSlider.position(80,10);
brightnessSlider.input(brightnessSliderChanged);
brightnessSlider.id("brightness");
for(var i = 0 ; i < numFadeCandies ; i++){
var channelSliders = [];
for(var j = 0 ; j < 8; j++){
var x = 10 + (i * 320);
var y = 60 + (22 * j);
var label = createElement("label","FadeCandy #"+(i+1)+" channel#"+(j+1));
label.position(x,y);
var ledSlider = createSlider(0, 64, 0);
ledSlider.position(x + label.width+5,y);
ledSlider.id('{"fc":'+i+',"ch":'+j+'}');
ledSlider.input(ledSliderChanged);
channelSliders.push(ledSlider);
}
ledSliders.push(channelSliders);
}
console.log(channelSliders);
}
function ledSliderChanged(event){
var slider = event.currentTarget;
slider.title = "leds: " + slider.value;
var id = slider.id;
var data = JSON.parse(id);
var leds = slider.value;
setChannel(packet,data.fc,data.ch,value,leds);
}
function brightnessSliderChanged(event){
value = brightnessSlider.value();
event.currentTarget.title = "brightness:"+value;
for(var fc = 0; fc < numFadeCandies; fc++){
for(var ch = 0; ch < 8; ch++){
setChannel(packet,fc,ch,value,ledSliders[fc][ch].value());
}
}
}
function draw() {
sendPacket();
}
function onFCMessage(event){
console.log(event);
message = JSON.parse(event.data);
console.log(message);
if(message.type === 'list_connected_devices'){
devices = message.devices;
}
if(message.type === 'server_info'){
devices = message.config.devices;
}
}
function onFCOpen(event){
socket.send(JSON.stringify({type:"server_info"}));
}
function onFCClose(event){
}
//write frame to FadeCandy
function sendPacket() {
if (socket.readyState != WebSocket.OPEN) {
// The server connection isn't open. Nothing to do.
return;
}
if (socket.bufferedAmount > packet.length) {
// The network is lagging, and we still haven't sent the previous frame.
// Don't flood the network, it will just make us laggy.
// If fcserver is running on the same computer, it should always be able
// to keep up with the frames we send, so we shouldn't reach this point.
return;
}
socket.send(packet.buffer);
}
function getIndex(fc,ch){
return 4 + (1536 * fc) + (64 * ch * 3);
}
function getChannel(packet,fc,ch){
var start = getIndex(fc,ch);
var end = start + 64 * 3;
return packet.slice(start,end);
}
function setChannel(packet,fc,ch,value,count){
var start = getIndex(fc,ch);
var end = getIndex(fc,ch+1);
var until = start + (count * 3);
for(var i = start; i < end; i++){
if(!isNaN(until)){
if(i < until){
packet[i] = value;
}else{
packet[i] = 0;
}
}else{
packet[i] = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment