This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
115 lines (83 loc) · 2.35 KB
/
test.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
let CT = require('./index').Device;
var fs = require('fs');
var PNG = require('pngjs').PNG;
const grafikk = require('@bitfocusas/grafikk');
var getips = require('get-ip-addresses').default;
let ip = getips().find( ip => ip.match(/^100\.127\./) );
if (ip === undefined) {
console.error("Unable to find device");
process.exit(1);
}
else {
ip = ip.replace(/\.2$/,".1");
}
let ct = new CT({ uri: 'ws://'+ip+':80/'})
let ongoing = false;
let ongoing2 = false;
let intensity = 255;
ct.on('event', (payload) => {
console.log("CT Event", payload);
if (payload.action === 'encoder_step') {
if (payload.direction == 'left') { if (intensity>=5) intensity-=5; }
if (payload.direction == 'right') { if (intensity<250) intensity+=5; }
}
if (payload.action === 'button_press') {
let pixels = Buffer.alloc(360*270*3);
fs.createReadStream('./research/img/bfstor.png').pipe(
new PNG({
filterType: 4
}
)).on('parsed', function() {
for (let pixel = 0; pixel < 360*270; pixel++) {
pixels.writeUInt8(this.data[(pixel*4) + 0], (pixel*3) + 0 );
pixels.writeUInt8(this.data[(pixel*4) + 1], (pixel*3) + 1 );
pixels.writeUInt8(this.data[(pixel*4) + 2], (pixel*3) + 2 );
}
if (!ongoing) {
ongoing = true;
ct.drawCenterScreen(pixels, () => {
ongoing = false;
})
}
});
let pixels2 = Buffer.alloc(240*240*3);
fs.createReadStream('./research/img/bf.png').pipe(
new PNG({
filterType: 4
}
)).on('parsed', function() {
for (let pixel = 0; pixel < 240*240; pixel++) {
pixels2.writeUInt8(this.data[(pixel*4) + 0], (pixel*3) + 0 );
pixels2.writeUInt8(this.data[(pixel*4) + 1], (pixel*3) + 1 );
pixels2.writeUInt8(this.data[(pixel*4) + 2], (pixel*3) + 2 );
}
if (!ongoing2) {
ongoing2 = true;
ct.drawRotaryRGB(pixels2, () => {
ongoing2 = false;
})
}
});
}
});
ct.on('error', (error) => {
console.error("CT error:", error);
});
ct.on('connect', () => {
console.log("connected!!");
setTimeout( () => {
for (var x = 7; x <= 27; x++) {
ct.buttonColor(x, intensity, 0, 0);
}
setInterval(start,40);
}, 500);
});
function start() {
let rndButton = 7 + Math.floor(Math.random() * 20);
ct.buttonColor(rndButton,
Math.floor(Math.random() * intensity),
Math.floor(Math.random() * intensity),
Math.floor(Math.random() * intensity)
);
}
ct.connect();