Skip to content

Commit

Permalink
firmware: flash custom hub name
Browse files Browse the repository at this point in the history
This adds support for customizing the hub name when flashing firmware.

Fixes: pybricks/support#52
  • Loading branch information
dlech committed Nov 20, 2021
1 parent 86c0e93 commit ae40c2f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/firmware/sagas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -79,7 +81,7 @@ describe('flashFirmware', () => {
flashFirmware,
{
bootloader: { connection: BootloaderConnectionState.Disconnected },
settings: { flashCurrentProgram: false },
settings: { flashCurrentProgram: false, hubName: 'test name' },
},
{
nextMessageId: createCountFunc(),
Expand Down Expand Up @@ -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));
Expand Down
17 changes: 16 additions & 1 deletion src/firmware/sagas.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ae40c2f

Please sign in to comment.