Extended config handling so now multiple macros can be read in correctly

This commit is contained in:
_N0x 2022-02-21 00:03:24 +01:00
parent 4818c739b5
commit 93815d9ed2
6 changed files with 148 additions and 136 deletions

View File

@ -7,6 +7,7 @@
#define DEFINES_H_ #define DEFINES_H_
/* THESE ARE THE AVALIABLE KEYS FOR THE "MOD_KEYS" PARAMETER */ /* THESE ARE THE AVALIABLE KEYS FOR THE "MOD_KEYS" PARAMETER */
#define MODKEY_NONE 0x00
#define MODKEY_LCTR 0x01 #define MODKEY_LCTR 0x01
#define MODKEY_LSHI 0x02 #define MODKEY_LSHI 0x02
#define MODKEY_LALT 0x03 #define MODKEY_LALT 0x03
@ -25,6 +26,7 @@
#define MODKEY_RALT_RCTR_RSHI 0x10 #define MODKEY_RALT_RCTR_RSHI 0x10
/* THESE ARE THE AVAIRABLE KEYS FOR THE "KEYS" PARAMETER */ /* THESE ARE THE AVAIRABLE KEYS FOR THE "KEYS" PARAMETER */
#define KEY_NONE 0x00
#define KEY_ErrorRollOver 0x01 #define KEY_ErrorRollOver 0x01
#define KEY_POSTFail 0x02 #define KEY_POSTFail 0x02
#define KEY_ErrorUndefined 0x03 #define KEY_ErrorUndefined 0x03

View File

