-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.js
184 lines (153 loc) · 4.15 KB
/
device.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const _ = require('lodash')
const SerialPort = require('serialport')
const now = require('performance-now')
const { DEBUG, INFO, WARNING, ERROR } = require('./log')
const notEqual = (a, b) => {
return a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2]
}
const {
ENCODING_RGB,
ENCODING_VGA,
ENCODING_POS_RGB,
ENCODING_POS_VGA,
arrayFromRGB,
rgbToVga
} = require('./colorEncoding')
module.exports = class Device {
constructor(numberOfLights, devicePort, verbosity) {
this.state = _.range(0, numberOfLights)
this.numberOfLights = numberOfLights
this.verbosity = verbosity || DEBUG
this.devicePort = devicePort
this.encoding = ENCODING_RGB
this.port = new SerialPort(devicePort, {
baudRate: 1152000 / 2,
parser: SerialPort.parsers.readline("\n")
})
this.setupCommunication()
this.dataBuffer = []
this.getFPS = () => {
const FPS = (1000/(now() - this.lastReceived)).toFixed(1)
return this.devicePort + ' - received pingback. FPS: ' + FPS
}
}
setState(rgbArray) {
const newState = _.range(this.numberOfLights)
for (let i = 0; i < this.numberOfLights; i++) {
newState[i] = arrayFromRGB(rgbArray[i])
}
this.state = newState
}
sendNextFrame() {
this.initEncoding()
let dim = 1
for (let i = 0; i < this.numberOfLights; i++) {
this.writePixel(i,
this.state[i][0]/dim,
this.state[i][1]/dim,
this.state[i][2]/dim
)
}
this.flush()
}
initEncoding() {
this.write([this.encoding]);
if (this.needsHeaderWithNumberOfLights()) {
this.write([this.numberOfLights]);
}
}
needsHeaderWithNumberOfLights () {
return this.encoding === ENCODING_POS_RGB
|| this.encoding === ENCODING_POS_VGA
}
writePixel(pos, r, g, b) {
switch (this.encoding) {
case ENCODING_RGB:
return this.write([r, g, b])
case ENCODING_VGA:
return this.write([rgbToVga(r, g, b)])
case ENCODING_POS_RGB:
return this.write([pos, r, g, b])
case ENCODING_POS_VGA:
return this.write([pos, rgbToVga(r, g, b)])
default:
this.logError('Invalid encoding!')
return
}
}
write(data) {
this.dataBuffer = this.dataBuffer.concat(data);
}
flush() {
this.port.write(Buffer.from(this.dataBuffer))
this.dataBuffer = [];
}
sendInitialKick(){
this.port.write(
Buffer.from([1, 2, 255, 0, 255]), () => {
this.logInfo('Initial kick of data sent')
}
)
}
setupCommunication() {
this.lastReceived = now();
this.port.on('open', () => {
this.logInfo('Port open. Data rate: ' + this.port.options.baudRate);
setTimeout(this.sendInitialKick.bind(this), 2000)
})
this.port.on('error', this.handleError.bind(this))
this.port.on('data', this.handleData.bind(this))
this.port.on('drain', this.handleData.bind(this))
this.port.on('close', this.handleClose.bind(this))
}
// open errors will be emitted as an error event
handleError(err) {
this.logError('Error: ' + err.message)
}
handleClose(err) {
this.logInfo('Port closed.')
const setRetry = function() { setTimeout(retry, 2000) }
var retry = () => {
console.log('retrying...', this.devicePort)
try {
this.port = new SerialPort(this.devicePort, {
baudRate: 1152000 / 2,
parser: SerialPort.parsers.readline("\n")
})
} catch (e) {
setRetry()
}
this.setupCommunication()
}
setRetry()
}
handleDrain(err) {
this.logInfo('Port drained.')
}
handleData(data) {
this.logDebug(this.getFPS)
this.lastReceived = now()
this.sendNextFrame()
}
logDebug(message) {
if (this.verbosity <= DEBUG) {
message = (typeof message === 'function') ? message() : message
console.log(this.devicePort, message)
}
}
logInfo(message) {
if (this.verbosity <= INFO) {
console.log(this.devicePort, message)
}
}
logWarning(message) {
if (this.verbosity <= WARNING) {
console.log(this.devicePort, message)
}
}
logError(message) {
if (this.verbosity <= ERROR) {
console.log(this.devicePort, message)
}
}
}