-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevice.js
301 lines (284 loc) · 10.8 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const EventEmitter = require('events')
const rgba = require('color-rgba')
const SerialConnection = require('./connections/serial')
const WSConnection = require('./connections/ws')
const {
BUTTONS,
COMMANDS,
DEFAULT_RECONNECT_INTERVAL,
HAPTIC,
MAX_BRIGHTNESS,
} = require('./constants')
class LoupedeckDevice extends EventEmitter {
static async list({ ignoreSerial = false, ignoreWebsocket = false } = {}) {
const ps = []
if (!ignoreSerial) ps.push(SerialConnection.discover())
if (!ignoreWebsocket) ps.push(WSConnection.discover())
// Run them in parallel
const rawDevices = await Promise.all(ps)
return rawDevices.flat()
}
constructor({ host, path, autoConnect = true, reconnectInterval = DEFAULT_RECONNECT_INTERVAL } = {}) {
super()
this.transactionID = 0
this.touches = {}
this.handlers = {
[COMMANDS.BUTTON_PRESS]: this.onButton.bind(this),
[COMMANDS.KNOB_ROTATE]: this.onRotate.bind(this),
[COMMANDS.SERIAL]: this.onSerial.bind(this),
[COMMANDS.TICK]: () => {},
[COMMANDS.TOUCH]: this.onTouch.bind(this, 'touchmove'),
[COMMANDS.TOUCH_END]: this.onTouch.bind(this, 'touchend'),
[COMMANDS.VERSION]: this.onVersion.bind(this),
}
// Track pending transactions
this.pendingTransactions = {}
// How long between reconnect attempts
this.reconnectInterval = reconnectInterval
// Host for websocket connections
this.host = host
// Path for serial connections
this.path = path
// Automatically connect?
if (autoConnect) this._connectBlind()
}
close() {
if (!this.connection) return
return this.connection.close()
}
async connect() {
// Explicitly asked for a serial connection (V0.2.X)
if (this.path) this.connection = new SerialConnection({ path: this.path })
// Explicitly asked for a websocket connection (V0.1.X)
else if (this.host) this.connection = new WSConnection({ host: this.host })
// Autodiscover
else {
const devices = await this.constructor.list()
if (devices.length > 0) {
const { connectionType, ...args } = devices[0]
this.connection = new connectionType(args)
}
if (!this.connection) {
return Promise.reject(this.onDisconnect(new Error('No devices found')))
}
}
this.connection.on('connect', this.onConnect.bind(this))
this.connection.on('message', this.onReceive.bind(this))
this.connection.on('disconnect', this.onDisconnect.bind(this))
return this.connection.connect()
}
_connectBlind() {
return this.connect().catch(() => {})
}
// Draw an arbitrary buffer to the device
// Buffer format must be 16bit 5-6-5
async drawBuffer({ id, width, height, x = 0, y = 0, autoRefresh = true }, buffer) {
const displayInfo = this.displays[id]
if (!displayInfo) throw new Error(`Display '${id}' is not available on this device!`)
if (!width) width = displayInfo.width
if (!height) height = displayInfo.height
const pixelCount = width * height * 2
if (buffer.length !== pixelCount) {
throw new Error(`Expected buffer length of ${pixelCount}, got ${buffer.length}!`)
}
// Header with x/y/w/h and display ID
const header = Buffer.alloc(8)
header.writeUInt16BE(x, 0)
header.writeUInt16BE(y, 2)
header.writeUInt16BE(width, 4)
header.writeUInt16BE(height, 6)
// Write to frame buffer
await this.send(COMMANDS.FRAMEBUFF, Buffer.concat([displayInfo.id, header, buffer]))
// Draw to display
if (autoRefresh) await this.refresh(id)
}
// Create a canvas with correct dimensions and pass back for drawing
drawCanvas({ id, width, height, ...args }, cb) {
const displayInfo = this.displays[id]
if (!displayInfo) throw new Error(`Display '${id}' is not available on this device!`)
if (!width) width = displayInfo.width
if (!height) height = displayInfo.height
let createCanvas
try {
createCanvas = require('canvas').createCanvas
} catch(e) {
throw new Error('Using callbacks requires the `canvas` library to be installed. Install it using `npm install canvas`.')
}
const canvas = createCanvas(width, height)
const ctx = canvas.getContext('2d', { pixelFormat: 'RGB16_565' }) // Loupedeck uses 16-bit (5-6-5) LE RGB colors
cb(ctx, width, height)
const buffer = canvas.toBuffer('raw')
return this.drawBuffer({ id, width, height, ...args }, buffer)
}
// Draw to a specific key index (0-11 on Live, 0-14 on Live S)
drawKey(index, cb) {
// Get offset x/y for key index
if (index < 0 || index >= this.columns * this.rows) throw new Error(`Key ${index} is not a valid key`)
const width = 90
const height = 90
const x = this.visibleX[0] + index % this.columns * width
const y = Math.floor(index / this.columns) * height
return this[cb instanceof Buffer ? 'drawBuffer' : 'drawCanvas']({ id: 'center', x, y, width, height }, cb)
}
// Draw to a specific screen
drawScreen(id, cb) {
return this[cb instanceof Buffer ? 'drawBuffer' : 'drawCanvas']({ id }, cb)
}
async getInfo() {
if (!this.connection || !this.connection.isReady()) throw new Error('Not connected!')
return {
serial: await this.send(COMMANDS.SERIAL),
version: await this.send(COMMANDS.VERSION)
}
}
onButton(buff) {
if (buff.length < 2) return
const id = BUTTONS[buff[0]]
const event = buff[1] === 0x00 ? 'down' : 'up'
this.emit(event, { id })
}
onConnect(info) {
this.emit('connect', info)
}
onDisconnect(error) {
this.emit('disconnect', error)
clearTimeout(this._reconnectTimer)
this.connection = null
// Normal disconnect, do not reconnect
if (!error) return
// Reconnect if desired
if (this.reconnectInterval) {
this._reconnectTimer = setTimeout(this._connectBlind.bind(this), this.reconnectInterval)
}
return error.message
}
onReceive(buff) {
const msgLength = buff[0]
const handler = this.handlers[buff[1]]
const transactionID = buff[2]
const response = handler ? handler(buff.slice(3, msgLength)) : buff
const resolver = this.pendingTransactions[transactionID]
if (resolver) resolver(response)
return response
}
onRotate(buff) {
const id = BUTTONS[buff[0]]
const delta = buff.readInt8(1)
this.emit('rotate', { id, delta })
}
onSerial(buff) {
return buff.toString().trim()
}
onTouch(event, buff) {
const x = buff.readUInt16BE(1)
const y = buff.readUInt16BE(3)
const id = buff[5]
// Create touch
const touch = { x, y, id, target: this.getTarget(x, y) }
// End touch, remove from local cache
if (event === 'touchend') {
delete this.touches[touch.id]
} else {
// First time seeing this touch, emit touchstart instead of touchmove
if (!this.touches[touch.id]) event = 'touchstart'
this.touches[touch.id] = touch
}
this.emit(event, { touches: Object.values(this.touches), changedTouches: [touch] })
}
onVersion(buff) {
return `${buff[0]}.${buff[1]}.${buff[2]}`
}
// Display the current framebuffer
refresh(id) {
const displayInfo = this.displays[id]
return this.send(COMMANDS.DRAW, displayInfo.id)
}
send(command, data = Buffer.alloc(0)) {
if (!this.connection || !this.connection.isReady()) return
this.transactionID = (this.transactionID + 1) % 256
// Skip transaction ID's of zero since the device seems to ignore them
if (this.transactionID === 0) this.transactionID++
const header = Buffer.alloc(3)
header[0] = Math.min(3 + data.length, 0xff)
header[1] = command
header[2] = this.transactionID
const packet = Buffer.concat([header, data])
this.connection.send(packet)
return new Promise(res => {
this.pendingTransactions[this.transactionID] = res
})
}
setBrightness(value) {
const byte = Math.max(0, Math.min(MAX_BRIGHTNESS, Math.round(value * MAX_BRIGHTNESS)))
return this.send(COMMANDS.SET_BRIGHTNESS, Buffer.from([byte]))
}
setButtonColor({ id, color }) {
const key = Object.keys(BUTTONS).find(k => BUTTONS[k] === id)
if (!key) throw new Error(`Invalid button ID: ${id}`)
const [r, g, b] = rgba(color)
const data = Buffer.from([key, r, g, b])
return this.send(COMMANDS.SET_COLOR, data)
}
vibrate(pattern = HAPTIC.SHORT) {
return this.send(COMMANDS.SET_VIBRATION, Buffer.from([pattern]))
}
}
class LoupedeckLive extends LoupedeckDevice {
buttons = [0, 1, 2, 3, 4, 5, 6, 7]
columns = 4
displays = {
center: { id: Buffer.from('\x00A'), width: 360, height: 270 }, // "A"
left: { id: Buffer.from('\x00L'), width: 60, height: 270 }, // "L"
right: { id: Buffer.from('\x00R'), width: 60, height: 270 }, // "R"
}
productId = '0004'
rows = 3
type = 'Loupedeck Live'
visibleX = [0, 480]
// Determine touch target based on x/y position
getTarget(x, y) {
if (x < 60) return { screen: 'left' }
if (x >= 420) return { screen: 'right' }
const column = Math.floor((x - 60) / 90)
const row = Math.floor(y / 90)
const key = row * this.columns + column
return {
screen: 'center',
key
}
}
}
class LoupedeckLiveS extends LoupedeckDevice {
buttons = [0, 1, 2, 3]
columns = 5
displays = {
center: { id: Buffer.from('\x00M'), width: 480, height: 270 },
}
productId = '0006'
rows = 3
type = 'Loupedeck Live S'
visibleX = [15, 464]
visibleY = [10, 269]
// Determine touch target based on x/y position
getTarget(x, y) {
// Clip to area, ensure to alway return a key
x = Math.max(x,this.visibleX[0])
x = Math.min(x,this.visibleX[1])
y = Math.max(y,this.visibleY[0])
y = Math.min(y,this.visibleY[1])
x -= this.visibleX[0];
y -= this.visibleY[0];
const column = Math.floor(x / 90)
const row = Math.floor(y / 90)
const key = row * this.columns + column
return {
screen: 'center',
key
}
}
}
module.exports = {
LoupedeckDevice,
LoupedeckLive,
LoupedeckLiveS,
}