Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically generate probe-methods and baudrates #18

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/flashing-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ApplicationType,
ApplicationTypeToFirmwareType,
mdiFirmware,
ManifestBaudrates,
} from './const';
import { setupPyodide, PyodideLoadState } from './setup-pyodide';

Expand Down Expand Up @@ -213,6 +214,35 @@ export class FlashingDialog extends LitElement {
`;
}

private generateProbeMethods(PyApplicationType: any) {
const baudrates = new Map();
const probe_methods: (keyof typeof ApplicationType)[] = [];

const valueToKeyMap = Object.entries(ApplicationType).reduce(
(acc, [key, value]) => {
acc[value] = key as keyof typeof ApplicationType;
return acc;
},
{} as Record<ApplicationType, keyof typeof ApplicationType>
);

const baudKeys = Object.keys(this.manifest.baudrates) as Array<
keyof ManifestBaudrates
>;

baudKeys.forEach(key => {
const pyAppTypeKey = valueToKeyMap[key];
const pyAppEnum = PyApplicationType[pyAppTypeKey];

if (pyAppTypeKey && pyAppEnum) {
probe_methods.push(pyAppEnum);
baudrates.set(pyAppEnum, this.manifest.baudrates[key]);
}
});

return { baudrates, probe_methods };
}

private async selectSerialPort() {
this.flashingStep = FlashingStep.SELECTING_PORT;
const options: SerialPortRequestOptions = {};
Expand Down Expand Up @@ -267,6 +297,10 @@ export class FlashingDialog extends LitElement {
'universal_silabs_flasher.const'
).ApplicationType;

// Generate probe-methods and baudrates from manifest values
const { baudrates, probe_methods } =
this.generateProbeMethods(PyApplicationType);

// Pyodide currently seems to have issues passing double proxied objects, especially
// with list comprehensions and generators. Until this is fixed, we need to
// explicitly convert the types with a wrapper function.
Expand All @@ -286,21 +320,8 @@ export class FlashingDialog extends LitElement {
`
)
.callKwargs({
baudrates: new Map([
[
PyApplicationType.GECKO_BOOTLOADER,
this.manifest.baudrates.bootloader,
],
[PyApplicationType.CPC, this.manifest.baudrates.cpc],
[PyApplicationType.EZSP, this.manifest.baudrates.ezsp],
[PyApplicationType.SPINEL, this.manifest.baudrates.spinel],
]),
probe_methods: [
PyApplicationType.GECKO_BOOTLOADER,
PyApplicationType.CPC,
PyApplicationType.EZSP,
PyApplicationType.SPINEL,
],
baudrates: baudrates,
probe_methods: probe_methods,
device: '/dev/webserial', // the device name is ignored
});

Expand Down