mirror of
https://github.com/sipeed/NanoKVM-USB.git
synced 2026-09-11 05:59:57 -05:00
add command GET_INFO
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Modifiers } from './keyboard.ts';
|
||||
import { Key as MouseKey, Mode as MouseMode } from './mouse.ts';
|
||||
import { CmdEvent, CmdPacket } from './proto.ts';
|
||||
import { CmdEvent, CmdPacket, InfoPacket } from './proto.ts';
|
||||
import { SerialPort } from './serial-port.ts';
|
||||
import { intToLittleEndianList } from './utils.ts';
|
||||
import { intToByte, intToLittleEndianList } from './utils.ts';
|
||||
|
||||
export class Device {
|
||||
addr: number;
|
||||
@@ -13,6 +13,15 @@ export class Device {
|
||||
this.serialPort = new SerialPort();
|
||||
}
|
||||
|
||||
async getInfo() {
|
||||
const data = new CmdPacket(this.addr, CmdEvent.GET_INFO).encode();
|
||||
await this.serialPort.write(data);
|
||||
|
||||
const rsp = await this.serialPort.read(14);
|
||||
const rspPacket = new CmdPacket(-1, -1, rsp);
|
||||
return new InfoPacket(rspPacket.DATA);
|
||||
}
|
||||
|
||||
async sendKeyboardData(modifiers: Modifiers, keys: number[]) {
|
||||
if (keys.length !== 6) {
|
||||
throw new Error('keyboard keys length must be 6');
|
||||
@@ -47,13 +56,6 @@ export class Device {
|
||||
}
|
||||
|
||||
async sendMouseRelativeData(msKey: MouseKey, x: number, y: number, scroll: number) {
|
||||
function intToByte(value: number): number {
|
||||
if (value < -128 || value > 127) {
|
||||
throw new Error('value must be in range -128 to 127 for a signed byte');
|
||||
}
|
||||
return (value + 256) % 256;
|
||||
}
|
||||
|
||||
const xByte = intToByte(x);
|
||||
const yByte = intToByte(y);
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { getBit } from './utils.ts';
|
||||
|
||||
export enum CmdEvent {
|
||||
GET_INFO = 0x01,
|
||||
SEND_KB_GENERAL_DATA = 0x02,
|
||||
@@ -106,3 +108,27 @@ export class CmdPacket {
|
||||
this.SUM &= 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
export class InfoPacket {
|
||||
CHIP_VERSION: string = 'V0.0';
|
||||
IS_CONNECTED: boolean = false;
|
||||
NUM_LOCK: boolean = false;
|
||||
CAPS_LOCK: boolean = false;
|
||||
SCROLL_LOCK: boolean = false;
|
||||
|
||||
constructor(data: number[]) {
|
||||
if (data[0] < 0x30) {
|
||||
throw new Error('version error');
|
||||
}
|
||||
|
||||
const versionE = data[0] - 0x30;
|
||||
const version = 1.0 + versionE / 10;
|
||||
this.CHIP_VERSION = `V${version.toFixed(1)}`;
|
||||
|
||||
this.IS_CONNECTED = data[1] !== 0;
|
||||
|
||||
this.NUM_LOCK = getBit(data[2], 0) === 1;
|
||||
this.CAPS_LOCK = getBit(data[2], 1) === 1;
|
||||
this.SCROLL_LOCK = getBit(data[2], 2) === 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,13 @@ export function setBit(number: number, bitPosition: number, value: boolean): num
|
||||
}
|
||||
}
|
||||
|
||||
export function intToByte(value: number): number {
|
||||
if (value < -128 || value > 127) {
|
||||
throw new Error('value must be in range -128 to 127 for a signed byte');
|
||||
}
|
||||
return (value + 256) % 256;
|
||||
}
|
||||
|
||||
export function intToLittleEndianList(number: number): number[] {
|
||||
const byteList: number[] = [];
|
||||
for (let i = 0; i < 2; i++) {
|
||||
|
||||
Reference in New Issue
Block a user