-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
159 lines (135 loc) · 3.91 KB
/
index.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
import { LitElement, html } from 'lit-element'
import { connect } from 'pwa-helpers/connect-mixin.js'
import { store } from '../../reduxStore.js'
import Controller from 'webusb-dmx512-controller/controller.js'
import { connectUsb } from '../../actions/index.js'
import '../connect-button/index.js'
import { getUsbDmxControllerConnected, getUniverses } from '../../selectors/index.js'
/*
* Handle the connection to a WebUSB DMX512 Controller
* @see https://github.com/NERDDISCO/webusb-dmx512-controller
*/
class UsbDmxManager extends connect(store)(LitElement) {
constructor() {
super()
this.controller = new Controller({
filters: [
// Arduino LLC (9025)
{ vendorId: 0x2341, productId: 0x8036 },
// Arduino LLC (9025)
{ vendorId: 0x2341, productId: 0x8037 },
// Arduino LLC (10755), Leonardo ETH (32832)
{ vendorId: 0x2a03, productId: 0x8040 }
]
})
// Automatically connect to already paired WebUSB DMX512 Controller
this.controller.autoConnect()
.then(() => {
this.connect()
})
.catch(error => {
console.error(error)
store.dispatch(connectUsb(false))
})
}
static get properties() {
return {
usbConnected: { type: Boolean },
universes: { type: Array }
}
}
_stateChanged(state) {
this.usbConnected = getUsbDmxControllerConnected(state)
this.universes = getUniverses(state)
}
connectedCallback() {
super.connectedCallback()
window.addEventListener('send-universe-to-usb-dmx-controller', this.listenSendUniverse.bind(this))
}
disconnectedCallback() {
super.disconnectedCallback()
window.removeEventListener('send-universe-to-usb-dmx-controller', this.listenSendUniverse.bind(this))
}
listenSendUniverse(e) {
if (this.usbConnected) {
// Send universe 0 to the USB DMX controller
this.controller.send(this.universes[0].channels)
.catch(error => {
console.error(error.code, error)
switch (error.code) {
// The device was disconnected
case 8:
// A transfer error has occurred
case 19:
// The transfer was cancelled
case 20:
store.dispatch(connectUsb(false))
break;
default:
break;
}
})
}
}
handleConnectButtonClick() {
// Disconnect from USB controller
if (this.usbConnected) {
if (this.controller.device === undefined) {
store.dispatch(connectUsb(false))
} else {
this.controller.disconnect().then(() => {
// Disconnected
store.dispatch(connectUsb(false))
}, error => {
console.error(error)
})
}
// Pair with USB controller
} else {
this.enable()
}
}
/*
* Enable WebUSB and request a USBPort
*/
enable() {
this.controller.enable().then(() => {
// Create a connection to the selected Arduino
this.connect()
})
.catch(error => {
console.error(error)
})
}
/*
* Connect to a selected USBPort
*/
connect() {
this.controller.connect().then(() => {
// USB is connected
store.dispatch(connectUsb(true))
// // Receive data
// this.controller.device.onReceive = data => {
// // const textDecoder = new TextDecoder()
// // console.log(textDecoder.decode(data))
// }
//
// // Receive error
// this.controller.device.onReceiveError = error => {
// // USB is disconnected
// store.dispatch(connectUsb(false))
// console.log(error)
// }
}, error => {
// USB is disconnected
store.dispatch(connectUsb(false))
console.error(error)
})
}
render() {
return html`
<connect-button type="usb" label="USB" @click="${() => this.handleConnectButtonClick()}"></connect-button>
`
}
}
customElements.define('usb-dmx-manager', UsbDmxManager)