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.
This commit is contained in:
David Lechner
2020-06-09 23:51:32 -05:00
committed by David Lechner
parent 4c46f11722
commit f721f959f0
4 changed files with 17 additions and 23 deletions
-1
View File
@@ -4,7 +4,6 @@
import { Action } from 'redux';
export enum HubRuntimeStatusType {
Disconnected = 'disconnected',
Idle = 'idle',
Loading = 'loading',
Loaded = 'loaded',
+2 -14
View File
@@ -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<AnyAction, AnyAction, RootState> = (action$, state$) =>
action$.pipe(
ofType<AnyAction, BLEDataAction>(BLEDataActionType.Notify),
@@ -52,4 +40,4 @@ const rxUartData: Epic<AnyAction, AnyAction, RootState> = (action$, state$) =>
}),
);
export default combineEpics(connect, disconnect, rxUartData);
export default combineEpics(rxUartData);
+12 -8
View File
@@ -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<HubRuntimeState, HubRuntimeStatusMessageAction> = (
const runtime: Reducer<HubRuntimeState, Action> = (
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:
+3
View File
@@ -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<void> {
return;
}
dispatch(didConnect());
// Try to force a soft reset so the hub is in a known state
dispatch(stop());
}
function disconnect(action: Action): void {