From f721f959f0cd4781eb68a03f64fdb40ac7b8af14 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 9 Jun 2020 10:06:33 -0500 Subject: [PATCH] do soft reset after connecting When we first connect, we don't know what state the hub is in. This adds a new state for that called Unknown and sends a stop request as soon as we connect to try to get to a known state. --- src/actions/hub.ts | 1 - src/epics/ble.ts | 16 ++-------------- src/reducers/hub.ts | 20 ++++++++++++-------- src/services/ble.ts | 3 +++ 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/actions/hub.ts b/src/actions/hub.ts index c541cfca..f6cdf256 100644 --- a/src/actions/hub.ts +++ b/src/actions/hub.ts @@ -4,7 +4,6 @@ import { Action } from 'redux'; export enum HubRuntimeStatusType { - Disconnected = 'disconnected', Idle = 'idle', Loading = 'loading', Loaded = 'loaded', diff --git a/src/epics/ble.ts b/src/epics/ble.ts index a613588a..7448c620 100644 --- a/src/epics/ble.ts +++ b/src/epics/ble.ts @@ -4,7 +4,7 @@ import { AnyAction } from 'redux'; import { Epic, combineEpics, ofType } from 'redux-observable'; import { map } from 'rxjs/operators'; -import { BLEConnectActionType, BLEDataAction, BLEDataActionType } from '../actions/ble'; +import { BLEDataAction, BLEDataActionType } from '../actions/ble'; import { HubRuntimeStatusType, checksum, updateStatus } from '../actions/hub'; import { sendData } from '../actions/terminal'; import { RootState } from '../reducers'; @@ -12,18 +12,6 @@ import { HubRuntimeState } from '../reducers/hub'; const decoder = new TextDecoder(); -const connect: Epic = (action$) => - action$.pipe( - ofType(BLEConnectActionType.DidConnect), - map(() => updateStatus(HubRuntimeStatusType.Idle)), - ); - -const disconnect: Epic = (action$) => - action$.pipe( - ofType(BLEConnectActionType.DidDisconnect), - map(() => updateStatus(HubRuntimeStatusType.Disconnected)), - ); - const rxUartData: Epic = (action$, state$) => action$.pipe( ofType(BLEDataActionType.Notify), @@ -52,4 +40,4 @@ const rxUartData: Epic = (action$, state$) => }), ); -export default combineEpics(connect, disconnect, rxUartData); +export default combineEpics(rxUartData); diff --git a/src/reducers/hub.ts b/src/reducers/hub.ts index 090d13d6..ffaf4b84 100644 --- a/src/reducers/hub.ts +++ b/src/reducers/hub.ts @@ -2,11 +2,9 @@ // Copyright (c) 2020 The Pybricks Authors import { Reducer, combineReducers } from 'redux'; -import { - HubMessageActionType, - HubRuntimeStatusMessageAction, - HubRuntimeStatusType, -} from '../actions/hub'; +import { Action } from '../actions'; +import { BLEConnectActionType } from '../actions/ble'; +import { HubMessageActionType, HubRuntimeStatusType } from '../actions/hub'; /** * Describes the state of the MicroPython runtime on the hub. @@ -16,6 +14,10 @@ export enum HubRuntimeState { * The hub is not connected. */ Disconnected = 'hub.runtime.disconnected', + /** + * The hub is connected but the state is not known yet. + */ + Unknown = 'hub.runtime.unknown', /** * The runtime is idle waiting for command after soft reboot. */ @@ -38,15 +40,17 @@ export enum HubRuntimeState { Error = 'hub.runtime.error', } -const runtime: Reducer = ( +const runtime: Reducer = ( state = HubRuntimeState.Disconnected, action, ) => { switch (action.type) { + case BLEConnectActionType.DidDisconnect: + return HubRuntimeState.Disconnected; + case BLEConnectActionType.DidConnect: + return HubRuntimeState.Unknown; case HubMessageActionType.RuntimeStatus: switch (action.newStatus) { - case HubRuntimeStatusType.Disconnected: - return HubRuntimeState.Disconnected; case HubRuntimeStatusType.Idle: return HubRuntimeState.Idle; case HubRuntimeStatusType.Loading: diff --git a/src/services/ble.ts b/src/services/ble.ts index e4a5ee5a..aba7b9ed 100644 --- a/src/services/ble.ts +++ b/src/services/ble.ts @@ -12,6 +12,7 @@ import { disconnect as disconnectAction, notify, } from '../actions/ble'; +import { stop } from '../actions/hub'; import * as notification from '../actions/notification'; import { RootState } from '../reducers'; import { BLEConnectionState } from '../reducers/ble'; @@ -104,6 +105,8 @@ async function connect(action: Action, dispatch: Dispatch): Promise { return; } dispatch(didConnect()); + // Try to force a soft reset so the hub is in a known state + dispatch(stop()); } function disconnect(action: Action): void {