Skip to content

Commit

Permalink
add back poll rate display
Browse files Browse the repository at this point in the history
for some reason i'm seeing 4000hz readings every now and then
  • Loading branch information
NikhilNarayana committed Oct 12, 2023
1 parent 6c7a93d commit 889e6cf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>

#include "Core/Config/MainSettings.h"
Expand Down Expand Up @@ -36,16 +37,22 @@ void GCPadWiiUConfigDialog::CreateLayout()

m_layout = new QVBoxLayout();
m_status_label = new QLabel();
m_poll_rate_label = new QLabel();
m_rumble = new QCheckBox(tr("Enable Rumble"));
m_simulate_bongos = new QCheckBox(tr("Simulate DK Bongos"));
m_simulate_bongos->setDisabled(true);
m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok);

UpdateAdapterStatus();
UpdatePollRate();

m_poll_rate_timer = new QTimer(this);

auto callback = [this] { QueueOnObject(this, &GCPadWiiUConfigDialog::UpdateAdapterStatus); };
GCAdapter::SetAdapterCallback(callback);

m_layout->addWidget(m_status_label);
m_layout->addWidget(m_poll_rate_label);
m_layout->addWidget(m_rumble);
m_layout->addWidget(m_simulate_bongos);
m_layout->addWidget(m_button_box);
Expand All @@ -55,6 +62,8 @@ void GCPadWiiUConfigDialog::CreateLayout()

void GCPadWiiUConfigDialog::ConnectWidgets()
{
connect(m_poll_rate_timer, &QTimer::timeout, this, &GCPadWiiUConfigDialog::UpdatePollRate);
m_poll_rate_timer->start(1500);
connect(m_rumble, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings);
connect(m_simulate_bongos, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings);
connect(m_button_box, &QDialogButtonBox::accepted, this, &GCPadWiiUConfigDialog::accept);
Expand Down Expand Up @@ -85,6 +94,12 @@ void GCPadWiiUConfigDialog::UpdateAdapterStatus()
m_simulate_bongos->setEnabled(detected);
}

void GCPadWiiUConfigDialog::UpdatePollRate()
{
QString poll_rate_text = tr("Poll Rate: %1 hz").arg(1000.0 / GCAdapter::ReadRate());
m_poll_rate_label->setText(poll_rate_text);
}

void GCPadWiiUConfigDialog::LoadSettings()
{
m_rumble->setChecked(Config::Get(Config::GetInfoForAdapterRumble(m_port)));
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class QCheckBox;
class QLabel;
class QDialogButtonBox;
class QVBoxLayout;
class QTimer;

class GCPadWiiUConfigDialog final : public QDialog
{
Expand All @@ -26,11 +27,14 @@ class GCPadWiiUConfigDialog final : public QDialog

private:
void UpdateAdapterStatus();
void UpdatePollRate();

int m_port;

QVBoxLayout* m_layout;
QLabel* m_status_label;
QLabel* m_poll_rate_label;
QTimer* m_poll_rate_timer;
QDialogButtonBox* m_button_box;

// Checkboxes
Expand Down
39 changes: 39 additions & 0 deletions Source/Core/InputCommon/GCAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ static std::optional<size_t> s_config_callback_id = std::nullopt;
static bool s_is_adapter_wanted = false;
static std::array<bool, SerialInterface::MAX_SI_CHANNELS> s_config_rumble_enabled{};

// slippi change: poll rate stuff
static u64 s_consecutive_slow_transfers = 0;
static double s_read_rate = 0.0;
// slippi change: poll rate stuff

static void ReadThreadFunc()
{
Common::SetCurrentThreadName("GCAdapter Read Thread");
Expand Down Expand Up @@ -197,15 +202,37 @@ static void ReadThreadFunc()
// Reset rumble once on initial reading
ResetRumble();

s_read_rate = 0.0;

while (s_read_adapter_thread_running.IsSet())
{
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
std::array<u8, CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE> input_buffer;

int payload_size = 0;

std::chrono::high_resolution_clock::time_point start =
std::chrono::high_resolution_clock::now();
const int error =
libusb_interrupt_transfer(s_handle, s_endpoint_in, input_buffer.data(),
int(input_buffer.size()), &payload_size, USB_TIMEOUT_MS);

std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();

double elapsed_ms =
std::chrono::duration_cast<std::chrono::nanoseconds>(now - start).count() / 1000000.0;

if (elapsed_ms > 15.0)
{
s_consecutive_slow_transfers++;
}
else
{
s_consecutive_slow_transfers = 0;
}

s_read_rate = elapsed_ms;

if (error != LIBUSB_SUCCESS)
{
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Read: libusb_interrupt_transfer failed: {}",
Expand Down Expand Up @@ -469,6 +496,18 @@ void StopScanThread()
}
}

// slippi change: for poll rate display
bool IsReadingAtReducedRate()
{
return s_consecutive_slow_transfers > 80;
}

double ReadRate()
{
return s_read_rate;
}
// slippi change: for poll rate display

static void Setup()
{
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/InputCommon/GCAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ void SetAdapterCallback(std::function<void(void)> func);
void StartScanThread();
void StopScanThread();

// slippi change: for poll rate display
void ResetAdapterIfNecessary();
bool IsReadingAtReducedRate();
double ReadRate();
// slippi change: for poll rate display

// Buttons have PAD_GET_ORIGIN set on new connection
// Netplay and CSIDevice_GCAdapter make use of this.
GCPadStatus Input(int chan);
Expand Down

0 comments on commit 889e6cf

Please sign in to comment.