2022-08-31 23:41:47 +00:00
|
|
|
const express = require('express')
|
|
|
|
const socketio = require('socket.io')
|
|
|
|
const sense = require('sense-hat-led').sync
|
|
|
|
const app = express()
|
|
|
|
const server = app.listen(3000, '0.0.0.0', () => console.log('server is running'))
|
|
|
|
const io = socketio(server)
|
2022-09-01 00:37:42 +00:00
|
|
|
app.use(express.static('public'))
|
2022-08-31 23:41:47 +00:00
|
|
|
sense.setRotation(180)
|
|
|
|
sense.lowLight = true
|
2022-09-01 05:03:01 +00:00
|
|
|
|
2022-09-01 05:20:50 +00:00
|
|
|
const blue = [0, 0, 100]
|
2022-09-01 05:37:32 +00:00
|
|
|
const leds = [
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
[0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
]
|
2022-09-01 06:08:01 +00:00
|
|
|
read_all_leds()
|
2022-09-01 05:40:43 +00:00
|
|
|
|
2022-08-31 23:41:47 +00:00
|
|
|
io.on('connection', (client) => {
|
2022-09-01 06:08:01 +00:00
|
|
|
read_all_leds()
|
2022-08-31 23:41:47 +00:00
|
|
|
client.on('action', ({ action, x, y }) => {
|
|
|
|
console.log({ action, x, y })
|
|
|
|
switch (action) {
|
|
|
|
case 'on':
|
2022-09-01 05:03:01 +00:00
|
|
|
sense.setPixel(Number(x), Number(y), blue)
|
2022-09-01 05:37:32 +00:00
|
|
|
leds[y][x] = 1
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'off':
|
|
|
|
sense.setPixel(Number(x), Number(y), [0, 0, 0])
|
2022-09-01 05:37:32 +00:00
|
|
|
leds[y][x] = 0
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'hello':
|
2022-09-01 05:03:01 +00:00
|
|
|
sense.flashMessage('HELLO', 1, blue)
|
2022-08-31 23:41:47 +00:00
|
|
|
sense.clear()
|
|
|
|
break
|
|
|
|
case 'flash':
|
2022-09-01 05:37:32 +00:00
|
|
|
sense.clear([255, 255, 255])
|
|
|
|
setTimeout(sense.clear, 10)
|
2022-08-31 23:41:47 +00:00
|
|
|
break
|
|
|
|
case 'clear':
|
|
|
|
sense.clear()
|
|
|
|
break
|
2022-09-01 05:47:00 +00:00
|
|
|
case 'temp':
|
|
|
|
read_all_leds()
|
|
|
|
break
|
2022-08-31 23:41:47 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2022-09-01 06:08:01 +00:00
|
|
|
read_all_leds()
|
2022-08-31 23:41:47 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-01 05:37:32 +00:00
|
|
|
function read_all_leds() {
|
2022-09-01 05:56:33 +00:00
|
|
|
const pixels = sense.getPixels()
|
|
|
|
for (let y = 0; y < 8; y++) {
|
|
|
|
for (let x = 0; x < 8; x++) {
|
2022-09-01 05:58:41 +00:00
|
|
|
leds[y][x] = sense.getPixel(x, y)[2] == 0 ? 0 : 1
|
2022-09-01 05:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 06:06:55 +00:00
|
|
|
io.emit('leds', leds)
|
2022-08-31 23:41:47 +00:00
|
|
|
}
|