Skip to content

Commit 9265dcd

Browse files
committed
More work on keyboard.
1 parent 155b1bc commit 9265dcd

27 files changed

+679
-215
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,13 @@ file(GLOB MAIN_SOURCES
697697
Source/Controllers/ControllerTypeStrings.cpp
698698
Source/Controllers/ControllerTypeStrings.h
699699
Source/Controllers/ControllerTypes.h
700-
Source/Controllers/HID_Keyboard.cpp
701-
Source/Controllers/HID_Keyboard.h
700+
Source/Controllers/HidControllers/HID_Keyboard.cpp
701+
Source/Controllers/HidControllers/HID_Keyboard.h
702+
Source/Controllers/HidControllers/HID_Keyboard_ControllerButtons.h
703+
Source/Controllers/HidControllers/HID_Keyboard_SerialPABotBase.cpp
704+
Source/Controllers/HidControllers/HID_Keyboard_SerialPABotBase.h
705+
Source/Controllers/HidControllers/HID_KeyboardWithScheduler.cpp
706+
Source/Controllers/HidControllers/HID_KeyboardWithScheduler.h
702707
Source/Controllers/JoystickTools.h
703708
Source/Controllers/KeyboardInput/GlobalQtKeyMap.cpp
704709
Source/Controllers/KeyboardInput/GlobalQtKeyMap.h
@@ -708,6 +713,9 @@ file(GLOB MAIN_SOURCES
708713
Source/Controllers/KeyboardInput/KeyboardStateTracker.h
709714
Source/Controllers/NullController.cpp
710715
Source/Controllers/NullController.h
716+
Source/Controllers/Schedulers/ControllerWithScheduler.h
717+
Source/Controllers/Schedulers/SuperscalarScheduler.cpp
718+
Source/Controllers/Schedulers/SuperscalarScheduler.h
711719
Source/Controllers/SerialPABotBase/SerialPABotBase.cpp
712720
Source/Controllers/SerialPABotBase/SerialPABotBase.h
713721
Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.cpp
@@ -721,8 +729,6 @@ file(GLOB MAIN_SOURCES
721729
Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_Protocol.cpp
722730
Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_Protocol.h
723731
Source/Controllers/SerialPABotBase/SerialPABotBase_SelectorWidget.h
724-
Source/Controllers/SuperscalarScheduler.cpp
725-
Source/Controllers/SuperscalarScheduler.h
726732
Source/Integrations/DiscordIntegrationSettings.cpp
727733
Source/Integrations/DiscordIntegrationSettings.h
728734
Source/Integrations/DiscordIntegrationTable.cpp
@@ -958,10 +964,10 @@ file(GLOB MAIN_SOURCES
958964
Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Routines.h
959965
Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.cpp
960966
Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h
967+
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.cpp
968+
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h
961969
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerSettings.cpp
962970
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerSettings.h
963-
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerState.cpp
964-
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerState.h
965971
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerWithScheduler.cpp
966972
Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerWithScheduler.h
967973
Source/NintendoSwitch/Controllers/NintendoSwitch_Joycon.cpp

SerialPrograms/Source/Controllers/ControllerTypeStrings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS{
1919

2020
const EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS{
2121
{ControllerType::None, "None"},
22+
{ControllerType::HID_Keyboard, "HID: Keyboard"},
2223
{ControllerType::NintendoSwitch_WiredController, "NS1: Wired Controller"},
2324
{ControllerType::NintendoSwitch_WiredProController, "NS1: Wired Pro Controller"},
2425
{ControllerType::NintendoSwitch_WirelessProController, "NS1: Wireless Pro Controller"},

SerialPrograms/Source/Controllers/ControllerTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ enum class ControllerInterface{
2222
enum class ControllerType{
2323
None,
2424

25+
HID_Keyboard,
26+
2527
NintendoSwitch_WiredController, // Generic 3rd party wired controller.
2628
NintendoSwitch_WiredProController, // The official Pro Controller, connected over USB.
2729
NintendoSwitch_WirelessProController,

SerialPrograms/Source/Controllers/HID_Keyboard.cpp renamed to SerialPrograms/Source/Controllers/HidControllers/HID_Keyboard.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ void Keyboard::stop() noexcept{
277277
}
278278

279279

280+
#if 0
280281
void Keyboard::keyboard_release_all(){
281282
// m_keyboard_manager->clear_state();
282283
}
@@ -286,7 +287,7 @@ void Keyboard::keyboard_press(const QKeyEvent& event){
286287
void Keyboard::keyboard_release(const QKeyEvent& event){
287288
// m_keyboard_manager->on_key_release(event);
288289
}
289-
290+
#endif
290291

291292

292293

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* HID Keyboard
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
* This is the raw (full) controller API that is exposed to programs.
6+
*
7+
*/
8+
9+
#ifndef PokemonAutomation_HID_Keyboard_H
10+
#define PokemonAutomation_HID_Keyboard_H
11+
12+
#include <vector>
13+
#include "Common/Cpp/Containers/Pimpl.h"
14+
#include "Controllers/Controller.h"
15+
#include "HID_Keyboard_ControllerButtons.h"
16+
17+
namespace PokemonAutomation{
18+
namespace HidControllers{
19+
20+
21+
class Keyboard;
22+
using KeyboardContext = ControllerContext<Keyboard>;
23+
24+
25+
26+
27+
class Keyboard : public AbstractController{
28+
public:
29+
using ContextType = KeyboardContext;
30+
31+
Keyboard(Logger& logger);
32+
virtual ~Keyboard();
33+
34+
35+
public:
36+
static const char NAME[];
37+
virtual const char* name() override{
38+
return NAME;
39+
};
40+
41+
42+
protected:
43+
// Must call before destruction begins.
44+
void stop() noexcept;
45+
46+
47+
public:
48+
// Standard Commands
49+
50+
virtual void issue_key(
51+
const Cancellable* cancellable,
52+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
53+
KeyboardKey key
54+
) = 0;
55+
virtual void issue_keys(
56+
const Cancellable* cancellable,
57+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
58+
const std::vector<KeyboardKey>& keys
59+
) = 0;
60+
61+
62+
public:
63+
// Keyboard Input
64+
65+
// virtual void keyboard_release_all() override;
66+
// virtual void keyboard_press(const QKeyEvent& event) override;
67+
// virtual void keyboard_release(const QKeyEvent& event) override;
68+
69+
70+
private:
71+
// class KeyboardManager;
72+
// Pimpl<KeyboardManager> m_keyboard_manager;
73+
};
74+
75+
76+
77+
78+
}
79+
}
80+
#endif
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* HID Keyboard Controller (With Scheduler)
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "HID_KeyboardWithScheduler.h"
8+
9+
namespace PokemonAutomation{
10+
namespace HidControllers{
11+
12+
13+
14+
void KeyboardControllerWithScheduler::issue_key(
15+
const Cancellable* cancellable,
16+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
17+
KeyboardKey key
18+
){
19+
SuperscalarScheduler::Schedule schedule;
20+
std::lock_guard<std::mutex> lg0(m_issue_lock);
21+
{
22+
if (cancellable){
23+
cancellable->throw_if_cancelled();
24+
}
25+
m_scheduler.issue_to_resource(
26+
schedule, std::make_unique<KeyboardCommand>(key),
27+
delay, hold, cooldown
28+
);
29+
}
30+
execute_schedule(cancellable, schedule);
31+
if (m_logging_throttler){
32+
m_logger.log(
33+
"issue_key(): " + std::to_string((size_t)key) +
34+
", delay = " + std::to_string(delay.count()) + "ms" +
35+
", hold = " + std::to_string(hold.count()) + "ms" +
36+
", cooldown = " + std::to_string(cooldown.count()) + "ms",
37+
COLOR_DARKGREEN
38+
);
39+
}
40+
}
41+
void KeyboardControllerWithScheduler::issue_keys(
42+
const Cancellable* cancellable,
43+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
44+
const std::vector<KeyboardKey>& keys
45+
){
46+
SuperscalarScheduler::Schedule schedule;
47+
std::lock_guard<std::mutex> lg0(m_issue_lock);
48+
{
49+
if (cancellable){
50+
cancellable->throw_if_cancelled();
51+
}
52+
for (KeyboardKey key : keys){
53+
m_scheduler.issue_wait_for_resource(schedule, (size_t)key);
54+
}
55+
for (KeyboardKey key : keys){
56+
m_scheduler.issue_to_resource(
57+
schedule, std::make_unique<KeyboardCommand>(key),
58+
WallDuration::zero(), hold, cooldown
59+
);
60+
}
61+
m_scheduler.issue_nop(schedule, delay);
62+
}
63+
execute_schedule(cancellable, schedule);
64+
if (m_logging_throttler){
65+
m_logger.log(
66+
"issue_keys(): "
67+
"delay = " + std::to_string(delay.count()) + "ms" +
68+
", hold = " + std::to_string(hold.count()) + "ms" +
69+
", cooldown = " + std::to_string(cooldown.count()) + "ms",
70+
COLOR_DARKGREEN
71+
);
72+
}
73+
}
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
}
98+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* HID Keyboard Controller (With Scheduler)
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
* This implements most of the Keyboard API using only the controller
6+
* state function. It uses SuperscalarScheduler to do this.
7+
*
8+
*/
9+
10+
#ifndef PokemonAutomation_HID_KeyboardWithScheduler_H
11+
#define PokemonAutomation_HID_KeyboardWithScheduler_H
12+
13+
#include <vector>
14+
#include <set>
15+
#include "Common/Cpp/CancellableScope.h"
16+
#include "Controllers/Schedulers/ControllerWithScheduler.h"
17+
#include "HID_Keyboard_ControllerButtons.h"
18+
19+
namespace PokemonAutomation{
20+
namespace HidControllers{
21+
22+
23+
24+
struct KeyboardControllerState{
25+
std::set<KeyboardKey> keys;
26+
};
27+
28+
class KeyboardCommand : public SchedulerResource{
29+
public:
30+
KeyboardCommand(KeyboardKey key)
31+
: SchedulerResource((size_t)key)
32+
{}
33+
};
34+
35+
36+
37+
class KeyboardControllerWithScheduler : public PokemonAutomation::ControllerWithScheduler{
38+
public:
39+
using PokemonAutomation::ControllerWithScheduler::ControllerWithScheduler;
40+
41+
42+
public:
43+
// Superscalar Commands (the "ssf" framework)
44+
45+
void issue_key(
46+
const Cancellable* cancellable,
47+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
48+
KeyboardKey key
49+
);
50+
void issue_keys(
51+
const Cancellable* cancellable,
52+
Milliseconds delay, Milliseconds hold, Milliseconds cooldown,
53+
const std::vector<KeyboardKey>& keys
54+
);
55+
};
56+
57+
58+
59+
60+
}
61+
}
62+
#endif

0 commit comments

Comments
 (0)