Skip to content

Commit 8bb9017

Browse files
committed
fix: StartUserProgram based on protocol version
1 parent 9d233c9 commit 8bb9017

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/ble-pybricks-service/actions.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,6 @@ export const sendStopUserProgramCommand = createAction((id: number) => ({
5555
id,
5656
}));
5757

58-
/**
59-
* Action that requests a start user program to be sent.
60-
* @param id Unique identifier for this transaction.
61-
*
62-
* @since Pybricks Profile v1.2.0.
63-
*/
64-
export const sendLegacyStartUserProgramCommand = createAction((id: number) => ({
65-
type: 'blePybricksServiceCommand.action.sendLegacyStartUserProgramCommand',
66-
id,
67-
}));
68-
6958
/**
7059
* Action that requests a start user program to be sent.
7160
* @param id Unique identifier for this transaction.

src/ble-pybricks-service/sagas.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import {
99
fork,
1010
put,
1111
race,
12+
select,
1213
take,
1314
takeEvery,
1415
} from 'typed-redux-saga/macro';
16+
import { RootState } from '../reducers';
1517
import { ensureError, hex } from '../utils';
1618
import {
1719
didFailToSendCommand,
@@ -35,6 +37,7 @@ import {
3537
import {
3638
EventType,
3739
ProtocolError,
40+
createLegacyStartUserProgramCommand,
3841
createStartReplCommand,
3942
createStartUserProgramCommand,
4043
createStopUserProgramCommand,
@@ -53,6 +56,7 @@ import {
5356
* the bytecodes to to the device.
5457
*/
5558
function* encodeRequest(): Generator {
59+
const { useLegacyStartUserProgram } = yield* select((s: RootState) => s.hub);
5660
// Using a loop to serialize sending data to avoid "busy" errors.
5761

5862
const chan = yield* actionChannel(
@@ -68,9 +72,15 @@ function* encodeRequest(): Generator {
6872
if (sendStopUserProgramCommand.matches(action)) {
6973
yield* put(writeCommand(action.id, createStopUserProgramCommand()));
7074
} else if (sendStartUserProgramCommand.matches(action)) {
71-
yield* put(
72-
writeCommand(action.id, createStartUserProgramCommand(action.slot)),
73-
);
75+
if (useLegacyStartUserProgram) {
76+
yield* put(
77+
writeCommand(action.id, createLegacyStartUserProgramCommand()),
78+
);
79+
} else {
80+
yield* put(
81+
writeCommand(action.id, createStartUserProgramCommand(action.slot)),
82+
);
83+
}
7484
} else if (sendStartReplCommand.matches(action)) {
7585
yield* put(writeCommand(action.id, createStartReplCommand()));
7686
} else if (sendWriteUserProgramMetaCommand.matches(action)) {

src/hub/reducers.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2020-2023 The Pybricks Authors
2+
// Copyright (c) 2020-2024 The Pybricks Authors
33

44
import { Reducer, combineReducers } from 'redux';
55
import * as semver from 'semver';
@@ -263,6 +263,18 @@ const useLegacyStdio: Reducer<boolean> = (state = false, action) => {
263263
return state;
264264
};
265265

266+
/**
267+
* When true, use Legacy StartUserProgram.
268+
*/
269+
const useLegacyStartUserProgram: Reducer<boolean> = (state = false, action) => {
270+
if (bleDIServiceDidReceiveSoftwareRevision.matches(action)) {
271+
// Behavior changed starting with Pybricks Profile v1.4.0.
272+
return !semver.satisfies(action.version, '^1.4.0');
273+
}
274+
275+
return state;
276+
};
277+
266278
export default combineReducers({
267279
runtime,
268280
downloadProgress,
@@ -272,4 +284,5 @@ export default combineReducers({
272284
preferredFileFormat,
273285
useLegacyDownload,
274286
useLegacyStdio,
287+
useLegacyStartUserProgram,
275288
});

0 commit comments

Comments
 (0)