From 9becbf9e6a31c6b4bd079ecb9393e262b6b20ca5 Mon Sep 17 00:00:00 2001 From: Frederik Walk Date: Sun, 21 May 2023 22:02:24 +0200 Subject: [PATCH] Move controller input to second core --- include/GlobalConfiguration.h | 2 +- .../peripherals/{Buttons.h => Controller.h} | 6 +-- include/utils/InputState.h | 22 +++++---- src/main.cpp | 23 ++++++---- .../{Buttons.cpp => Controller.cpp} | 46 +++++++++---------- src/utils/InputState.cpp | 39 ++++++++-------- 6 files changed, 75 insertions(+), 63 deletions(-) rename include/peripherals/{Buttons.h => Controller.h} (95%) rename src/peripherals/{Buttons.cpp => Controller.cpp} (64%) diff --git a/include/GlobalConfiguration.h b/include/GlobalConfiguration.h index f44b20d..cf4de62 100644 --- a/include/GlobalConfiguration.h +++ b/include/GlobalConfiguration.h @@ -1,7 +1,7 @@ #ifndef _GLOBALCONFIGURATION_H_ #define _GLOBALCONFIGURATION_H_ -#include "peripherals/Buttons.h" +#include "peripherals/Controller.h" #include "peripherals/Drum.h" #include "peripherals/StatusLed.h" diff --git a/include/peripherals/Buttons.h b/include/peripherals/Controller.h similarity index 95% rename from include/peripherals/Buttons.h rename to include/peripherals/Controller.h index 63e8e0f..7f40bfb 100644 --- a/include/peripherals/Buttons.h +++ b/include/peripherals/Controller.h @@ -1,5 +1,5 @@ -#ifndef _PERIPHERALS_BUTTONS_H_ -#define _PERIPHERALS_BUTTONS_H_ +#ifndef _PERIPHERALS_CONTROLLER_H_ +#define _PERIPHERALS_CONTROLLER_H_ #include "utils/InputState.h" @@ -107,4 +107,4 @@ class Buttons { } // namespace Doncon::Peripherals -#endif // _PERIPHERALS_BUTTONS_H_ \ No newline at end of file +#endif // _PERIPHERALS_CONTROLLER_H_ \ No newline at end of file diff --git a/include/utils/InputState.h b/include/utils/InputState.h index 8596efb..d66dcd4 100644 --- a/include/utils/InputState.h +++ b/include/utils/InputState.h @@ -20,20 +20,24 @@ struct InputState { Pad don_left, ka_left, don_right, ka_right; }; - struct DPad { - bool up, down, left, right; - }; + struct Controller { + struct DPad { + bool up, down, left, right; + }; - struct Buttons { - bool north, east, south, west; - bool l, r; - bool start, select, home, share; + struct Buttons { + bool north, east, south, west; + bool l, r; + bool start, select, home, share; + }; + + DPad dpad; + Buttons buttons; }; public: Drum drum; - DPad dpad; - Buttons buttons; + Controller controller; private: xinput_report_t m_xinput_report; diff --git a/src/main.cpp b/src/main.cpp index 00fb8ca..701264c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "peripherals/Buttons.h" +#include "peripherals/Controller.h" #include "peripherals/Drum.h" #include "peripherals/StatusLed.h" #include "usb/usb_driver.h" @@ -13,31 +13,37 @@ using namespace Doncon; -queue_t input_queue; +queue_t drum_input_queue; +queue_t controller_input_queue; void core1_task() { multicore_lockout_victim_init(); Utils::InputState input_state; + Peripherals::Buttons buttons(Config::Default::button_config); Peripherals::StatusLed led(Config::Default::led_config); while (true) { - if (queue_try_remove(&input_queue, &input_state)) { + buttons.updateInputState(input_state); + + queue_try_add(&controller_input_queue, &input_state.controller); + + if (queue_try_remove(&drum_input_queue, &input_state.drum)) { led.setInputState(input_state); } led.update(); - sleep_ms(1); + // sleep_ms(1); } } int main() { - queue_init(&input_queue, sizeof(Utils::InputState), 1); + queue_init(&drum_input_queue, sizeof(Utils::InputState::Drum), 1); + queue_init(&controller_input_queue, sizeof(Utils::InputState::Controller), 1); Utils::InputState input_state; Peripherals::Drum drum(Config::Default::drum_config); - Peripherals::Buttons buttons(Config::Default::button_config); // Move to core 1? usb_mode_t mode = USB_MODE_XBOX360; usb_driver_init(mode); @@ -48,12 +54,13 @@ int main() { while (true) { drum.updateInputState(input_state); - buttons.updateInputState(input_state); + + queue_try_remove(&controller_input_queue, &input_state.controller); usb_driver_send_and_receive_report(input_state.getReport(mode)); usb_driver_task(); - queue_try_add(&input_queue, &input_state); + queue_try_add(&drum_input_queue, &input_state); } return 0; diff --git a/src/peripherals/Buttons.cpp b/src/peripherals/Controller.cpp similarity index 64% rename from src/peripherals/Buttons.cpp rename to src/peripherals/Controller.cpp index dee3b76..4e15e65 100644 --- a/src/peripherals/Buttons.cpp +++ b/src/peripherals/Controller.cpp @@ -1,4 +1,4 @@ -#include "peripherals/Buttons.h" +#include "peripherals/Controller.h" #include "hardware/gpio.h" #include "pico/time.h" @@ -23,25 +23,25 @@ void Buttons::Button::setState(bool state, uint8_t debounce_delay) { void Buttons::socdClean(Utils::InputState &input_state) { // Last input has priority - if (input_state.dpad.up && input_state.dpad.down) { + if (input_state.controller.dpad.up && input_state.controller.dpad.down) { if (m_socd_state.lastVertical == Id::DOWN) { - input_state.dpad.down = false; + input_state.controller.dpad.down = false; } else if (m_socd_state.lastVertical == Id::UP) { - input_state.dpad.up = false; + input_state.controller.dpad.up = false; } - } else if (input_state.dpad.up) { + } else if (input_state.controller.dpad.up) { m_socd_state.lastVertical = Id::UP; } else { m_socd_state.lastVertical = Id::DOWN; } - if (input_state.dpad.left && input_state.dpad.right) { + if (input_state.controller.dpad.left && input_state.controller.dpad.right) { if (m_socd_state.lastHorizontal == Id::RIGHT) { - input_state.dpad.right = false; + input_state.controller.dpad.right = false; } else if (m_socd_state.lastHorizontal == Id::LEFT) { - input_state.dpad.left = false; + input_state.controller.dpad.left = false; } - } else if (input_state.dpad.left) { + } else if (input_state.controller.dpad.left) { m_socd_state.lastHorizontal = Id::LEFT; } else { m_socd_state.lastHorizontal = Id::RIGHT; @@ -83,20 +83,20 @@ void Buttons::updateInputState(Utils::InputState &input_state) { button.second.setState(gpio_state & button.second.getGpioMask(), m_config.debounce_delay_ms); } - input_state.dpad.up = m_buttons.at(Id::UP).getState(); - input_state.dpad.down = m_buttons.at(Id::DOWN).getState(); - input_state.dpad.left = m_buttons.at(Id::LEFT).getState(); - input_state.dpad.right = m_buttons.at(Id::RIGHT).getState(); - input_state.buttons.north = m_buttons.at(Id::NORTH).getState(); - input_state.buttons.east = m_buttons.at(Id::EAST).getState(); - input_state.buttons.south = m_buttons.at(Id::SOUTH).getState(); - input_state.buttons.west = m_buttons.at(Id::WEST).getState(); - input_state.buttons.l = m_buttons.at(Id::L).getState(); - input_state.buttons.r = m_buttons.at(Id::R).getState(); - input_state.buttons.start = m_buttons.at(Id::START).getState(); - input_state.buttons.select = m_buttons.at(Id::SELECT).getState(); - input_state.buttons.home = m_buttons.at(Id::HOME).getState(); - input_state.buttons.share = m_buttons.at(Id::SHARE).getState(); + input_state.controller.dpad.up = m_buttons.at(Id::UP).getState(); + input_state.controller.dpad.down = m_buttons.at(Id::DOWN).getState(); + input_state.controller.dpad.left = m_buttons.at(Id::LEFT).getState(); + input_state.controller.dpad.right = m_buttons.at(Id::RIGHT).getState(); + input_state.controller.buttons.north = m_buttons.at(Id::NORTH).getState(); + input_state.controller.buttons.east = m_buttons.at(Id::EAST).getState(); + input_state.controller.buttons.south = m_buttons.at(Id::SOUTH).getState(); + input_state.controller.buttons.west = m_buttons.at(Id::WEST).getState(); + input_state.controller.buttons.l = m_buttons.at(Id::L).getState(); + input_state.controller.buttons.r = m_buttons.at(Id::R).getState(); + input_state.controller.buttons.start = m_buttons.at(Id::START).getState(); + input_state.controller.buttons.select = m_buttons.at(Id::SELECT).getState(); + input_state.controller.buttons.home = m_buttons.at(Id::HOME).getState(); + input_state.controller.buttons.share = m_buttons.at(Id::SHARE).getState(); socdClean(input_state); } diff --git a/src/utils/InputState.cpp b/src/utils/InputState.cpp index 05adf69..c7024db 100644 --- a/src/utils/InputState.cpp +++ b/src/utils/InputState.cpp @@ -6,8 +6,9 @@ namespace Doncon::Utils { InputState::InputState() - : drum({{false, 0}, {false, 0}, {false, 0}, {false, 0}}), dpad({false, false, false, false}), - buttons({false, false, false, false, false, false, false, false, false, false}), + : drum({{false, 0}, {false, 0}, {false, 0}, {false, 0}}), + controller( + {{false, false, false, false}, {false, false, false, false, false, false, false, false, false, false}}), m_xinput_report({0x00, sizeof(xinput_report_t), 0, 0, 0, 0, 0, 0, 0, 0, {}}) {} usb_report_t InputState::getReport(usb_mode_t mode) { @@ -29,24 +30,24 @@ usb_report_t InputState::getReport(usb_mode_t mode) { } usb_report_t InputState::getXinputReport() { - m_xinput_report.buttons1 = 0 // - | (dpad.up ? (1 << 0) : 0) // Dpad Up - | ((dpad.down || drum.don_left.triggered) ? (1 << 1) : 0) // Dpad Down - | ((dpad.left || drum.ka_left.triggered) ? (1 << 2) : 0) // Dpad Left - | (dpad.right ? (1 << 3) : 0) // Dpad Right - | (buttons.start ? (1 << 4) : 0) // Start - | (buttons.select ? (1 << 5) : 0) // Select - | (false ? (1 << 6) : 0) // L3 - | (false ? (1 << 7) : 0); // R3 + m_xinput_report.buttons1 = 0 // + | (controller.dpad.up ? (1 << 0) : 0) // Dpad Up + | ((controller.dpad.down || drum.don_left.triggered) ? (1 << 1) : 0) // Dpad Down + | ((controller.dpad.left || drum.ka_left.triggered) ? (1 << 2) : 0) // Dpad Left + | (controller.dpad.right ? (1 << 3) : 0) // Dpad Right + | (controller.buttons.start ? (1 << 4) : 0) // Start + | (controller.buttons.select ? (1 << 5) : 0) // Select + | (false ? (1 << 6) : 0) // L3 + | (false ? (1 << 7) : 0); // R3 - m_xinput_report.buttons2 = 0 // - | (buttons.l ? (1 << 0) : 0) // L1 - | (buttons.r ? (1 << 1) : 0) // R1 - | (buttons.home ? (1 << 2) : 0) // Guide - | ((buttons.south || drum.don_right.triggered) ? (1 << 4) : 0) // A - | ((buttons.east || drum.ka_right.triggered) ? (1 << 5) : 0) // B - | (buttons.west ? (1 << 6) : 0) // X - | (buttons.north ? (1 << 7) : 0); // Y + m_xinput_report.buttons2 = 0 // + | (controller.buttons.l ? (1 << 0) : 0) // L1 + | (controller.buttons.r ? (1 << 1) : 0) // R1 + | (controller.buttons.home ? (1 << 2) : 0) // Guide + | ((controller.buttons.south || drum.don_right.triggered) ? (1 << 4) : 0) // A + | ((controller.buttons.east || drum.ka_right.triggered) ? (1 << 5) : 0) // B + | (controller.buttons.west ? (1 << 6) : 0) // X + | (controller.buttons.north ? (1 << 7) : 0); // Y m_xinput_report.lt = 0; m_xinput_report.rt = 0;