IT WORKED

This commit is contained in:
Rasmus Rasmussen 2026-01-14 20:33:45 +01:00
parent 9685b53bf1
commit 27d6c45a16
4 changed files with 95 additions and 11 deletions

View File

@ -17,11 +17,13 @@ enum CLICK_EVENTS : uint8_t {
NONE = 0, NONE = 0,
LEFT_CLICK = 1, LEFT_CLICK = 1,
RIGHT_CLICK = 2, RIGHT_CLICK = 2,
KEYBOARD = 3,
}; };
struct CLICK_EVENT { struct CLICK_EVENT {
short x; short x;
short y; short y;
char character;
CLICK_EVENTS event; CLICK_EVENTS event;
}; };

View File

@ -105,11 +105,7 @@ public:
auto c_button = _window->content_buttons[i]; auto c_button = _window->content_buttons[i];
if (event.x >= c_button.x && event.x <= (c_button.x + c_button.width) && event.y >= c_button.y && event.y <= (c_button.y + c_button.height)) { if (event.x >= c_button.x && event.x <= (c_button.x + c_button.width) && event.y >= c_button.y && event.y <= (c_button.y + c_button.height)) {
if (c_button.action == "show resources") { if (c_button.action == "show resources") {
for (short j = 0; j < _window->content_buttons.size(); ++j) { for (short j = 0; j < _window->content_buttons.size(); ++j) _window->content_buttons[j].pressed = false;
_window->content_buttons[j].pressed = false;
}
Serial.println("I'm tired, boss...");
_window->content_buttons[i].pressed = true; _window->content_buttons[i].pressed = true;
_display_state->update_display.store(true); _display_state->update_display.store(true);
@ -117,11 +113,7 @@ public:
} }
if (c_button.action == "show tasks") { if (c_button.action == "show tasks") {
for (short j = 0; j < _window->content_buttons.size(); ++j) { for (short j = 0; j < _window->content_buttons.size(); ++j) _window->content_buttons[j].pressed = false;
_window->content_buttons[j].pressed = false;
}
Serial.println("ok...");
_window->content_buttons[i].pressed = true; _window->content_buttons[i].pressed = true;
_display_state->update_display.store(true); _display_state->update_display.store(true);

View File

@ -1,8 +1,10 @@
#include "event_manager.h" #include "event_manager.h"
#include "GLOBALS.h" #include "GLOBALS.h"
#include "tft_handler.h" #include "tft_handler.h"
#include "HardwareSerial.h"
#include "input_manager.h" #include "input_manager.h"
#include <Wire.h>
#define CARDKB_ADDR 0x5F // M5Stack CardKB I2C address
void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf) { void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf) {
_display_state = display_state; _display_state = display_state;
@ -16,6 +18,8 @@ void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf) {
.color = 0x0000, .color = 0x0000,
}; };
Wire.begin();
for (int i = 0; i < NUM_BUTTONS; i++) { for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP); pinMode(BUTTON_PINS[i], INPUT_PULLUP);
} }
@ -41,6 +45,91 @@ void InputManager::update() {
lastButtonState[i] = reading; lastButtonState[i] = reading;
} }
Wire.requestFrom(CARDKB_ADDR, 1);
if (Wire.available()) {
char c = Wire.read();
if (c != 0) {
handle_keyboard_input(c);
}
}
}
void InputManager::handle_keyboard_input(char key) {
bool needs_redraw = false;
Serial.printf("key: %x\n", key & 0xff);
CLICK_EVENT event;
switch(key) {
case 'd': // Right arrow alternative
case 'D':
mi.x = (mi.x + 5 > 476) ? 476 : mi.x + 5;
needs_redraw = true;
break;
case 's': // Down arrow alternative
case 'S':
mi.y = (mi.y + 5 > 316) ? 316 : mi.y + 5;
needs_redraw = true;
break;
case 'w': // Up arrow alternative
case 'W':
mi.y = (mi.y - 5 < 0) ? 0 : mi.y - 5;
needs_redraw = true;
break;
case 'a': // Left arrow alternative
case 'A':
mi.x = (mi.x - 5 < 0) ? 0 : mi.x - 5;
needs_redraw = true;
break;
case 0xB4: // Left arrow key (CardKB sends special codes)
mi.x = (mi.x - 5 < 0) ? 0 : mi.x - 5;
needs_redraw = true;
break;
case 0xB7: // Right arrow key
mi.x = (mi.x + 5 > 476) ? 476 : mi.x + 5;
needs_redraw = true;
break;
case 0xB5: // Up arrow key
mi.y = (mi.y - 5 < 0) ? 0 : mi.y - 5;
needs_redraw = true;
break;
case 0xB6: // Down arrow key
mi.y = (mi.y + 5 > 316) ? 316 : mi.y + 5;
needs_redraw = true;
break;
case 0xD: // Enter key - left click
case ' ':
event.x = mi.x;
event.y = mi.y;
event.event = CLICK_EVENTS::LEFT_CLICK;
break;
case 'r': // 'R' key - right click
case 'R':
event.x = mi.x;
event.y = mi.y;
event.event = CLICK_EVENTS::RIGHT_CLICK;
break;
default:
break;
}
EventManager::getInstance().publish(event);
if (needs_redraw) {
_display_state->update_display.store(true);
}
} }
void InputManager::handle_button_press(short buttonIndex) { void InputManager::handle_button_press(short buttonIndex) {

View File

@ -18,6 +18,7 @@ private:
TFT_Handler* _tf; TFT_Handler* _tf;
Mouse_Icon mi; Mouse_Icon mi;
void handle_keyboard_input(char key);
void handle_button_press(short buttonIndex); void handle_button_press(short buttonIndex);
bool are_buttons_pressed(short btn1, short btn2); bool are_buttons_pressed(short btn1, short btn2);