mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 01:23:52 +00:00
bootloader: add withResponse option
This takes advantage of new (not implemented yet) Web Bluetooth APIs to write with or without response
This commit is contained in:
@@ -75,10 +75,14 @@ export function didError(err: Error): BootloaderConnectionDidErrorAction {
|
||||
export interface BootloaderConnectionSendAction
|
||||
extends Action<BootloaderConnectionActionType.Send> {
|
||||
readonly data: ArrayBuffer;
|
||||
readonly withResponse: boolean;
|
||||
}
|
||||
|
||||
export function send(data: ArrayBuffer): BootloaderConnectionSendAction {
|
||||
return { type: BootloaderConnectionActionType.Send, data };
|
||||
export function send(
|
||||
data: ArrayBuffer,
|
||||
withResponse = false,
|
||||
): BootloaderConnectionSendAction {
|
||||
return { type: BootloaderConnectionActionType.Send, data, withResponse };
|
||||
}
|
||||
|
||||
export interface BootloaderConnectionDidSendAction
|
||||
|
||||
@@ -81,7 +81,17 @@ async function send(action: Action, dispatch: Dispatch): Promise<void> {
|
||||
if (!char) {
|
||||
throw Error('Not connected');
|
||||
}
|
||||
await char.writeValue((action as BootloaderConnectionSendAction).data);
|
||||
const sendAction = action as BootloaderConnectionSendAction;
|
||||
// Fall back to legacy WebBluetooth writeValue if new methods are not
|
||||
// available.
|
||||
const writeValue = sendAction.withResponse
|
||||
? // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
char.writeValueWithResponse || char.writeValue
|
||||
: // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
||||
// @ts-ignore
|
||||
char.writeValueWithoutResponse || char.writeValue;
|
||||
await writeValue(sendAction.data);
|
||||
dispatch(didSend());
|
||||
} catch (err) {
|
||||
dispatch(didSend(err));
|
||||
|
||||
Reference in New Issue
Block a user