Skip to content

Commit

Permalink
add abstraction for native/polyfill webserial
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Dec 30, 2021
1 parent 911a46c commit dde9c68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as VueGL from "vue-gl";

import './style.scss'
// import 'chartjs-plugin-crosshair'
import 'web-serial-polyfill';

import App from './App.vue'
import router from './router'
Expand Down
7 changes: 5 additions & 2 deletions src/store/serial/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { concatUint8Array } from '../util';
import { AsyncQueue, AsyncSemaphore } from './async';
import { Log } from '@/log';
import { CBOR } from './cbor';
import { getWebSerial } from './webserial';

const WebSerial = getWebSerial();

const BAUD_RATE = 921600;

Expand All @@ -22,7 +25,7 @@ export class Serial {
private queue = new AsyncQueue();
private waitingCommands = new AsyncSemaphore(1);

private port?: SerialPort
private port?: any

private writer?: WritableStreamDefaultWriter<any>;
private reader?: ReadableStreamDefaultReader<any>;
Expand All @@ -34,7 +37,7 @@ export class Serial {

async connect(errorCallback: any = undefined): Promise<any> {
try {
this.port = await navigator.serial.requestPort({
this.port = await WebSerial.requestPort({
filters: SERIAL_FILTERS
});
this.queue = new AsyncQueue();
Expand Down
8 changes: 8 additions & 0 deletions src/store/serial/webserial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { serial } from 'web-serial-polyfill';

export function getWebSerial() {
if (navigator.serial) {
return navigator.serial;
}
return serial;
}

0 comments on commit dde9c68

Please sign in to comment.