diff --git a/Desktop_Test/GLOBALS.h b/Desktop_Test/GLOBALS.h index d0a4b09..029ce05 100644 --- a/Desktop_Test/GLOBALS.h +++ b/Desktop_Test/GLOBALS.h @@ -17,11 +17,13 @@ enum CLICK_EVENTS : uint8_t { NONE = 0, LEFT_CLICK = 1, RIGHT_CLICK = 2, + KEYBOARD = 3, }; struct CLICK_EVENT { short x; short y; + char character; CLICK_EVENTS event; }; diff --git a/Desktop_Test/example_programs.h b/Desktop_Test/example_programs.h index 8c3c173..046197e 100644 --- a/Desktop_Test/example_programs.h +++ b/Desktop_Test/example_programs.h @@ -105,11 +105,7 @@ public: 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 (c_button.action == "show resources") { - for (short j = 0; j < _window->content_buttons.size(); ++j) { - _window->content_buttons[j].pressed = false; - } - - Serial.println("I'm tired, boss..."); + for (short j = 0; j < _window->content_buttons.size(); ++j) _window->content_buttons[j].pressed = false; _window->content_buttons[i].pressed = true; _display_state->update_display.store(true); @@ -117,11 +113,7 @@ public: } if (c_button.action == "show tasks") { - for (short j = 0; j < _window->content_buttons.size(); ++j) { - _window->content_buttons[j].pressed = false; - } - - Serial.println("ok..."); + for (short j = 0; j < _window->content_buttons.size(); ++j) _window->content_buttons[j].pressed = false; _window->content_buttons[i].pressed = true; _display_state->update_display.store(true); diff --git a/Desktop_Test/input_manager.cpp b/Desktop_Test/input_manager.cpp index 73587d2..94ac43c 100644 --- a/Desktop_Test/input_manager.cpp +++ b/Desktop_Test/input_manager.cpp @@ -1,8 +1,10 @@ #include "event_manager.h" #include "GLOBALS.h" #include "tft_handler.h" -#include "HardwareSerial.h" #include "input_manager.h" +#include + +#define CARDKB_ADDR 0x5F // M5Stack CardKB I2C address void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf) { _display_state = display_state; @@ -16,6 +18,8 @@ void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf) { .color = 0x0000, }; + Wire.begin(); + for (int i = 0; i < NUM_BUTTONS; i++) { pinMode(BUTTON_PINS[i], INPUT_PULLUP); } @@ -41,6 +45,91 @@ void InputManager::update() { 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) { diff --git a/Desktop_Test/input_manager.h b/Desktop_Test/input_manager.h index afe4536..bc5c4cb 100644 --- a/Desktop_Test/input_manager.h +++ b/Desktop_Test/input_manager.h @@ -18,6 +18,7 @@ private: TFT_Handler* _tf; Mouse_Icon mi; + void handle_keyboard_input(char key); void handle_button_press(short buttonIndex); bool are_buttons_pressed(short btn1, short btn2);