code clean up and added comments

This commit is contained in:
2025-05-30 09:17:55 -04:00
parent 5184d5c744
commit 94ea767285
8 changed files with 104 additions and 93 deletions
+20 -6
View File
@@ -7,19 +7,29 @@
#include "parse_keys.h"
#include "usb_descriptors.h"
// take a list of Javascript keys representing pressed keys and turn into
// a 8 byte USB boot keyboard report format
void parse_key_list(char * keys) {
uint8_t keypos = 2;
uint8_t keypos = 2; // bytes 2-7 are used for normal keys
uint8_t code = 0x00;
static unsigned char boot_report[8];
memset(boot_report, 0x00, 8);
// Javascript sends the list as comma delimited, so split into individual
// keys by splitting at commas
char * token = strtok(keys, ",");
while (token != NULL) {
code = parse_key_single(token);
if (code && keypos < 9) {
// add scan code to the boot report unless we have already added
// the 8th byte (6 scan codes) to the report
if (code && keypos < 8) {
boot_report[keypos] = code;
keypos++;
}
// if a scan code was not returned, it might be a modifier
// search for the correct modifier bit and add it to byte 0 of the
// USB boot keyboard report
if (code == 0x00) {
code = parse_mod(token);
if (code) {
@@ -29,6 +39,7 @@ void parse_key_list(char * keys) {
token = strtok(NULL, ",");
}
// print resulting HID boot keyboard report to CDC for debugging
printf("HID report: ");
for(int i=0; i<8; i++){
printf("%02X ",boot_report[i]);
@@ -38,24 +49,27 @@ void parse_key_list(char * keys) {
tud_hid_report(REPORT_ID_KEYBOARD, boot_report, 8);
}
uint8_t parse_key_single(char * key) {
//printf("single key: %s\n", key);
// take a single Javascript key code and convert it to USB HID scancode
// if it is a regular key, i.e. not a modifier key
uint8_t parse_key_single(char * key) {
// search keycode dictionary defined in parse_keys.h to see if it's a
// regular key
for (int i=0; i < NKEYS; i++) {
keycode_dict * keycode = &keytable[i];
if (strcmp(keycode->key, key) == 0){
//printf("key found");
return keycode->val;
}
}
// key not found in lookup table
return 0x00;
}
// parse modifier keys and turn them into the appropriate modifier bit code
uint8_t parse_mod(char * key) {
for (int i=0; i < NMODS; i++) {
keycode_dict * modcode = &modtable[i];
if (strcmp(modcode->key, key) == 0){
//printf("key found");
return modcode->val;
}
}