Skip to content

Commit 95d414a

Browse files
committed
Rewrite the FastCodeEntry. All FCE programs (including auto-host) no longer require TickPrecise.
1 parent 7e13f1b commit 95d414a

File tree

8 files changed

+274
-201
lines changed

8 files changed

+274
-201
lines changed

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_DigitEntry.cpp

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,44 @@
44
*
55
*/
66

7-
//#include <cstring>
8-
//#include <sstream>
97
#include "ClientSource/Libraries/MessageConverter.h"
108
#include "NintendoSwitch_Commands_PushButtons.h"
119
#include "NintendoSwitch_Commands_Superscalar.h"
1210
#include "NintendoSwitch_Commands_DigitEntry.h"
13-
//#include "NintendoSwitch_Messages_DigitEntry.h"
14-
15-
16-
#if 0
17-
void enter_digits_str(uint8_t count, const char* digits){
18-
enter_digits(count, (const uint8_t*)digits);
19-
}
20-
void enter_digits(uint8_t count, const uint8_t* digits){
21-
enter_digits(*PokemonAutomation::global_connection, count, digits);
22-
}
23-
#endif
24-
2511

2612
namespace PokemonAutomation{
2713
namespace NintendoSwitch{
2814

2915

3016
#define CODE_ENTRY_FAST
3117

32-
// 3 seems to work maybe 95% of the time.
33-
#define CODE_DELAY 4
18+
//#define CODE_DELAY 4
3419

3520

3621

3722
const uint8_t XCORD[] = {1, 0, 1, 2, 0, 1, 2, 0, 1, 2};
3823
const uint8_t YCORD[] = {3, 0, 0, 0, 1, 1, 1, 2, 2, 2};
3924

40-
void code_entry_scroll(ProControllerContext& context, DpadPosition direction){
41-
pbf_wait(context, CODE_DELAY);
25+
void code_entry_scroll(
26+
ProControllerContext& context,
27+
DpadPosition direction,
28+
Milliseconds scroll_delay
29+
){
30+
pbf_wait(context, scroll_delay);
4231
ssf_issue_scroll(context, direction, 0);
4332
}
44-
uint16_t scroll_to(
33+
Milliseconds scroll_to(
4534
ProControllerContext& context,
46-
uint8_t start_digit, uint8_t end_digit, bool actually_scroll
35+
uint8_t start_digit, uint8_t end_digit, bool actually_scroll,
36+
Milliseconds scroll_delay
4737
){
4838
// Returns the # of ticks needed to scroll from "start_digit" to "end_digit".
4939
// If "actually_scroll" is true, then it does the scrolling as well.
5040

5141
uint8_t xcoord = XCORD[start_digit];
5242
uint8_t ycoord = YCORD[start_digit];
5343

54-
uint16_t cost = 0;
44+
Milliseconds cost = 0ms;
5545

5646
// These shortcuts over the OK and backspace buttons aren't stable.
5747
//#ifdef CODE_ENTRY_FAST
@@ -63,7 +53,7 @@ uint16_t scroll_to(
6353
// pbf_wait(1);
6454
// code_entry_scroll(SSF_SCROLL_RIGHT);
6555
// }
66-
// cost += 3 * CODE_DELAY;
56+
// cost += 3 * scroll_delay;
6757
// xcoord = 0;
6858
// ycoord = 0;
6959
// }
@@ -76,7 +66,7 @@ uint16_t scroll_to(
7666
//// pbf_wait(context, 1);
7767
// code_entry_scroll(SSF_SCROLL_LEFT);
7868
// }
79-
// cost += 3 * CODE_DELAY;
69+
// cost += 3 * scroll_delay;
8070
// xcoord = 2;
8171
// ycoord = 0;
8272
// }
@@ -89,9 +79,9 @@ uint16_t scroll_to(
8979
// If we need to move up, do it first since it's always safe to do so.
9080
if (ycoord > desired_y){
9181
if (actually_scroll){
92-
code_entry_scroll(context, SSF_SCROLL_UP);
82+
code_entry_scroll(context, SSF_SCROLL_UP, scroll_delay);
9383
}
94-
cost += CODE_DELAY;
84+
cost += scroll_delay;
9585
ycoord--;
9686
continue;
9787
}
@@ -101,9 +91,9 @@ uint16_t scroll_to(
10191
// it will automatically move to zero even if the X-coordinate is wrong.
10292
if (ycoord < desired_y){
10393
if (actually_scroll){
104-
code_entry_scroll(context, SSF_SCROLL_DOWN);
94+
code_entry_scroll(context, SSF_SCROLL_DOWN, scroll_delay);
10595
}
106-
cost += CODE_DELAY;
96+
cost += scroll_delay;
10797
ycoord++;
10898

10999
// If we land on zero, the X-axis is set to 1.
@@ -117,17 +107,17 @@ uint16_t scroll_to(
117107
// moved away from the bottom row if the digit is not zero.
118108
if (xcoord < desired_x){
119109
if (actually_scroll){
120-
code_entry_scroll(context, SSF_SCROLL_RIGHT);
110+
code_entry_scroll(context, SSF_SCROLL_RIGHT, scroll_delay);
121111
}
122-
cost += CODE_DELAY;
112+
cost += scroll_delay;
123113
xcoord++;
124114
continue;
125115
}
126116
if (xcoord > desired_x){
127117
if (actually_scroll){
128-
code_entry_scroll(context, SSF_SCROLL_LEFT);
118+
code_entry_scroll(context, SSF_SCROLL_LEFT, scroll_delay);
129119
}
130-
cost += CODE_DELAY;
120+
cost += scroll_delay;
131121
xcoord--;
132122
continue;
133123
}
@@ -143,9 +133,14 @@ uint16_t scroll_to(
143133

144134

145135
void enter_digits(ProControllerContext& context, uint8_t count, const uint8_t* digits){
146-
#if 0
147-
context.issue_request(DeviceRequest_enter_digits(count, digits));
148-
#else
136+
Milliseconds delay = 4 * 8ms;
137+
Milliseconds hold = 5 * 8ms;
138+
Milliseconds cool = 3 * 8ms;
139+
Milliseconds timing_variation = context->timing_variation();
140+
delay += timing_variation;
141+
hold += timing_variation;
142+
cool += timing_variation;
143+
149144
// Enter code.
150145
uint8_t last_digit = 1;
151146
uint8_t s = 0;
@@ -172,28 +167,28 @@ void enter_digits(ProControllerContext& context, uint8_t count, const uint8_t* d
172167
}
173168

174169
// Calculate costs to scroll to destination.
175-
uint16_t cost0 = scroll_to(context, last_digit, front, false);
176-
uint16_t cost1 = scroll_to(context, last_digit, back, false);
170+
Milliseconds cost0 = scroll_to(context, last_digit, front, false, delay);
171+
Milliseconds cost1 = scroll_to(context, last_digit, back, false, delay);
177172

178173
// Now actually scroll to the destination.
179174
uint8_t digit = cost0 <= cost1 ? front : back;
180-
cost1 = scroll_to(context, last_digit, digit, true);
175+
cost1 = scroll_to(context, last_digit, digit, true, delay);
181176

182177
// If no scrolling is needed, we still need to wait a non-zero time.
183178
//
184179
// This wait doesn't actually slow anything down since the A button
185180
// needs to wait 8 cycles anyway. Instead, this wait delays the
186181
// LB button so that it doesn't happen the same time as the A button
187182
// from the previous iteration.
188-
if (cost1 == 0){
189-
pbf_wait(context, CODE_DELAY);
183+
if (cost1 == 0ms){
184+
pbf_wait(context, delay);
190185
}
191186
if (shift_left){
192-
ssf_press_button(context, BUTTON_L, 0);
187+
ssf_press_button(context, BUTTON_L, timing_variation, hold, cool);
193188
}
194189

195190
// Enter digit.
196-
ssf_press_button(context, BUTTON_A, 0);
191+
ssf_press_button(context, BUTTON_A, timing_variation, hold, cool);
197192

198193
// Update pointers.
199194
if (cost0 <= cost1){
@@ -216,16 +211,16 @@ void enter_digits(ProControllerContext& context, uint8_t count, const uint8_t* d
216211
scroll_to(context, last_digit, digit, true);
217212

218213
// Enter digit.
219-
ssf_press_button(context, BUTTON_A, 0);
214+
ssf_press_button(context, BUTTON_A, 0ms, hold, cool);
220215

221216
s++;
222217
last_digit = digit;
223218
}
224219
#endif
225-
pbf_wait(context, CODE_DELAY);
226-
ssf_press_button(context, BUTTON_PLUS, CODE_DELAY);
227-
ssf_press_button(context, BUTTON_PLUS, CODE_DELAY);
228-
#endif
220+
221+
pbf_wait(context, delay);
222+
ssf_press_button(context, BUTTON_PLUS, delay);
223+
ssf_press_button(context, BUTTON_PLUS, delay);
229224
}
230225

231226

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,29 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
282282
VideoOverlaySet overlays(overlay);
283283

284284

285-
return_to_academy_after_loss(env, console, context);
285+
// ssf_issue_scroll(context, DPAD_LEFT, 0);
286+
// ssf_press_button(context, BUTTON_A | BUTTON_L, 3);
287+
// ssf_press_button(context, BUTTON_L, 0);
288+
289+
#if 1
290+
codeboard_enter_digits(
291+
logger, context, KeyboardLayout::QWERTY,
292+
"JRH5T9",
293+
true,
294+
CodeboardDelays{
295+
.hold = 5 * 8ms,
296+
.cool = 3 * 8ms,
297+
.press_delay = 4 * 8ms * 1,
298+
.move_delay = 5 * 8ms * 1,
299+
.wrap_delay = 6 * 8ms * 1,
300+
}
301+
);
302+
#endif
303+
304+
ssf_flush_pipeline(context);
305+
306+
307+
// return_to_academy_after_loss(env, console, context);
286308

287309

288310

0 commit comments

Comments
 (0)