FINALLY figured out instable hid connection

This commit is contained in:
devl0rd 2020-04-28 20:40:37 -06:00
parent 799a82034b
commit bfff6828bc
7 changed files with 126 additions and 115 deletions

11
.vscode/settings.json vendored
View file

@ -6,6 +6,15 @@
"iosfwd": "c", "iosfwd": "c",
"queue": "c", "queue": "c",
"sdl.h": "c", "sdl.h": "c",
"context.h": "c" "context.h": "c",
"array": "c",
"deque": "c",
"list": "c",
"vector": "c",
"xhash": "c",
"xstring": "c",
"xutility": "c",
"string_view": "c",
"type_traits": "c"
} }
} }

View file

@ -346,9 +346,9 @@ function smoothGyroMouse(gyro) {
} else { } else {
gyroHistory.shift(); gyroHistory.shift();
gyroHistory.push(gyro); gyroHistory.push(gyro);
gyro.x = (gyroHistory[0].x + gyroHistory[1].x + gyroHistory[2].x) / 3; gyro.x = ((gyroHistory[0].x * 1) + (gyroHistory[1].x * 3) + (gyroHistory[2].x * 5)) / 9;
gyro.y = (gyroHistory[0].y + gyroHistory[1].y + gyroHistory[2].y) / 3; gyro.y = ((gyroHistory[0].y * 1) + (gyroHistory[1].y * 3) + (gyroHistory[2].y * 5)) / 9;
gyro.z = (gyroHistory[0].z + gyroHistory[1].z + gyroHistory[2].z) / 3; gyro.z = ((gyroHistory[0].z * 1) + (gyroHistory[1].z * 3) + (gyroHistory[2].z * 5)) / 9;
if (gyro.x < 0.01 && gyro.x > 0) { if (gyro.x < 0.01 && gyro.x > 0) {
gyro.x = 0; gyro.x = 0;
} else if (gyro.x > -0.01 && gyro.x < 0) { } else if (gyro.x > -0.01 && gyro.x < 0) {
@ -465,6 +465,7 @@ function handleTouchInput(hid) {
} }
} }
function handleGyroAndAccel(hid) { function handleGyroAndAccel(hid) {
var gyro = { x: hid.get("gyroX"), y: hid.get("gyroY"), z: hid.get("gyroZ") } var gyro = { x: hid.get("gyroX"), y: hid.get("gyroY"), z: hid.get("gyroZ") }
var accel = { x: hid.get("accelX"), y: hid.get("accelY"), z: hid.get("accelZ") } var accel = { x: hid.get("accelX"), y: hid.get("accelY"), z: hid.get("accelZ") }
for (axis in gyro) { for (axis in gyro) {
@ -475,6 +476,7 @@ function handleGyroAndAccel(hid) {
} }
hidStreamClient.on('data', function (data) { hidStreamClient.on('data', function (data) {
console.log("dataLength: " + data.length)
switchHidBuffer = new Buffer.from(data); switchHidBuffer = new Buffer.from(data);
var hid = parseInputStruct(switchHidBuffer) var hid = parseInputStruct(switchHidBuffer)
var controllerCount = hid.get("controllerCount"); var controllerCount = hid.get("controllerCount");

View file

@ -11,7 +11,7 @@ void gamePadSend(JoyConSocket *connection)
JoyPkg pkg; JoyPkg pkg;
/* Recieve switch input and generate the package */ /* Recieve switch input and generate the package */
hidScanInput(); hidScanInput();
unsigned int controllersConnected = 0; u32 controllersConnected = 0;
HidControllerID player1Id; HidControllerID player1Id;
if (hidGetHandheldMode()) if (hidGetHandheldMode())
{ {
@ -31,86 +31,89 @@ void gamePadSend(JoyConSocket *connection)
} }
pkg.frameRate = frameRate; pkg.frameRate = frameRate;
pkg.controllerCount = controllersConnected; pkg.controllerCount = controllersConnected;
pkg.heldKeys1 = (u32)hidKeysHeld(player1Id);
pkg.heldKeys1 = !(u32)hidKeysHeld(player1Id) ? 0 : (u32)hidKeysHeld(player1Id);
hidJoystickRead(&lJoy, player1Id, JOYSTICK_LEFT); hidJoystickRead(&lJoy, player1Id, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, player1Id, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, player1Id, JOYSTICK_RIGHT);
pkg.lJoyX1 = lJoy.dx; pkg.lJoyX1 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY1 = lJoy.dy; pkg.lJoyY1 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX1 = rJoy.dx; pkg.rJoyX1 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY1 = rJoy.dy; pkg.rJoyY1 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys2 = (u32)hidKeysHeld(CONTROLLER_PLAYER_2); pkg.heldKeys2 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_2) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_2);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_2, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_2, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_2, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_2, JOYSTICK_RIGHT);
pkg.lJoyX2 = lJoy.dx; pkg.lJoyX2 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY2 = lJoy.dy; pkg.lJoyY2 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX2 = rJoy.dx; pkg.rJoyX2 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY2 = rJoy.dy; pkg.rJoyY2 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys3 = (u32)hidKeysHeld(CONTROLLER_PLAYER_3); pkg.heldKeys3 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_3) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_3);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_3, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_3, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_3, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_3, JOYSTICK_RIGHT);
pkg.lJoyX3 = lJoy.dx; pkg.lJoyX3 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY3 = lJoy.dy; pkg.lJoyY3 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX3 = rJoy.dx; pkg.rJoyX3 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY3 = rJoy.dy; pkg.rJoyY3 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys4 = (u32)hidKeysHeld(CONTROLLER_PLAYER_4); pkg.heldKeys4 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_4) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_4);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_4, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_4, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_4, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_4, JOYSTICK_RIGHT);
pkg.lJoyX4 = lJoy.dx; pkg.lJoyX4 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY4 = lJoy.dy; pkg.lJoyY4 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX4 = rJoy.dx; pkg.rJoyX4 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY4 = rJoy.dy; pkg.rJoyY4 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys5 = (u32)hidKeysHeld(CONTROLLER_PLAYER_5); pkg.heldKeys5 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_5) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_5);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_5, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_5, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_5, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_5, JOYSTICK_RIGHT);
pkg.lJoyX5 = lJoy.dx;
pkg.lJoyY5 = lJoy.dy;
pkg.rJoyX5 = rJoy.dx;
pkg.rJoyY5 = rJoy.dy;
pkg.heldKeys6 = (u32)hidKeysHeld(CONTROLLER_PLAYER_6); pkg.lJoyX5 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY5 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX5 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY5 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys6 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_6) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_6);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_6, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_6, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_6, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_6, JOYSTICK_RIGHT);
pkg.lJoyX6 = lJoy.dx; pkg.lJoyX6 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY6 = lJoy.dy; pkg.lJoyY6 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX6 = rJoy.dx; pkg.rJoyX6 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY6 = rJoy.dy; pkg.rJoyY6 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys7 = (u32)hidKeysHeld(CONTROLLER_PLAYER_7); pkg.heldKeys7 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_7) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_7);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_7, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_7, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_7, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_7, JOYSTICK_RIGHT);
pkg.lJoyX7 = lJoy.dx; pkg.lJoyX7 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY7 = lJoy.dy; pkg.lJoyY7 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX7 = rJoy.dx; pkg.rJoyX7 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY7 = rJoy.dy; pkg.rJoyY7 = !rJoy.dy ? 0 : rJoy.dy;
pkg.heldKeys8 = (u32)hidKeysHeld(CONTROLLER_PLAYER_8); pkg.heldKeys8 = !(u32)hidKeysHeld(CONTROLLER_PLAYER_8) ? 0 : (u32)hidKeysHeld(CONTROLLER_PLAYER_8);
hidJoystickRead(&lJoy, CONTROLLER_PLAYER_8, JOYSTICK_LEFT); hidJoystickRead(&lJoy, CONTROLLER_PLAYER_8, JOYSTICK_LEFT);
hidJoystickRead(&rJoy, CONTROLLER_PLAYER_8, JOYSTICK_RIGHT); hidJoystickRead(&rJoy, CONTROLLER_PLAYER_8, JOYSTICK_RIGHT);
pkg.lJoyX8 = lJoy.dx; pkg.lJoyX8 = !lJoy.dx ? 0 : lJoy.dx;
pkg.lJoyY8 = lJoy.dy; pkg.lJoyY8 = !lJoy.dy ? 0 : lJoy.dy;
pkg.rJoyX8 = rJoy.dx; pkg.rJoyX8 = !rJoy.dx ? 0 : rJoy.dx;
pkg.rJoyY8 = rJoy.dy; pkg.rJoyY8 = !rJoy.dy ? 0 : rJoy.dy;
touchPosition touch; touchPosition touch;
hidTouchRead(&touch, 0); hidTouchRead(&touch, 0);
pkg.touchX1 = touch.px; pkg.touchX1 = !touch.px ? 0 : touch.px;
pkg.touchY1 = touch.py; pkg.touchY1 = !touch.py ? 0 : touch.py;
hidTouchRead(&touch, 1); hidTouchRead(&touch, 1);
pkg.touchX2 = touch.px; pkg.touchX2 = !touch.px ? 0 : touch.px;
pkg.touchY2 = touch.py; pkg.touchY2 = !touch.py ? 0 : touch.py;
SixAxisSensorValues sixaxis; SixAxisSensorValues sixaxis;
// You can read back up to 17 successive values at once // You can read back up to 17 successive values at once
hidSixAxisSensorValuesRead(&sixaxis, player1Id, 1); hidSixAxisSensorValuesRead(&sixaxis, player1Id, 1);
pkg.accelX = sixaxis.accelerometer.x; pkg.accelX = !sixaxis.accelerometer.x ? 0 : sixaxis.accelerometer.x;
pkg.accelY = sixaxis.accelerometer.y; pkg.accelY = !sixaxis.accelerometer.y ? 0 : sixaxis.accelerometer.y;
pkg.accelZ = sixaxis.accelerometer.z; pkg.accelZ = !sixaxis.accelerometer.z ? 0 : sixaxis.accelerometer.z;
pkg.gyroX = sixaxis.gyroscope.x; pkg.gyroX = !sixaxis.gyroscope.x ? 0 : sixaxis.gyroscope.x;
pkg.gyroY = sixaxis.gyroscope.y; pkg.gyroY = !sixaxis.gyroscope.y ? 0 : sixaxis.gyroscope.y;
pkg.gyroZ = sixaxis.gyroscope.z; pkg.gyroZ = !sixaxis.gyroscope.z ? 0 : sixaxis.gyroscope.z;
sendJoyConInput(connection, &pkg); sendJoyConInput(connection, &pkg);
} }

