0

我们需要关于如何使用 websocket 和 fadecandy 服务器来映射 64 个 LED 灯条的帮助。

这是 fadecandy 服务器中的映射

服务器配置 JSON:

    {
    "listen": [ null, 7890 ],
    "verbose": true,
    "color": {
        "gamma": 2.5,
        "whitepoint": [ 0.7, 0.7, 0.7 ]
    },
    "devices": [
        {
            "type": "fadecandy",
            "serial": "YSEELLIWMMPNTTUT",
            "map": [
                [ 0, 0, 0, 60 ],
                [ 0, 60, 64, 60 ],
                [ 0, 120, 128, 60 ],
                [ 0, 180, 192, 60 ],
                [ 0, 240, 256, 60 ],
                [ 0, 300, 320, 60 ],
                [ 0, 360, 384, 60 ],
                [ 0, 420, 448, 60 ]
            ]
        },
        {
            "type": "fadecandy",
            "serial": "IMOQHJFLPHOVWJUD",
            "map": [
                [ 0, 480, 0, 60 ],
                [ 0, 540, 64, 60 ],
                [ 0, 600, 128, 60 ],
                [ 0, 660, 192, 60 ],
                [ 0, 720, 256, 60 ],
                [ 0, 780, 320, 60 ],
                [ 0, 840, 384, 60 ],
                [ 0, 900, 448, 60 ]
            ]
        },
        {
            "type": "fadecandy",
            "serial": "KEEODXMCGEVDJHIZ",
            "map": [
                [ 0, 960, 0, 60 ],
                [ 0, 1020, 64, 60 ],
                [ 0, 1080, 128, 60 ],
                [ 0, 1140, 192, 60 ],
                [ 0, 1200, 256, 60 ],
                [ 0, 1260, 320, 60 ],
                [ 0, 1320, 384, 60 ],
                [ 0, 1380, 448, 60 ]
            ]
        }
    ]
}

我们如何使用 fadecandy 服务器映射到 websocket?

4

1 回答 1

0

Fade Candy 有很好的文档记录和示例,你应该从它开始。

在从 webSockets 控制 64 个 LED 的条带方面,请使用mouse.html 示例。只要您的FadeCandy 服务器正在运行,您就应该能够通过鼠标示例控制 64 个 LED 中的 40 个。您可以轻松调整示例,例如var leds = 40;将变为var leds = 40;

关键元素在代码的底部:

  1. 连接到 websocket 服务器
  2. 发送 FadeCandy 数据包

第一部分很简单:

var socket = new WebSocket('ws://localhost:7890');

        socket.onclose = function(event) {
           console.log('Not connected to fcserver');
        }
        socket.onopen = function(event) {
            console.log('Connected');
        }

数据部分稍微复杂一些,但不是很多。完整的 websocket FadeCandy 数据包规范可在此处获得,但是您可以查看writeFrame()html 示例中的函数以了解如何对像素进行采样、打包和发送。ganzfeld 示例中使用的版本更简单,因为它向所有像素发送一种颜色。从那里开始可能会更简单,然后再更改单个像素。

关于 Java,FadeCandy 有一个建立在 TCP 之上的简单协议,称为OPC(开放像素控制)。最简单的开始方法可能是使用处理示例(在您的情况下,strip64_dot、strip64_flames、strip64_unmapped 会有所帮助)

更新 这里是一个基本的代码示例,将前 64 个像素从网络摄像头发送到 FadeCandy:

var webcam;
var fadeCandy;
//how many LEDs are on the strip
var leds = 64;

function setup() {
  createCanvas(390, 240);
  //setup cam
  webcam = createCapture(VIDEO);
  //hide capture html element
  webcam.hide();
  //setup FC
  // Connect to a Fadecandy server running on the same computer, on the default port
  fadeCandy = new WebSocket('ws://localhost:7890');
  fadeCandy.onclose = onFCClose();
  fadeCandy.onopen = onFCOpen();
}

function draw() {
  background(255);
  image(webcam, 0, 0, 320, 240);
  writeFrame();
}
function onFCOpen(event){
  console.log("FadeCandy connected!");
  console.log(event);
}
function onFCClose(event){
  console.log("FadeCandy disconnected!");
  console.log(event);
}

//write frame to FadeCandy
function writeFrame() {
	var packet = new Uint8ClampedArray(4 + leds * 3);

	if (fadeCandy.readyState != WebSocket.OPEN) {
		// The server connection isn't open. Nothing to do.
		return;
	}

	if (fadeCandy.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;
	}

// 	// Dest position in our packet. Start right after the header.
	var dest = 4;

  //sample pixels
	webcam.loadPixels();
	/*
	    sample pixels for each led
	    i = counter for the led
	    j = counter for pixel
	    the j counter is incremented by 4 -> 4 channels (red,green,blue,alpha)
	*/
	for(var i = 0,j = 0; i < leds; i++,j+= 4){
	  packet[dest++] = webcam.pixels[j];
	  packet[dest++] = webcam.pixels[j+1];
	  packet[dest++] = webcam.pixels[j+2];
	}
	
	fadeCandy.send(packet.buffer);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/addons/p5.dom.min.js"></script>

您可能希望对像素进行不同的采样,但这应该让您了解如何:

  1. 通过 WebSockets 连接到 FadeCandy
  2. 发送 FadeCandy 像素数据
于 2016-03-22T11:46:01.267 回答