174 lines
6.4 KiB
JavaScript
174 lines
6.4 KiB
JavaScript
/**
|
||
* Sense HAT 服务端
|
||
*/
|
||
|
||
require('dotenv').config() // 读取系统环境变量
|
||
|
||
// HTTP状态测试路由
|
||
const app = require('express')()
|
||
app.get('/', (req, res) => res.sendStatus(200))
|
||
|
||
// 启动HTTP服务
|
||
const port = process.env.PORT || 3000
|
||
const host = process.env.HOST || 'localhost'
|
||
const server = app.listen(port, host, () => {
|
||
console.log(`rpi sense hat server is running on port ${port}`)
|
||
})
|
||
|
||
// Sense HAT 初始化
|
||
const sensehat = {
|
||
led: {
|
||
/*
|
||
require('sense-hat-led').sync中包含以下方法:
|
||
sleep(n) 暂停 n 秒
|
||
clear() 清除所有
|
||
clear(rgb) or clear([r,g,b]) 填充颜色
|
||
setPixel(x,y,r,g,b) or setPixel(x,y,rgb) 设置一个点
|
||
getPixel(x,y) 返回 [r,g,b] 获取一个点
|
||
setPixels([[r,g,b]*64])
|
||
getPixels() -> [[r,g,b]*64]
|
||
flipH(redraw=true) 水平反转
|
||
flipV(redraw=true) 垂直反转
|
||
getter and setter rotation 默认为 0
|
||
setRotation(r, redraw=true) 设置旋转,r 初始为 0,r 只能设置为 0/90/180/270
|
||
loadImage(filePath, redraw=true)
|
||
showLetter(character, textColour=[255,255,255], backColour=[0,0,0])
|
||
showMessage(textString, scrollSpeed=0.1(秒), textColour=[255,255,255], backColour=[0,0,0])
|
||
flashMessage(textString, speed=0.1(秒), textColour=[255,255,255], backColour=[0,0,0])
|
||
|
||
# 未实现 # getter and setter lowLight 默认为 true
|
||
# 未实现 # getter and setter gamma = Array of length 32 containing Integers between 0 and 31
|
||
# 未实现 # gammaReset() 恢复默认 gamma
|
||
*/
|
||
...require('sense-hat-led').sync,
|
||
get pixels() { return sensehat.led.getPixels() },
|
||
},
|
||
}
|
||
sensehat.led.clear()
|
||
sensehat.led.setRotation(180)
|
||
sensehat.led.loadImage('images/heart.png')
|
||
|
||
// socketio 服务
|
||
const io = require('socket.io')(server, { cors: { origin: '*' } })
|
||
io.on('connection', (client) => {
|
||
|
||
// 初始化客户端led矩阵
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
|
||
// 处理客户端请求
|
||
client.on('action', ({ action, color, x, y, pixels, image, character, delay, speed, background, text }) => {
|
||
|
||
x = Number(x)
|
||
y = Number(y)
|
||
delay = Number(delay || 200)
|
||
speed = Number(speed || 0.1)
|
||
character = character || ' '
|
||
background = background || [0, 0, 0]
|
||
|
||
switch (action) {
|
||
|
||
case 'clear':
|
||
color = color || [0, 0, 0]
|
||
sensehat.led.clear(color)
|
||
io.emit('action', { action: 'clear', color })
|
||
break
|
||
|
||
case 'setPixel':
|
||
if (color == undefined) color = [255, 255, 255]
|
||
sensehat.led.setPixel(x, y, color)
|
||
io.emit('action', { action: 'setPixel', x, y, color })
|
||
break
|
||
|
||
case 'getPixel':
|
||
color = sensehat.led.getPixel(x, y)
|
||
client.emit('action', { action: 'setPixel', x, y, color })
|
||
break
|
||
|
||
case 'setPixels':
|
||
pixels.forEach((color, i) => {
|
||
let x = i % 8
|
||
let y = Math.floor(i / 8)
|
||
sensehat.led.setPixel(x, y, color)
|
||
})
|
||
|
||
// for (let i = 0; i < pixels.length; i++) {
|
||
// var color = pixels[i]
|
||
// var x = i % 8
|
||
// var y = Math.floor(i / 8)
|
||
// }
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'getPixels':
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'flipH':
|
||
sensehat.led.flipH()
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'flipV':
|
||
sensehat.led.flipV()
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'turnLeft':
|
||
var heading_angle = (Number(sensehat.led.rotation) + 90) % 360
|
||
sensehat.led.rotation = heading_angle
|
||
sensehat.led.setRotation(heading_angle)
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'turnRight':
|
||
var heading_angle = (Number(sensehat.led.rotation) + 360 - 90) % 360
|
||
sensehat.led.rotation = heading_angle
|
||
sensehat.led.setRotation(heading_angle)
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'loadImage':
|
||
try {
|
||
sensehat.led.loadImage(`images/${image}.png`)
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
} catch (error) {
|
||
console.error(error)
|
||
}
|
||
break
|
||
case 'showLetter':
|
||
if (color == undefined) color = [255, 255, 255]
|
||
sensehat.led.showLetter(character, color, background)
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'showMessage':
|
||
if (color == undefined) color = [255, 255, 255]
|
||
sensehat.led.showMessage(text, speed, color, background)
|
||
sensehat.led.clear()
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'flashMessage':
|
||
if (color == undefined) color = [255, 255, 255]
|
||
sensehat.led.flashMessage(text, speed, color, background)
|
||
sensehat.led.clear()
|
||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||
break
|
||
|
||
case 'flash':
|
||
if (color == undefined) color = [255, 255, 255]
|
||
sensehat.led.clear(color)
|
||
io.emit('action', { action: 'clear', color })
|
||
setTimeout(() => {
|
||
sensehat.led.clear()
|
||
io.emit('action', { action: 'clear' })
|
||
}, delay)
|
||
break
|
||
|
||
default:
|
||
break
|
||
}
|
||
|
||
})
|
||
})
|