Skip to content

Commit 4cd9283

Browse files
authored
Merge pull request #859 from remap-keys/change-firmware-blob-loading-timing
Change a firmware blob loading timing.
2 parents fdcc52c + 1b9b6da commit 4cd9283

File tree

6 files changed

+83
-22
lines changed

6 files changed

+83
-22
lines changed

src/actions/firmware.action.ts

+54-15
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const FLASH_FIRMWARE_DIALOG_CLEAR = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/Cle
4848
export const FLASH_FIRMWARE_DIALOG_UPDATE_KEYBOARD_NAME = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateKeyboardName`;
4949
export const FLASH_FIRMWARE_DIALOG_UPDATE_FLASH_MODE = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateFlashMode`;
5050
export const FLASH_FIRMWARE_DIALOG_UPDATE_BUILDING_FIRMWARE_TASK = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateBuildingFirmwareTask`;
51+
export const FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE_BLOB = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateFirmwareBlob`;
5152
export const FlashFirmwareDialogActions = {
5253
updateFirmware: (firmware: IFirmware | null) => {
5354
return {
@@ -117,6 +118,12 @@ export const FlashFirmwareDialogActions = {
117118
value: task,
118119
};
119120
},
121+
updateFirmwareBlob: (blob: Buffer | undefined) => {
122+
return {
123+
type: FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE_BLOB,
124+
value: blob,
125+
};
126+
},
120127
};
121128

122129
type ActionTypes = ReturnType<
@@ -171,9 +178,9 @@ export const firmwareActionsThunk = {
171178
created_at: new Date(),
172179
})
173180
);
181+
await dispatch(firmwareActionsThunk.loadFirmwareBlob());
174182
},
175-
// eslint-disable-next-line no-undef
176-
flashFirmware:
183+
loadFirmwareBlob:
177184
(): ThunkPromiseAction<void> =>
178185
async (
179186
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
@@ -182,21 +189,17 @@ export const firmwareActionsThunk = {
182189
const handleError = (error: string, cause?: any) => {
183190
console.error(error);
184191
dispatch(NotificationActions.addError(error, cause));
185-
dispatch(FlashFirmwareDialogActions.appendLog(`Error: ${error}`));
186-
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
192+
dispatch(FlashFirmwareDialogActions.clear());
187193
};
188194

189-
dispatch(FlashFirmwareDialogActions.updateLogs([]));
190-
dispatch(FlashFirmwareDialogActions.updateProgressRate(0));
191-
dispatch(FlashFirmwareDialogActions.updateMode('flashing'));
192-
dispatch(FlashFirmwareDialogActions.updateFlashing(true));
193-
const { entities, storage, serial, common } = getState();
194-
const firmwareWriter = serial.writer;
195+
dispatch(FlashFirmwareDialogActions.updateMode('loading'));
196+
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
197+
198+
const { common, entities, storage } = getState();
195199
const firmware = common.firmware.flashFirmwareDialog.firmware!;
196200
const bootloaderType =
197201
common.firmware.flashFirmwareDialog.bootloaderType!;
198202
const flashMode = common.firmware.flashFirmwareDialog.flashMode;
199-
200203
let flashBytes: Buffer | undefined;
201204
if (flashMode === 'fetch_and_flash') {
202205
const definitionDocument = entities.keyboardDefinitionDocument!;
@@ -268,11 +271,48 @@ export const firmwareActionsThunk = {
268271
return;
269272
}
270273
}
274+
dispatch(FlashFirmwareDialogActions.updateFirmwareBlob(flashBytes));
275+
dispatch(FlashFirmwareDialogActions.updateMode('instruction'));
276+
},
277+
// eslint-disable-next-line no-undef
278+
flashFirmware:
279+
(): ThunkPromiseAction<void> =>
280+
async (
281+
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
282+
getState: () => RootState
283+
) => {
284+
const handleError = (error: string, cause?: any) => {
285+
console.error(error);
286+
dispatch(NotificationActions.addError(error, cause));
287+
dispatch(FlashFirmwareDialogActions.appendLog(`Error: ${error}`));
288+
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
289+
};
290+
291+
dispatch(FlashFirmwareDialogActions.updateLogs([]));
292+
dispatch(FlashFirmwareDialogActions.updateProgressRate(0));
293+
dispatch(FlashFirmwareDialogActions.updateMode('flashing'));
294+
dispatch(FlashFirmwareDialogActions.updateFlashing(true));
295+
const { serial, common } = getState();
296+
const firmwareWriter = serial.writer;
297+
const bootloaderType =
298+
common.firmware.flashFirmwareDialog.bootloaderType!;
299+
300+
let flashBytes: Buffer | undefined =
301+
common.firmware.flashFirmwareDialog.firmwareBlob;
302+
if (flashBytes === undefined) {
303+
dispatch(
304+
NotificationActions.addError('Firmware binary is not loaded.')
305+
);
306+
dispatch(FlashFirmwareDialogActions.clear());
307+
return;
308+
}
309+
271310
dispatch(
272311
FlashFirmwareDialogActions.appendLog(
273-
'Reading the firmware binary done.'
312+
'Firmware binary has already been loaded. Start writing the firmware.'
274313
)
275314
);
315+
276316
dispatch(FlashFirmwareDialogActions.updateProgressRate(15));
277317

278318
const writeResult = await firmwareWriter.write(
@@ -341,15 +381,14 @@ const createFlashBytes = (
341381
return buffer;
342382
}
343383
} catch (error) {
344-
console.error(error);
384+
console.error('Creating a flashed bytes failed.', error);
345385
dispatch(
346386
NotificationActions.addError(
347387
'Creating the firmware binary failed.',
348388
error
349389
)
350390
);
351-
dispatch(FlashFirmwareDialogActions.appendLog(`Error: ${error}`));
352-
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
391+
dispatch(FlashFirmwareDialogActions.clear());
353392
return undefined;
354393
}
355394
};

src/components/catalog/keyboard/build/CatalogBuild.container.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
IFirmwareBuildingTask,
1515
IKeyboardDefinitionDocument,
1616
} from '../../../../services/storage/Storage';
17-
import { FlashFirmwareDialogActions } from '../../../../actions/firmware.action';
17+
import {
18+
firmwareActionsThunk,
19+
FlashFirmwareDialogActions,
20+
} from '../../../../actions/firmware.action';
1821

