-
Notifications
You must be signed in to change notification settings - Fork 3
/
multiplexer.js
30 lines (27 loc) · 860 Bytes
/
multiplexer.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
const _ = require('lodash')
module.exports = class Multiplexer {
constructor(numberOfLights, devices, mapping) {
this.numberOfLights = numberOfLights
this.devices = devices
this.targetDevice = []
this.targetPosition = []
for (let i = 0; i < numberOfLights; i++) {
const [ device, position ] = mapping(i)
this.targetDevice[i] = device
this.targetPosition[i] = position
}
}
setState(newState) {
const newStates = this.devices.map(
device => _.range(device.numberOfLights)
)
const targetDevice = this.targetDevice
const targetPosition = this.targetPosition
for (let i = 0; i < this.numberOfLights; i++) {
newStates[targetDevice[i]][targetPosition[i]] = newState[i]
}
for (let i = 0; i < this.devices.length; i++) {
this.devices[i].setState(newStates[i])
}
}
}