@ -1,7 +1,4 @@
#include "../include/defines.h"
#include <stdint.h> #include <stdint.h>
#include <string.h>
#ifndef KEYCODE_CONV_H_ #ifndef KEYCODE_CONV_H_
#define KEYCODE_CONV_H_ #define KEYCODE_CONV_H_
@ -16,6 +13,5 @@ struct keyCode {
{ KC, #KC } { KC, #KC }
uint8_t get_keycode_by_name(char *key_name); uint8_t get_keycode_by_name(char *key_name);
uint8_t get_modkeycode_by_name(char *modkey_name);
#endif /* KEYCODE_CONV_H_ */ #endif /* KEYCODE_CONV_H_ */

View File

@ -6,6 +6,7 @@
#include <cjson/cJSON.h> #include <cjson/cJSON.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
/* define declarations */ /* define declarations */
@ -29,6 +30,8 @@ enum MacroNumber {
/* method declarations */ /* method declarations */
char *read_in_config(const char *conf_name); char *read_in_config(const char *conf_name);
void set_action(const cJSON *action, f8_macro_actionP pma);
uint8_t get_key_from_json(cJSON *key);
f8_macroP * f8_macroP *
get_f8_macros(char *conf_name) { get_f8_macros(char *conf_name) {
@ -45,7 +48,10 @@ get_f8_macros(char *conf_name) {
const cJSON *macro = NULL; const cJSON *macro = NULL;
/* pmarse the read in config file */ /* pmarse the read in config file */
cJSON *config_json = cJSON_Parse(read_in_config(conf_name)); char *conf = read_in_config(conf_name);
cJSON *config_json = cJSON_Parse(conf);
free(conf);
if (config_json == NULL) { if (config_json == NULL) {
const char *error_ptr = cJSON_GetErrorPtr(); const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) { if (error_ptr != NULL) {
@ -55,11 +61,12 @@ get_f8_macros(char *conf_name) {
} }
/* READ IN VALUES */ /* READ IN VALUES */
int i = 0;
macros = cJSON_GetObjectItemCaseSensitive(config_json, JO_MACROS); macros = cJSON_GetObjectItemCaseSensitive(config_json, JO_MACROS);
cJSON_ArrayForEach(macro, macros) { cJSON_ArrayForEach(macro, macros) {
/* create new macro-pointer */ /* create new macro-pointer */
f8_macroP pm = calloc(1, sizeof(*pm)); f8_macroP pm = return_macros[i++];
/* Get the macro number from the JSON and set the offest */ /* Get the macro number from the JSON and set the offest */
cJSON *macro_no = cJSON_GetObjectItemCaseSensitive(macro, JO_MACRO_NO); cJSON *macro_no = cJSON_GetObjectItemCaseSensitive(macro, JO_MACRO_NO);
@ -98,57 +105,64 @@ get_f8_macros(char *conf_name) {
break; break;
} }
printf("For Macro %d the values are\r\n",
(int)cJSON_GetNumberValue(macro_no));
/* Move through all the actions in the current macro and set the values /* Move through all the actions in the current macro and set the values
*/ */
cJSON *action = NULL; cJSON *action = NULL;
cJSON *actions = cJSON_GetObjectItemCaseSensitive(macro, JO_ACTIONS); cJSON *actions = cJSON_GetObjectItemCaseSensitive(macro, JO_ACTIONS);
int i = 0; int j = 0;
cJSON_ArrayForEach(action, actions) { cJSON_ArrayForEach(action, actions) {
printf(" >> Action %d\r\n", j);
f8_macro_actionP pma = calloc(1, sizeof(*pma)); f8_macro_actionP pma = calloc(1, sizeof(*pma));
cJSON *mod_keys = set_action(action, pma);
cJSON_GetObjectItemCaseSensitive(action, JO_MOD_KEYS); /* append the new action to the macro */
// cJSON *delay = cJSON_GetObjectItemCaseSensitive(action, pm->actions[j++] = pma;
// JO_DELAY); }
}
cJSON_Delete(config_json);
return return_macros;
}
void
set_action(const cJSON *action, f8_macro_actionP pma) {
cJSON *mod_keys = cJSON_GetObjectItemCaseSensitive(action, JO_MOD_KEYS);
cJSON *delay = cJSON_GetObjectItemCaseSensitive(action, JO_DELAY);
cJSON *keys = cJSON_GetObjectItemCaseSensitive(action, JO_KEYS); cJSON *keys = cJSON_GetObjectItemCaseSensitive(action, JO_KEYS);
printf(" >> %s\r\n", pma->modifier = get_key_from_json(mod_keys);
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 0)));
printf(" >> %s\r\n",
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 1)));
printf(" >> %s\r\n",
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 2)));
printf(" >> %s\r\n",
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 3)));
printf(" >> %s\r\n",
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 4)));
printf(" >> %s\r\n",
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 5)));
pma->modifier = printf(" >> Delay: %.2f\r\n", cJSON_GetNumberValue(delay));
get_modkeycode_by_name(cJSON_GetStringValue(mod_keys)); pma->delay = 0x00; // TODO: Set actual value from JSON
pma->delay = 0x00; // TODO: Set acutal value from JSON
pma->action1 = get_keycode_by_name(
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 0)));
pma->action2 = get_keycode_by_name(
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 1)));
pma->action3 = get_keycode_by_name(
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 2)));
pma->action4 = get_keycode_by_name(
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 3)));
pma->action5 = get_keycode_by_name(
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 4)));
pma->action6 = get_keycode_by_name(
cJSON_GetStringValue(cJSON_GetArrayItem(keys, 5)));
/* append the new action to the macro */ pma->action1 = get_key_from_json(cJSON_GetArrayItem(keys, 0));
pm->actions[i++] = pma; pma->action2 = get_key_from_json(cJSON_GetArrayItem(keys, 1));
} pma->action3 = get_key_from_json(cJSON_GetArrayItem(keys, 2));
pma->action4 = get_key_from_json(cJSON_GetArrayItem(keys, 3));
pma->action5 = get_key_from_json(cJSON_GetArrayItem(keys, 4));
pma->action6 = get_key_from_json(cJSON_GetArrayItem(keys, 5));
} }
return return_macros; /* returns the key-code from the given string. If no keycode is matched or the
* string is empty keycode 0x00 will be supplied */
uint8_t
get_key_from_json(cJSON *key) {
uint8_t key_val = 0x00; /* default the key value to zero */
if (strcmp(cJSON_GetStringValue(key), "") != 0) {
key_val = get_keycode_by_name(cJSON_GetStringValue(key));
}
printf(" >> Got keyvalue 0x%02x for key \"%s\"\r\n", key_val,
cJSON_GetStringValue(key));
return key_val;
} }
char * char *
@ -168,6 +182,7 @@ read_in_config(const char *conf_name) {
char *config = malloc(sb.st_size); char *config = malloc(sb.st_size);
fread(config, sb.st_size, 1, fp); fread(config, sb.st_size, 1, fp);
fclose(fp);
return config; return config;
} }

