-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdevice.ts
More file actions
214 lines (194 loc) · 5.11 KB
/
device.ts
File metadata and controls
214 lines (194 loc) · 5.11 KB
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
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import EventEmitter from "events";
import { Logging } from "../logging/logging";
import { BoardId } from "./board-id";
/**
* Specific identified error types.
*
* New members may be added over time.
*/
export type WebUSBErrorCode =
/**
* Device not selected, e.g. because the user cancelled the dialog.
*/
| "no-device-selected"
/**
* Device not found, perhaps because it doesn't have new enough firmware (for V1).
*/
| "update-req"
/**
* Unable to claim the interface, usually because it's in use in another tab/window.
*/
| "clear-connect"
/**
* The device was found to be disconnected.
*/
| "device-disconnected"
/**
* A communication timeout occurred.
*/
| "timeout-error"
/**
* This is the fallback error case suggesting that the user reconnects their device.
*/
| "reconnect-microbit";
/**
* Error type used for all interactions with this module.
*
* The code indicates the error type and may be suitable for providing
* translated error messages.
*
* The message is the underlying message text and will usually be in
* English.
*/
export class WebUSBError extends Error {
code: WebUSBErrorCode;
constructor({ code, message }: { code: WebUSBErrorCode; message?: string }) {
super(message);
this.code = code;
}
}
export interface MicrobitWebUSBConnectionOptions {
// We should copy this type when extracting a library, and make it optional.
// Coupling for now to make it easy to evolve.
logging: Logging;
}
/**
* Tracks WebUSB connection status.
*/
export enum ConnectionStatus {
/**
* Not supported.
*/
NOT_SUPPORTED = "NOT_SUPPORTED",
/**
* Supported but no device available.
*
* This will be the case even when a device is physically connected
* but has not been connected via the browser security UI.
*/
NO_AUTHORIZED_DEVICE = "NO_DEVICE",
/**
* Authorized device available but we haven't connected to it.
*/
NOT_CONNECTED = "NOT_CONNECTED",
/**
* Connected.
*/
CONNECTED = "CONNECTED",
}
/**
* Tracks user connection action.
*/
export enum ConnectionAction {
FLASH = "FLASH",
CONNECT = "CONNECT",
DISCONNECT = "DISCONNECT",
}
export const EVENT_STATUS = "status";
export const EVENT_SERIAL_DATA = "serial_data";
export const EVENT_SERIAL_RESET = "serial_reset";
export const EVENT_SERIAL_ERROR = "serial_error";
export const EVENT_FLASH = "flash";
export const EVENT_START_USB_SELECT = "start_usb_select";
export const EVENT_END_USB_SELECT = "end_usb_select";
export class HexGenerationError extends Error {}
export interface FlashDataSource {
/**
* The data required for a partial flash.
*
* @param boardId the id of the board.
* @throws HexGenerationError if we cannot generate hex data.
*/
partialFlashData(boardId: BoardId): Promise<Uint8Array>;
/**
* A full hex.
*
* @param boardId the id of the board.
* @throws HexGenerationError if we cannot generate hex data.
*/
fullFlashData(boardId: BoardId): Promise<Uint8Array>;
/**
* The file system represented by file name keys and data values.
*/
files(): Promise<Record<string, Uint8Array>>;
}
export const enum SerialOption {
/**
* Don't read serial data.
*/
None,
/**
* Emit a reset to clear any listeners, then read serial data.
*/
Reset,
/**
* Read serial data. No reset is emitted.
*/
NoReset,
}
export interface ConnectOptions {
serial: SerialOption;
}
export interface DeviceConnection extends EventEmitter {
status: ConnectionStatus;
/**
* Initializes the device.
*/
initialize(): Promise<void>;
/**
* Removes all listeners.
*/
dispose(): void;
/**
* Connects to a currently paired device or requests pairing.
* Throws on error.
*
* @returns the final connection status.
*/
connect(options?: ConnectOptions): Promise<ConnectionStatus>;
/**
* Flash the micro:bit.
*
* @param dataSource The data to use.
* @param options Flash options and progress callback.
*/
flash(
dataSource: FlashDataSource,
options: {
/**
* True to use a partial flash where possible, false to force a full flash.
*/
partial: boolean;
/**
* A progress callback. Called with undefined when the process is complete or has failed.
*
* Requesting a partial flash doesn't guarantee one is performed. Partial flashes are avoided
* if too many blocks have changed and failed partial flashes are retried as full flashes.
* The partial parameter reports the flash type currently in progress.
*/
progress: (percentage: number | undefined, partial: boolean) => void;
}
): Promise<void>;
/**
* Disconnect from the device.
*/
disconnect(): Promise<void>;
/**
* Write serial data to the device.
*
* Does nothting if there is no connection.
*
* @param data The data to write.
* @returns A promise that resolves when the write is complete.
*/
serialWrite(data: string): Promise<void>;
/**
* Clear device to enable chooseDevice.
*/
clearDevice(): void;
}