|
20 | 20 | * SOFTWARE. |
21 | 21 | */ |
22 | 22 |
|
23 | | -import { Registers } from "./registers"; |
24 | | -import { USB, USBDevice } from "webusb"; |
25 | | -import { stdin } from "process"; |
26 | | - |
27 | | -const devicesFound = (devices: USBDevice[], selectFn?: (device: USBDevice) => void) => { |
28 | | - stdin.setRawMode!(true); |
29 | | - stdin.setEncoding("utf8"); |
30 | | - stdin.on("readable", () => { |
31 | | - const input = process.stdin.read().toString(); |
32 | | - if (input === "\u0003") { |
| 23 | +import { stdin } from 'process'; |
| 24 | +import { USB } from 'webusb'; |
| 25 | +import { Registers } from './registers'; |
| 26 | + |
| 27 | +// Handle single character input from the user |
| 28 | +const readHandler = <T>(inputHandler: (input: string, onResolve: (result: T) => void) => void, stream: NodeJS.ReadStream = stdin): Promise<T> => { |
| 29 | + return new Promise(resolve => { |
| 30 | + stream.setRawMode!(true); |
| 31 | + stream.setEncoding('utf8'); |
| 32 | + |
| 33 | + const onResolve = (result: T) => { |
| 34 | + stream.removeListener('readable', read); |
| 35 | + stream.setRawMode!(false); |
| 36 | + resolve(result); |
| 37 | + }; |
| 38 | + |
| 39 | + const read = () => { |
| 40 | + let input: string | Buffer; |
| 41 | + while (input = stream.read()) { |
| 42 | + if (input) { |
| 43 | + inputHandler(input.toString(), onResolve); |
| 44 | + } |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + stream.addListener('readable', read); |
| 49 | + }); |
| 50 | +}; |
| 51 | + |
| 52 | +// Select a device from the list |
| 53 | +const devicesFound = async (devices: USBDevice[]): Promise<USBDevice | undefined> => { |
| 54 | + if (devices.length === 0) { |
| 55 | + throw new Error('No devices found'); |
| 56 | + } |
| 57 | + |
| 58 | + console.log('Select a device to read registers:'); |
| 59 | + devices.forEach((device, index) => { |
| 60 | + console.log(`${index + 1}: ${device.productName || device.serialNumber}`); |
| 61 | + }); |
| 62 | + |
| 63 | + const device = await readHandler<USBDevice>((input, resolve) => { |
| 64 | + if (input === '\u0003') { |
33 | 65 | process.exit(); |
34 | 66 | } else { |
35 | 67 | const index = parseInt(input); |
36 | 68 | if (index && index <= devices.length) { |
37 | | - stdin.setRawMode!(false); |
38 | | - selectFn!(devices[index - 1]); |
| 69 | + resolve(devices[index - 1]); |
39 | 70 | } |
40 | 71 | } |
41 | 72 | }); |
42 | 73 |
|
43 | | - console.log("select a device to see it's active configuration:"); |
44 | | - devices.forEach((device, index) => { |
45 | | - console.log(`${index + 1}: ${device.productName || device.serialNumber}`); |
46 | | - }); |
| 74 | + return device; |
47 | 75 | }; |
48 | 76 |
|
49 | 77 | const usb = new USB({ devicesFound }); |
50 | 78 | const registers = new Registers(usb); |
51 | 79 |
|
52 | 80 | (async () => { |
53 | | - const values = await registers.read(16); |
54 | | - values.forEach((register, index) => { |
55 | | - console.log(`R${index}: ${register}`); |
56 | | - }); |
| 81 | + try { |
| 82 | + const values = await registers.read(16); |
| 83 | + values.forEach((register, index) => { |
| 84 | + console.log(`R${index}: ${register}`); |
| 85 | + }); |
| 86 | + } catch(error) { |
| 87 | + console.error(error.message || error); |
| 88 | + } |
| 89 | + process.exit(); |
57 | 90 | })(); |
0 commit comments