-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (92 loc) · 3.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const fs = require('fs');
const path = require('path');
const getPixels = require('get-pixels');
const jimp = require('jimp');
const express = require('express')();
const config = require('./config');
const leds = require('./lib/hardware/leds');
const gif = require('./lib/parser/gif');
const telegram = require('./lib/communication/telegram');
// telegram.start();
const scenes = [
{
name: 'kaminfeuer',
script: path.join(__dirname, 'scenes', 'kaminfeuer.js')
}
]
console.log('Starting server...')
express.listen(config.server.port, () => {
console.log(`Server started on port ${config.server.port}.`);
});
scenes.forEach(scene => {
express.get(`/scene/${scene.name}`, (req, res) => {
res.send(scene.script)
});
})
express.get('/test', async (req, res) => {
leds.startLEDControl()
const gifPath = path.join(__dirname, 'scenes', 'kaminfeuer_small.gif');
const gifFrames = await gif.getColorArrayFromGif(gifPath);
const resolutionFactorWidth = Math.floor(gifFrames[0].width / config.leds.gridWidth);
const resolutionFactorHeight = Math.floor(gifFrames[0].height / config.leds.gridHeight);
res.send('Starting GIF animation...');
// Set LED colors
gifFrames.forEach((gifFrame, frameIndex) => {
setTimeout(() => {
for (let j = 0; j < config.leds.gridHeight; j++) {
const rowIndex = j * resolutionFactorHeight;
for (let i = 0; i < config.leds.gridWidth; i++) {
const columnIndex = i * resolutionFactorWidth;
if (process.env.PI === "true") {
leds.setColor(j, i, gifFrame.grid[rowIndex][columnIndex]);
}
}
}
leds.updateLEDsOnPI();
}, frameIndex * 500);
});
setTimeout(() => {
leds.finishLEDControl();
}, 2000);
});
// GifUtil.read(path.join(__dirname, 'scenes', '3frames.gif')).then(gif => {
// gif.frames.forEach(gifFrame => {
// const pixelColors = JSON.parse(JSON.stringify(gifFrame.bitmap.data)).data;
// const colorArray = []
// for (let i = 0; i < pixelColors.length; i += 4) {
// colorArray.push({
// r: pixelColors[i],
// g: pixelColors[i + 1],
// b: pixelColors[i + 2],
// a: pixelColors[i + 3]
// })
// }
// const frameWidth = gifFrame.bitmap.width;
// const frameHeight = gifFrame.bitmap.height;
// const grid = [];
// // Create grid with image size
// for (let i = 0; i < frameHeight; i++) {
// const row = [];
// for (let j = 0; j < frameWidth; j++) {
// row.push(undefined);
// }
// grid.push(row);
// }
// // Set colors from image
// colorArray.forEach((colorValue, index) => {
// const row = Math.floor(index / frameWidth);
// const column = index % frameWidth;
// grid[row][column] = colorValue;
// })
// const resolutionFactorWidth = Math.floor(frameWidth / config.leds.gridWidth);
// const resolutionFactorHeight = Math.floor(frameHeight / config.leds.gridHeight);
// // Set LED colors
// for (let j = 0; j < config.leds.gridHeight; j++) {
// const rowIndex = j * resolutionFactorHeight;
// for (let i = 0; i < config.leds.gridWidth; i++) {
// const columnIndex = i * resolutionFactorWidth;
// leds.setColor(rowIndex, columnIndex, grid[rowIndex][columnIndex]);
// }
// }
// })
// });