1922
// eslint-disable-next-line no-unused-vars
2023
const mapStateToProps = (state: RootState) => {
@@ -92,6 +95,7 @@ const mapDispatchToProps = (dispatch: any) => {
9295
created_at: new Date(),
9396
})
9497
);
98+
dispatch(firmwareActionsThunk.loadFirmwareBlob());
9599
},
96100
updateBuildableFirmwareCodeParameterValues: (
97101
values: IBuildableFirmwareCodeParameterValues

src/components/catalog/keyboard/firmware/CatalogFirmware.container.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
IFirmware,
88
IKeyboardDefinitionDocument,
99
} from '../../../../services/storage/Storage';
10-
import { FlashFirmwareDialogActions } from '../../../../actions/firmware.action';
10+
import {
11+
firmwareActionsThunk,
12+
FlashFirmwareDialogActions,
13+
} from '../../../../actions/firmware.action';
1114

1215
// eslint-disable-next-line no-unused-vars
1316
const mapStateToProps = (state: RootState) => {
@@ -56,6 +59,7 @@ const mapDispatchToProps = (_dispatch: any) => {
5659
FlashFirmwareDialogActions.updateFlashMode('fetch_and_flash')
5760
);
5861
_dispatch(FlashFirmwareDialogActions.updateFirmware(firmware));
62+
_dispatch(firmwareActionsThunk.loadFirmwareBlob());
5963
},
6064
},
6165
};

