@@ -19,9 +19,20 @@ import { UhkHidDeviceService } from './uhk-hid-device.service';
1919 */
2020enum 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 /**
0 commit comments