This commit is contained in:
赵鑫 2022-09-05 14:00:43 +08:00
parent b37e3c32ab
commit f84e54e870

View File

@ -27,7 +27,6 @@ const sensehat = {
sleep(n) 暂停 n sleep(n) 暂停 n
clear() 清除所有 clear() 清除所有
clear(rgb) or clear([r,g,b]) 填充颜色 clear(rgb) or clear([r,g,b]) 填充颜色
setPixel(x,y,r,g,b) or setPixel(x,y,rgb) 设置一个点 setPixel(x,y,r,g,b) or setPixel(x,y,rgb) 设置一个点
getPixel(x,y) 返回 [r,g,b] 获取一个点 getPixel(x,y) 返回 [r,g,b] 获取一个点
setPixels([[r,g,b]*64]) setPixels([[r,g,b]*64])
@ -60,6 +59,7 @@ const io = require('socket.io')(server, { cors: { origin: '*' } })
io.on('connection', (client) => { io.on('connection', (client) => {
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels }) io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
client.on('action', ({ action, x, y, color, pixels, delay }) => { client.on('action', ({ action, x, y, color, pixels, delay }) => {
x = Number(x), y = Number(y), delay = Number(delay) | 200
switch (action) { switch (action) {
case 'clear': case 'clear':
if (color == undefined) color = [0, 0, 0] if (color == undefined) color = [0, 0, 0]
@ -73,18 +73,26 @@ io.on('connection', (client) => {
setTimeout(() => { setTimeout(() => {
sensehat.led.clear() sensehat.led.clear()
io.emit('action', { action: 'clear' }) io.emit('action', { action: 'clear' })
}, delay | 200) }, delay)
break break
case 'setPixel': case 'setPixel':
if (color == undefined) color = [255, 255, 255] if (color == undefined) color = [255, 255, 255]
x = Number(x), y = Number(y)
sensehat.led.setPixel(x, y, color) sensehat.led.setPixel(x, y, color)
io.emit('action', { action: 'setPixel', x, y, color }) io.emit('action', { action: 'setPixel', x, y, color })
break break
// case 'getPixel': case 'getPixel':
// color = sensehat.led.getPixel(x, y) color = sensehat.led.getPixel(x, y)
// client.emit('action', { action: 'setPixel', x, y, color }) client.emit('action', { action: 'setPixel', x, y, color })
// break break
case 'setPixels':
for (let i = 0; i < pixels.length; i++) {
var color = pixels[i]
var x = i % 8
var y = Math.floor(i / 8)
sensehat.led.setPixel(x, y, color)
}
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
case 'getPixels': case 'getPixels':
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels }) io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break break