mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 01:23:52 +00:00
wire terminal to ble
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { combineEpics, Epic, ofType } from 'redux-observable';
|
||||
import { BLEDataActionType, BLEDataAction } from '../actions/ble';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { TerminalDataAction, sendData } from '../actions/terminal';
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
const rxUartData: Epic = (action$) =>
|
||||
action$.pipe(
|
||||
ofType(BLEDataActionType.ReceivedData),
|
||||
map(
|
||||
(a: BLEDataAction): TerminalDataAction =>
|
||||
sendData(decoder.decode(a.value.buffer)),
|
||||
),
|
||||
);
|
||||
|
||||
export default combineEpics(rxUartData);
|
||||
@@ -0,0 +1,14 @@
|
||||
import { combineEpics, Epic } from 'redux-observable';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import ble from './ble';
|
||||
import terminal from './terminal';
|
||||
|
||||
const rootEpic: Epic = (action$, store$, dependencies) =>
|
||||
combineEpics(ble, terminal)(action$, store$, dependencies).pipe(
|
||||
catchError((error, source) => {
|
||||
console.error(error);
|
||||
return source;
|
||||
}),
|
||||
);
|
||||
|
||||
export default rootEpic;
|
||||
@@ -0,0 +1,28 @@
|
||||
import { combineEpics, Epic, ofType } from 'redux-observable';
|
||||
import { write, BLEThunkAction } from '../actions/ble';
|
||||
import { Subject } from 'rxjs';
|
||||
import { map, tap, ignoreElements } from 'rxjs/operators';
|
||||
import { TerminalDataAction, TerminalDataActionType } from '../actions/terminal';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
const terminalOutputSubject = new Subject<string>();
|
||||
export const terminalOutput = terminalOutputSubject.asObservable();
|
||||
|
||||
// When terminal has focus this receives the input. All input is sent over BLE connection.
|
||||
const receiveTerminalData: Epic = (action$) =>
|
||||
action$.pipe(
|
||||
ofType(TerminalDataActionType.ReceivedData),
|
||||
map((a: TerminalDataAction): BLEThunkAction => write(encoder.encode(a.value))),
|
||||
);
|
||||
|
||||
// Request to write to the terminal are handled by an observable rather than
|
||||
// using redux state.
|
||||
const sendTerminalData: Epic = (action$) =>
|
||||
action$.pipe(
|
||||
ofType(TerminalDataActionType.SendData),
|
||||
tap((a: TerminalDataAction): void => terminalOutputSubject.next(a.value)),
|
||||
ignoreElements(),
|
||||
);
|
||||
|
||||
export default combineEpics(receiveTerminalData, sendTerminalData);
|
||||
Reference in New Issue
Block a user