mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-04 09:02:50 +00:00
fix: add vendor ids for additional devices
fix: use proper semver parsing for device versions Fixes #2
This commit is contained in:
0
src/lib/components/action-extension.ts
Normal file
0
src/lib/components/action-extension.ts
Normal file
@@ -1,17 +1,33 @@
|
|||||||
import {LineBreakTransformer} from "$lib/serial/line-break-transformer"
|
import {LineBreakTransformer} from "$lib/serial/line-break-transformer"
|
||||||
import {serialLog} from "$lib/serial/connection"
|
import {serialLog} from "$lib/serial/connection"
|
||||||
import type {Chord} from "$lib/serial/chord"
|
import type {Chord} from "$lib/serial/chord"
|
||||||
|
import {SemVer} from "$lib/serial/sem-ver"
|
||||||
import {parseChordActions, parsePhrase, stringifyChordActions, stringifyPhrase} from "$lib/serial/chord"
|
import {parseChordActions, parsePhrase, stringifyChordActions, stringifyPhrase} from "$lib/serial/chord"
|
||||||
import {browser} from "$app/environment"
|
import {browser} from "$app/environment"
|
||||||
|
|
||||||
export const VENDOR_ID = 0x239a
|
const PORT_FILTERS: Map<string, SerialPortFilter> = new Map([
|
||||||
|
["ONE M0", {usbProductId: 32783, usbVendorId: 9114}],
|
||||||
|
["LITE S2", {usbProductId: 33070, usbVendorId: 12346}],
|
||||||
|
["LITE M0", {usbProductId: 32796, usbVendorId: 9114}],
|
||||||
|
["X", {usbProductId: 33163, usbVendorId: 12346}],
|
||||||
|
])
|
||||||
|
|
||||||
if (browser && navigator.serial === undefined && import.meta.env.TAURI_FAMILY !== undefined) {
|
if (browser && navigator.serial === undefined && import.meta.env.TAURI_FAMILY !== undefined) {
|
||||||
await import("./tauri-serial")
|
await import("./tauri-serial")
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getViablePorts(): Promise<SerialPort[]> {
|
export async function getViablePorts(): Promise<SerialPort[]> {
|
||||||
return navigator.serial.getPorts().then(ports => ports.filter(it => it.getInfo().usbVendorId === VENDOR_ID))
|
return navigator.serial.getPorts().then(ports =>
|
||||||
|
ports.filter(it => {
|
||||||
|
const {usbProductId, usbVendorId} = it.getInfo()
|
||||||
|
for (const filter of PORT_FILTERS.values()) {
|
||||||
|
if (filter.usbProductId === usbProductId && filter.usbVendorId === usbVendorId) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function canAutoConnect() {
|
export async function canAutoConnect() {
|
||||||
@@ -29,7 +45,7 @@ export class CharaDevice {
|
|||||||
|
|
||||||
private lock?: Promise<true>
|
private lock?: Promise<true>
|
||||||
|
|
||||||
version!: [number, number, number]
|
version!: SemVer
|
||||||
company!: "CHARACHORDER"
|
company!: "CHARACHORDER"
|
||||||
device!: "ONE" | "LITE"
|
device!: "ONE" | "LITE"
|
||||||
chipset!: "M0" | "S2"
|
chipset!: "M0" | "S2"
|
||||||
@@ -42,7 +58,7 @@ export class CharaDevice {
|
|||||||
this.port =
|
this.port =
|
||||||
!manual && ports.length === 1
|
!manual && ports.length === 1
|
||||||
? ports[0]
|
? ports[0]
|
||||||
: await navigator.serial.requestPort({filters: [{usbVendorId: VENDOR_ID}]})
|
: await navigator.serial.requestPort({filters: [...PORT_FILTERS.values()]})
|
||||||
|
|
||||||
await this.port.open({baudRate: this.baudRate})
|
await this.port.open({baudRate: this.baudRate})
|
||||||
const info = this.port.getInfo()
|
const info = this.port.getInfo()
|
||||||
@@ -57,8 +73,7 @@ export class CharaDevice {
|
|||||||
})
|
})
|
||||||
await this.port.close()
|
await this.port.close()
|
||||||
|
|
||||||
const [version] = await this.send("VERSION")
|
this.version = new SemVer(await this.send("VERSION").then(([version]) => version))
|
||||||
this.version = version.split(".").map(Number) as [number, number, number]
|
|
||||||
const [company, device, chipset] = await this.send("ID")
|
const [company, device, chipset] = await this.send("ID")
|
||||||
this.company = company as "CHARACHORDER"
|
this.company = company as "CHARACHORDER"
|
||||||
this.device = device as "ONE" | "LITE"
|
this.device = device as "ONE" | "LITE"
|
||||||
|
|||||||
27
src/lib/serial/sem-ver.ts
Normal file
27
src/lib/serial/sem-ver.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
export class SemVer {
|
||||||
|
major: number
|
||||||
|
minor: number
|
||||||
|
patch: number
|
||||||
|
preRelease?: string
|
||||||
|
meta?: string
|
||||||
|
|
||||||
|
constructor(versionString: string) {
|
||||||
|
const [, major, minor, patch, preRelease, meta] =
|
||||||
|
/^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+))?$/.exec(
|
||||||
|
versionString,
|
||||||
|
)!
|
||||||
|
this.major = Number.parseInt(major)
|
||||||
|
this.minor = Number.parseInt(minor)
|
||||||
|
this.patch = Number.parseInt(patch)
|
||||||
|
if (preRelease) this.preRelease = preRelease
|
||||||
|
if (meta) this.meta = meta
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return (
|
||||||
|
`${this.major}.${this.minor}.${this.patch}` +
|
||||||
|
(this.preRelease ? `-${this.preRelease}` : "") +
|
||||||
|
(this.meta ? `+${this.meta}` : "")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
{$serialPort.device}
|
{$serialPort.device}
|
||||||
{$serialPort.chipset}
|
{$serialPort.chipset}
|
||||||
<br />
|
<br />
|
||||||
Version {$serialPort.version.map(it => it.toString()).join(".")}
|
Version {$serialPort.version}
|
||||||
</p>
|
</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user