Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern const OemController_State0x3f OemController_State0x3f_NEUTRAL_STATE = {
.right_joystick = {0x00, 0x80, 0x00, 0x80},
};

const OemController_State0x30 OemController_State0x30_NEUTRAL_STATE = {
const OemController_State0x30 OemController_State0x30_NEUTRAL_STATE = {
.buttons = {
.button3 = 0,
.button4 = 0,
Expand Down
90 changes: 87 additions & 3 deletions Common/ControllerStates/NintendoSwitch_OemController_State.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

#include <stdbool.h>
#include <stdint.h>
#include <cstdint>
#include "string.h"

//#define PACKED __attribute__((packed))

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -56,6 +60,25 @@ struct OemController_State0x30_Buttons{
uint8_t vibrator;
};

static inline void write_bits(uint8_t* raw, int bit, int len, uint32_t value) {
uint64_t mask = ((1ULL << len) - 1);
value &= (uint32_t)mask;
int byte = bit / 8;
int shift = bit % 8;
// Touch only the bytes covered by this field, including at the packet end.
const int byte_count = (shift + len + 7) / 8;
uint64_t existing = 0;
for (int i = 0; i < byte_count; i++)
existing |= (uint64_t)raw[byte + i] << (i * 8);
existing &= ~(mask << shift);
existing |= ((uint64_t)value << shift);
for (int i = 0; i < byte_count; i++)
raw[byte + i] = (uint8_t)(existing >> (i * 8));
}


#pragma pack(push, 1)

struct OemController_State0x30_Gyro{
int16_t accel_x;
int16_t accel_y;
Expand All @@ -65,17 +88,74 @@ struct OemController_State0x30_Gyro{
int16_t rotation_z;
};

struct OemController_State0x30_GyroQuaternion{
uint8_t raw[36];

void set_quat_accel_x0(int16_t v) { memcpy(&raw[0], &v, 2); }
void set_quat_accel_y0(int16_t v) { memcpy(&raw[2], &v, 2); }
void set_quat_accel_z0(int16_t v) { memcpy(&raw[4], &v, 2); }
void set_quat_accel_x1(int16_t v) { memcpy(&raw[12], &v, 2); }
void set_quat_accel_y1(int16_t v) { memcpy(&raw[14], &v, 2); }
void set_quat_accel_z1(int16_t v) { memcpy(&raw[16], &v, 2); }
void set_quat_accel_x2(int16_t v) { memcpy(&raw[24], &v, 2); }
void set_quat_accel_y2(int16_t v) { memcpy(&raw[26], &v, 2); }
void set_quat_accel_z2(int16_t v) { memcpy(&raw[28], &v, 2); }

void set_packing_mode(uint32_t v) { write_bits(raw, 48, 2, v); }
void set_max_index(uint32_t v) { write_bits(raw, 50, 2, v); }
void set_last_sample_0(uint32_t v) { write_bits(raw, 52, 21, v); }
void set_last_sample_1(uint32_t v) {
write_bits(raw, 73, 7, v & 0x7F);
write_bits(raw, 80, 14, v >> 7);
}
void set_last_sample_2(uint32_t v) {
write_bits(raw, 94, 2, v & 0x3);
write_bits(raw, 144, 19, v >> 2);
}
void set_delta_last_first_0(uint32_t v) { write_bits(raw, 163, 13, v); }
void set_delta_last_first_1(uint32_t v) { write_bits(raw, 176, 13, v); }
void set_delta_last_first_2(uint32_t v) {
write_bits(raw, 189, 3, v & 0x7);
write_bits(raw, 240, 10, v >> 3);
}
void set_delta_mid_avg_0(uint32_t v) { write_bits(raw, 250, 7, v); }
void set_delta_mid_avg_1(uint32_t v) { write_bits(raw, 257, 7, v); }
void set_delta_mid_avg_2(uint32_t v) { write_bits(raw, 264, 7, v); }
void set_timestamp_start(uint32_t v) {
write_bits(raw, 271, 1, v & 0x1);
write_bits(raw, 272, 10, v >> 1);
}
void set_timestamp_count(uint32_t v) { write_bits(raw, 282, 6, v); }

};

#pragma pack(pop)

static_assert(sizeof(OemController_State0x30_Gyro) == 12, "Gyro struct must be 12 bytes");
static_assert(sizeof(OemController_State0x30_GyroQuaternion) == 36, "Quaternion gyro struct must be 36 bytes");

struct OemController_State0x30_GyroX3{
OemController_State0x30_Gyro time0;
OemController_State0x30_Gyro time1;
OemController_State0x30_Gyro time2;
struct Samples{
OemController_State0x30_Gyro time0;
OemController_State0x30_Gyro time1;
OemController_State0x30_Gyro time2;
};

union{
Samples samples;
OemController_State0x30_GyroQuaternion quaternion;
};
};

static_assert(sizeof(OemController_State0x30_GyroX3) == 36, "GyroX3 struct must be 36 bytes");

struct OemController_State0x30{
OemController_State0x30_Buttons buttons;
OemController_State0x30_GyroX3 gyro;
};

static_assert(sizeof(OemController_State0x30) == 46, "Report 0x30 state must be 46 bytes");

extern const OemController_State0x30 OemController_State0x30_NEUTRAL_STATE;

bool OemController_State0x30_equals(
Expand All @@ -97,6 +177,10 @@ struct OemController_Status{
uint8_t player_lights;

uint8_t paired_mac_address[6];

// 0 = Standard
// 1 = Quaternion
//uint8_t gyro_protocol = 1;
};

enum class DisconnectReason{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ struct Message_Command_NS1_OemController_FullState : MessageHeader{
};


#define PABB2_MESSAGE_CMD_NS1_OEM_CONTROLLER_MOTION 0x99
struct Message_Command_NS1_OemController_Motion : MessageHeader{
uint16_t milliseconds;
NintendoSwitch::OemController_State0x30_Buttons buttons;
int32_t acceleration_ug[3];
int32_t angular_velocity_mdps[3];
};
static_assert(sizeof(Message_Command_NS1_OemController_Motion) == 40, "OEM motion message must be 40 bytes");
static_assert(alignof(Message_Command_NS1_OemController_Motion) == 1, "OEM motion message must be packed");



}
}
Expand Down
2 changes: 1 addition & 1 deletion Common/PABotBase2/PABotBase2_MessageProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace PABotBase2{



#define PABB2_MESSAGE_PROTOCOL_VERSION 2026061806
#define PABB2_MESSAGE_PROTOCOL_VERSION 2026061807


struct MessageHeader{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h"
#include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h"
#include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h"
#include "NintendoSwitch/Controllers/NintendoSwitch_GyroTests.h"
#include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h"
#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h"
#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h"
Expand Down Expand Up @@ -76,6 +77,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){
NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret);
NintendoSwitch::PokemonBDSP::add_tests_DialogDetector(ret);
NintendoSwitch::PokemonBDSP::add_tests_MenuDetector(ret);
NintendoSwitch::add_tests_Gyro(ret);
NintendoSwitch::PokemonFRLG::add_tests(ret);
NintendoSwitch::PokemonRSE::add_tests(ret);
NintendoSwitch::PokemonHome::add_tests(ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Controllers/ControllerTypes.h"
#include "Controllers/Controller.h"
#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h"
#include "NintendoSwitch/Controllers/NintendoSwitch_Gyro.h"

namespace PokemonAutomation{

Expand Down Expand Up @@ -96,36 +97,23 @@ class JoyconController : public AbstractController{
const JoystickPosition& position
) = 0;

// Gyro: Accelerometer (experimental - API subject to change)
virtual void issue_gyro_accel_x(
Cancellable* cancellable,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
) = 0;
virtual void issue_gyro_accel_y(
Cancellable* cancellable,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
) = 0;
virtual void issue_gyro_accel_z(
Cancellable* cancellable,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
) = 0;
virtual void issue_gyro_rotate_x(
Cancellable* cancellable,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
) = 0;
virtual void issue_gyro_rotate_y(
// Throws if the connected controller cannot execute physical gyro motion.
virtual void check_gyro_motion_supported() const = 0;

// Issue a complete physical gyro trajectory. The callback is evaluated
// synchronously while this command is constructed and is never retained.
// A non-positive duration is a no-op.
virtual void issue_gyro_motion(
Cancellable* cancellable,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
Milliseconds duration,
const GyroFunction& function
) = 0;
virtual void issue_gyro_rotate_z(

// Issue prepared canonical samples without resampling. The selected
// controller's frame and transport range are checked before queueing.
virtual void issue_gyro_motion(
Cancellable* cancellable,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
const GyroMotion& motion
) = 0;

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
*/

#include <stdexcept>
#include "Common/Cpp/PrettyPrint.h"
#include "NintendoSwitch_ControllerWithScheduler.h"

Expand Down Expand Up @@ -152,32 +153,52 @@ void ControllerWithScheduler::issue_right_joystick(



void ControllerWithScheduler::issue_gyro(
void ControllerWithScheduler::issue_gyro_motion(
Cancellable* cancellable,
SwitchResource id, const char* name,
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
int16_t value
ControllerClass controller_class,
Milliseconds duration,
const GyroFunction& function
){
if (duration <= Milliseconds::zero()){
return;
}
// Reject an unsupported controller before invoking the program's callback.
transform_gyro_state(controller_class, {});
issue_gyro_motion(cancellable, controller_class, prepare_gyro_motion(cancellable, duration, function));
}

void ControllerWithScheduler::issue_gyro_motion(
Cancellable* cancellable,
ControllerClass controller_class,
const GyroMotion& motion
){
const std::vector<GyroMotionSample> samples = Internal::transform_gyro_motion(
cancellable, controller_class, motion
);
if (samples.empty()){
return;
}

SuperscalarScheduler::Schedule schedule;
std::lock_guard<Mutex> lg0(m_issue_lock);
std::unique_lock<Mutex> issue_lock(m_issue_lock);
{
std::lock_guard<Mutex> lg1(m_state_lock);
if (cancellable){
cancellable->throw_if_cancelled();
}
m_scheduler.issue_to_resource(
schedule,
std::make_unique<SwitchCommand_Gyro>(id, value),
delay, hold, cooldown
);
for (const GyroMotionSample& sample : samples){
m_scheduler.issue_to_resource(
schedule,
std::make_unique<SwitchCommand_GyroState>(sample.state),
sample.duration, sample.duration, WallDuration::zero()
);
}
}
execute_schedule(cancellable, schedule);
if (m_logging_throttler){
m_logger.log(
std::string(name) + "(): (" + std::to_string(value) + ")" +
", delay = " + std::to_string(delay.count()) + "ms" +
", hold = " + std::to_string(hold.count()) + "ms" +
", cooldown = " + std::to_string(cooldown.count()) + "ms",
"issue_gyro_motion(): duration = " + std::to_string(motion.duration().count()) +
"ms, samples = " + std::to_string(samples.size()),
COLOR_DARKGREEN
);
}
Expand Down
Loading
Loading