Skip to content

Commit c61d7f8

Browse files
universal-hex: Create enum for Board IDs and improve documentation.
1 parent 3eec9c1 commit c61d7f8

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

docs/quick-guide.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ nav_order: 2
88

99
## ES5 UMD Bundle
1010

11+
Create a Universal Hex from two Intel Hex strings:
12+
1113
```js
1214
var universalHex = microbitUh.createUniversalHex([
1315
{
14-
hex: intelHexString1,
15-
boardId: 0x1,
16+
hex: intelHexStringV1,
17+
boardId: microbitUh.microbitBoardId.V1,
1618
},
1719
{
18-
hex: intelHexString2,
19-
boardId: 0x2,
20+
hex: intelHexStringV2,
21+
boardId: microbitUh.microbitBoardId.V2,
2022
},
2123
]);
24+
```
2225

26+
Separate a Universal Hex into its Intel Hex strings:
27+
28+
```js
2329
if (microbitUh.isUniversalHex(intelHexStr)) {
2430
var separatedBinaries = microbitUh.separateUniversalHex(intelHexStr);
2531
}

src/ihex.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Generate and process Intel Hex records.
3+
* @packageDocumentation
34
*
45
* (c) 2020 Micro:bit Educational Foundation and contributors.
56
* SPDX-License-Identifier: MIT

src/universal-hex.ts

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
/**
2-
* Converts standard Intel Hex strings into a Universal Hex with records
3-
* organised in self contained 512-byte blocks.
2+
* Convert between standard Intel Hex strings and Universal Hex strings.
3+
*
4+
* This module provides the main functionality to convert Intel Hex strings
5+
* (with their respective Board IDs) into the Universal Hex format.
6+
*
7+
* It can also separate a Universal Hex string into the individual Intel Hex
8+
* strings that forms it.
9+
*
10+
* The content here assumes familiarity with the
11+
* [Universal Hex Specification](https://github.com/microbit-foundation/spec-universal-hex)
12+
* and the rest of
13+
* [this library documentation](https://microbit-foundation.github.io/microbit-universal-hex/).
14+
* @packageDocumentation
415
*
516
* (c) 2020 Micro:bit Educational Foundation and the project contributors.
617
* SPDX-License-Identifier: MIT
@@ -10,17 +21,43 @@ import * as ihex from './ihex';
1021
const V1_BOARD_IDS = [0x9900, 0x9901];
1122
const BLOCK_SIZE = 512;
1223

24+
/**
25+
* The Board ID is used to identify the different targets from a Universal Hex.
26+
* In this case the target represents a micro:bit version.
27+
* For micro:bit V1 (v1.3, v1.3B and v1.5) the `boardId` is `0x9900`, and for
28+
* V2 `0x9903`.
29+
*/
30+
enum microbitBoardId {
31+
V1 = 0x9900,
32+
V2 = 0x9903,
33+
}
34+
35+
/**
36+
* Very simple interface to group an Intel Hex string with a Board ID.
37+
*
38+
* The Board ID is used in an Universal Hex file to identify the target, in this
39+
* case the micro:bit version.
40+
*/
1341
interface IndividualHex {
42+
/** The Intel Hex in string format. */
1443
hex: string;
15-
boardId: number;
44+
/**
45+
* The Board ID identifies the target for the Intel Hex string.
46+
* Any number can be used in this field, but to target a micro:bit the
47+
* micro:bit firmware will process the data
48+
*/
49+
boardId: number | microbitBoardId;
1650
}
1751

1852
/**
19-
* Converts an Intel Hex file string into a Universal Hex ready hex string using
20-
* custom records and 512 byte blocks.
53+
* Converts an Intel Hex string into a Hex string using the 512 byte blocks
54+
* format and the Universal Hex specific record types.
2155
*
22-
* More information on the format:
23-
* https://github.com/microbit-foundation/universal-hex
56+
* The output of this function is not a fully formed Universal Hex, but one part
57+
* of a Universal Hex, ready to be merged by the calling code.
58+
*
59+
* More information on this "block" format:
60+
* https://github.com/microbit-foundation/spec-universal-hex
2461
*
2562
* @throws {Error} When the Board ID is not between 0 and 2^16.
2663
* @throws {Error} When there is an EoF record not at the end of the file.
@@ -29,7 +66,10 @@ interface IndividualHex {
2966
* byte blocks and the customer records.
3067
* @returns New Intel Hex string with the custom format.
3168
*/
32-
function iHexToCustomFormatBlocks(iHexStr: string, boardId: number): string {
69+
function iHexToCustomFormatBlocks(
70+
iHexStr: string,
71+
boardId: number | microbitBoardId
72+
): string {
3373
// Hex files for v1.3 and v1.5 continue using the normal Data Record Type
3474
const replaceDataRecord = !V1_BOARD_IDS.includes(boardId);
3575

@@ -121,11 +161,14 @@ function iHexToCustomFormatBlocks(iHexStr: string, boardId: number): string {
121161
}
122162

123163
/**
124-
* Converts an Intel Hex file string into a Universal Hex ready hex string using
125-
* custom records and sections aligned with 512-byte boundaries.
164+
* Converts an Intel Hex string into a Hex string using custom records and
165+
* aligning the content size to a 512-byte boundary.
166+
*
167+
* The output of this function is not a fully formed Universal Hex, but one part
168+
* of a Universal Hex, ready to be merged by the calling code.
126169
*
127-
* More information on the format:
128-
* https://github.com/microbit-foundation/universal-hex
170+
* More information on this "section" format:
171+
* https://github.com/microbit-foundation/spec-universal-hex
129172
*
130173
* @throws {Error} When the Board ID is not between 0 and 2^16.
131174
* @throws {Error} When there is an EoF record not at the end of the file.
@@ -134,7 +177,10 @@ function iHexToCustomFormatBlocks(iHexStr: string, boardId: number): string {
134177
* byte blocks and the customer records.
135178
* @returns New Intel Hex string with the custom format.
136179
*/
137-
function iHexToCustomFormatSection(iHexStr: string, boardId: number): string {
180+
function iHexToCustomFormatSection(
181+
iHexStr: string,
182+
boardId: number | microbitBoardId
183+
): string {
138184
const sectionLines: string[] = [];
139185
let sectionLen = 0;
140186
let ih = 0;
@@ -214,13 +260,19 @@ function iHexToCustomFormatSection(iHexStr: string, boardId: number): string {
214260
}
215261

216262
/**
217-
* Creates a Universal Hex from an collection of Intel Hex strings and their
263+
* Creates a Universal Hex from a collection of Intel Hex strings and their
218264
* board IDs.
219265
*
220-
* @param hexes An array of objects containing an Intel Hex strings and the
221-
* board ID associated with it.
222-
* @param blocks Indicate if the Universal Hex should be blocks instead of
223-
* sections.
266+
* For the current micro:bit board versions use the values from the
267+
* `microbitBoardId` enum.
268+
*
269+
* @param hexes An array of objects containing an Intel Hex string and the board
270+
* ID associated with it.
271+
* @param blocks Indicate if the Universal Hex format should be "blocks"
272+
* instead of "sections". The current specification recommends using the
273+
* default "sections" format as is much quicker in micro:bits with DAPLink
274+
* version 0234.
275+
* @returns A Universal Hex string.
224276
*/
225277
function createUniversalHex(hexes: IndividualHex[], blocks = false): string {
226278
if (!hexes.length) return '';
@@ -257,8 +309,9 @@ function createUniversalHex(hexes: IndividualHex[], blocks = false): string {
257309
* Very simple test only checking for the opening Extended Linear Address and
258310
* Block Start records.
259311
*
260-
* The string is manually checked as this method can be x20 faster than breaking
261-
* the string into records and checking their types with the ihex functions.
312+
* The string is manually iterated as this method can be x20 faster than
313+
* breaking the string into records and checking their types with the ihex
314+
* functions.
262315
*
263316
* @param hexStr Hex string to check
264317
* @return True if the hex is an Universal Hex.
@@ -281,7 +334,7 @@ function isUniversalHex(hexStr: string): boolean {
281334
}
282335

283336
/**
284-
* Separates a Universal Hex into the individual hexes.
337+
* Separates a Universal Hex into its individual Intel Hexes.
285338
*
286339
* @param universalHexStr Universal Hex string with the Universal Hex.
287340
* @returns An array of object with boardId and hex keys.
@@ -367,6 +420,8 @@ function separateUniversalHex(universalHexStr: string): IndividualHex[] {
367420
}
368421

369422
export {
423+
microbitBoardId,
424+
IndividualHex,
370425
iHexToCustomFormatBlocks,
371426
iHexToCustomFormatSection,
372427
createUniversalHex,

src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* General utilities.
3+
* @packageDocumentation
34
*
45
* (c) 2020 Micro:bit Educational Foundation and the project contributors.
56
* SPDX-License-Identifier: MIT

0 commit comments

Comments
 (0)