This commit is contained in:
赵鑫 2022-09-05 11:39:47 +08:00
parent 85cdc3abaf
commit 1a2d5c4e65

View File

@ -57,6 +57,7 @@ sensehat.led.loadImage('images/heart.png')
// socketio 服务 // socketio 服务
const io = require('socket.io')(server, { cors: { origin: '*' } }) 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 })
client.on('action', ({ action, x, y, color, pixels, delay }) => { client.on('action', ({ action, x, y, color, pixels, delay }) => {
switch (action) { switch (action) {
case 'clear': case 'clear':
@ -64,20 +65,24 @@ io.on('connection', (client) => {
sensehat.led.clear(color) sensehat.led.clear(color)
client.emit('action', { action: 'clear', color }) client.emit('action', { action: 'clear', color })
break break
case 'flash': // case 'flash':
if (color == undefined) color = [255, 255, 255] // if (color == undefined) color = [255, 255, 255]
sensehat.led.clear(color) // sensehat.led.clear(color)
setTimeout(sensehat.led.clear, delay | 200) // setTimeout(sensehat.led.clear, delay | 200)
client.emit('action', { action: 'clear' }) // client.emit('action', { action: 'clear' })
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 'setPixel': // case 'setPixel':
if (color == undefined) color = [255, 255, 255] // if (color == undefined) color = [255, 255, 255]
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
case 'getPixels':
const pixels = sensehat.led.setPixels()
io.emit('action', { action: 'setPixels', pixels })
break break
// case 'on': // case 'on':
// sense.setPixel(Number(x), Number(y), color_blue) // sense.setPixel(Number(x), Number(y), color_blue)
@ -94,7 +99,6 @@ io.on('connection', (client) => {
// break // break
default: default:
console.log('okokokokok')
break break
} }