mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 17:45:22 +00:00
convert ble service to saga
This commit is contained in:
committed by
David Lechner
parent
f3425e2f0f
commit
bda007c17a
@@ -3,7 +3,6 @@
|
||||
// actions/ble-uart.ts: Actions for Bluetooth Low Energy nRF UART service
|
||||
|
||||
import { Action } from 'redux';
|
||||
import { assert } from '../utils';
|
||||
import { createCountFunc } from '../utils/iter';
|
||||
|
||||
/**
|
||||
@@ -36,7 +35,6 @@ export type BleUartWriteAction = Action<BleUartActionType.Write> & {
|
||||
};
|
||||
|
||||
export function write(value: Uint8Array): BleUartWriteAction {
|
||||
assert(value.length <= 20, 'value can be at most 20 bytes');
|
||||
return { type: BleUartActionType.Write, id: nextId(), value };
|
||||
}
|
||||
|
||||
|
||||
+110
-36
@@ -1,62 +1,136 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
// File: actions/ble.ts
|
||||
// Actions for managing Bluetooth Low Energy connections.
|
||||
|
||||
import { Action } from 'redux';
|
||||
|
||||
/**
|
||||
* Bluetooth low energy connection action types.
|
||||
* Bluetooth low energy device action types.
|
||||
*/
|
||||
export enum BLEConnectActionType {
|
||||
export enum BleDeviceActionType {
|
||||
/**
|
||||
* Connecting to a device has been requested.
|
||||
*/
|
||||
Connect = 'ble.action.connect',
|
||||
Connect = 'ble.device.action.connect',
|
||||
/**
|
||||
* The connection completed successfully.
|
||||
*/
|
||||
DidConnect = 'ble.action.did.connect',
|
||||
DidConnect = 'ble.device.action.didConnect',
|
||||
/**
|
||||
* The connection did not complete successfully.
|
||||
*/
|
||||
DidFailToConnect = 'ble.device.action.didFailToConnect',
|
||||
/**
|
||||
* Disconnecting from a device has been requested.
|
||||
*/
|
||||
Disconnect = 'ble.action.disconnect',
|
||||
Disconnect = 'ble.device.action.disconnect',
|
||||
/**
|
||||
* End async disconnect (can be sent without sending BeginDisconnect first).
|
||||
* The device was disconnected.
|
||||
*/
|
||||
DidDisconnect = 'ble.action.did.disconnect',
|
||||
DidDisconnect = 'ble.device.action.didDisconnect',
|
||||
}
|
||||
|
||||
export type BleDeviceConnectAction = Action<BleDeviceActionType.Connect>;
|
||||
|
||||
/**
|
||||
* Creates an action that indicates connecting has been requested.
|
||||
*/
|
||||
export function connect(): BleDeviceConnectAction {
|
||||
return { type: BleDeviceActionType.Connect };
|
||||
}
|
||||
|
||||
export type BleDeviceDidConnectAction = Action<BleDeviceActionType.DidConnect>;
|
||||
|
||||
/**
|
||||
* Creates an action that indicates a device was connected.
|
||||
*/
|
||||
export function didConnect(): BleDeviceDidConnectAction {
|
||||
return { type: BleDeviceActionType.DidConnect };
|
||||
}
|
||||
|
||||
export enum BleDeviceFailToConnectReasonType {
|
||||
NoWebBluetooth = 'ble.device.didFailToConnect.noWebBluetooth',
|
||||
Canceled = 'ble.device.didFailToConnect.canceled',
|
||||
NoGatt = 'ble.device.didFailToConnect.noGatt',
|
||||
NoService = 'ble.device.didFailToConnect.noService',
|
||||
Unknown = 'ble.device.didFailToConnect.unknown',
|
||||
}
|
||||
|
||||
type Reason<T extends BleDeviceFailToConnectReasonType> = {
|
||||
reason: T;
|
||||
};
|
||||
|
||||
export type BleDeviceFailToConnectNoWebBluetoothReason = Reason<
|
||||
BleDeviceFailToConnectReasonType.NoWebBluetooth
|
||||
>;
|
||||
|
||||
export type BleDeviceFailToConnectCanceledReason = Reason<
|
||||
BleDeviceFailToConnectReasonType.Canceled
|
||||
>;
|
||||
|
||||
export type BleDeviceFailToConnectNoGattReason = Reason<
|
||||
BleDeviceFailToConnectReasonType.NoGatt
|
||||
>;
|
||||
|
||||
export type BleDeviceFailToConnectNoServiceReason = Reason<
|
||||
BleDeviceFailToConnectReasonType.NoService
|
||||
>;
|
||||
|
||||
export type BleDeviceFailToConnectUnknownReason = Reason<
|
||||
BleDeviceFailToConnectReasonType.Unknown
|
||||
> & {
|
||||
err: Error;
|
||||
};
|
||||
|
||||
export type BleDeviceDidFailToConnectReason =
|
||||
| BleDeviceFailToConnectNoWebBluetoothReason
|
||||
| BleDeviceFailToConnectCanceledReason
|
||||
| BleDeviceFailToConnectNoGattReason
|
||||
| BleDeviceFailToConnectNoServiceReason
|
||||
| BleDeviceFailToConnectUnknownReason;
|
||||
|
||||
export type BleDeviceDidFailToConnectAction = Action<
|
||||
BleDeviceActionType.DidFailToConnect
|
||||
> &
|
||||
BleDeviceDidFailToConnectReason;
|
||||
|
||||
/**
|
||||
* Creates an action that indicates a device failed to connect.
|
||||
*/
|
||||
export function didFailToConnect(
|
||||
reason: BleDeviceDidFailToConnectReason,
|
||||
): BleDeviceDidFailToConnectAction {
|
||||
return { type: BleDeviceActionType.DidFailToConnect, ...reason };
|
||||
}
|
||||
|
||||
export type BleDeviceDisconnectAction = Action<BleDeviceActionType.Disconnect>;
|
||||
|
||||
/**
|
||||
* Creates an action that indicates disconnecting was requested.
|
||||
*/
|
||||
export function disconnect(): BleDeviceDisconnectAction {
|
||||
return { type: BleDeviceActionType.Disconnect };
|
||||
}
|
||||
|
||||
export type BleDeviceDidDisconnectAction = Action<BleDeviceActionType.DidDisconnect>;
|
||||
|
||||
/**
|
||||
* Creates an action that indicates a device was disconnected.
|
||||
*/
|
||||
export function didDisconnect(): BleDeviceDidDisconnectAction {
|
||||
return { type: BleDeviceActionType.DidDisconnect };
|
||||
}
|
||||
|
||||
/**
|
||||
* Common type for all BLE connection actions.
|
||||
*/
|
||||
export type BLEConnectAction = Action<BLEConnectActionType>;
|
||||
|
||||
/**
|
||||
* Creates an action that indicates connecting has been requested.
|
||||
*/
|
||||
export function connect(): BLEConnectAction {
|
||||
return { type: BLEConnectActionType.Connect };
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action that indicates a device was connected.
|
||||
*/
|
||||
export function didConnect(): BLEConnectAction {
|
||||
return { type: BLEConnectActionType.DidConnect };
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action that indicates disconnecting was requested.
|
||||
*/
|
||||
export function disconnect(): BLEConnectAction {
|
||||
return { type: BLEConnectActionType.Disconnect };
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action that indicates a device was disconnected.
|
||||
*/
|
||||
export function didDisconnect(): BLEConnectAction {
|
||||
return { type: BLEConnectActionType.DidDisconnect };
|
||||
}
|
||||
export type BLEConnectAction =
|
||||
| BleDeviceConnectAction
|
||||
| BleDeviceDidConnectAction
|
||||
| BleDeviceDidFailToConnectAction
|
||||
| BleDeviceDisconnectAction
|
||||
| BleDeviceDidDisconnectAction;
|
||||
|
||||
/**
|
||||
* High-level BLE actions.
|
||||
|
||||
Reference in New Issue
Block a user