Skip to content

Commit

Permalink
Merge branch 'development' into test-palette-parse
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpargon authored Feb 7, 2025
2 parents 8b73a1f + bb9e137 commit c86f383
Show file tree
Hide file tree
Showing 43 changed files with 1,083 additions and 622 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"ts-node": "^10.0.0",
"typescript": "^5.4.5",
"vite": "^5.4.12",
"vitest": "^2.1.2",
"vitest": "^2.1.9",
"webpack-cli": "^5.1.4"
},
"dependencies": {
Expand All @@ -147,6 +147,7 @@
"@radix-ui/react-toggle-group": "^1.0.4",
"@radix-ui/react-tooltip": "^1.0.7",
"@serialport/parser-delimiter": "^12.0.0",
"@serialport/stream": "12.0.0",
"@types/react-color": "^3.0.11",
"@types/w3c-web-hid": "^1.0.3",
"@xstate/react": "^4.1.1",
Expand Down
5 changes: 0 additions & 5 deletions src/api/comms/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ class Device implements DeviceClass {
}
}

static delay = (ms: number) =>
new Promise(res => {
setTimeout(res, ms);
});

static getHWFVirtual = (dev: DygmaDeviceType) => {
let result: DygmaDeviceType;
Hardware.serial.forEach(hdev => {
Expand Down
5 changes: 2 additions & 3 deletions src/api/comms/DeviceTalker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import log from "electron-log/renderer";
import serial, { isSerialType, DeviceType } from "./serial";
import serial, { DeviceType, isSerialType } from "./serial";
import hid from "./hid";

class DeviceTalker {
Expand All @@ -19,8 +19,7 @@ class DeviceTalker {
log.verbose(`the device is ${device.type} type, and connected as: ${result}`);
return result;
}
const result = hid.connect(device);
return result;
return hid.connect(device);
};
}

Expand Down
48 changes: 48 additions & 0 deletions src/api/comms/deviceMap/DeviceMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import DeviceMap from "./DeviceMap";
import log from "electron-log/renderer";


describe('DeviceMap', () => {
beforeEach(() => {
vi.mock("electron-log/renderer", () => ({
default: {
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
verbose: vi.fn(),
debug: vi.fn(),
silly: vi.fn(),
},
}));
});

afterEach(() => {
vi.clearAllMocks();
});

it('should return false with new key', async () => {
const map = new DeviceMap();
expect(map.isUpdated("key", "value")).toBe(false);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith("comparison between keys", false, undefined, "value");
});
it('should return false with differing values', async () => {
const map = new DeviceMap();
map.set("key", "original");
expect(map.isUpdated("key", "value")).toBe(false);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith("comparison between keys", false, "original", "value");

});
it('should return true with same value', async () => {
const map = new DeviceMap();
map.set("key", "value");
expect(map.isUpdated("key", "value")).toBe(true);

expect(log.warn).toHaveBeenCalledTimes(1);
expect(log.warn).toHaveBeenCalledWith("comparison between keys", true, "value", "value");
});
});
3 changes: 1 addition & 2 deletions src/api/comms/deviceMap/DeviceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import log from "electron-log/renderer";
export default class DeviceMap extends Map<string, string> {
isUpdated(key: string, value: string): boolean {
log.warn("comparison between keys", this.get(key) === value, this.get(key), value);
if (this.get(key) === value) return true;
return false;
return this.get(key) === value;
}
}
4 changes: 2 additions & 2 deletions src/api/comms/serial/SerialAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import log from "electron-log/renderer";
import type { SerialPort as SP } from "serialport";
import type { PortInfo } from "@serialport/bindings-cpp";
import { DygmaDeviceType } from "@Renderer/types/dygmaDefs";
import { DeviceType } from "@Types/devices";
import Hardware from "../../hardware";
import { DeviceType } from "../../../renderer/types/devices";

const { SerialPort } = eval('require("serialport")');
const { DelimiterParser } = eval('require("@serialport/parser-delimiter")');
Expand Down Expand Up @@ -51,7 +51,7 @@ interface SerialProperties {
}

const checkProperties = async (path: string): Promise<SerialProperties> => {
let callbacks: any[] = [];
let callbacks: ((value: string | PromiseLike<string>) => void)[] = [];
let result = "";

function rawCommand(cmd: string, serialPort: SP): Promise<string> {
Expand Down
12 changes: 7 additions & 5 deletions src/api/flash/RaiseTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import log from "electron-log/renderer";
import type { SerialPort as SP } from "serialport";
import { DygmaDeviceType } from "@Renderer/types/dygmaDefs";
import { delay } from "./delay";
import { delay } from "../../main/utils/delay";
import { findDevice } from "./findDevice";
import Device from "../comms/Device";
import { arduino } from "./raiseFlasher/arduino-flasher";
Expand Down Expand Up @@ -59,7 +59,7 @@ export const RaiseResetKeyboard = async (port: SP, stateUpdate: (state: string,
let bootCount = 6;
while (bootCount > 0) {
stateUpdate("reset", 20 + (10 - bootCount) * 8);
const newPort = await findDevice("bootloader", "Bootloader detected");
const newPort = await findDevice("bootloader");
if (newPort) {
stateUpdate("reset", 100);
result = newPort;
Expand Down Expand Up @@ -101,7 +101,7 @@ export const resetKeyboard = async (currentDevice: Device, stateUpdate: any) =>
try {
let bootCount = 10;
while (bootCount > 0) {
if (await findDevice("bootloader", "Bootloader detected")) {
if (await findDevice("bootloader")) {
stateUpdate("reset", 100);
bootCount = -1;
resolve("Detected Bootloader mode");
Expand Down Expand Up @@ -130,7 +130,7 @@ export const detectKeyboard = async (stateUpdate: any, device: DygmaDeviceType)
try {
let searchFor = 10;
while (searchFor > 0) {
const newPort = await findDevice("serial", "Keyboard detected", device);
const newPort = await findDevice("serial", device);
if (newPort) {
stateUpdate("reset", 100);
searchFor = -1;
Expand Down Expand Up @@ -158,7 +158,9 @@ export const updateFirmware = async (filename: string[], stateUpdate: any, path:
stateUpdate("neuron", 0);

const focus = Focus.getInstance();
if (focus.closed || focus._port === undefined) await focus.open(path, device, null);
if (focus.closed || focus._port === undefined) {
await focus.open(path, device);
}
await new Promise((resolve, reject) => {
arduino.flash(filename, stateUpdate, async (err: any, result: any) => {
if (err) reject(new Error(`Flash error ${result}`));
Expand Down
2 changes: 1 addition & 1 deletion src/api/flash/defyFlasher/NRf52833-flasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { serialConnection, rawCommand, noWaitCommand } from "../serialConnection
import { PACKET_SIZE, TYPE_DAT, TYPE_ELA, TYPE_ESA } from "../flasherConstants";
import { HexType } from "../types";
import ihexDecode from "../ihexDecode";
import { delay } from "../delay";
import { delay } from "../../../main/utils/delay";

let serialPort;

Expand Down
2 changes: 1 addition & 1 deletion src/api/flash/defyFlasher/flash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DeviceClass } from "@Renderer/types/devices";
import { DeviceTools } from "@Renderer/DeviceContext";
import Device, { State } from "src/api/comms/Device";
import Hardware from "../../hardware";
import { delay } from "../delay";
import { delay } from "../../../main/utils/delay";
import NRf52833 from "./NRf52833-flasher";

class FlashDefyWireless {
Expand Down
2 changes: 1 addition & 1 deletion src/api/flash/defyFlasher/rp2040-flasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import log from "electron-log/renderer";
import fs from "fs";
import * as path from "path";
import SideFlaser from "./sideFlasher";
import { delay } from "../delay";
import { delay } from "../../../main/utils/delay";

/**
* Object rp2040 with flash method.
Expand Down
2 changes: 1 addition & 1 deletion src/api/flash/defyFlasher/sideFlasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { crc32 } from "easy-crc";
import log from "electron-log/renderer";
import type { PortInfo } from "@serialport/bindings-cpp";
import { delay } from "../delay";
import { delay } from "../../../main/utils/delay";

const { SerialPort } = eval('require("serialport")');
const { DelimiterParser } = eval('require("@serialport/parser-delimiter")');
Expand Down
65 changes: 34 additions & 31 deletions src/api/flash/findDevice.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import { DygmaDeviceType } from "@Renderer/types/dygmaDefs";
import { DeviceTools } from "@Renderer/DeviceContext";
import log from "electron-log/renderer";
import { DeviceTools } from "@Renderer/DeviceContext";
import { DygmaDeviceType } from "@Renderer/types/dygmaDefs";
import { ExtendedPort } from "../comms/serial/SerialAPI";
import Hardware from "../hardware";

function getDeviceList(deviceType: "serial" | "nonSerial" | "bootloader", desiredDevice?: DygmaDeviceType): DygmaDeviceType[] {
if (deviceType === "serial" && desiredDevice?.info.keyboardType !== undefined) {
return [desiredDevice];
}
return Hardware[deviceType];
}

export const findDevice = async (
deviceType: "serial" | "nonSerial" | "bootloader",
message: string,
desiredDevice?: DygmaDeviceType,
) => {
log.info("Going to list devices");
let devices = Hardware[deviceType] as DygmaDeviceType[];
const bootloader = deviceType === "bootloader";
const list: ExtendedPort[] = (await DeviceTools.enumerateSerial(bootloader)).foundDevices as ExtendedPort[];
if (deviceType === "serial" && desiredDevice?.info.keyboardType !== undefined) devices = [desiredDevice];
log.verbose("List of Devices: ", list);
const detected = list.find(dev => {
log.info("Searching for Device");
let found = false;
devices.forEach(device => {
): Promise<ExtendedPort> => {
log.info(`Going to list devices for type: ${deviceType}`);
const lookupDevices: DygmaDeviceType[] = getDeviceList(deviceType, desiredDevice);

const isBootloader = deviceType === "bootloader";
const hwDevices: ExtendedPort[] = (await DeviceTools.enumerateSerial(isBootloader)).foundDevices;
log.verbose("List of Devices: ", hwDevices);

const matchingDevice: ExtendedPort = hwDevices.find(hwDevice =>
lookupDevices.some(lookupDevice => {
log.info(
`Dev bootloader: ${dev.device.bootloader} & HW: ${bootloader}, Dev KBType: ${device.info.keyboardType} & HW: ${dev.device.info.keyboardType}`,
`Bootloader - Lookup: ${isBootloader} & HW: ${hwDevice.device.bootloader} | KBType - Lookup: ${lookupDevice.info.keyboardType} & HW: ${hwDevice.device.info.keyboardType}`,
);
if (
found !== true && bootloader
? dev.device.bootloader !== undefined &&
dev.device.bootloader === bootloader &&
device.usb.vendorId === dev.device.usb.vendorId &&
device.usb.productId === dev.device.usb.productId
: device.usb.vendorId === dev.device.usb.vendorId &&
device.usb.productId === dev.device.usb.productId &&
device.info.keyboardType === dev.device.info.keyboardType
) {
found = true;

if (lookupDevice.usb.vendorId !== hwDevice.device.usb.vendorId) {
return false;
}
if (lookupDevice.usb.productId !== hwDevice.device.usb.productId) {
return false;
}
if (isBootloader) {
return hwDevice.device.bootloader === true;
}
});
return found;
});
log.info(message, detected);
return detected;
return lookupDevice.info.keyboardType === hwDevice.device.info.keyboardType;
}),
);
log.info(`${deviceType === "serial" ? "keyboard" : deviceType} detected`, matchingDevice);
return matchingDevice;
};
Loading

0 comments on commit c86f383

Please sign in to comment.