88#include " Common/Cpp/PrettyPrint.h"
99#include " Common/Cpp/Concurrency/ReverseLockGuard.h"
1010#include " Common/NintendoSwitch/NintendoSwitch_Protocol_ESP32.h"
11+ #include " Common/PokemonSwSh/PokemonProgramIDs.h"
1112#include " ClientSource/Libraries/MessageConverter.h"
1213#include " ClientSource/Connection/BotBaseMessage.h"
1314#include " CommonFramework/GlobalSettingsPanel.h"
1415#include " CommonFramework/Options/Environment/ThemeSelectorOption.h"
1516#include " NintendoSwitch_SerialPABotBase_WirelessController.h"
1617
18+ // #include <iostream>
19+ // using std::cout;
20+ // using std::endl;
21+
1722namespace PokemonAutomation {
1823namespace NintendoSwitch {
1924
@@ -32,6 +37,7 @@ SerialPABotBase_WirelessController::SerialPABotBase_WirelessController(
3237 controller_type,
3338 connection
3439 )
40+ , m_controller_type(controller_type)
3541 , m_stopping(false )
3642 , m_status_thread(&SerialPABotBase_WirelessController::status_thread, this )
3743{}
@@ -57,7 +63,7 @@ void SerialPABotBase_WirelessController::stop(){
5763
5864class SerialPABotBase_WirelessController ::MessageControllerStatus : public BotBaseRequest{
5965public:
60- pabb_esp32_RequestStatus params;
66+ pabb_esp32_request_status params;
6167 MessageControllerStatus ()
6268 : BotBaseRequest(false )
6369 {
@@ -67,6 +73,19 @@ class SerialPABotBase_WirelessController::MessageControllerStatus : public BotBa
6773 return BotBaseMessage (PABB_MSG_ESP32_REQUEST_STATUS, params);
6874 }
6975};
76+ class SerialPABotBase_WirelessController ::MessageControllerColors : public BotBaseRequest{
77+ public:
78+ pabb_esp32_get_colors params;
79+ MessageControllerColors (uint32_t controller_type)
80+ : BotBaseRequest(false )
81+ {
82+ params.seqnum = 0 ;
83+ params.controller_type = controller_type;
84+ }
85+ virtual BotBaseMessage message () const override {
86+ return BotBaseMessage (PABB_MSG_ESP32_REQUEST_GET_COLORS, params);
87+ }
88+ };
7089class SerialPABotBase_WirelessController ::MessageControllerState : public BotBaseRequest{
7190public:
7291 pabb_esp32_report30 params;
@@ -93,8 +112,31 @@ int register_message_converters_ESP32(){
93112 }
94113 std::ostringstream ss;
95114 ss << " ESP32_controller_status() - " ;
96- if (body.size () != sizeof (pabb_esp32_RequestStatus)){ ss << " (invalid size)" << std::endl; return ss.str (); }
97- const auto * params = (const pabb_esp32_RequestStatus*)body.c_str ();
115+ if (body.size () != sizeof (pabb_esp32_request_status)){ ss << " (invalid size)" << std::endl; return ss.str (); }
116+ const auto * params = (const pabb_esp32_request_status*)body.c_str ();
117+ ss << " seqnum = " << (uint64_t )params->seqnum ;
118+ return ss.str ();
119+ }
120+ );
121+ register_message_converter (
122+ PABB_MSG_ESP32_REQUEST_GET_COLORS,
123+ [](const std::string& body){
124+ std::ostringstream ss;
125+ ss << " pabb_esp32_get_colors() - " ;
126+ if (body.size () != sizeof (pabb_esp32_get_colors)){ ss << " (invalid size)" << std::endl; return ss.str (); }
127+ const auto * params = (const pabb_esp32_get_colors*)body.c_str ();
128+ ss << " seqnum = " << (uint64_t )params->seqnum ;
129+ ss << " , controller = " << (uint64_t )params->controller_type ;
130+ return ss.str ();
131+ }
132+ );
133+ register_message_converter (
134+ PABB_MSG_ESP32_REQUEST_SET_COLORS,
135+ [](const std::string& body){
136+ std::ostringstream ss;
137+ ss << " pabb_esp32_set_colors() - " ;
138+ if (body.size () != sizeof (pabb_esp32_set_colors)){ ss << " (invalid size)" << std::endl; return ss.str (); }
139+ const auto * params = (const pabb_esp32_set_colors*)body.c_str ();
98140 ss << " seqnum = " << (uint64_t )params->seqnum ;
99141 return ss.str ();
100142 }
@@ -154,6 +196,68 @@ void SerialPABotBase_WirelessController::status_thread(){
154196 constexpr std::chrono::milliseconds PERIOD (1000 );
155197 std::atomic<WallClock> last_ack (current_time ());
156198
199+ // Read controller colors.
200+ std::string color_html;
201+ #if 1
202+ try {
203+ m_logger.log (" Reading Controller Colors..." );
204+ uint32_t controller_id = PABB_CID_NONE;
205+ switch (m_controller_type){
206+ case ControllerType::NintendoSwitch_WirelessProController:
207+ controller_id = PABB_CID_NINTENDO_SWITCH_WIRELESS_PRO_CONTROLLER;
208+ break ;
209+ case ControllerType::NintendoSwitch_LeftJoycon:
210+ controller_id = PABB_CID_NINTENDO_SWITCH_LEFT_JOYCON;
211+ break ;
212+ case ControllerType::NintendoSwitch_RightJoycon:
213+ controller_id = PABB_CID_NINTENDO_SWITCH_RIGHT_JOYCON;
214+ break ;
215+ default :;
216+ }
217+ BotBaseMessage response = m_serial->issue_request_and_wait (
218+ MessageControllerColors (controller_id),
219+ &m_scope
220+ );
221+ ControllerColors colors{};
222+ if (response.body .size () == sizeof (seqnum_t ) + sizeof (ControllerColors)){
223+ memcpy (&colors, response.body .data () + sizeof (seqnum_t ), sizeof (ControllerColors));
224+ }else {
225+ m_logger.log (
226+ " Invalid response size to PABB_MSG_ESP32_GET_COLORS: body = " + std::to_string (response.body .size ()),
227+ COLOR_RED
228+ );
229+ m_handle.set_status_line1 (" Error: See log for more information." , COLOR_RED);
230+ return ;
231+ }
232+ m_logger.log (" Reading Controller Colors... Done" );
233+
234+ switch (m_controller_type){
235+ case ControllerType::NintendoSwitch_WirelessProController:{
236+ Color left (colors.left_grip [0 ], colors.left_grip [1 ], colors.left_grip [2 ]);
237+ Color body (colors.body [0 ], colors.body [1 ], colors.body [2 ]);
238+ Color right (colors.right_grip [0 ], colors.right_grip [1 ], colors.right_grip [2 ]);
239+ color_html += html_color_text (" ⬤" , left);
240+ color_html += " " + html_color_text (" ⬤" , body);
241+ color_html += " " + html_color_text (" ⬤" , right);
242+ break ;
243+ }
244+ case ControllerType::NintendoSwitch_LeftJoycon:
245+ case ControllerType::NintendoSwitch_RightJoycon:{
246+ Color body (colors.body [0 ], colors.body [1 ], colors.body [2 ]);
247+ color_html = html_color_text (" ⬤" , body);
248+ break ;
249+ }
250+ default :;
251+ }
252+
253+ }catch (Exception& e){
254+ e.log (m_logger);
255+ m_handle.set_status_line1 (" Error: See log for more information." , COLOR_RED);
256+ return ;
257+ }
258+ #endif
259+
260+
157261 std::thread watchdog ([&, this ]{
158262 WallClock next_ping = current_time ();
159263 while (true ){
@@ -212,6 +316,7 @@ void SerialPABotBase_WirelessController::status_thread(){
212316 ? html_color_text (" Yes" , theme_friendly_darkblue ())
213317 : html_color_text (" No" , COLOR_RED)
214318 );
319+ str += " - " + color_html;
215320
216321 m_handle.set_status_line1 (str);
217322 }catch (OperationCancelledException&){
0 commit comments