View File

@ -30,9 +30,6 @@ get_firmware_buffer(char *filename) {
p_fb->buffer = malloc(p_fb->size); p_fb->buffer = malloc(p_fb->size);
fread(p_fb->buffer, p_fb->size, 1, firmware); fread(p_fb->buffer, p_fb->size, 1, firmware);
/* for testing if the buffer could be read correclty */
// printf("buffer size: %d", p_fb->size);
fclose(firmware); fclose(firmware);
return p_fb; return p_fb;
@ -61,8 +58,7 @@ void
set_program(f_bffrP p_fb, f8_macroP mp) { set_program(f_bffrP p_fb, f8_macroP mp) {
for (int i = 0; i < MAX_ACTION; i++) { for (int i = 0; i < MAX_ACTION; i++) {
// Check if an action for that position exists // Check if an action for that position exists
if (mp) { if (mp && mp->actions[i]) {
if (mp->actions[i]) {
/* calculate the offset for the action /* calculate the offset for the action
* -> general prog_offset + current prog_action i times bytes * -> general prog_offset + current prog_action i times bytes
* per action * per action
@ -70,25 +66,23 @@ set_program(f_bffrP p_fb, f8_macroP mp) {
int actionpointer = mp->offset + (i * sizeof(*mp->actions[0])); int actionpointer = mp->offset + (i * sizeof(*mp->actions[0]));
/* set the modifiers */ /* set the modifiers */
memset(p_fb->buffer + actionpointer + 0, memset(p_fb->buffer + actionpointer + 0, mp->actions[i]->modifier,
mp->actions[i]->modifier, 1);
/* set timing delay */
memset(p_fb->buffer + actionpointer + 1, mp->actions[i]->delay,
1); 1);
/* set timing delay */
memset(p_fb->buffer + actionpointer + 1, mp->actions[i]->delay, 1);
/* set action 1-6 */ /* set action 1-6 */
memset(p_fb->buffer + actionpointer + 2, memset(p_fb->buffer + actionpointer + 2, mp->actions[i]->action1,
mp->actions[i]->action1, 1); 1);
memset(p_fb->buffer + actionpointer + 3, memset(p_fb->buffer + actionpointer + 3, mp->actions[i]->action2,
mp->actions[i]->action2, 1); 1);
memset(p_fb->buffer + actionpointer + 4, memset(p_fb->buffer + actionpointer + 4, mp->actions[i]->action3,
mp->actions[i]->action3, 1); 1);
memset(p_fb->buffer + actionpointer + 5, memset(p_fb->buffer + actionpointer + 5, mp->actions[i]->action4,
mp->actions[i]->action4, 1); 1);
memset(p_fb->buffer + actionpointer + 6, memset(p_fb->buffer + actionpointer + 6, mp->actions[i]->action5,
mp->actions[i]->action5, 1); 1);
memset(p_fb->buffer + actionpointer + 7, memset(p_fb->buffer + actionpointer + 7, mp->actions[i]->action6,
mp->actions[i]->action6, 1); 1);
}
} }
} }
} }

View File

@ -1,9 +1,13 @@
#include "../include/keycodes_conv.h" #include "../include/keycodes_conv.h"
#include "../include/defines.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
const keyCode keycodes[] = { const keyCode keycodes[] = {
KEYCODE(KEY_NONE),
KEYCODE(KEY_ErrorRollOver), KEYCODE(KEY_ErrorRollOver),
KEYCODE(KEY_POSTFail), KEYCODE(KEY_POSTFail),
KEYCODE(KEY_ErrorUndefined), KEYCODE(KEY_ErrorUndefined),
@ -222,41 +226,34 @@ const keyCode keycodes[] = {
KEYCODE(KEY_RightShift), KEYCODE(KEY_RightShift),
KEYCODE(KEY_RightAlt), KEYCODE(KEY_RightAlt),
KEYCODE(KEY_RightGUI), KEYCODE(KEY_RightGUI),
};
const keyCode modkeycodes[] = { /* SPECIAL KEYCODES FOR THE MODIFIFER KEY */
KEYCODE(MODKEY_LCTR), KEYCODE(MODKEY_LSHI), KEYCODE(MODKEY_NONE),
KEYCODE(MODKEY_LALT), KEYCODE(MODKEY_LWIN), KEYCODE(MODKEY_LCTR),
KEYCODE(MODKEY_RCTR), KEYCODE(MODKEY_RSHI), KEYCODE(MODKEY_LSHI),
KEYCODE(MODKEY_RALT), KEYCODE(MODKEY_RWIN), KEYCODE(MODKEY_LALT),
KEYCODE(MODKEY_RWIN_RSHI), KEYCODE(MODKEY_RWIN_RCTR), KEYCODE(MODKEY_LWIN),
KEYCODE(MODKEY_RWIN_RALT), KEYCODE(MODKEY_RWIN_RCTR_RSHI), KEYCODE(MODKEY_RCTR),
KEYCODE(MODKEY_RCTR_RALT), KEYCODE(MODKEY_RCTR_RSHI), KEYCODE(MODKEY_RSHI),
KEYCODE(MODKEY_RALT_RSHI), KEYCODE(MODKEY_RALT_RCTR_RSHI), KEYCODE(MODKEY_RALT),
KEYCODE(MODKEY_RWIN),
KEYCODE(MODKEY_RWIN_RSHI),
KEYCODE(MODKEY_RWIN_RCTR),
KEYCODE(MODKEY_RWIN_RALT),
KEYCODE(MODKEY_RWIN_RCTR_RSHI),
KEYCODE(MODKEY_RCTR_RALT),
KEYCODE(MODKEY_RCTR_RSHI),
KEYCODE(MODKEY_RALT_RSHI),
KEYCODE(MODKEY_RALT_RCTR_RSHI),
}; };
uint8_t uint8_t
get_keycode_by_name(char *key_name) { get_keycode_by_name(char *key_name) {
uint8_t keycode = -1; uint8_t keycode = 0x00;
for (int i = 0; i < (sizeof(keycodes) / sizeof(keycodes[0])); i++) { for (int i = 0; i < (sizeof(keycodes) / sizeof(keycodes[0])); i++) {
if (strcmp(keycodes[i].key_name, key_name) == 0) { if (strcmp(keycodes[i].key_name, key_name) == 0) {
keycode = keycodes[i].key_code; keycode = keycodes[i].key_code;
printf(" >> Getting keycode for %s: 0x%02x\r\n", key_name, keycode);
break;
}
}
return keycode;
}
uint8_t
get_modkeycode_by_name(char *modkey_name) {
uint8_t keycode = -1;
for (int i = 0; i < (sizeof(modkeycodes) / sizeof(modkeycodes[0])); i++) {
if (strcmp(modkeycodes[i].key_name, modkey_name) == 0) {
keycode = modkeycodes[i].key_code;
break; break;
} }
} }

View File

@ -11,23 +11,31 @@ void testing(f_bffrP p_fb);
char *updated_file_name(char *orig); char *updated_file_name(char *orig);
int int
main(int argc, char *argv[]) { main(int argc, char *argv
[]) {
/* Read in the firmware file into a f_bffr_t struct */ /* Read in the firmware file into a f_bffr_t struct */
f_bffrP p_fb = get_firmware_buffer(argv[1]); f_bffrP p_fb = get_firmware_buffer(argv[1]);
/* Call testing method */ /* Call testing method */
// testing(p_fb); // testing(p_fb);
f8_macroP *my_macros = get_f8_macros("config.json"); f8_macroP *my_macros = get_f8_macros(argv[2]);
int len = sizeof(my_macros); int len = sizeof(my_macros);
printf("Length of my_macros: %d\r\n", len); printf("Length of my_macros: %d\r\n", len);
for (int i = 0; i < len; i++) { for (int i = 0; i < len;
i++) {
set_program(p_fb, my_macros[i]); set_program(p_fb, my_macros[i]);
free(my_macros[i]);
} }
write_firmware_buffer(updated_file_name(argv[1]), p_fb); char *new_name = updated_file_name(argv[1]);
write_firmware_buffer(new_name, p_fb);
free(new_name);
free(p_fb->buffer); free(p_fb->buffer);
free(p_fb); free(p_fb);