diff --git a/src/actions/hub.ts b/src/actions/hub.ts index d3b765d2..816b2b36 100644 --- a/src/actions/hub.ts +++ b/src/actions/hub.ts @@ -77,3 +77,10 @@ export function downloadAndRun(data: ArrayBuffer): HubThunkAction { dispatch(updateStatus(HubRuntimeStatusType.Loaded)); }; } + +// CTRL+C, CTRL+C, CTRL+D +const stopCommand = new Uint8Array([0x03, 0x03, 0x04]); + +export function stop(): HubThunkAction { + return write(stopCommand); +} diff --git a/src/components/StopButton.tsx b/src/components/StopButton.tsx new file mode 100644 index 00000000..0cd013d2 --- /dev/null +++ b/src/components/StopButton.tsx @@ -0,0 +1,37 @@ +import { connect } from 'react-redux'; +import { ThunkDispatch } from 'redux-thunk'; +import { AnyAction } from 'redux'; +import { RootState } from '../reducers'; +import { HubRuntimeState } from '../reducers/hub'; +import { stop } from '../actions/hub'; +import ActionButton, { ActionButtonProps } from './ActionButton'; + +type Dispatch = ThunkDispatch<{}, {}, AnyAction>; + +type StateProps = Pick; +type DispatchProps = Pick; +type OwnProps = Pick; + +const mapStateToProps = (state: RootState): StateProps => ({ + enabled: state.hub.runtime === HubRuntimeState.Running, +}); + +const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({ + onAction: (): void => { + dispatch(stop()); + }, +}); + +const mergeProps = ( + stateProps: StateProps, + dispatchProps: DispatchProps, + ownProps: OwnProps, +): ActionButtonProps => ({ + tooltip: 'Stop everything', + icon: 'stop.svg', + ...ownProps, + ...stateProps, + ...dispatchProps, +}); + +export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(ActionButton); diff --git a/src/components/Toolbar.tsx b/src/components/Toolbar.tsx index 70d50fef..e3f2a3ac 100644 --- a/src/components/Toolbar.tsx +++ b/src/components/Toolbar.tsx @@ -7,6 +7,7 @@ import { BLEConnectionState } from '../reducers/ble'; import ActionButton from './ActionButton'; import BluetoothButton from './BluetoothButton'; import RunButton from './RunButton'; +import StopButton from './StopButton'; interface ToolbarState { bleState: BLEConnectionState; @@ -35,12 +36,7 @@ class Toolbar extends React.Component<{}, ToolbarState> { - +