rpi_sense_hat_server/server.js
2022-09-05 14:45:36 +08:00

137 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

require('dotenv').config() // 读取系统环境变量
// HTTP 请求路由
const app = require('express')()
app.get('/', (req, res) => res.sendStatus(200))
// 服务器启动
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}`)
})
// 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]
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
# 未实现 # getter and setter lowLight 默认为 true
# 未实现 # getter and setter gamma = Array of length 32 containing Integers between 0 and 31
# 未实现 # gammaReset() 恢复默认 gamma
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])
*/
get pixels() { return sensehat.led.getPixels() },
},
}
sensehat.led.clear()
sensehat.led.loadImage('images/heart.png')
// socketio 服务
const io = require('socket.io')(server, { cors: { origin: '*' } })
io.on('connection', (client) => {
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
client.on('action', ({ action, x, y, color, pixels, delay, image, character, speed, backcolor }) => {
x = Number(x), y = Number(y), delay = Number(delay) | 200, speed = Number(speed) | 0.2
if (character == undefined) character = ' '
if (backcolor == undefined) backcolor = [0, 0, 0]
switch (action) {
case 'clear':
if (color == undefined) color = [0, 0, 0]
sensehat.led.clear(color)
io.emit('action', { action: 'clear', color })
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
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]
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':
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':
sensehat.led.showLetter(character)
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
break
default:
break
}
})
})