gitpush
This commit is contained in:
parent
1cabca570d
commit
51be6724fe
75
server.js
75
server.js
@ -1,29 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Sense HAT 服务端
|
||||||
|
*/
|
||||||
|
|
||||||
require('dotenv').config() // 读取系统环境变量
|
require('dotenv').config() // 读取系统环境变量
|
||||||
|
|
||||||
// HTTP 请求路由
|
// HTTP状态测试路由
|
||||||
const app = require('express')()
|
const app = require('express')()
|
||||||
app.get('/', (req, res) => res.sendStatus(200))
|
app.get('/', (req, res) => res.sendStatus(200))
|
||||||
|
|
||||||
// 服务器启动
|
// 启动HTTP服务
|
||||||
const port = process.env.PORT || 3000
|
const port = process.env.PORT || 3000
|
||||||
const host = process.env.HOST || 'localhost'
|
|
||||||
const server = app.listen(port, host, () => {
|
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 全局数据 及 初始化
|
// 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 = {
|
const sensehat = {
|
||||||
led: {
|
led: {
|
||||||
...require('sense-hat-led').sync,
|
|
||||||
/*
|
/*
|
||||||
|
require('sense-hat-led').sync中包含以下方法:
|
||||||
sleep(n) 暂停 n 秒
|
sleep(n) 暂停 n 秒
|
||||||
clear() 清除所有
|
clear() 清除所有
|
||||||
clear(rgb) or clear([r,g,b]) 填充颜色
|
clear(rgb) or clear([r,g,b]) 填充颜色
|
||||||
@ -44,6 +39,7 @@ const sensehat = {
|
|||||||
# 未实现 # getter and setter gamma = Array of length 32 containing Integers between 0 and 31
|
# 未实现 # getter and setter gamma = Array of length 32 containing Integers between 0 and 31
|
||||||
# 未实现 # gammaReset() 恢复默认 gamma
|
# 未实现 # gammaReset() 恢复默认 gamma
|
||||||
*/
|
*/
|
||||||
|
...require('sense-hat-led').sync,
|
||||||
get pixels() { return sensehat.led.getPixels() },
|
get pixels() { return sensehat.led.getPixels() },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -54,17 +50,39 @@ 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) => {
|
||||||
|
|
||||||
|
// 初始化客户端led矩阵
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
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 = ' '
|
client.on('action', ({ action, color, x, y, pixels, image, character, delay, speed, background, text }) => {
|
||||||
if (background == undefined) background = [0, 0, 0]
|
|
||||||
|
x = Number(x)
|
||||||
|
y = Number(y)
|
||||||
|
delay = Number(delay || 200)
|
||||||
|
speed = Number(speed || 0.1)
|
||||||
|
character = character || ' '
|
||||||
|
background = background || [0, 0, 0]
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
|
||||||
case 'clear':
|
case 'clear':
|
||||||
if (color == undefined) color = [0, 0, 0]
|
color = color || [0, 0, 0]
|
||||||
sensehat.led.clear(color)
|
sensehat.led.clear(color)
|
||||||
io.emit('action', { action: 'clear', color })
|
io.emit('action', { action: 'clear', color })
|
||||||
break
|
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':
|
case 'flash':
|
||||||
if (color == undefined) color = [255, 255, 255]
|
if (color == undefined) color = [255, 255, 255]
|
||||||
sensehat.led.clear(color)
|
sensehat.led.clear(color)
|
||||||
@ -74,15 +92,7 @@ io.on('connection', (client) => {
|
|||||||
io.emit('action', { action: 'clear' })
|
io.emit('action', { action: 'clear' })
|
||||||
}, delay)
|
}, delay)
|
||||||
break
|
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':
|
case 'setPixels':
|
||||||
for (let i = 0; i < pixels.length; i++) {
|
for (let i = 0; i < pixels.length; i++) {
|
||||||
var color = pixels[i]
|
var color = pixels[i]
|
||||||
@ -92,29 +102,35 @@ io.on('connection', (client) => {
|
|||||||
}
|
}
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'getPixels':
|
case 'getPixels':
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'flipH':
|
case 'flipH':
|
||||||
sensehat.led.flipH()
|
sensehat.led.flipH()
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'flipV':
|
case 'flipV':
|
||||||
sensehat.led.flipV()
|
sensehat.led.flipV()
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'turnLeft':
|
case 'turnLeft':
|
||||||
var heading_angle = (Number(sensehat.led.rotation) + 90) % 360
|
var heading_angle = (Number(sensehat.led.rotation) + 90) % 360
|
||||||
sensehat.led.rotation = heading_angle
|
sensehat.led.rotation = heading_angle
|
||||||
sensehat.led.setRotation(heading_angle)
|
sensehat.led.setRotation(heading_angle)
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'turnRight':
|
case 'turnRight':
|
||||||
var heading_angle = (Number(sensehat.led.rotation) + 360 - 90) % 360
|
var heading_angle = (Number(sensehat.led.rotation) + 360 - 90) % 360
|
||||||
sensehat.led.rotation = heading_angle
|
sensehat.led.rotation = heading_angle
|
||||||
sensehat.led.setRotation(heading_angle)
|
sensehat.led.setRotation(heading_angle)
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'loadImage':
|
case 'loadImage':
|
||||||
try {
|
try {
|
||||||
sensehat.led.loadImage(`images/${image}.png`)
|
sensehat.led.loadImage(`images/${image}.png`)
|
||||||
@ -128,18 +144,21 @@ io.on('connection', (client) => {
|
|||||||
sensehat.led.showLetter(character, color, background)
|
sensehat.led.showLetter(character, color, background)
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'showMessage':
|
case 'showMessage':
|
||||||
if (color == undefined) color = [255, 255, 255]
|
if (color == undefined) color = [255, 255, 255]
|
||||||
sensehat.led.showMessage(text, speed, color, background)
|
sensehat.led.showMessage(text, speed, color, background)
|
||||||
sensehat.led.clear()
|
sensehat.led.clear()
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'flashMessage':
|
case 'flashMessage':
|
||||||
if (color == undefined) color = [255, 255, 255]
|
if (color == undefined) color = [255, 255, 255]
|
||||||
sensehat.led.flashMessage(text, speed, color, background)
|
sensehat.led.flashMessage(text, speed, color, background)
|
||||||
sensehat.led.clear()
|
sensehat.led.clear()
|
||||||
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
io.emit('action', { action: 'setPixels', pixels: sensehat.led.pixels })
|
||||||
break
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user