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
This commit is contained in:
committed by
László Monda
parent
901a5eb5d1
commit
d621b1e5e6
@@ -19,9 +19,20 @@ import { UhkHidDeviceService } from './uhk-hid-device.service';
|
|||||||
*/
|
*/
|
||||||
enum Command {
|
enum Command {
|
||||||
UploadConfig = 8,
|
UploadConfig = 8,
|
||||||
ApplyConfig = 9
|
ApplyConfig = 9,
|
||||||
|
LaunchEepromTransfer = 12,
|
||||||
|
GetKeyboardState = 16
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum EepromTransfer {
|
||||||
|
ReadHardwareConfig = 0,
|
||||||
|
WriteHardwareConfig = 1,
|
||||||
|
ReadUserConfig = 2,
|
||||||
|
WriteUserConfig = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IpcMain pair of the UHK Communication
|
* IpcMain pair of the UHK Communication
|
||||||
* Functionality:
|
* Functionality:
|
||||||
@@ -70,28 +81,16 @@ export class DeviceService {
|
|||||||
.subscribe();
|
.subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* IpcMain handler. Send the UserConfiguration to the UHK Device and send a response with the result.
|
|
||||||
* @param {Electron.Event} event - ipc event
|
|
||||||
* @param {string} json - UserConfiguration in JSON format
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private async saveUserConfiguration(event: Electron.Event, json: string): Promise<void> {
|
private async saveUserConfiguration(event: Electron.Event, json: string): Promise<void> {
|
||||||
const response = new IpcResponse();
|
const response = new IpcResponse();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const buffer: Buffer = new Buffer(JSON.parse(json).data);
|
this.sendUserConfigToKeyboard(json);
|
||||||
const fragments = this.getTransferBuffers(buffer);
|
await this.writeUserConfigToEeprom();
|
||||||
for (const fragment of fragments) {
|
|
||||||
await this.device.write(fragment);
|
|
||||||
}
|
|
||||||
|
|
||||||
const applyBuffer = new Buffer([Command.ApplyConfig]);
|
|
||||||
await this.device.write(applyBuffer);
|
|
||||||
this.device.close();
|
this.device.close();
|
||||||
|
|
||||||
response.success = true;
|
response.success = true;
|
||||||
this.logService.info('[DeviceService] Transferring finished');
|
this.logService.info('transferring finished');
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
this.logService.error('[DeviceService] Transferring error', error);
|
this.logService.error('[DeviceService] Transferring error', error);
|
||||||
@@ -99,6 +98,46 @@ export class DeviceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
event.sender.send(IpcEvents.device.saveUserConfigurationReply, response);
|
event.sender.send(IpcEvents.device.saveUserConfigurationReply, response);
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IpcMain handler. Send the UserConfiguration to the UHK Device and send a response with the result.
|
||||||
|
* @param {string} json - UserConfiguration in JSON format
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private async sendUserConfigToKeyboard(json: string): Promise<void> {
|
||||||
|
const buffer: Buffer = new Buffer(JSON.parse(json).data);
|
||||||
|
const fragments = this.getTransferBuffers(buffer);
|
||||||
|
for (const fragment of fragments) {
|
||||||
|
await this.device.write(fragment);
|
||||||
|
}
|
||||||
|
|
||||||
|
const applyBuffer = new Buffer([Command.ApplyConfig]);
|
||||||
|
await this.device.write(applyBuffer);
|
||||||
|
this.logService.info('[DeviceService] Transferring finished');
|
||||||
|
}
|
||||||
|
|
||||||
|
private async writeUserConfigToEeprom(): Promise<void> {
|
||||||
|
this.logService.info('[DeviceService] Start write user configuration to eeprom');
|
||||||
|
|
||||||
|
const buffer = await this.device.write(new Buffer([Command.LaunchEepromTransfer, EepromTransfer.WriteUserConfig]));
|
||||||
|
await this.waitUntilKeyboardBusy();
|
||||||
|
|
||||||
|
this.logService.info('[DeviceService] End write user configuration to eeprom');
|
||||||
|
}
|
||||||
|
|
||||||
|
private async waitUntilKeyboardBusy(): Promise<void> {
|
||||||
|
while (true) {
|
||||||
|
const buffer = await this.device.write(new Buffer([Command.GetKeyboardState]));
|
||||||
|
if (buffer[1] === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
this.logService.debug('Keyboard is busy, wait...');
|
||||||
|
await snooze(200);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,6 +12,20 @@ if (eepromTransferId === undefined) {
|
|||||||
const device = uhk.getUhkDevice();
|
const device = uhk.getUhkDevice();
|
||||||
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransfer, eepromTransferId])));
|
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransfer, eepromTransferId])));
|
||||||
const buffer = Buffer.from(device.readSync());
|
const buffer = Buffer.from(device.readSync());
|
||||||
if(buffer[1] === 1) {
|
const responseCode = buffer[0];
|
||||||
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getKeyboardState])));
|
if (responseCode !== 0) {
|
||||||
|
console.error(`Write user config to eeprom failed. Response code: ${responseCode}`);
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function waitUntilKeyboardBusy() {
|
||||||
|
|
||||||
|
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getKeyboardState])));
|
||||||
|
const keyboardStateBuffer = Buffer.from(device.readSync());
|
||||||
|
|
||||||
|
if (keyboardStateBuffer[1] === 1) {
|
||||||
|
setTimeout(waitUntilKeyboardBusy, 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
waitUntilKeyboardBusy();
|
||||||
|
|||||||
Reference in New Issue
Block a user