77#include < QtGlobal>
88#include < QSerialPortInfo>
99#include < QMessageBox>
10- #include " Common/Cpp/PrettyPrint.h"
1110#include " Common/Cpp/Exceptions.h"
1211#include " Common/Cpp/PanicDump.h"
13- // #include "Common/Cpp/Options/TimeExpressionOption.h"
1412#include " Common/Microcontroller/DeviceRoutines.h"
15- // #include "Common/NintendoSwitch/NintendoSwitch_ControllerDefs.h"
1613#include " ClientSource/Libraries/MessageConverter.h"
1714#include " ClientSource/Connection/SerialConnection.h"
18- // #include "ClientSource/Connection/BotBase.h"
1915#include " ClientSource/Connection/PABotBase.h"
20- #include " CommonFramework/Globals.h"
2116#include " CommonFramework/GlobalSettingsPanel.h"
2217#include " CommonFramework/Options/Environment/ThemeSelectorOption.h"
23- #include " NintendoSwitch/Commands/NintendoSwitch_Messages_Device.h"
2418#include " SerialPABotBase.h"
2519#include " SerialPABotBase_Connection.h"
2620
@@ -41,7 +35,7 @@ SerialPABotBase_Connection::SerialPABotBase_Connection(
4135)
4236 : m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
4337{
44- set_label_text (" Not Connected" , COLOR_RED);
38+ set_status_line0 (" Not Connected" , COLOR_RED);
4539
4640 // No port selected.
4741 if (port == nullptr || port->isNull ()){
@@ -60,14 +54,14 @@ SerialPABotBase_Connection::SerialPABotBase_Connection(
6054 );
6155 std::string text = " Cannot connect to Prolific controller." ;
6256 m_logger.log (text, COLOR_RED);
63- set_label_text (text, COLOR_RED);
57+ set_status_line0 (text, COLOR_RED);
6458 return ;
6559 }
6660
6761 std::string name = port->systemLocation ().toUtf8 ().data ();
6862 std::string error;
6963 try {
70- set_label_text (" Connecting..." , COLOR_DARKGREEN);
64+ set_status_line0 (" Connecting..." , COLOR_DARKGREEN);
7165
7266 std::unique_ptr<SerialConnection> connection (new SerialConnection (name, PABB_BAUD_RATE));
7367 m_botbase.reset (new PABotBase (m_logger, std::move (connection), nullptr ));
@@ -79,7 +73,7 @@ SerialPABotBase_Connection::SerialPABotBase_Connection(
7973 error = e.message ();
8074 }
8175 if (!error.empty ()){
82- set_label_text (" Unable to open port." , COLOR_RED);
76+ set_status_line0 (" Unable to open port." , COLOR_RED);
8377 return ;
8478 }
8579
@@ -142,34 +136,6 @@ BotBaseController* SerialPABotBase_Connection::botbase(){
142136}
143137
144138
145- void SerialPABotBase_Connection::set_label_text (const std::string& text, Color color){
146- m_label = html_color_text (text, color);
147-
148- std::string status = m_label;
149- if (!m_uptime.empty ()){
150- if (!status.empty ()){
151- status += " <br>" ;
152- }
153- status += m_uptime;
154- }
155- set_status (status);
156- }
157- void SerialPABotBase_Connection::set_uptime_text (const std::string& text, Color color){
158- m_uptime = html_color_text (text, color);
159-
160- std::string status = m_label;
161- if (!m_uptime.empty ()){
162- if (!status.empty ()){
163- status += " <br>" ;
164- }
165- status += m_uptime;
166- }
167- set_status (status);
168- }
169-
170-
171-
172-
173139
174140std::map<ControllerType, std::set<ControllerFeature>> SerialPABotBase_Connection::supported_controllers () const {
175141 std::lock_guard<std::mutex> lg (m_lock);
@@ -238,7 +204,7 @@ void SerialPABotBase_Connection::thread_body(){
238204 m_botbase->connect ();
239205 }catch (InvalidConnectionStateException&){
240206 m_botbase->stop ();
241- set_label_text (" " );
207+ set_status_line0 (" " );
242208 return ;
243209 }catch (SerialProtocolException& e){
244210 error = e.message ();
@@ -247,7 +213,7 @@ void SerialPABotBase_Connection::thread_body(){
247213 }
248214 if (!error.empty ()){
249215 m_botbase->stop ();
250- set_label_text (error, COLOR_RED);
216+ set_status_line0 (error, COLOR_RED);
251217 return ;
252218 }
253219 }
@@ -273,95 +239,16 @@ void SerialPABotBase_Connection::thread_body(){
273239 if (error.empty ()){
274240// std::string text = "Program: " + program_name(m_program_id) + " (" + std::to_string(m_protocol) + ")";
275241 std::string text = program_name (m_program_id) + " (" + std::to_string (m_protocol) + " )" ;
276- set_label_text (text, theme_friendly_darkblue ());
242+ set_status_line0 (text, theme_friendly_darkblue ());
277243 declare_ready (controllers);
278244 }else {
279245 m_ready.store (false , std::memory_order_relaxed);
280- set_label_text (error, COLOR_RED);
246+ set_status_line0 (error, COLOR_RED);
281247// signal_pre_not_ready();
282248 m_botbase->stop ();
283249 return ;
284250 }
285251 }
286-
287-
288- std::thread watchdog ([this ]{
289- while (true ){
290- if (!m_ready.load (std::memory_order_relaxed)){
291- break ;
292- }
293-
294- auto last = current_time () - m_botbase->last_ack ();
295- std::chrono::duration<double > seconds = last;
296- if (last > 2 * SERIAL_REFRESH_RATE){
297- std::string text = " Last Ack: " + tostr_fixed (seconds.count (), 3 ) + " seconds ago" ;
298- set_uptime_text (text, COLOR_RED);
299- // m_logger.log("Connection issue detected. Turning on all logging...");
300- // settings.log_everything.store(true, std::memory_order_release);
301- }
302-
303- std::unique_lock<std::mutex> lg (m_sleep_lock);
304- if (!m_ready.load (std::memory_order_relaxed)){
305- break ;
306- }
307-
308- m_cv.wait_for (lg, SERIAL_REFRESH_RATE);
309- }
310- });
311-
312- CancellableHolder<CancellableScope> scope;
313-
314- while (true ){
315- if (!m_ready.load (std::memory_order_relaxed)){
316- break ;
317- }
318-
319- std::string str;
320- std::string error;
321- try {
322- pabb_MsgAckRequestI32 response;
323- m_botbase->issue_request_and_wait (
324- NintendoSwitch::DeviceRequest_system_clock (),
325- &scope
326- ).convert <PABB_MSG_ACK_REQUEST_I32>(m_logger, response);
327- uint32_t wallclock = response.data ;
328- if (wallclock == 0 ){
329- str = " Unknown" ;
330- }else {
331- // str = ticks_to_time(NintendoSwitch::TICKS_PER_SECOND, wallclock);
332- str = tostr_u_commas (wallclock);
333- }
334- }catch (InvalidConnectionStateException&){
335- break ;
336- }catch (SerialProtocolException& e){
337- error = e.message ();
338- }catch (ConnectionException& e){
339- error = e.message ();
340- }
341- if (error.empty ()){
342- std::string text = " State Reports: " + str;
343- set_uptime_text (text, theme_friendly_darkblue ());
344- }else {
345- set_uptime_text (error, COLOR_RED);
346- error.clear ();
347- m_ready.store (false , std::memory_order_relaxed);
348- break ;
349- }
350-
351- // cout << "lock()" << endl;
352- std::unique_lock<std::mutex> lg (m_sleep_lock);
353- // cout << "lock() - done" << endl;
354- if (!m_ready.load (std::memory_order_relaxed)){
355- break ;
356- }
357- m_cv.wait_for (lg, SERIAL_REFRESH_RATE);
358- }
359-
360- {
361- std::unique_lock<std::mutex> lg (m_sleep_lock);
362- m_cv.notify_all ();
363- }
364- watchdog.join ();
365252}
366253
367254
0 commit comments