View file

@ -52,7 +52,7 @@ static const SocketInitConfig socketInitConf = {
.tcp_rx_buf_max_size = 0x400000, .tcp_rx_buf_max_size = 0x400000,
.udp_tx_buf_size = 0x1400, .udp_tx_buf_size = 0x1400,
.udp_rx_buf_size = 0x2500, .udp_rx_buf_size = 0x3500,
.sb_efficiency = 2, .sb_efficiency = 2,
}; };
@ -123,8 +123,7 @@ void init()
/* Init all switch required systems */ /* Init all switch required systems */
switchInit(); switchInit();
pcvSetClockRate(PcvModule_CpuBus, 1785000000); //Overclock CPU pcvSetClockRate(PcvModule_CpuBus, 1785000000); //Overclock CPU
pcvSetClockRate(PcvModule_GPU, 921000000); //Overclock GPU
pcvSetClockRate(PcvModule_EMC, 1600000000); //OC memory to docked frequency
renderContext = createRenderer(); renderContext = createRenderer();
videoContext = createVideoContext(); videoContext = createVideoContext();
videoContext->renderContext = renderContext; videoContext->renderContext = renderContext;
@ -139,8 +138,6 @@ void unInit()
freeVideoContext(videoContext); freeVideoContext(videoContext);
unInitGyro(); unInitGyro();
pcvSetClockRate(PcvModule_CpuBus, 1020000000); //Reset CPU clock to default pcvSetClockRate(PcvModule_CpuBus, 1020000000); //Reset CPU clock to default
pcvSetClockRate(PcvModule_GPU, 768000000); //Reset GPU clock to docked frequency
// pcvSetClockRate(PcvModule_EMC, 1600000000); //Leave memory in docked frequency
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {

View file

@ -10,58 +10,58 @@
/* Data to send to server */ /* Data to send to server */
typedef struct typedef struct
{ {
u32 heldKeys1; uint32_t heldKeys1;
s32 lJoyX1; int32_t lJoyX1;
s32 lJoyY1; int32_t lJoyY1;
s32 rJoyX1; int32_t rJoyX1;
s32 rJoyY1; int32_t rJoyY1;
u32 heldKeys2; uint32_t heldKeys2;
s32 lJoyX2; int32_t lJoyX2;
s32 lJoyY2; int32_t lJoyY2;
s32 rJoyX2; int32_t rJoyX2;
s32 rJoyY2; int32_t rJoyY2;
u32 heldKeys3; uint32_t heldKeys3;
s32 lJoyX3; int32_t lJoyX3;
s32 lJoyY3; int32_t lJoyY3;
s32 rJoyX3; int32_t rJoyX3;
s32 rJoyY3; int32_t rJoyY3;
u32 heldKeys4; uint32_t heldKeys4;
s32 lJoyX4; int32_t lJoyX4;
s32 lJoyY4; int32_t lJoyY4;
s32 rJoyX4; int32_t rJoyX4;
s32 rJoyY4; int32_t rJoyY4;
u32 heldKeys5; uint32_t heldKeys5;
s32 lJoyX5; int32_t lJoyX5;
s32 lJoyY5; int32_t lJoyY5;
s32 rJoyX5; int32_t rJoyX5;
s32 rJoyY5; int32_t rJoyY5;
u32 heldKeys6; uint32_t heldKeys6;
s32 lJoyX6; int32_t lJoyX6;
s32 lJoyY6; int32_t lJoyY6;
s32 rJoyX6; int32_t rJoyX6;
s32 rJoyY6; int32_t rJoyY6;
u32 heldKeys7; uint32_t heldKeys7;
s32 lJoyX7; int32_t lJoyX7;
s32 lJoyY7; int32_t lJoyY7;
s32 rJoyX7; int32_t rJoyX7;
s32 rJoyY7; int32_t rJoyY7;
u32 heldKeys8; uint32_t heldKeys8;
s32 lJoyX8; int32_t lJoyX8;
s32 lJoyY8; int32_t lJoyY8;
s32 rJoyX8; int32_t rJoyX8;
s32 rJoyY8; int32_t rJoyY8;
u32 touchX1; uint32_t touchX1;
u32 touchY1; uint32_t touchY1;
u32 touchX2; uint32_t touchX2;
u32 touchY2; uint32_t touchY2;
float accelX; float_t accelX;
float accelY; float_t accelY;
float accelZ; float_t accelZ;
float gyroX; float_t gyroX;
float gyroY; float_t gyroY;
float gyroZ; float_t gyroZ;
unsigned int controllerCount; uint32_t controllerCount;
unsigned int frameRate; uint32_t frameRate;
} JoyPkg; } JoyPkg;
/* Init nx network and av network */ /* Init nx network and av network */

View file

@ -9,7 +9,7 @@
float timeThen = 0; float timeThen = 0;
float timeNow = 1; float timeNow = 1;
float delta = 1; float delta = 1;
unsigned int frameRate = 0; u32 frameRate = 0;
void initDelta() void initDelta()
{ {
timeThen = svcGetSystemTick(); timeThen = svcGetSystemTick();
@ -489,7 +489,7 @@ void handleFrame(RenderContext *renderContext, VideoContext *videoContext)
if (++videoContext->video_frame_count % 60 == 0) if (++videoContext->video_frame_count % 60 == 0)
{ {
new_time = svcGetSystemTick(); new_time = svcGetSystemTick();
frameRate = (unsigned int)(60.0 / ((new_time - old_time) / 19200000.0)); frameRate = (u32)(60.0 / ((new_time - old_time) / 19200000.0));
old_time = new_time; old_time = new_time;
} }
} }

View file

@ -5,7 +5,7 @@
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
#include "context.h" #include "context.h"
unsigned int frameRate; u32 frameRate;
/* Allocates a render context */ /* Allocates a render context */
RenderContext *createRenderer(void); RenderContext *createRenderer(void);