Skip to content

Commit d621b1e

Browse files
ert78gbmondalaci
authored andcommitted
feat(device): Write user configuration into the eeprom (#411)
* feat(device): Write user configuration into the eeprom * when save user config settings wait until is not keyboard busy
1 parent 901a5eb commit d621b1e

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

packages/uhk-agent/src/services/device.service.ts

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@ import { UhkHidDeviceService } from './uhk-hid-device.service';
1919
*/
2020
enum Command {
2121
UploadConfig = 8,
22-
ApplyConfig = 9
22+
ApplyConfig = 9,
23+
LaunchEepromTransfer = 12,
24+
GetKeyboardState = 16
2325
}
2426

27+
enum EepromTransfer {
28+
ReadHardwareConfig = 0,
29+
WriteHardwareConfig = 1,
30+
ReadUserConfig = 2,
31+
WriteUserConfig = 3
32+
}
33+
34+
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
35+
2536
/**
2637
* IpcMain pair of the UHK Communication
2738
* Functionality:
@@ -70,35 +81,63 @@ export class DeviceService {
7081
.subscribe();
7182
}
7283

73-
/**
74-
* IpcMain handler. Send the UserConfiguration to the UHK Device and send a response with the result.
75-
* @param {Electron.Event} event - ipc event
76-
* @param {string} json - UserConfiguration in JSON format
77-
* @returns {Promise<void>}
78-
* @private
79-
*/
8084
private async saveUserConfiguration(event: Electron.Event, json: string): Promise<void> {
8185
const response = new IpcResponse();
8286

8387
try {
84-
const buffer: Buffer = new Buffer(JSON.parse(json).data);
85-
const fragments = this.getTransferBuffers(buffer);
86-
for (const fragment of fragments) {
87-
await this.device.write(fragment);
88-
}
89-
90-
const applyBuffer = new Buffer([Command.ApplyConfig]);
91-
await this.device.write(applyBuffer);
88+
this.sendUserConfigToKeyboard(json);
89+
await this.writeUserConfigToEeprom();
9290
this.device.close();
91+
9392
response.success = true;
94-
this.logService.info('[DeviceService] Transferring finished');
93+
this.logService.info('transferring finished');
9594
}
9695
catch (error) {
9796
this.logService.error('[DeviceService] Transferring error', error);
9897
response.error = {message: error.message};
9998
}
10099

101100
event.sender.send(IpcEvents.device.saveUserConfigurationReply, response);
101+
102+
return Promise.resolve();
103+
}
104+
105+
/**
106+
* IpcMain handler. Send the UserConfiguration to the UHK Device and send a response with the result.
107+
* @param {string} json - UserConfiguration in JSON format
108+
* @returns {Promise<void>}
109+
* @private
110+
*/
111+
private async sendUserConfigToKeyboard(json: string): Promise<void> {
112+
const buffer: Buffer = new Buffer(JSON.parse(json).data);
113+
const fragments = this.getTransferBuffers(buffer);
114+
for (const fragment of fragments) {
115+
await this.device.write(fragment);
116+
}
117+
118+
const applyBuffer = new Buffer([Command.ApplyConfig]);
119+
await this.device.write(applyBuffer);
120+
this.logService.info('[DeviceService] Transferring finished');
121+
}
122+
123+
private async writeUserConfigToEeprom(): Promise<void> {
124+
this.logService.info('[DeviceService] Start write user configuration to eeprom');
125+
126+
const buffer = await this.device.write(new Buffer([Command.LaunchEepromTransfer, EepromTransfer.WriteUserConfig]));
127+
await this.waitUntilKeyboardBusy();
128+
129+
this.logService.info('[DeviceService] End write user configuration to eeprom');
130+
}
131+
132+
private async waitUntilKeyboardBusy(): Promise<void> {
133+
while (true) {
134+
const buffer = await this.device.write(new Buffer([Command.GetKeyboardState]));
135+
if (buffer[1] === 0) {
136+
break;
137+
}
138+
this.logService.debug('Keyboard is busy, wait...');
139+
await snooze(200);
140+
}
102141
}
103142

104143
/**

packages/usb/eeprom.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ if (eepromTransferId === undefined) {
1212
const device = uhk.getUhkDevice();
1313
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransfer, eepromTransferId])));
1414
const buffer = Buffer.from(device.readSync());
15-
if(buffer[1] === 1) {
15+
const responseCode = buffer[0];
16+
if (responseCode !== 0) {
17+
console.error(`Write user config to eeprom failed. Response code: ${responseCode}`);
18+
process.exit(1);
19+
}
20+
21+
function waitUntilKeyboardBusy() {
22+
1623
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getKeyboardState])));
24+
const keyboardStateBuffer = Buffer.from(device.readSync());
25+
26+
if (keyboardStateBuffer[1] === 1) {
27+
setTimeout(waitUntilKeyboardBusy, 200);
28+
}
1729
}
30+
31+
waitUntilKeyboardBusy();

0 commit comments

Comments
 (0)