rpi_sense_hat_server/server.js

174 lines
6.4 KiB
JavaScript
Raw Normal View History

2022-09-12 07:53:35 +00:00
/**
* Sense HAT 服务端
*/
2022-09-05 01:22:27 +00:00
require('dotenv').config() // 读取系统环境变量
2022-09-12 07:53:35 +00:00
// HTTP状态测试路由
2022-09-05 01:06:25 +00:00
const app = require('express')()
2022-09-04 08:07:37 +00:00
app.get('/', (req, res) => res.sendStatus(200))
2022-09-05 01:22:27 +00:00
2022-09-12 07:53:35 +00:00
// 启动HTTP服务
2022-09-05 01:06:25 +00:00
const port = process.env.PORT || 3000
2022-09-12 07:56:06 +00:00
const host = process.env.HOST || 'localhost'
2022-09-04 11:20:59 +00:00
const server = app.listen(port, host, () => {
2022-09-12 07:53:35 +00:00
console.log(`rpi sense hat server is running on port ${port}`)
2022-09-04 11:20:59 +00:00
})
2022-09-12 07:53:35 +00:00
// Sense HAT 初始化
2022-09-04 11:20:59 +00:00
const sensehat = {
2022-09-05 01:29:29 +00:00
led: {
2022-09-05 02:00:14 +00:00
/*
2022-09-12 07:53:35 +00:00
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
2022-09-05 02:00:14 +00:00
*/
2022-09-12 07:53:35 +00:00
...require('sense-hat-led').sync,
2022-09-05 02:23:01 +00:00
get pixels() { return sensehat.led.getPixels() },
2022-09-05 01:29:29 +00:00
},
2022-09-04 11:20:59 +00:00
}
2022-09-05 02:29:40 +00:00
sensehat.led.clear()
2022-09-05 07:51:43 +00:00
sensehat.led.setRotation(180)
2022-09-05 02:23:28 +00:00
sensehat.led.loadImage('images/heart.png')
2022-09-05 02:00:14 +00:00
2022-09-05 01:22:27 +00:00
// socketio 服务
const io = require('socket.io')(server, { cors: { origin: '*' } })
2022-09-04 07:26:20 +00:00
io.on('connection', (client) => {
2022-09-12 07:53:35 +00:00
// 初始化客户端led矩阵
2022-09-05 03:39:47 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
2022-09-12 07:53:35 +00:00
// 处理客户端请求
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]
2022-09-04 17:01:48 +00:00
switch (action) {
2022-09-12 07:53:35 +00:00
2022-09-05 02:29:40 +00:00
case 'clear':
2022-09-12 07:53:35 +00:00
color = color || [0, 0, 0]
2022-09-05 03:56:18 +00:00
sensehat.led.clear(color)
2022-09-05 04:03:51 +00:00
io.emit('action', { action: 'clear', color })
2022-09-05 03:56:18 +00:00
break
2022-09-12 07:53:35 +00:00
2022-09-05 04:09:18 +00:00
case 'setPixel':
if (color == undefined) color = [255, 255, 255]
2022-09-05 05:51:26 +00:00
sensehat.led.setPixel(x, y, color)
2022-09-05 05:46:43 +00:00
io.emit('action', { action: 'setPixel', x, y, color })
2022-09-05 04:09:18 +00:00
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:00:43 +00:00
case 'getPixel':
color = sensehat.led.getPixel(x, y)
client.emit('action', { action: 'setPixel', x, y, color })
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:00:43 +00:00
case 'setPixels':
2022-09-12 08:12:20 +00:00
pixels.forEach((color, i) = {
let x = i % 8
let y = Math.floor(i / 8)
2022-09-05 06:00:43 +00:00
sensehat.led.setPixel(x, y, color)
2022-09-12 08:12:20 +00:00
})
// for (let i = 0; i < pixels.length; i++) {
// var color = pixels[i]
// var x = i % 8
// var y = Math.floor(i / 8)
// }
2022-09-05 06:00:43 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
2022-09-12 07:53:35 +00:00
2022-09-05 03:39:47 +00:00
case 'getPixels':
2022-09-05 03:46:43 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
2022-09-05 03:18:56 +00:00
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:05:20 +00:00
case 'flipH':
sensehat.led.flipH()
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:05:20 +00:00
case 'flipV':
sensehat.led.flipV()
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
2022-09-12 07:56:06 +00:00
2022-09-05 06:19:37 +00:00
case 'turnLeft':
2022-09-05 06:32:51 +00:00
var heading_angle = (Number(sensehat.led.rotation) + 90) % 360
2022-09-05 06:35:58 +00:00
sensehat.led.rotation = heading_angle
2022-09-05 06:32:51 +00:00
sensehat.led.setRotation(heading_angle)
2022-09-05 06:30:02 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
2022-09-05 06:19:37 +00:00
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:19:37 +00:00
case 'turnRight':
2022-09-05 06:32:51 +00:00
var heading_angle = (Number(sensehat.led.rotation) + 360 - 90) % 360
2022-09-05 06:35:58 +00:00
sensehat.led.rotation = heading_angle
2022-09-05 06:32:51 +00:00
sensehat.led.setRotation(heading_angle)
2022-09-05 06:30:02 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
2022-09-05 06:19:37 +00:00
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:35:58 +00:00
case 'loadImage':
2022-09-05 06:40:48 +00:00
try {
sensehat.led.loadImage(`images/${image}.png`)
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
} catch (error) {
console.error(error)
}
2022-09-05 06:35:58 +00:00
break
2022-09-05 06:45:36 +00:00
case 'showLetter':
2022-09-05 06:47:08 +00:00
if (color == undefined) color = [255, 255, 255]
2022-09-05 06:52:24 +00:00
sensehat.led.showLetter(character, color, background)
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
2022-09-12 07:53:35 +00:00
2022-09-05 06:52:24 +00:00
case 'showMessage':
if (color == undefined) color = [255, 255, 255]
sensehat.led.showMessage(text, speed, color, background)
2022-09-05 07:15:34 +00:00
sensehat.led.clear()
2022-09-05 06:45:36 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
2022-09-12 07:53:35 +00:00
2022-09-05 07:12:16 +00:00
case 'flashMessage':
if (color == undefined) color = [255, 255, 255]
sensehat.led.flashMessage(text, speed, color, background)
2022-09-05 07:15:34 +00:00
sensehat.led.clear()
2022-09-05 07:12:16 +00:00
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
2022-09-12 07:53:35 +00:00
2022-09-12 08:12:20 +00:00
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
2022-09-04 17:01:48 +00:00
default:
break
2022-09-04 16:51:48 +00:00
}
2022-09-04 17:01:48 +00:00
2022-09-04 16:51:48 +00:00
})
2022-09-04 07:26:20 +00:00
})