This commit is contained in:
赵鑫 2022-09-12 15:53:35 +08:00
parent 1cabca570d
commit 51be6724fe

111
server.js
View File

@ -1,49 +1,45 @@
/**
* Sense HAT 服务端
*/
require('dotenv').config() // 读取系统环境变量
// HTTP 请求路由
// 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 at http://${host}:${port}`)
console.log(`rpi sense hat server is running on port ${port}`)
})
// Sense HAT 全局数据 及 初始化
const R = [255, 0, 0]
const G = [0, 255, 0]
const B = [0, 0, 255]
const Y = [255, 255, 0]
const P = [255, 0, 255]
const C = [0, 255, 255]
const O = [255, 255, 255]
const _ = [0, 0, 0]
// 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 初始为 0r 只能设置为 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])
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 初始为 0r 只能设置为 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
# 未实现 # 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() },
},
}
@ -54,17 +50,39 @@ 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, x, y, color, pixels, delay, image, character, speed, background, text }) => {
x = Number(x), y = Number(y), delay = Number(delay) | 200, speed = Number(speed) | 0.2
if (character == undefined) character = ' '
if (background == undefined) background = [0, 0, 0]
// 处理客户端请求
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':
if (color == undefined) color = [0, 0, 0]
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 'flash':
if (color == undefined) color = [255, 255, 255]
sensehat.led.clear(color)
@ -74,15 +92,7 @@ io.on('connection', (client) => {
io.emit('action', { action: 'clear' })
}, delay)
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':
for (let i = 0; i < pixels.length; i++) {
var color = pixels[i]
@ -92,29 +102,35 @@ io.on('connection', (client) => {
}
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`)
@ -128,18 +144,21 @@ io.on('connection', (client) => {
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
default:
break
}