PocketPutr/Desktop_Test/input_manager.cpp
2025-12-14 13:00:01 +01:00

95 lines
2.0 KiB
C++

#include "GLOBALS.h"
#include "tft_handler.h"
#include "HardwareSerial.h"
#include "input_manager.h"
void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf, CLICK_EVENT* event) {
_display_state = display_state;
_event = event;
_tf = tf;
mi = {
.x = 5,
.y = 5,
.size_x = 4,
.size_y = 4,
.color = 0x0000,
};
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
}
void InputManager::update() {
for (int i = 0; i < NUM_BUTTONS; i++) {
int reading = digitalRead(BUTTON_PINS[i]);
// Check if button state changed
if (reading != lastButtonState[i]) {
lastDebounceTime[i] = millis();
}
// Only register change after debounce delay
if ((millis() - lastDebounceTime[i]) > DEBOUNCE_DELAY) {
if (reading != buttonState[i]) {
buttonState[i] = reading;
// Button was pressed (went from HIGH to LOW)
if (buttonState[i] == LOW) {
// Add your button handling code here
handle_button_press(i);
}
}
}
lastButtonState[i] = reading;
}
}
void InputManager::handle_button_press(int buttonIndex) {
// Add your custom logic here for each button
switch(buttonIndex) {
case 0:
if (mi.x > 480)
mi.x = 479;
else
mi.x += 5;
break;
case 1:
if (mi.y > 320)
mi.y = 319;
else
mi.y += 5;
break;
case 2:
if (mi.y < 0)
mi.y = 1;
else
mi.y -= 5;
break;
case 3:
if (mi.x < 0)
mi.x = 1;
else
mi.x -= 5;
break;
case 4:
_event->event = CLICK_EVENTS::LEFT_CLICK;
_event->x = mi.x;
_event->y = mi.y;
break;
case 5:
break;
}
_display_state->update_display.store(true);
}
void InputManager::draw_button() {
_tf->draw_box(mi.x, mi.y, mi.size_x, mi.size_y, mi.color);
}
bool InputManager::are_buttons_pressed(int btn1, int btn2) {
return (buttonState[btn1] == LOW && buttonState[btn2] == LOW);
}