I'm working on USB HID keyboard, using TinyUSB, and I'm encountering an error when sending strings that included chars with shift modifier.
I'm parsing an ASCII string char by char, converted using a array. I send HID key reports in batches of 6 keys and is send when one of these conditions is true:
- The buffer reaches 6
- The next char have different modifier
- I'ts the last char in data
For the string "hapa.pt", I expect it the full string to be typed and it work when I send keycode individually, but when I send together in the array of 6 I get this: "hap.t"
I'm trying to understand if the problem is in my logic. Can someone help me understand why this happens? Thanks!
Code: Select all
void read_line() {
uint8_t *data = malloc(512);
if(!data) return;
int len = uart_read_bytes(UART_NUM_1, data, (512 - 1), pdMS_TO_TICKS(500));
if(len > 0) {
int counter = 0;
uint8_t modifier = 0;
uint8_t keycode[6] = {0};
for (int i = 0; i < len; i++) {
uint8_t entry = ascii2keycode[(uint8_t)data[i]];
modifier = (entry & 0x80) ? KEYBOARD_MODIFIER_LEFTSHIFT : 0x00; // 0x00
uint8_t next = ((ascii2keycode[(uint8_t)data[i+1]]) & 0x80) ? KEYBOARD_MODIFIER_LEFTSHIFT : 0x00;
if(counter < 6) {
keycode[counter] = entry & 0x7F;
vTaskDelay(pdMS_TO_TICKS(20));
counter++;
}
if(counter == 6 || modifier != next || i + 1 == len) {
tud_hid_keyboard_report(HID_ITF_PROTOCOL_KEYBOARD, modifier, keycode);
vTaskDelay(pdMS_TO_TICKS(20));
tud_hid_keyboard_report(HID_ITF_PROTOCOL_KEYBOARD, 0, NULL);
vTaskDelay(pdMS_TO_TICKS(20));
counter = 0;
memset(keycode, '\0', sizeof(keycode));
}
}
}
free(data);
data = NULL;
return;
}