Skip to content

Commit 6dd9c62

Browse files
committed
Big refactor to generalize serial -> controller. Still more work to do.
1 parent 173967a commit 6dd9c62

File tree

55 files changed

+1527
-743
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1527
-743
lines changed

Common/Cpp/LifetimeSanitizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace PokemonAutomation{
2626
const std::set<std::string> SANITIZER_FILTER = {
2727
// "MultiSwitchProgramSession",
2828
// "MultiSwitchProgramWidget2",
29+
// "ControllerRequirements",
2930
};
3031

3132
SpinLock sanitizer_lock;

SerialPrograms/CMakeLists.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ file(GLOB MAIN_SOURCES
115115
../Common/Cpp/Json/JsonValue.h
116116
../Common/Cpp/LifetimeSanitizer.cpp
117117
../Common/Cpp/LifetimeSanitizer.h
118+
../Common/Cpp/ListenerSet.h
118119
../Common/Cpp/Options/BatchOption.cpp
119120
../Common/Cpp/Options/BatchOption.h
120121
../Common/Cpp/Options/BooleanCheckBoxOption.cpp
@@ -599,15 +600,23 @@ file(GLOB MAIN_SOURCES
599600
Source/ComputerPrograms/Framework/ComputerProgramSession.h
600601
Source/ComputerPrograms/Framework/ComputerProgramWidget.cpp
601602
Source/ComputerPrograms/Framework/ComputerProgramWidget.h
603+
Source/Controllers/ControllerCapabilities.h
604+
Source/Controllers/Controllers.cpp
605+
Source/Controllers/Controllers.h
606+
Source/Controllers/ControllerConnection.cpp
607+
Source/Controllers/ControllerConnection.h
608+
Source/Controllers/ControllerSession.cpp
609+
Source/Controllers/ControllerSession.h
610+
Source/Controllers/ControllerSelectorWidget.cpp
611+
Source/Controllers/ControllerSelectorWidget.h
612+
Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.cpp
613+
Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.h
614+
Source/Controllers/SerialPABotBase/SerialPABotBase_Descriptor.cpp
615+
Source/Controllers/SerialPABotBase/SerialPABotBase_Descriptor.h
616+
Source/Controllers/SerialPABotBase/SerialPABotBase_Handle.cpp
602617
Source/Controllers/SerialPABotBase/SerialPABotBase_Globals.h
603618
Source/Controllers/SerialPABotBase/SerialPABotBase_Handle.cpp
604619
Source/Controllers/SerialPABotBase/SerialPABotBase_Handle.h
605-
Source/Controllers/SerialPABotBase/SerialPABotBase_Option.cpp
606-
Source/Controllers/SerialPABotBase/SerialPABotBase_Option.h
607-
Source/Controllers/SerialPABotBase/SerialPABotBase_Session.cpp
608-
Source/Controllers/SerialPABotBase/SerialPABotBase_Session.h
609-
Source/Controllers/SerialPABotBase/SerialPABotBase_Widget.cpp
610-
Source/Controllers/SerialPABotBase/SerialPABotBase_Widget.h
611620
Source/Integrations/DiscordIntegrationSettings.cpp
612621
Source/Integrations/DiscordIntegrationSettings.h
613622
Source/Integrations/DiscordIntegrationTable.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,13 @@ SOURCES += \
305305
Source/ComputerPrograms/Framework/ComputerProgramOption.cpp \
306306
Source/ComputerPrograms/Framework/ComputerProgramSession.cpp \
307307
Source/ComputerPrograms/Framework/ComputerProgramWidget.cpp \
308+
Source/Controllers/ControllerConnection.cpp \
309+
Source/Controllers/ControllerSelectorWidget.cpp \
310+
Source/Controllers/ControllerSession.cpp \
311+
Source/Controllers/Controllers.cpp \
312+
Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.cpp \
313+
Source/Controllers/SerialPABotBase/SerialPABotBase_Descriptor.cpp \
308314
Source/Controllers/SerialPABotBase/SerialPABotBase_Handle.cpp \
309-
Source/Controllers/SerialPABotBase/SerialPABotBase_Option.cpp \
310-
Source/Controllers/SerialPABotBase/SerialPABotBase_Session.cpp \
311-
Source/Controllers/SerialPABotBase/SerialPABotBase_Widget.cpp \
312315
Source/Integrations/DiscordIntegrationSettings.cpp \
313316
Source/Integrations/DiscordIntegrationTable.cpp \
314317
Source/Integrations/DiscordSettingsOption.cpp \
@@ -1147,6 +1150,7 @@ HEADERS += \
11471150
../Common/Cpp/Json/JsonTools.h \
11481151
../Common/Cpp/Json/JsonValue.h \
11491152
../Common/Cpp/LifetimeSanitizer.h \
1153+
../Common/Cpp/ListenerSet.h \
11501154
../Common/Cpp/Options/BatchOption.h \
11511155
../Common/Cpp/Options/BooleanCheckBoxOption.h \
11521156
../Common/Cpp/Options/ButtonOption.h \
@@ -1415,11 +1419,15 @@ HEADERS += \
14151419
Source/ComputerPrograms/Framework/ComputerProgramOption.h \
14161420
Source/ComputerPrograms/Framework/ComputerProgramSession.h \
14171421
Source/ComputerPrograms/Framework/ComputerProgramWidget.h \
1422+
Source/Controllers/ControllerCapabilities.h \
1423+
Source/Controllers/ControllerConnection.h \
1424+
Source/Controllers/ControllerSelectorWidget.h \
1425+
Source/Controllers/ControllerSession.h \
1426+
Source/Controllers/Controllers.h \
1427+
Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.h \
1428+
Source/Controllers/SerialPABotBase/SerialPABotBase_Descriptor.h \
14181429
Source/Controllers/SerialPABotBase/SerialPABotBase_Globals.h \
14191430
Source/Controllers/SerialPABotBase/SerialPABotBase_Handle.h \
1420-
Source/Controllers/SerialPABotBase/SerialPABotBase_Option.h \
1421-
Source/Controllers/SerialPABotBase/SerialPABotBase_Session.h \
1422-
Source/Controllers/SerialPABotBase/SerialPABotBase_Widget.h \
14231431
Source/Integrations/DiscordIntegrationSettings.h \
14241432
Source/Integrations/DiscordIntegrationTable.h \
14251433
Source/Integrations/DiscordSettingsOption.h \

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,34 @@ FileWindowLogger::FileWindowLogger(const std::string& path)
5959
m_thread = std::thread(&FileWindowLogger::thread_loop, this);
6060
}
6161
void FileWindowLogger::operator+=(FileWindowLoggerWindow& widget){
62-
auto scope_check = m_sanitizer.check_scope();
62+
// auto scope_check = m_sanitizer.check_scope();
6363
std::lock_guard<std::mutex> lg(m_lock);
6464
m_windows.insert(&widget);
6565
}
6666
void FileWindowLogger::operator-=(FileWindowLoggerWindow& widget){
67-
auto scope_check = m_sanitizer.check_scope();
67+
// auto scope_check = m_sanitizer.check_scope();
6868
std::lock_guard<std::mutex> lg(m_lock);
6969
m_windows.erase(&widget);
7070
}
7171

7272
void FileWindowLogger::log(const std::string& msg, Color color){
73-
auto scope_check = m_sanitizer.check_scope();
73+
// auto scope_check = m_sanitizer.check_scope();
7474
std::unique_lock<std::mutex> lg(m_lock);
7575
m_last_log_tracker += msg;
7676
m_cv.wait(lg, [this]{ return m_queue.size() < m_max_queue_size; });
7777
m_queue.emplace_back(msg, color);
7878
m_cv.notify_all();
7979
}
8080
void FileWindowLogger::log(std::string&& msg, Color color){
81-
auto scope_check = m_sanitizer.check_scope();
81+
// auto scope_check = m_sanitizer.check_scope();
8282
std::unique_lock<std::mutex> lg(m_lock);
8383
m_last_log_tracker += msg;
8484
m_cv.wait(lg, [this]{ return m_queue.size() < m_max_queue_size; });
8585
m_queue.emplace_back(std::move(msg), color);
8686
m_cv.notify_all();
8787
}
8888
std::vector<std::string> FileWindowLogger::get_last() const{
89-
auto scope_check = m_sanitizer.check_scope();
89+
// auto scope_check = m_sanitizer.check_scope();
9090
std::unique_lock<std::mutex> lg(m_lock);
9191
return m_last_log_tracker.snapshot();
9292
}
@@ -160,7 +160,7 @@ QString FileWindowLogger::to_window_str(const std::string& msg, Color color){
160160
return QString::fromStdString(str);
161161
}
162162
void FileWindowLogger::internal_log(const std::string& msg, Color color){
163-
auto scope_check = m_sanitizer.check_scope();
163+
// auto scope_check = m_sanitizer.check_scope();
164164
std::string line = normalize_newlines(msg);
165165
{
166166
if (!m_windows.empty()){
@@ -176,7 +176,7 @@ void FileWindowLogger::internal_log(const std::string& msg, Color color){
176176
}
177177
}
178178
void FileWindowLogger::thread_loop(){
179-
auto scope_check = m_sanitizer.check_scope();
179+
// auto scope_check = m_sanitizer.check_scope();
180180
std::unique_lock<std::mutex> lg(m_lock);
181181
while (true){
182182
m_cv.wait(lg, [&]{

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <QFile>
1616
#include <QTextEdit>
1717
#include <QMainWindow>
18-
#include "Common/Cpp/LifetimeSanitizer.h"
19-
#include "Logger.h"
18+
#include "Common/Cpp/AbstractLogger.h"
19+
//#include "Common/Cpp/LifetimeSanitizer.h"
2020

2121
namespace PokemonAutomation{
2222

@@ -68,7 +68,7 @@ class FileWindowLogger : public Logger{
6868
std::set<FileWindowLoggerWindow*> m_windows;
6969
std::thread m_thread;
7070

71-
LifetimeSanitizer m_sanitizer;
71+
// LifetimeSanitizer m_sanitizer;
7272
};
7373

7474

SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ CollapsibleGroupBox* make_panel_header(
5555
const std::string& display_name,
5656
const std::string& doc_link,
5757
const std::string& description,
58-
FeedbackType feedback,
59-
PABotBaseLevel serial_level
58+
FeedbackType feedback
6059
){
6160
CollapsibleGroupBox* header = make_panel_header(parent, display_name, doc_link, description);
6261
QLayout* layout = header->widget()->layout();
@@ -91,6 +90,7 @@ CollapsibleGroupBox* make_panel_header(
9190
text->setWordWrap(true);
9291
layout->addWidget(text);
9392

93+
#if 0
9494
switch (serial_level){
9595
case PABotBaseLevel::NOT_PABOTBASE:
9696
break;
@@ -114,6 +114,7 @@ CollapsibleGroupBox* make_panel_header(
114114
break;
115115
}
116116
}
117+
#endif
117118
return header;
118119
}
119120

SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ CollapsibleGroupBox* make_panel_header(
3030
const std::string& display_name,
3131
const std::string& doc_link,
3232
const std::string& description,
33-
FeedbackType feedback,
34-
PABotBaseLevel serial_level
33+
FeedbackType feedback
3534
);
3635

3736

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Controller Capabilities
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Controllers_ControllerCapabilities_H
8+
#define PokemonAutomation_Controllers_ControllerCapabilities_H
9+
10+
#include <string>
11+
#include <set>
12+
#include <map>
13+
#include "Common/Cpp/LifetimeSanitizer.h"
14+
15+
//#include <iostream>
16+
//using std::cout;
17+
//using std::endl;
18+
19+
namespace PokemonAutomation{
20+
21+
22+
23+
class ControllerRequirements{
24+
public:
25+
#if 0
26+
template <class... Args>
27+
ControllerRequirements(Args&&... args)
28+
: m_map(std::forward<Args>(args)...)
29+
, m_sanitizer("ControllerRequirements")
30+
{}
31+
#endif
32+
ControllerRequirements(std::initializer_list<std::map<std::string, std::set<std::string>>::value_type> args)
33+
: m_map(std::move(args))
34+
, m_sanitizer("ControllerRequirements")
35+
{}
36+
37+
const std::map<std::string, std::set<std::string>>& map() const{
38+
auto scope_check = m_sanitizer.check_scope();
39+
return m_map;
40+
}
41+
42+
bool is_compatible_with(const std::string& device, const std::set<std::string>& features) const{
43+
auto scope_check = m_sanitizer.check_scope();
44+
45+
auto iter0 = m_map.find(device);
46+
if (iter0 == m_map.end()){
47+
return false;
48+
}
49+
50+
const std::set<std::string>& required = iter0->second;
51+
for (const std::string& feature : required){
52+
if (features.find(feature) == features.end()){
53+
return false;
54+
}
55+
}
56+
return true;
57+
}
58+
59+
60+
private:
61+
std::map<std::string, std::set<std::string>> m_map;
62+
63+
LifetimeSanitizer m_sanitizer;
64+
};
65+
66+
67+
68+
69+
}
70+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Controller Connection
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#include "ControllerConnection.h"
8+
9+
//#include <iostream>
10+
//using std::cout;
11+
//using std::endl;
12+
13+
namespace PokemonAutomation{
14+
15+
16+
17+
void ControllerConnection::add_status_listener(StatusListener& listener){
18+
m_status_listeners.add(listener);
19+
}
20+
void ControllerConnection::remove_status_listener(StatusListener& listener){
21+
m_status_listeners.remove(listener);
22+
}
23+
24+
std::string ControllerConnection::status_text() const{
25+
SpinLockGuard lg(m_status_text_lock);
26+
return m_status_text;
27+
}
28+
29+
void ControllerConnection::signal_ready_changed(bool ready){
30+
m_status_listeners.run_method_unique(&StatusListener::ready_changed, ready);
31+
}
32+
void ControllerConnection::signal_status_text_changed(const std::string& text){
33+
// cout << "m_status_listeners.size() = " << m_status_listeners.count_unique() << endl;
34+
m_status_listeners.run_method_unique(&StatusListener::status_text_changed, text);
35+
}
36+
37+
38+
39+
40+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Controller Connection
2+
*
3+
* From: https://github.com/PokemonAutomation/Arduino-Source
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Controllers_ControllerConnection_H
8+
#define PokemonAutomation_Controllers_ControllerConnection_H
9+
10+
#include "Common/Cpp/ListenerSet.h"
11+
#include "Common/Cpp/Concurrency/SpinLock.h"
12+
#include "Controllers.h"
13+
14+
namespace PokemonAutomation{
15+
16+
class BotBaseRequest; // REMOVE
17+
18+
19+
class ControllerConnection{
20+
public:
21+
struct StatusListener{
22+
virtual void ready_changed(bool ready) = 0;
23+
virtual void status_text_changed(const std::string& text) = 0;
24+
};
25+
26+
void add_status_listener(StatusListener& listener);
27+
void remove_status_listener(StatusListener& listener);
28+
29+
30+
public:
31+
virtual ~ControllerConnection() = default;
32+
33+
bool ready() const{ return m_ready.load(std::memory_order_acquire); }
34+
std::string status_text() const;
35+
36+
37+
public:
38+
virtual std::string stop_pending_commands() = 0;
39+
virtual std::string set_next_command_replace() = 0;
40+
virtual std::string send_request(const BotBaseRequest& request) = 0; // REMOVE
41+
42+
protected:
43+
void signal_ready_changed(bool ready);
44+
void signal_status_text_changed(const std::string& text);
45+
46+
protected:
47+
std::atomic<bool> m_ready;
48+
49+
mutable SpinLock m_status_text_lock;
50+
std::string m_status_text;
51+
52+
private:
53+
ListenerSet<StatusListener> m_status_listeners;
54+
};
55+
56+
57+
58+
59+
}
60+
#endif

0 commit comments

Comments
 (0)