Skip to content

Commit

Permalink
fix wireless getTrackerSettings, optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
JovannMC committed Jul 19, 2024
1 parent 04451de commit 2e98624
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ export default class HaritoraX extends EventEmitter {
writeToBluetooth(trackerName, fpsModeCharacteristic, fpsMode === 50 ? 1 : 2);
writeToBluetooth(trackerName, correctionCharacteristic, sensorAutoCorrectionBit);
writeToBluetooth(trackerName, ankleCharacteristic, ankleMotionDetection ? 1 : 0);
logSettings(trackerName, settings);
} else {
// GX dongle(s)
const sensorModeBit = sensorMode === 1 ? SENSOR_MODE_1 : SENSOR_MODE_2; // Default to mode 2
Expand All @@ -396,16 +395,6 @@ export default class HaritoraX extends EventEmitter {
return;
}

logSettings(trackerName, {
sensorMode,
fpsMode,
sensorAutoCorrection,
ankleMotionDetection,
hexValue,
});

trackerSettings.set(trackerName, [sensorMode, fpsMode, sensorAutoCorrection, ankleMotionDetection]);

try {
let ports = com.getActivePorts();
let trackerPort = com.getTrackerPort(trackerName);
Expand All @@ -427,6 +416,15 @@ export default class HaritoraX extends EventEmitter {
}
}

logSettings(trackerName, settings);
log(
`Tracker ${trackerName} settings applied: ${JSON.stringify([
sensorMode,
fpsMode,
sensorAutoCorrection,
ankleMotionDetection,
])}`
);
trackerSettings.set(trackerName, [sensorMode, fpsMode, sensorAutoCorrection, ankleMotionDetection]);
return;
}
Expand Down Expand Up @@ -507,14 +505,7 @@ export default class HaritoraX extends EventEmitter {
};

if (trackerModelEnabled === "wired") {
const settings = trackerSettings.get("HaritoraXWired");
logSettings("HaritoraX", ...settings);
return {
sensorMode: settings[0],
fpsMode: settings[1],
sensorAutoCorrection: settings[2],
ankleMotionDetection: settings[3],
};
return getTrackerSettingsFromMap("HaritoraXWired");
} else if (trackerModelEnabled === "wireless" && bluetoothEnabled && isWirelessBT(trackerName)) {
if (forceBluetoothRead || !trackerSettings.has(trackerName)) {
try {
Expand All @@ -540,17 +531,12 @@ export default class HaritoraX extends EventEmitter {
error(`Error reading characteristic: ${err}`);
}
} else {
const settings = trackerSettings.get(trackerName);
logSettings(trackerName, ...settings);
return {
sensorMode: settings[0],
fpsMode: settings[1],
sensorAutoCorrection: settings[2],
ankleMotionDetection: settings[3],
};
return getTrackerSettingsFromMap(trackerName);
}
} else if (trackerModelEnabled === "wireless" && comEnabled && !isWirelessBT(trackerName)) {
return getTrackerSettingsFromMap(trackerName);
} else {
log(`Tracker ${trackerName} settings not found`);
error(`Cannot get settings for ${trackerName} settings.`);
return null;
}
}
Expand Down Expand Up @@ -1008,7 +994,7 @@ function processIMUData(data: Buffer, trackerName: string, ankleValue?: number)
main.emit("imu", trackerName, rotation, gravity, ankle ? ankle : ankleValue);
if (!isWirelessBT(trackerName)) main.emit("mag", trackerName, magStatus);
} catch (err) {
error(`Error decoding tracker ${trackerName} IMU packet data: ${err}`);
error(`Error decoding tracker ${trackerName} IMU packet data: ${err}`, false);
}
}

Expand All @@ -1020,6 +1006,8 @@ function processIMUData(data: Buffer, trackerName: string, ankleValue?: number)
**/

function decodeIMUPacket(data: Buffer, trackerName: string) {
if (!trackerName) return;

try {
if (data.length < 14) {
error(`Too few bytes to decode IMU packet, data: ${data.toString("utf-8")}`);
Expand Down Expand Up @@ -1108,7 +1096,7 @@ function decodeIMUPacket(data: Buffer, trackerName: string) {

return { rotation, gravity, ankle, magStatus };
} catch (err) {
error(`Error decoding IMU packet: ${err}`, true);
error(`Error decoding IMU packet: ${err}`, false);
}
}

Expand Down Expand Up @@ -1544,6 +1532,21 @@ async function removeActiveDevices(deviceTypeToRemove: string) {
return;
}

/*
* getTrackerSettings() function helpers
*/

function getTrackerSettingsFromMap(trackerName: string) {
const settings = trackerSettings.get(trackerName);
logSettings(trackerName, settings);
return {
sensorMode: settings[0],
fpsMode: settings[1],
sensorAutoCorrection: settings[2],
ankleMotionDetection: settings[3],
};
}

/*
* set(All)TrackerSettings() function helpers
*/
Expand Down

0 comments on commit 2e98624

Please sign in to comment.