From dbbec16acf9922602344fe53ebf3f12c037e422b Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 2 Jan 2024 20:18:40 -0600 Subject: [PATCH] Lift RawController code --- .../PoolBox/application/src/font_writer.cpp | 3 +- examples/PoolBox/application/src/main.cpp | 5 +- include/PSX/Periphery/periphery.hpp | 10 ++ include/PSX/Periphery/raw_controller.hpp | 135 ++++++++++++++++++ src/Library/src/Periphery/periphery.cpp | 12 ++ 5 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 include/PSX/Periphery/periphery.hpp create mode 100644 include/PSX/Periphery/raw_controller.hpp create mode 100644 src/Library/src/Periphery/periphery.cpp diff --git a/examples/PoolBox/application/src/font_writer.cpp b/examples/PoolBox/application/src/font_writer.cpp index 77ca0faf..1b480c2b 100644 --- a/examples/PoolBox/application/src/font_writer.cpp +++ b/examples/PoolBox/application/src/font_writer.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -31,7 +32,7 @@ void font_writer_update() { auto state = JabyEngine::State::create(JabyEngine::Make::PositionI16(8, 8), wiggle_count); new_font_writer.write(state, "012345 ABCDEFGHIJKL\nabcedfghijkl\n", JabyEngine::GPU::Color24::Blue(), &wiggle); new_font_writer.write(state, "%i (0x%p)\nWiggle (%s)\n", JabyEngine::GPU::Color24::Green(), &wiggle, wiggle_count, 0xAABBCCDD, Text[wiggle_count&0x1]); - bios_font_writer.write(state, "!!PLANSCHBECKEN!!", JabyEngine::GPU::Color24::White()); + bios_font_writer.write(state, "!!PLANSCHBECKEN!!", JabyEngine::Periphery::controller[0][0].button.is_down(JabyEngine::Periphery::GenericButton::D7) ? JabyEngine::GPU::Color24::Blue() : JabyEngine::GPU::Color24::White()); if(timer.is_expired_for(50_ms)) { timer.reset(); diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index f9f35972..bf3d5e6d 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -5,6 +5,7 @@ #include "assets.hpp" #include #include +#include #include #include @@ -22,6 +23,8 @@ static void setup() { } static void update() { + Periphery::query_controller(); + FontWriter::FontWriter cursor; const auto end_pos = cursor.write(FontWriter::Position::create(0, 32), "Cody is cute\n&\na \x1b[8;0;0mBAAAAABY!!!"); @@ -45,7 +48,7 @@ void main() { JabyEngine::HighResTime::enable(); while(true) { - const auto start = Overlay::TimerTest::start_measuring(); + const auto start = Overlay::TimerTest::start_measuring(); update(); render(); Overlay::TimerTest::end_measuring(start, GPU::Display::frames_per_sec); diff --git a/include/PSX/Periphery/periphery.hpp b/include/PSX/Periphery/periphery.hpp new file mode 100644 index 00000000..40175930 --- /dev/null +++ b/include/PSX/Periphery/periphery.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "raw_controller.hpp" + +namespace JabyEngine { + namespace Periphery { + extern RawController controller[1][1]; + + void query_controller(); + } +} \ No newline at end of file diff --git a/include/PSX/Periphery/raw_controller.hpp b/include/PSX/Periphery/raw_controller.hpp new file mode 100644 index 00000000..bfcc7884 --- /dev/null +++ b/include/PSX/Periphery/raw_controller.hpp @@ -0,0 +1,135 @@ +#pragma once +#include "../jabyengine_defines.h" + +namespace JabyEngine { + namespace Periphery { + enum struct ControllerType : uint8_t { + Unkown = 0x0, + Mouse = 0x1, + NegCon = 0x2, + HyperBlaster = 0x3, // Konami Lightgun + Controller = 0x4, + ArcadeFlightStick = 0x5, + GCon = 0x6, + DualShock = 0x7, + MultiTap = 0x8 + }; + + enum struct GenericButton : uint16_t { + D8 = (1 << 0), + D9 = (1 << 1), + D10 = (1 << 2), + D11 = (1 << 3), + D12 = (1 << 4), + D13 = (1 << 5), + D14 = (1 << 6), + D15 = (1 << 7), + D0 = (1 << 8), + D1 = (1 << 9), + D2 = (1 << 10), + D3 = (1 << 11), + D4 = (1 << 12), + D5 = (1 << 13), + D6 = (1 << 14), + D7 = (1 << 15) + }; + + struct LED { + enum struct State : uint8_t { + Off = 0x0, + On = 0x1 + }; + + enum struct Lock { + Off = 0x2, + On = 0x3 + }; + }; + + class RawController { + public: + enum struct State : uint8_t + { + Disconnected = 0, + EnterConfigMode = (1 << 0), + LockAnalog = (1 << 1), + UnlockRumble = (1 << 2), + ExitConfigMode = (1 << 3), + Stable = (1 << 4) + }; + + protected: + struct Header { + uint8_t type; + State state; + uint8_t rumble0; + uint8_t rumble1; + + void clear() { + this->type = 0; + this->state = State::Disconnected; + } + }; + + public: + class ButtonState { + private: + uint16_t oldState; + uint16_t currentState; + + static bool is_down(uint16_t data, uint16_t button) { + return ((data & button) == 0); + } + + void exchange_state() { + this->oldState = this->currentState; + } + + public: + template + bool is_down(T button) const { + return ButtonState::is_down(this->currentState, static_cast(button)); + } + + template + bool was_down(T button) const { + return ButtonState::is_down(this->oldState, static_cast(button)); + } + + template + bool went_down(T button) const { + return (!ButtonState::was_down(button) && ButtonState::is_down(button)); + } + + template + bool went_up(T button) const { + return (ButtonState::was_down(button) && !ButtonState::is_down(button)); + } + + friend class RawController; + }; + + Header header; + ButtonState button; + uint32_t special; + + public: + ControllerType get_type() const { + return static_cast((this->header.type >> 4)); + } + + ButtonState get_button_state() const { + return this->button; + } + + //For debugging only + uint8_t get_raw_type() const { + return this->header.type; + } + + uint16_t get_raw_button_state() const { + return this->button.currentState; + } + }; + } +} \ No newline at end of file diff --git a/src/Library/src/Periphery/periphery.cpp b/src/Library/src/Periphery/periphery.cpp new file mode 100644 index 00000000..36c2f6ef --- /dev/null +++ b/src/Library/src/Periphery/periphery.cpp @@ -0,0 +1,12 @@ +#include +#include + +namespace JabyEngine { + namespace Periphery { + RawController controller[1][1]; + + void query_controller() { + printf("Needs implementation\n"); + } + } +} \ No newline at end of file