forked from microbit-foundation/python-editor-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard-id.ts
58 lines (51 loc) · 1.25 KB
/
board-id.ts
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
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
/**
* Validates micro:bit board IDs.
*/
export class BoardId {
private static v1Normalized = new BoardId(0x9900);
private static v2Normalized = new BoardId(0x9903);
constructor(public id: number) {
if (!this.isV1() && !this.isV2()) {
throw new Error(`Could not recognise the Board ID ${id.toString(16)}`);
}
}
isV1(): boolean {
return this.id === 0x9900 || this.id === 0x9901;
}
isV2(): boolean {
return (
this.id === 0x9903 ||
this.id === 0x9904 ||
this.id === 0x9905 ||
this.id === 0x9906
);
}
/**
* Return the board ID using the default ID for the board type.
* Used to integrate with MicropythonFsHex.
*/
normalize() {
return this.isV1() ? BoardId.v1Normalized : BoardId.v2Normalized;
}
/**
* toString matches the input to parse.
*
* @returns the ID as a string.
*/
toString() {
return this.id.toString(16);
}
/**
* @param value The ID as a hex string with no 0x prefix (e.g. 9900).
* @returns the valid board ID
* @throws if the ID isn't known.
*/
static parse(value: string): BoardId {
return new BoardId(parseInt(value, 16));
}
}