diff --git a/CHANGELOG.md b/CHANGELOG.md index 0080ee9e5..fd2576824 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ ## [Unreleased] +### Added +- Hub name setting for selecting hub name when flashing firmware ([support#52]). + +[support#52]: https://github.com/pybricks/support/issues/52 + ## [1.1.0-beta.6] - 2021-09-21 ### Added diff --git a/src/firmware/sagas.test.ts b/src/firmware/sagas.test.ts index 5ca74b2bc..23046fd0a 100644 --- a/src/firmware/sagas.test.ts +++ b/src/firmware/sagas.test.ts @@ -63,6 +63,8 @@ describe('flashFirmware', () => { 'mpy-abi-version': 5, 'mpy-cross-options': ['-mno-unicode'], 'user-mpy-offset': 100, + 'hub-name-offset': 90, + 'max-hub-name-size': 10, }; const zip = new JSZip(); @@ -79,7 +81,7 @@ describe('flashFirmware', () => { flashFirmware, { bootloader: { connection: BootloaderConnectionState.Disconnected }, - settings: { flashCurrentProgram: false }, + settings: { flashCurrentProgram: false, hubName: 'test name' }, }, { nextMessageId: createCountFunc(), @@ -185,7 +187,7 @@ describe('flashFirmware', () => { // hub indicates success - saga.put(programResponse(0x62, totalFirmwareSize)); + saga.put(programResponse(0x33, totalFirmwareSize)); action = await saga.take(); expect(action).toEqual(didProgress(1)); diff --git a/src/firmware/sagas.ts b/src/firmware/sagas.ts index 680350e13..c96d8e3db 100644 --- a/src/firmware/sagas.ts +++ b/src/firmware/sagas.ts @@ -1,7 +1,12 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020-2021 The Pybricks Authors -import { FirmwareReader, FirmwareReaderError, HubType } from '@pybricks/firmware'; +import { + FirmwareReader, + FirmwareReaderError, + HubType, + encodeHubName, +} from '@pybricks/firmware'; import cityHubZip from '@pybricks/firmware/build/cityhub.zip'; import moveHubZip from '@pybricks/firmware/build/movehub.zip'; import technicHubZip from '@pybricks/firmware/build/technichub.zip'; @@ -241,6 +246,16 @@ function* loadFirmware( firmwareView.setUint32(metadata['user-mpy-offset'], mpy.data.length, true); firmware.set(mpy.data, metadata['user-mpy-offset'] + 4); + // if the firmware supports it, we can set a custom hub name + if (metadata['max-hub-name-size']) { + const hubName = yield* select((s: RootState) => s.settings.hubName); + + // empty string means use default name (don't write over firmware) + if (hubName) { + firmware.set(encodeHubName(hubName, metadata), metadata['hub-name-offset']); + } + } + if (metadata['checksum-type'] !== 'sum') { yield* put( didFailToFinish(