pw_web_ui: Export WebSerial.WebSerialTransport helper class

This exports a helper class to connect to serial port and subscribe
to uint8 chunks easily without having to re-implement that logic.

Change-Id: Ie6563d0cfc71acf2d3eb79d5b10309da38b746ff
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/103002
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Asad Memon <asadmemon@google.com>
This commit is contained in:
Asad Memon 2022-07-22 21:06:09 +00:00 committed by CQ Bot Account
parent 61a775bea9
commit 33fe97272b
10 changed files with 40 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{
"name": "pigweedjs",
"version": "0.0.1",
"version": "0.0.2",
"description": "An open source collection of embedded-targeted libraries",
"author": "The Pigweed Authors",
"license": "Apache-2.0",

View File

@ -19,6 +19,30 @@ Following Pigweed modules are included in the NPM package:
- `pw_tokenizer <https://pigweed.dev/pw_tokenizer/#typescript>`_
- `pw_transfer <https://pigweed.dev/pw_transfer/#typescript>`_
To help with connecting to WebSerial and listening for serial data, a helper
class is also included under ``WebSerial.WebSerialTransport``. Here is an example
usage:
.. code:: javascript
import { WebSerial, pw_hdlc } from 'pigweedjs';
const transport = new WebSerial.WebSerialTransport();
const decoder = new pw_hdlc.Decoder();
// Present device selection prompt to user
await transport.connect();
// Listen and decode HDLC frames
transport.chunks.subscribe((item) => {
const decoded = decoder.process(item);
for (const frame of decoded) {
if (frame.address === 1) {
const decodedLine = new TextDecoder().decode(frame.data);
console.log(decodedLine);
}
}
});
Installation
=============

View File

@ -18,3 +18,4 @@ export * as pw_status from "../pw_status/ts";
export * as pw_tokenizer from "../pw_tokenizer/ts";
export * as pw_protobuf_compiler from "../pw_protobuf_compiler/ts";
export * as pw_transfer from "../pw_transfer/ts";
export * as WebSerial from "./transport/web_serial_transport";

View File

@ -21,7 +21,8 @@ import {
pw_hdlc,
pw_rpc,
pw_tokenizer,
pw_transfer
pw_transfer,
WebSerial
} from "../dist/index.umd";
import {ProtoCollection} from "../dist/protos/collection.umd";
@ -61,4 +62,8 @@ describe('Pigweed Bundle', () => {
expect(pw_transfer.Manager).toBeDefined();
});
it('has WebSerialTransport defined', () => {
expect(WebSerial.WebSerialTransport).toBeDefined();
});
});

View File

@ -14,7 +14,7 @@
/* eslint-env browser */
import {Subject} from 'rxjs';
import type {SerialConnectionEvent, SerialPort, Serial, SerialPortRequestOptions, SerialOptions} from "../../types/serial"
import type {SerialConnectionEvent, SerialPort, Serial, SerialPortRequestOptions, SerialOptions} from "pigweed/types/serial"
/**
* AsyncQueue is a queue that allows values to be dequeued
* before they are enqueued, returning a promise that resolves

View File

@ -15,7 +15,7 @@
/* eslint-env browser */
import {BehaviorSubject, Observable, Subject, Subscription} from 'rxjs';
import DeviceTransport from './device_transport';
import type {SerialPort, Serial, SerialOptions, Navigator, SerialPortFilter} from "../../types/serial"
import type {SerialPort, Serial, SerialOptions, Navigator, SerialPortFilter} from "pigweed/types/serial"
const DEFAULT_SERIAL_OPTIONS: SerialOptions & {baudRate: number} = {
// Some versions of chrome use `baudrate` (linux)
@ -62,7 +62,7 @@ export class WebSerialTransport implements DeviceTransport {
private serial: Serial = (navigator as unknown as Navigator).serial,
private filters: SerialPortFilter[] = [],
private serialOptions = DEFAULT_SERIAL_OPTIONS
) {}
) { }
/**
* Send a UInt8Array chunk of data to the connected device.

View File

@ -16,8 +16,8 @@
import {last, take} from 'rxjs/operators';
import {SerialMock} from './serial_mock';
import {DeviceLockedError, WebSerialTransport} from './web_serial_transport';
import type {Serial} from "../../types/serial"
import {WebSerialTransport, DeviceLockedError} from './web_serial_transport';
import type {Serial} from "pigweed/types/serial"
describe('WebSerialTransport', () => {
let serialMock: SerialMock;

View File

@ -19,6 +19,9 @@
],
"pigweed/protos/*": [
"./dist/protos/*"
],
"pigweed/types/*": [
"./ts/types/*"
]
}
},