Feat: Keyboard strokes working and in buffer

This commit is contained in:
AfonsoCMSousa
2026-08-05 02:23:54 +01:00
parent fb27ad1e0c
commit ba16f38c79
6 changed files with 161 additions and 56 deletions
+59 -10
View File
@@ -4,6 +4,7 @@
#include "types.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
@@ -21,9 +22,9 @@
#define SCANCODE_CAPSLOCK 0x3A
#define RELEASE_MASK 0x80
static char _keyboard_buffer[KEYBOARD_BUFFER_SIZE];
static uint16_t _kb_head = 0;
static uint16_t _kb_tail = 0;
static volatile unsigned char _keyboard_buffer[KEYBOARD_BUFFER_SIZE];
static volatile uint16_t _kb_head = 0;
static volatile uint16_t _kb_tail = 0;
static char __g_shift_pressed = 0;
static char __g_caps_lock_active = 0;
@@ -396,7 +397,7 @@ static inline char __keyboard_buffer_pop() {
}
static inline char __convert_ascii(uint8_t __scancode) {
char __code = __scancode & ~RELEASE_MASK;
unsigned char __code = __scancode & ~RELEASE_MASK;
if (__scancode & 0x80) {
// Key-released
if (__code == SCANCODE_LSHIFT || __code == SCANCODE_RSHIFT) {
@@ -434,12 +435,61 @@ static inline char __convert_ascii(uint8_t __scancode) {
return __char;
}
uint8_t __scancode_h[128] = {0};
static uint32_t __total_isr_calls = 0;
void __keyboard_handle_scancode(uint8_t __scancode) {
char __temp = __convert_ascii(__scancode);
if (__temp == '\0') {
return;
}
__keyboard_buffer_push(__temp);
__total_isr_calls++;
char __temp = __convert_ascii(__scancode);
static uint8_t __index = 0;
if (__index < sizeof(__scancode_h)) {
__scancode_h[__index++] = __scancode;
}
if (__temp == '\0') {
return;
}
__keyboard_buffer_push(__temp);
}
void __dump_scancode_history() {
printf("Scancode History: \n");
printf("Total ISR Calls: %u\n", __total_isr_calls);
for (size_t i = 0; i < sizeof(__scancode_h); i++) {
if (__scancode_h[i] != 0) {
printf("[%x] = %c\n", __scancode_h[i], __convert_ascii(__scancode_h[i]));
}
}
printf("\n");
}
static inline char __gets_internal(char *buffer, size_t max_length) {
size_t index = 0;
char __c;
while (index < max_length - 1) {
__c = __keyboard_buffer_pop();
if (__c == '\0') {
continue;
}
if (__c == '\n' || __c == '\r') {
buffer[index] = '\0';
break;
}
buffer[index++] = __c;
}
buffer[index] = '\0';
return index;
}
void gets(char *buffer, size_t max_length) {
__gets_internal(buffer, max_length);
}
/*
@@ -509,7 +559,6 @@ int scanf(const char *fmt, ...) {
va_end(args);
return result;
}
*/
#endif