IT WORKED
This commit is contained in:
parent
9685b53bf1
commit
27d6c45a16
@ -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;
|
||||
};
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
#include "event_manager.h"
|
||||
#include "GLOBALS.h"
|
||||
#include "tft_handler.h"
|
||||
#include "HardwareSerial.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) {
|
||||
_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) {
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user