From f812ceff6057b02202569e4028b94371e2dd08ca Mon Sep 17 00:00:00 2001 From: sorgelig Date: Tue, 25 Feb 2020 20:01:51 +0800 Subject: [PATCH] mr.Spinner: more natural axis range for paddles, option for single device, support for paddle and spinner at the same time. --- PaddleTwoControllersUSB/Gamepad.cpp | 4 +- .../PaddleTwoControllersUSB.ino | 157 +++++++++--------- 2 files changed, 78 insertions(+), 83 deletions(-) diff --git a/PaddleTwoControllersUSB/Gamepad.cpp b/PaddleTwoControllersUSB/Gamepad.cpp index d847bce..6f3db68 100644 --- a/PaddleTwoControllersUSB/Gamepad.cpp +++ b/PaddleTwoControllersUSB/Gamepad.cpp @@ -46,8 +46,8 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = { 0xa1, 0x00, // COLLECTION (Physical) 0x09, 0x30, // USAGE (X) 0x09, 0x31, // USAGE (Y) - 0x15, 0x80, // LOGICAL_MINIMUM (-128) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255) 0x95, 0x02, // REPORT_COUNT (2) 0x75, 0x08, // REPORT_SIZE (8) 0x81, 0x02, // INPUT (Data,Var,Abs) diff --git a/PaddleTwoControllersUSB/PaddleTwoControllersUSB.ino b/PaddleTwoControllersUSB/PaddleTwoControllersUSB.ino index 783a92e..4d04025 100644 --- a/PaddleTwoControllersUSB/PaddleTwoControllersUSB.ino +++ b/PaddleTwoControllersUSB/PaddleTwoControllersUSB.ino @@ -80,6 +80,9 @@ // Optional parameter. Leave it commented out. //#define SPINNER_SENSITIVITY 1 +// Set it to 1 if you want only single input device. +#define DEV_NUM 2 + ///////////////////////////////////////////////////////////////// // pins map @@ -111,46 +114,16 @@ const char *gp_serial = "MiSTer PD/SP v1"; #include #include "Gamepad.h" -Gamepad_ Gamepad[2]; +Gamepad_ Gamepad[DEV_NUM]; ResponsiveAnalogRead analog[2] = {ResponsiveAnalogRead(pdlpin[0], true),ResponsiveAnalogRead(pdlpin[1], true)}; int8_t pdlena[2] = {0,0}; uint16_t drvpos[2]; int16_t drvX[2] = {0,0}; -void setup() -{ - float snap = .01; - float thresh = 8.0; - - for(int idx=0; idx<2; idx++) - { - Gamepad[idx].reset(); - - pinMode(encpin[idx][0], INPUT_PULLUP); - pinMode(encpin[idx][1], INPUT_PULLUP); - pinMode(dbtnpin[idx], INPUT_PULLUP); - drv_proc(idx); - drvpos[idx] = 0; - attachInterrupt(digitalPinToInterrupt(encpin[idx][0]), idx ? drv1_isr : drv0_isr, CHANGE); - attachInterrupt(digitalPinToInterrupt(encpin[idx][1]), idx ? drv1_isr : drv0_isr, CHANGE); - - pdlena[idx] = 0; - pinMode(pbtnpin[idx], INPUT_PULLUP); - pinMode(pdlpin[idx], INPUT); - analog[idx].setSnapMultiplier(snap); - analog[idx].setActivityThreshold(thresh); - } -} - -void loop() -{ - sendState(0); - sendState(1); -} - -const uint16_t sp_max = ((SPINNER_PPR*4*270UL)/360); -int32_t sp_clamp[2] = {0,0}; +#define SP_MAX ((SPINNER_PPR*4*270UL)/360) +const uint16_t sp_max = SP_MAX; +int32_t sp_clamp[2] = {SP_MAX/2,SP_MAX/2}; void drv_proc(int8_t idx) { @@ -185,35 +158,66 @@ void drv1_isr() drv_proc(1); } -const int16_t sp_step = (SPINNER_PPR*10)/(20*SPINNER_SENSITIVITY); +void setup() +{ + float snap = .01; + float thresh = 8.0; -void sendState(byte idx) + for(int idx=0; idx>2) ^ 0x80; - } - else + for(int idx=0; idx>2); + } + else + { + #ifdef PADDLE_EMU + newX = ((sp_clamp[idx]*255)/sp_max); + #endif + } + if(!Gamepad[idx]._GamepadReport.b3 && !Gamepad[idx]._GamepadReport.b4) { static uint16_t prev[2] = {0,0}; @@ -223,38 +227,29 @@ void sendState(byte idx) { newR = 1; prev[idx] += sp_step; - //Serial.println("RIGHT"); } else if(diff <= -sp_step) { newL = 1; prev[idx] -= sp_step; - //Serial.println("LEFT"); } } -#ifdef PADDLE_EMU - uint16_t val = (sp_clamp[idx]*255)/sp_max; - newX = val ^ 0x80; -#endif + int8_t diff = newX - Gamepad[idx]._GamepadReport.X; - } - - int8_t diff = newX - Gamepad[idx]._GamepadReport.X; - - // Only report controller state if it has changed - if (diff - || ((Gamepad[idx]._GamepadReport.b0 ^ newA) & 1) - || ((Gamepad[idx]._GamepadReport.b2 ^ newC) & 1) - || ((Gamepad[idx]._GamepadReport.b3 ^ newL) & 1) - || ((Gamepad[idx]._GamepadReport.b4 ^ newR) & 1)) - { - //if(!idx) Serial.println(newX); - Gamepad[idx]._GamepadReport.X = newX; - Gamepad[idx]._GamepadReport.b0 = newA; - Gamepad[idx]._GamepadReport.b2 = newC; - Gamepad[idx]._GamepadReport.b3 = newL; - Gamepad[idx]._GamepadReport.b4 = newR; - Gamepad[idx].send(); + // Only report controller state if it has changed + if (diff + || ((Gamepad[idx]._GamepadReport.b0 ^ newA) & 1) + || ((Gamepad[idx]._GamepadReport.b2 ^ newC) & 1) + || ((Gamepad[idx]._GamepadReport.b3 ^ newL) & 1) + || ((Gamepad[idx]._GamepadReport.b4 ^ newR) & 1)) + { + Gamepad[idx]._GamepadReport.X = newX; + Gamepad[idx]._GamepadReport.b0 = newA; + Gamepad[idx]._GamepadReport.b2 = newC; + Gamepad[idx]._GamepadReport.b3 = newL; + Gamepad[idx]._GamepadReport.b4 = newR; + Gamepad[idx].send(); + } } }