From eaf0c1bd41d4387eb980dd7d67d039564d622276 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Mon, 21 Dec 2020 15:09:49 -0500 Subject: [PATCH] translateAxis only when required, add debug to esp32-n64-bt but too unreliable to use --- readme.md | 2 ++ src/N64.cpp | 59 ++++++++++++++++++++++++++++++++++++++------ src/Playstation.cpp | 11 ++++----- src/gamepad/common.h | 10 ++++++++ src/util.cpp | 16 ++++++++++++ 5 files changed, 84 insertions(+), 14 deletions(-) diff --git a/readme.md b/readme.md index 673bebe..9993537 100644 --- a/readme.md +++ b/readme.md @@ -27,3 +27,5 @@ Code and/or inspiration was (or will be) taken from these places, in no particul * [dreamcast_usb](https://github.com/raphnet/dreamcast_usb) * [SNES-NRF24](https://github.com/baldengineer/SNES-NRF24) * https://github.com/NicoHood/Nintendo + * https://github.com/darthcloud/BlueRetro + * https://github.com/raphnet/gc_n64_usb-v3 diff --git a/src/N64.cpp b/src/N64.cpp index a83b77b..cac0740 100644 --- a/src/N64.cpp +++ b/src/N64.cpp @@ -4,14 +4,33 @@ //#define PRINT_Y_AXIS_VALUES 1 //#define PRINT_X_AXIS_VALUES 1 //#define PLOT_CONSOLE_POLLING 1 +#define DEBUG #ifndef GAMEPAD_COUNT #define GAMEPAD_COUNT 1 #endif +#define AXIS_CENTER_IN 0 +#define AXIS_MAX_IN 60 +#define AXIS_MIN_IN -60 + #include "gamepad/Gamepad.h" #include "util.cpp" +/* + LOOKING AT THE PLUG + /---------\ + PIN 1-> / o o o \ + /-------------\ + + +PIN # USAGE + + GND + DATA + VCC +3.3V ONLY +*/ + #define DATA_PIN 13 #define LINE_WRITE_HIGH pinMode(DATA_PIN, INPUT_PULLUP) @@ -263,7 +282,9 @@ void updateOffsetsAndResolution() { ControllerData controller; void setup() { +#ifdef DEBUG Serial.begin(115200); +#endif gamepad.begin(); @@ -286,7 +307,7 @@ void setup() { } void loop() { - Serial.println("sending command to n64"); + //Serial.println("sending command to n64"); // send command 0x01 to n64 controller sendCommand(0x01); @@ -317,19 +338,41 @@ void loop() { } auto hat = calculateDpadDirection(controller.DPadUp, controller.DPadDown, controller.DPadLeft, controller.DPadRight); auto cHat = dpadToAxis(calculateDpadDirection(controller.CUp, controller.CDown, controller.CLeft, controller.CRight)); - // todo: need to scale max/min to our max/min - gamepad.setAxis(c, controller.xAxis, controller.yAxis, cHat.x, cHat.y, 0, 0, hat); + gamepad.setAxis(c, translateAxis(controller.xAxis), translateAxis(controller.yAxis), cHat.x, cHat.y, 0, 0, hat); // polling must not occur faster than every 20 ms delay(14); //checkUpdateCombo(&controller); - //Serial.printf("DPAD: %i %i %i %i \n", controller.DPadUp, controller.DPadDown, controller.DPadLeft, controller.DPadRight); - //Serial.printf("C: %i %i %i %i \n", controller.CUp, controller.CDown, controller.CLeft, controller.CRight); - //Serial.printf("Y: %i X: %i\n", controller.yAxis, controller.xAxis); - //Serial.print("C: "); - //Serial.println(controller.CUp); +#ifdef DEBUG + Serial.print("buttons: "); + Serial.print(controller.buttonA ? "A" : "-"); + Serial.print(controller.buttonB ? "B" : "-"); + Serial.print(controller.buttonZ ? "Z" : "-"); + Serial.print(controller.buttonL ? "L" : "-"); + Serial.print(controller.buttonR ? "R" : "-"); + Serial.print(controller.buttonStart ? "S" : "-"); + Serial.print(" DPAD: "); + Serial.print(controller.DPadUp ? "U" : "-"); + Serial.print(controller.DPadDown ? "D" : "-"); + Serial.print(controller.DPadLeft ? "L" : "-"); + Serial.print(controller.DPadRight ? "R" : "-"); + Serial.print(" C: "); + Serial.print(controller.CUp ? "U" : "-"); + Serial.print(controller.CDown ? "D" : "-"); + Serial.print(controller.CLeft ? "L" : "-"); + Serial.print(controller.CRight ? "R" : "-"); + Serial.print(" Y: "); + Serial.print(controller.yAxis); + Serial.print(" YT: "); + Serial.print(translateAxis(controller.yAxis)); + Serial.print(" X: "); + Serial.print(controller.xAxis); + Serial.print(" XT: "); + Serial.print(translateAxis(controller.xAxis)); + Serial.println(); +#endif //delay(500); } diff --git a/src/Playstation.cpp b/src/Playstation.cpp index 821e4c3..471e24a 100644 --- a/src/Playstation.cpp +++ b/src/Playstation.cpp @@ -51,9 +51,14 @@ PIN # USAGE #define JOYSTICK_STATE_SIZE 6 +#define AXIS_CENTER_IN 128 +#define AXIS_MAX_IN 255 +#define AXIS_MIN_IN 0 + //#define DEBUG #include "gamepad/Gamepad.h" +#include "util.cpp" GAMEPAD_CLASS gamepad; @@ -180,12 +185,6 @@ class Joystick_ { } } - int16_t translateAxis(uint8_t v) { - //map(value, fromLow, fromHigh, toLow, toHigh) - // todo: don't map at all if translation isn't required... - return v == 128 ? AXIS_CENTER : map(v, 0, 255, AXIS_MIN, AXIS_MAX); - } - #ifdef DEBUG void printBin(uint8_t c) { //Serial.print(c); diff --git a/src/gamepad/common.h b/src/gamepad/common.h index 00a7679..efce58d 100644 --- a/src/gamepad/common.h +++ b/src/gamepad/common.h @@ -83,6 +83,16 @@ #define DPAD_CENTERED DPAD_CENTER // end aliases +#ifndef AXIS_CENTER_IN +#define AXIS_CENTER_IN 0 +#endif +#ifndef AXIS_MAX_IN +#define AXIS_MAX_IN 32768 +#endif +#ifndef AXIS_MIN_IN +#define AXIS_MIN_IN -32767 +#endif + #if 0 #define BUTTON_1 1 #define BUTTON_2 2 diff --git a/src/util.cpp b/src/util.cpp index 567cbec..7e6e53a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -61,3 +61,19 @@ struct Axis dpadToAxis(uint8_t dpad) { // todo: panic here? return axis(AXIS_CENTER, AXIS_CENTER); } + +/* +long map(long x, long in_min, long in_max, long out_min, long out_max) { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} +*/ + +inline int16_t translateAxis(int16_t v) { + // don't map at all if translation isn't required... +#if AXIS_CENTER_IN == AXIS_CENTER && AXIS_MIN_IN == AXIS_MIN && AXIS_MAX_IN == AXIS_MAX + return v; // noop +#else + //return v == AXIS_CENTER_IN ? AXIS_CENTER : map(v, AXIS_MIN_IN, AXIS_MAX_IN, AXIS_MIN, AXIS_MAX); + return v == AXIS_CENTER_IN ? AXIS_CENTER : (v - AXIS_MIN_IN) * (AXIS_MAX - AXIS_MIN) / (AXIS_MAX_IN - AXIS_MIN_IN) + AXIS_MIN; +#endif +}