@@ -50,6 +50,11 @@ export class UsbDeviceChannel implements IDeviceChannel<Uint8Array, Uint8Array>
5050 private _readyFlag = false ;
5151 private _readyPromise : Promise < boolean > ;
5252 public get ready ( ) { return this . _readyPromise ; }
53+ public get connected ( ) {
54+ return ! this . _disposed
55+ && this . _readyFlag
56+ && this . device . opened
57+ }
5358
5459 private _disposed = false ;
5560
@@ -74,67 +79,6 @@ export class UsbDeviceChannel implements IDeviceChannel<Uint8Array, Uint8Array>
7479 return true ;
7580 }
7681
77- public async dispose ( ) {
78- if ( this . _disposed ) {
79- return ;
80- }
81-
82- this . _disposed = true ;
83- this . _readyPromise = Promise . resolve ( false ) ;
84-
85- try {
86- await this . device . close ( ) ;
87- } catch ( e ) {
88- if (
89- e instanceof DOMException &&
90- e . name === 'NotFoundError' &&
91- e . message ===
92- "Failed to execute 'close' on 'USBDevice': The device was disconnected."
93- ) {
94- // Device was already closed, no-op.
95- return ;
96- }
97-
98- throw e ;
99- }
100- }
101-
102- public async sendCommands (
103- commandBuffer : Uint8Array
104- ) : Promise < DeviceCommunicationError | undefined > {
105- if ( ! this . _readyFlag || this . _disposed || this . deviceOut === undefined ) {
106- throw new DeviceNotReadyError ( ) ;
107- }
108- if ( this . _commOptions . debug ) {
109- console . debug ( 'Sending command buffer to device via USB.' ) ;
110- console . time ( 'commandBufferSendTime' ) ;
111- }
112-
113- try {
114- // TOOD: Add timeout in case of communication hang.
115- await this . device . transferOut ( this . deviceOut . endpointNumber , commandBuffer ) ;
116- return ;
117- } catch ( e : unknown ) {
118- if ( typeof e === 'string' ) {
119- return new DeviceCommunicationError ( e ) ;
120- }
121- if ( e instanceof Error ) {
122- return new DeviceCommunicationError ( undefined , e ) ;
123- }
124- // Dunno what this is but we can't wrap it.
125- throw e ;
126- } finally {
127- if ( this . _commOptions . debug ) {
128- console . timeEnd ( 'commandBufferSendTime' ) ;
129- console . debug ( 'Completed sending commands.' ) ;
130- }
131- }
132- }
133-
134- public getDeviceInfo ( ) {
135- return deviceToInfo ( this . device ) ;
136- }
137-
13882 private async connect ( ) {
13983 const d = this . device ;
14084
@@ -203,8 +147,69 @@ export class UsbDeviceChannel implements IDeviceChannel<Uint8Array, Uint8Array>
203147 }
204148 }
205149
206- public async getInput ( ) : Promise < Uint8Array [ ] | DeviceCommunicationError > {
207- if ( this . deviceIn === undefined || this . _disposed ) { return new DeviceCommunicationError ( 'Channel is disposed.' ) ; }
150+ public async dispose ( ) {
151+ if ( this . _disposed ) {
152+ return ;
153+ }
154+
155+ this . _disposed = true ;
156+ this . _readyPromise = Promise . resolve ( false ) ;
157+
158+ try {
159+ await this . device . close ( ) ;
160+ } catch ( e ) {
161+ if (
162+ e instanceof DOMException &&
163+ e . name === 'NotFoundError' &&
164+ e . message ===
165+ "Failed to execute 'close' on 'USBDevice': The device was disconnected."
166+ ) {
167+ // Device was already closed, no-op.
168+ return ;
169+ }
170+
171+ throw e ;
172+ }
173+ }
174+
175+ public async sendCommands (
176+ commandBuffer : Uint8Array
177+ ) : Promise < DeviceNotReadyError | undefined > {
178+ if ( this . deviceOut === undefined || ! this . connected ) {
179+ return new DeviceNotReadyError ( ) ;
180+ }
181+ if ( this . _commOptions . debug ) {
182+ console . debug ( 'Sending command buffer to device via USB.' ) ;
183+ console . time ( 'commandBufferSendTime' ) ;
184+ }
185+
186+ try {
187+ // TOOD: Add timeout in case of communication hang.
188+ await this . device . transferOut ( this . deviceOut . endpointNumber , commandBuffer ) ;
189+ return ;
190+ } catch ( e : unknown ) {
191+ if ( typeof e === 'string' ) {
192+ return new DeviceCommunicationError ( e ) ;
193+ }
194+ if ( e instanceof Error ) {
195+ return new DeviceCommunicationError ( undefined , e ) ;
196+ }
197+ // Dunno what this is but we can't wrap it.
198+ throw e ;
199+ } finally {
200+ if ( this . _commOptions . debug ) {
201+ console . timeEnd ( 'commandBufferSendTime' ) ;
202+ console . debug ( 'Completed sending commands.' ) ;
203+ }
204+ }
205+ }
206+
207+ public getDeviceInfo ( ) {
208+ return deviceToInfo ( this . device ) ;
209+ }
210+
211+ public async getInput ( ) : Promise < Uint8Array [ ] | DeviceNotReadyError > {
212+ if ( this . deviceIn === undefined || ! this . connected ) { return new DeviceNotReadyError ( 'Channel is not connected.' ) ; }
208213 const result = await this . device . transferIn (
209214 this . deviceIn . endpointNumber ,
210215 this . deviceIn . packetSize * 8 ) ; // Usually 64 * 8 = 512
0 commit comments