Skip to content

Commit c74d357

Browse files
committed
Initial work on gyro.
1 parent 17b646a commit c74d357

17 files changed

+543
-252
lines changed

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ ControllerModeStatus SerialPABotBase_Connection::read_device_specs(
229229
desired_controller
230230
);
231231

232-
PABB_ESP32_NintendoSwitch_ControllerColors colors;
232+
PABB_NintendoSwitch_ControllerColors colors;
233233
{
234234
Color color(profile.body_color);
235235
colors.body[0] = color.red();
@@ -258,7 +258,7 @@ ControllerModeStatus SerialPABotBase_Connection::read_device_specs(
258258
m_botbase->issue_request_and_wait(
259259
MessageControllerWriteSpi(
260260
desired_controller,
261-
0x00006050, sizeof(PABB_ESP32_NintendoSwitch_ControllerColors),
261+
0x00006050, sizeof(PABB_NintendoSwitch_ControllerColors),
262262
&colors
263263
),
264264
// MessageControllerSetColors(desired_controller, colors),

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_ESP32.h

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -93,68 +93,10 @@ class MessageControllerWriteSpi : public BotBaseRequest{
9393
return BotBaseMessage(PABB_MSG_ESP32_REQUEST_WRITE_SPI, data);
9494
}
9595
};
96-
#if 0
97-
class MessageControllerGetColors : public BotBaseRequest{
98-
public:
99-
pabb_Message_ESP32_GetColors params;
100-
MessageControllerGetColors(ControllerType controller_type)
101-
: BotBaseRequest(false)
102-
{
103-
uint32_t controller_id = PABB_CID_NONE;
104-
switch (controller_type){
105-
case ControllerType::NintendoSwitch_WirelessProController:
106-
controller_id = PABB_CID_NINTENDO_SWITCH_WIRELESS_PRO_CONTROLLER;
107-
break;
108-
case ControllerType::NintendoSwitch_LeftJoycon:
109-
controller_id = PABB_CID_NINTENDO_SWITCH_LEFT_JOYCON;
110-
break;
111-
case ControllerType::NintendoSwitch_RightJoycon:
112-
controller_id = PABB_CID_NINTENDO_SWITCH_RIGHT_JOYCON;
113-
break;
114-
default:;
115-
}
116-
params.seqnum = 0;
117-
params.controller_type = controller_id;
118-
}
119-
virtual BotBaseMessage message() const override{
120-
return BotBaseMessage(PABB_MSG_ESP32_REQUEST_GET_COLORS, params);
121-
}
122-
};
123-
class MessageControllerSetColors : public BotBaseRequest{
124-
public:
125-
pabb_Message_ESP32_SetColors params;
126-
MessageControllerSetColors(
127-
ControllerType controller_type,
128-
const NintendoSwitch_ControllerColors& colors
129-
)
130-
: BotBaseRequest(false)
131-
{
132-
uint32_t controller_id = PABB_CID_NONE;
133-
switch (controller_type){
134-
case ControllerType::NintendoSwitch_WirelessProController:
135-
controller_id = PABB_CID_NINTENDO_SWITCH_WIRELESS_PRO_CONTROLLER;
136-
break;
137-
case ControllerType::NintendoSwitch_LeftJoycon:
138-
controller_id = PABB_CID_NINTENDO_SWITCH_LEFT_JOYCON;
139-
break;
140-
case ControllerType::NintendoSwitch_RightJoycon:
141-
controller_id = PABB_CID_NINTENDO_SWITCH_RIGHT_JOYCON;
142-
break;
143-
default:;
144-
}
145-
params.seqnum = 0;
146-
params.controller_type = controller_id;
147-
params.colors = colors;
148-
}
149-
virtual BotBaseMessage message() const override{
150-
return BotBaseMessage(PABB_MSG_ESP32_REQUEST_SET_COLORS, params);
151-
}
152-
};
153-
#endif
15496
class MessageControllerStateButtons : public BotBaseRequest{
15597
public:
15698
pabb_Message_ESP32_CommandButtonState params;
157-
MessageControllerStateButtons(uint16_t milliseconds, const PABB_ESP32_NintendoSwitch_ButtonState& state)
99+
MessageControllerStateButtons(uint16_t milliseconds, const PABB_NintendoSwitch_ButtonState& state)
158100
: BotBaseRequest(true)
159101
{
160102
params.seqnum = 0;
@@ -170,8 +112,8 @@ class MessageControllerStateFull : public BotBaseRequest{
170112
pabb_Message_ESP32_CommandFullState params;
171113
MessageControllerStateFull(
172114
uint16_t milliseconds,
173-
const PABB_ESP32_NintendoSwitch_ButtonState& buttons,
174-
const PABB_ESP32_NintendoSwitch_GyroState& gyro
115+
const PABB_NintendoSwitch_ButtonState& buttons,
116+
const PABB_NintendoSwitch_GyroStateX3& gyro
175117
)
176118
: BotBaseRequest(true)
177119
{

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerWithScheduler.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,38 @@ void ControllerWithScheduler::issue_right_joystick(
166166
);
167167
}
168168
}
169+
170+
171+
172+
void ControllerWithScheduler::issue_gyro(
173+
const Cancellable* cancellable,
174+
SwitchGyro& gyro, const char* name,
175+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
176+
int16_t value
177+
){
178+
std::lock_guard<std::mutex> lg0(m_issue_lock);
179+
std::lock_guard<std::mutex> lg1(m_state_lock);
180+
if (cancellable){
181+
cancellable->throw_if_cancelled();
182+
}
183+
184+
this->issue_wait_for_resource(cancellable, gyro);
185+
gyro.value = value;
186+
this->issue_to_resource(cancellable, gyro, delay, hold, cooldown);
187+
188+
if (m_logging_throttler){
189+
m_logger.log(
190+
std::string(name) + "(): (" + std::to_string(value) + ")" +
191+
", delay = " + std::to_string(delay.count()) + "ms" +
192+
", hold = " + std::to_string(hold.count()) + "ms" +
193+
", cooldown = " + std::to_string(cooldown.count()) + "ms",
194+
COLOR_DARKGREEN
195+
);
196+
}
197+
}
198+
199+
200+
169201
void ControllerWithScheduler::issue_full_controller_state(
170202
const Cancellable* cancellable,
171203
Milliseconds hold,

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerWithScheduler.h

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,39 @@ struct SwitchButton_Joystick : public ExecutionResource{
2727
uint8_t x;
2828
uint8_t y;
2929
};
30-
struct ProControllerSchedulerState{
30+
struct SwitchGyro : public ExecutionResource{
31+
int16_t value;
32+
};
33+
struct ControllerSchedulerState{
3134
ExecutionResource m_buttons[TOTAL_BUTTONS];
3235
SwitchButton_Dpad m_dpad;
3336
SwitchButton_Joystick m_left_joystick;
3437
SwitchButton_Joystick m_right_joystick;
3538

39+
SwitchGyro m_accel_x;
40+
SwitchGyro m_accel_y;
41+
SwitchGyro m_accel_z;
42+
SwitchGyro m_rotation_x;
43+
SwitchGyro m_rotation_y;
44+
SwitchGyro m_rotation_z;
45+
3646
bool is_active() const{
3747
for (size_t c = 0; c < TOTAL_BUTTONS; c++){
3848
if (m_buttons[c].is_busy()){
3949
return true;
4050
}
4151
}
42-
if (m_dpad.is_busy()){
43-
return true;
44-
}
45-
if (m_left_joystick.is_busy()){
46-
return true;
47-
}
48-
if (m_right_joystick.is_busy()){
49-
return true;
50-
}
52+
if (m_dpad.is_busy()) return true;
53+
if (m_left_joystick.is_busy()) return true;
54+
if (m_right_joystick.is_busy()) return true;
55+
56+
if (m_accel_x.is_busy()) return true;
57+
if (m_accel_y.is_busy()) return true;
58+
if (m_accel_z.is_busy()) return true;
59+
if (m_rotation_x.is_busy()) return true;
60+
if (m_rotation_y.is_busy()) return true;
61+
if (m_rotation_z.is_busy()) return true;
62+
5163
return false;
5264
}
5365

@@ -59,6 +71,12 @@ struct ProControllerSchedulerState{
5971
ret.emplace_back(&m_dpad);
6072
ret.emplace_back(&m_left_joystick);
6173
ret.emplace_back(&m_right_joystick);
74+
ret.emplace_back(&m_accel_x);
75+
ret.emplace_back(&m_accel_y);
76+
ret.emplace_back(&m_accel_z);
77+
ret.emplace_back(&m_rotation_x);
78+
ret.emplace_back(&m_rotation_y);
79+
ret.emplace_back(&m_rotation_z);
6280
return ret;
6381
}
6482
};
@@ -98,7 +116,7 @@ inline SplitDpad convert_unified_to_split_dpad(DpadPosition dpad){
98116

99117

100118
class ControllerWithScheduler :
101-
protected ProControllerSchedulerState,
119+
protected ControllerSchedulerState,
102120
protected SuperscalarScheduler
103121
{
104122
public:
@@ -134,6 +152,56 @@ class ControllerWithScheduler :
134152
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
135153
uint8_t x, uint8_t y
136154
);
155+
156+
void issue_gyro(
157+
const Cancellable* cancellable,
158+
SwitchGyro& gyro, const char* name,
159+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
160+
int16_t value
161+
);
162+
void issue_gyro_accel_x(
163+
const Cancellable* cancellable,
164+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
165+
int16_t value
166+
){
167+
issue_gyro(cancellable, m_accel_x, "issue_gyro_accel_x", delay, hold, cooldown, value);
168+
}
169+
void issue_gyro_accel_y(
170+
const Cancellable* cancellable,
171+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
172+
int16_t value
173+
){
174+
issue_gyro(cancellable, m_accel_y, "issue_gyro_accel_y", delay, hold, cooldown, value);
175+
}
176+
void issue_gyro_accel_z(
177+
const Cancellable* cancellable,
178+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
179+
int16_t value
180+
){
181+
issue_gyro(cancellable, m_accel_z, "issue_gyro_accel_z", delay, hold, cooldown, value);
182+
}
183+
void issue_gyro_rotate_x(
184+
const Cancellable* cancellable,
185+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
186+
int16_t value
187+
){
188+
issue_gyro(cancellable, m_rotation_x, "issue_gyro_rotate_x", delay, hold, cooldown, value);
189+
}
190+
void issue_gyro_rotate_y(
191+
const Cancellable* cancellable,
192+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
193+
int16_t value
194+
){
195+
issue_gyro(cancellable, m_rotation_y, "issue_gyro_rotate_y", delay, hold, cooldown, value);
196+
}
197+
void issue_gyro_rotate_z(
198+
const Cancellable* cancellable,
199+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
200+
int16_t value
201+
){
202+
issue_gyro(cancellable, m_rotation_z, "issue_gyro_rotate_z", delay, hold, cooldown, value);
203+
}
204+
137205
void issue_full_controller_state(
138206
const Cancellable* cancellable,
139207
Milliseconds hold,

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_Joycon.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,38 @@ class JoyconController : public AbstractController{
7575
Milliseconds delay, Milliseconds hold, Milliseconds cooldown
7676
) = 0;
7777

78+
// Gyro: Accelerometer (experimental - API subject to change)
79+
virtual void issue_gyro_accel_x(
80+
const Cancellable* cancellable,
81+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
82+
int16_t value
83+
) = 0;
84+
virtual void issue_gyro_accel_y(
85+
const Cancellable* cancellable,
86+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
87+
int16_t value
88+
) = 0;
89+
virtual void issue_gyro_accel_z(
90+
const Cancellable* cancellable,
91+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
92+
int16_t value
93+
) = 0;
94+
virtual void issue_gyro_rotate_x(
95+
const Cancellable* cancellable,
96+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
97+
int16_t value
98+
) = 0;
99+
virtual void issue_gyro_rotate_y(
100+
const Cancellable* cancellable,
101+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
102+
int16_t value
103+
) = 0;
104+
virtual void issue_gyro_rotate_z(
105+
const Cancellable* cancellable,
106+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
107+
int16_t value
108+
) = 0;
109+
78110
//
79111
// Press all the following buttons/joysticks simultaneously for the
80112
// specified duration. No wait is added at the end. Thus you can issue

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ProController.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,38 @@ class ProController : public AbstractController{
9393
uint8_t x, uint8_t y
9494
) = 0;
9595

96+
// Gyro: Accelerometer (experimental - API subject to change)
97+
virtual void issue_gyro_accel_x(
98+
const Cancellable* cancellable,
99+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
100+
int16_t value
101+
) = 0;
102+
virtual void issue_gyro_accel_y(
103+
const Cancellable* cancellable,
104+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
105+
int16_t value
106+
) = 0;
107+
virtual void issue_gyro_accel_z(
108+
const Cancellable* cancellable,
109+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
110+
int16_t value
111+
) = 0;
112+
virtual void issue_gyro_rotate_x(
113+
const Cancellable* cancellable,
114+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
115+
int16_t value
116+
) = 0;
117+
virtual void issue_gyro_rotate_y(
118+
const Cancellable* cancellable,
119+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
120+
int16_t value
121+
) = 0;
122+
virtual void issue_gyro_rotate_z(
123+
const Cancellable* cancellable,
124+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
125+
int16_t value
126+
) = 0;
127+
96128
//
97129
// Press all the following buttons/joysticks simultaneously for the
98130
// specified duration. No wait is added at the end. Thus you can issue

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_Controller.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,6 @@ void SerialPABotBase_Controller::wait_for_all(const Cancellable* cancellable){
9393

9494

9595

96-
void SerialPABotBase_Controller::send_botbase_request(
97-
const Cancellable* cancellable,
98-
const BotBaseRequest& request
99-
){
100-
std::lock_guard<std::mutex> lg0(m_issue_lock);
101-
std::lock_guard<std::mutex> lg1(m_state_lock);
102-
if (!is_ready()){
103-
throw InvalidConnectionStateException();
104-
}
105-
m_serial->issue_request(request, cancellable);
106-
}
107-
BotBaseMessage SerialPABotBase_Controller::send_botbase_request_and_wait(
108-
const Cancellable* cancellable,
109-
const BotBaseRequest& request
110-
){
111-
std::lock_guard<std::mutex> lg0(m_issue_lock);
112-
std::lock_guard<std::mutex> lg1(m_state_lock);
113-
if (!is_ready()){
114-
throw InvalidConnectionStateException();
115-
}
116-
return m_serial->issue_request_and_wait(request, cancellable);
117-
}
118-
11996

12097

12198

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_Controller.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ class SerialPABotBase_Controller : public ControllerWithScheduler{
3434

3535
void wait_for_all(const Cancellable* cancellable);
3636

37-
void send_botbase_request(
38-
const Cancellable* cancellable,
39-
const BotBaseRequest& request
40-
);
41-
BotBaseMessage send_botbase_request_and_wait(
42-
const Cancellable* cancellable,
43-
const BotBaseRequest& request
44-
);
45-
4637

4738
protected:
4839
// These are set on construction and never changed again. So it is safe to

0 commit comments

Comments
 (0)