src/components/common/firmware/FlashFirmwareDialog.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ export default function FlashFirmwareDialog(
116116
value={props.logs!.join('\n')}
117117
/>
118118
</div>
119-
) : (
119+
) : props.mode === 'instruction' ? (
120120
<React.Fragment>
121121
<div className="flash-firmware-dialog-instruction">
122-
<Typography variant="body1">3 Steps to Flash</Typography>
122+
<Typography variant="body1">Three Steps to Flash</Typography>
123123
<div className="flash-firmware-dialog-instruction-items">
124124
<div className="flash-firmware-dialog-instruction-item">
125125
<img src={instructionImage1} alt="instructionImage1" />
@@ -158,17 +158,23 @@ export default function FlashFirmwareDialog(
158158
.
159159
</Typography>
160160
</React.Fragment>
161+
) : (
162+
<Typography variant="body1">Now loading the firmware...</Typography>
161163
)}
162164
</DialogContent>
163165
<DialogActions>
164166
<Button
165167
color="primary"
166168
onClick={onClickFlash}
167-
disabled={props.flashing}
169+
disabled={props.mode === 'loading' || props.flashing}
168170
>
169171
Flash
170172
</Button>
171-
<Button autoFocus onClick={onClickClose} disabled={props.flashing}>
173+
<Button
174+
autoFocus
175+
onClick={onClickClose}
176+
disabled={props.mode === 'loading' || props.flashing}
177+
>
172178
Close
173179
</Button>
174180
</DialogActions>

src/store/reducers.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ import {
201201
FLASH_FIRMWARE_DIALOG_UPDATE_BOOTLOADER_TYPE,
202202
FLASH_FIRMWARE_DIALOG_UPDATE_BUILDING_FIRMWARE_TASK,
203203
FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE,
204+
FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE_BLOB,
204205
FLASH_FIRMWARE_DIALOG_UPDATE_FLASH_MODE,
205206
FLASH_FIRMWARE_DIALOG_UPDATE_FLASHING,
206207
FLASH_FIRMWARE_DIALOG_UPDATE_KEYBOARD_NAME,
@@ -1196,7 +1197,8 @@ const flashFirmwareDialogReducer = (
11961197
draft.common.firmware.flashFirmwareDialog.flashing = false;
11971198
draft.common.firmware.flashFirmwareDialog.progressRate = 0;
11981199
draft.common.firmware.flashFirmwareDialog.logs = [''];
1199-
draft.common.firmware.flashFirmwareDialog.mode = 'instruction';
1200+
draft.common.firmware.flashFirmwareDialog.firmware = null;
1201+
draft.common.firmware.flashFirmwareDialog.mode = 'loading';
12001202
break;
12011203
case FLASH_FIRMWARE_DIALOG_UPDATE_LOGS:
12021204
draft.common.firmware.flashFirmwareDialog.logs = [''];
@@ -1214,6 +1216,9 @@ const flashFirmwareDialogReducer = (
12141216
draft.common.firmware.flashFirmwareDialog.buildingFirmwareTask =
12151217
action.value;
12161218
break;
1219+
case FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE_BLOB:
1220+
draft.common.firmware.flashFirmwareDialog.firmwareBlob = action.value;
1221+
break;
12171222
}
12181223
};
12191224

src/store/state.ts

+3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export const CONDITION_NOT_SELECTED: IConditionNotSelected = '---';
168168
export const ALL_FLASH_FIRMWARE_DIALOG_MODE = [
169169
'instruction',
170170
'flashing',
171+
'loading',
171172
] as const;
172173
type flashFirmwareDialogModeTuple = typeof ALL_FLASH_FIRMWARE_DIALOG_MODE;
173174
export type IFlashFirmwareDialogMode = flashFirmwareDialogModeTuple[number];
@@ -463,6 +464,7 @@ export type RootState = {
463464
bootloaderType: IBootloaderType;
464465
flashMode: IFlashFirmwareDialogFlashMode;
465466
buildingFirmwareTask: IFirmwareBuildingTask | null;
467+
firmwareBlob: Buffer | undefined;
466468
};
467469
uploadFirmwareDialog: {
468470
open: boolean;
@@ -725,6 +727,7 @@ export const INIT_STATE: RootState = {
725727
bootloaderType: 'caterina',
726728
flashMode: 'fetch_and_flash',
727729
buildingFirmwareTask: null,
730+
firmwareBlob: undefined,
728731
},
729732
uploadFirmwareDialog: {
730733
open: false,

0 commit comments

Comments
 (0)