Skip to content

Commit

Permalink
Auto detect GX(6/2) and wired ports
Browse files Browse the repository at this point in the history
Auto detect GX(6/2) and wired ports if ports aren't specified
  • Loading branch information
JovannMC committed Jul 22, 2024
1 parent c34cf1f commit 2396ce3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 36 deletions.
34 changes: 31 additions & 3 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,47 @@ export default class HaritoraX extends EventEmitter {
* Starts the connection to the trackers with the specified mode.
*
* @param {string} connectionMode - Connect to the trackers with the specified mode (COM or bluetooth).
* @param {string[]} [portNames] - The port names to connect to. (COM only)
* @param {string[]} portNames - The port names to connect to. (COM only)
* @param {number} heartbeatInterval - The interval to send the heartbeat signal to the trackers. (COM only)
*
* @example
* device.startConnection("COM");
**/
startConnection(connectionMode: string, autoFind: boolean = false, portNames?: string[], heartbeatInterval: number = 10000) {
async startConnection(connectionMode: string, portNames?: string[], heartbeatInterval: number = 10000) {
if (!isConnectionModeSupported(connectionMode))
error(`${connectionMode} connection not supported for ${trackerModelEnabled}`, true);

if (connectionMode === "com") {
com = new COM(trackerModelEnabled, heartbeatInterval);
comEnabled = true;
com.startConnection(autoFind, portNames);

if (!portNames) {
log("No port names provided, attempting to get device ports...");
if (trackerModelEnabled === "wired") {
log("Wired model enabled, getting their ports...");
//const HaritoraPorts = await com.getDevicePorts("Haritora");
const HaritoraX10Ports = await com.getDevicePorts("HaritoraX 1.0");
const HaritoraX11Ports = await com.getDevicePorts("HaritoraX 1.1");
const HaritoraX11bPorts = await com.getDevicePorts("HaritoraX 1.1b");

//log(`Got Haritora ports: ${HaritoraPorts}`);
log(`Got HaritoraX 1.0 ports: ${HaritoraX10Ports}`);
log(`Got HaritoraX 1.1 ports: ${HaritoraX11Ports}`);
log(`Got HaritoraX 1.1b ports: ${HaritoraX11bPorts}`);
//portNames = HaritoraPorts.concat(HaritoraX10Ports, HaritoraX11Ports, HaritoraX11bPorts);
portNames = HaritoraX10Ports.concat(HaritoraX11Ports, HaritoraX11bPorts);
} else if (trackerModelEnabled === "wireless") {
log("Wireless model enabled, getting their dongles' ports...");
const GX6Ports = await com.getDevicePorts("GX6");
const GX2Ports = await com.getDevicePorts("GX2");

log(`Got GX6 ports: ${GX6Ports}`);
log(`Got GX2 ports: ${GX2Ports}`);
portNames = GX6Ports.concat(GX2Ports);
}
}

com.startConnection(portNames);
canProcessComData = true;
} else if (connectionMode === "bluetooth") {
bluetooth = new Bluetooth();
Expand Down
64 changes: 32 additions & 32 deletions src/mode/com.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const deviceInformation: Map<string, string[]> = new Map([
["rightElbow", ["", "", "", "", ""]],
]);

const devices = [
{ name: "GX2", vid: "1915", pid: "520F" },
{ name: "GX6", vid: "04DA", pid: "3F18" },
{ name: "HaritoraX 1.1b", vid: "", pid: "" },
{ name: "HaritoraX 1.1", vid: "", pid: "" },
{ name: "HaritoraX 1.0", vid: "", pid: "" },
// { name: "Haritora", vid: "", pid: "" }, - these were before Shiftall (and users had to build it themselves), so they may not have the same VID/PID
];

// Stores the ports that are currently active as objects for access later
let activePorts: ActivePorts = {};
let trackersAssigned = false;
Expand All @@ -54,22 +63,32 @@ export default class COM extends EventEmitter {
log(`Initialized COM module with settings: ${trackerModelEnabled} ${heartbeatInterval}`);
}

async startConnection(autoFind: boolean, portNames: string[]) {
const devices = [
{ name: "GX2", vid: "1915", pid: "520F" },
{ name: "GX6", vid: "04DA", pid: "3F18" },
];
async getDevicePorts(device: string) {
const ports = await Binding.list();
const availablePorts = ports
.map(port => {
const deviceMatch = devices.find(deviceItem => port.vendorId === deviceItem.vid && port.productId === deviceItem.pid);
return {
...port,
deviceName: deviceMatch ? deviceMatch.name : undefined,
};
})
.filter(port_1 => port_1.deviceName !== undefined);
let foundPorts = [];
for (const port_2 of availablePorts) {
if (port_2.deviceName === device) foundPorts.push(port_2.path);
}
return foundPorts;
}

const initializeSerialPort = (port: string, deviceName: string) => {
startConnection(portNames: string[]) {
const initializeSerialPort = (port: string) => {
try {
const serial = new SerialPortStream({ path: port, baudRate: BAUD_RATE, binding: Binding });
const parser = serial.pipe(new ReadlineParser({ delimiter: "\n" }));
activePorts[port] = serial;

serial.on("open", () => {
this.emit("connected", port);
log(`Connected to COM port for ${deviceName}: ${port}`);
});
serial.on("open", () => this.emit("connected", port));
parser.on("data", (data) => processData(data, port));
serial.on("close", () => this.emit("disconnected", port));
serial.on("error", (err) => {
Expand All @@ -82,28 +101,9 @@ export default class COM extends EventEmitter {
}
};

if (autoFind) {
const ports = await Binding.list();
const availablePorts = ports
.map((port) => {
const device = devices.find((device) => port.vendorId === device.vid && port.productId === device.pid);
return {
...port,
deviceName: device ? device.name : undefined,
};
})
.filter((port) => port.deviceName !== undefined);

for (const port of availablePorts) {
log(`Found COM port for ${port.deviceName}: ${port.path}`);
initializeSerialPort(port.path, port.deviceName);
}
} else {
for (const port of portNames) {
// For manual selection, device name is not logged since VID/PID matching is not performed
log(`Opening COM port: ${port}`);
initializeSerialPort(port, "Unknown Device");
}
for (const port of portNames) {
log(`Opening COM port: ${port}`);
initializeSerialPort(port);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (mode === "bt" || mode === "bluetooth") {
}
}, 3000);*/
} else {
device.startConnection("com", true);
device.startConnection("com");

device.on("connect", (trackerName) => {
console.log(`Connected to tracker ${trackerName}`);
Expand Down

0 comments on commit 2396ce3

Please sign in to comment.