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:
Róbert Kiss
2017-09-15 00:13:00 +02:00
committed by László Monda
parent 901a5eb5d1
commit d621b1e5e6
2 changed files with 72 additions and 19 deletions

View File

@@ -19,9 +19,20 @@ import { UhkHidDeviceService } from './uhk-hid-device.service';
*/
enum Command {
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
* Functionality:
@@ -70,28 +81,16 @@ export class DeviceService {
.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> {
const response = new IpcResponse();
try {
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.sendUserConfigToKeyboard(json);
await this.writeUserConfigToEeprom();
this.device.close();
response.success = true;
this.logService.info('[DeviceService] Transferring finished');
this.logService.info('transferring finished');
}
catch (error) {
this.logService.error('[DeviceService] Transferring error', error);
@@ -99,6 +98,46 @@ export class DeviceService {
}
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);
}
}
/**

View File

@@ -12,6 +12,20 @@ if (eepromTransferId === undefined) {
const device = uhk.getUhkDevice();
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.launchEepromTransfer, eepromTransferId])));
const buffer = Buffer.from(device.readSync());
if(buffer[1] === 1) {
device.write(uhk.getTransferData(new Buffer([uhk.usbCommands.getKeyboardState])));
const responseCode = buffer[0];
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();