-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialtest.js
123 lines (105 loc) · 3.33 KB
/
serialtest.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
113
114
115
116
117
118
119
120
121
122
123
const Datastream = require('./lib/Datastream.class');
const LED_COUNT = 150;
const UPDATE_FPS = 30;
const dataPackage = Array.from({ length: LED_COUNT }, e => Array(3).fill(0));
// const dataPackage = Array.from({ length: LED_COUNT }, e => Array(4).fill(0));
// dataPackage.forEach((arr, i) => arr[0] = i);
if (process.argv[2] === 'test') {
console.log(`TEST mode activated, sending random LED values at ${UPDATE_FPS}fps`);
}
if (process.argv[2] !== 'test') {
const HomeKit = require('./lib/HomeKit.class');
const HKDeviceHandler = require('./lib/rgb.class');
let HKDevices = [
new HKDeviceHandler({
id: 'homekitPixelrail',
deviceName: `Pixeltest`,
model: 'Pixelrail',
service: 'Lightbulb',
serial: 'A1000001',
}, {
startLed: 0,
ledCount: 6,
// switchType: d.switchType || 'render'
}),
new HKDeviceHandler({
id: 'homekitPixelrail',
deviceName: `Pixeltest2`,
model: 'Pixelrail',
service: 'Lightbulb',
serial: 'A1000002',
}, {
startLed: 6,
ledCount: 6,
// switchType: d.switchType || 'render'
})
];
HKDevices.forEach(device =>
device.on(HomeKit.EVENTS.UPDATE_PIXEL, ({pixel, r, g, b}) => {
console.log('updatePixel triggered', pixel, r,g,b);
pixel.forEach(i => {
const oldRGB =[
dataPackage[i][0],
dataPackage[i][1],
dataPackage[i][2]
];
// dataPackage[i] = [r,g,b];
colorFade(oldRGB, [r,g,b], 1000, ({ r, g, b }) => {
// console.log('color fade', r,g,b);
dataPackage[i] = [
// i,
r, g, b
];
});
});
})
)
}
const stream = new Datastream();
stream.on(Datastream.EVENTS.CONNECTION_OPEN, _ => {
console.log('open! sending data...');
sendDatastream();
stream.start();
})
async function sendDatastream() {
if (process.argv[2] === 'test') {
for (let i = 0; i < LED_COUNT; i++) {
dataPackage[i] = [
// i,
randomValue(0, 5) * 10,
randomValue(0, 5) * 10,
randomValue(0, 5) * 10
];
}
}
stream.setData(dataPackage);
starttime = Date.now();
setTimeout(_ => {
sendDatastream();
}, 1000 / UPDATE_FPS);
}
async function sleep(time) {
return new Promise((resolve) => {
setTimeout(() => resolve(), time);
})
}
const randomValue = (min, max) => Math.floor(Math.random() * (+max - +min)) + +min;
function colorFade(start, end, duration, callback) {
var interval = 25;
var steps = duration / interval;
var step_u = 1.0 / steps;
var u = 0.0;
var theInterval = setInterval(function () {
if (u >= 1.0) {
clearInterval(theInterval);
}
var r = Math.round(lerp(start[0], end[0], u));
var g = Math.round(lerp(start[1], end[1], u));
var b = Math.round(lerp(start[2], end[2], u));
callback({r,g,b});
u += step_u;
}, interval);
}
function lerp (a, b, u) {
return (1 - u) * a + u * b;
}