Skip to content

Commit 9b05fa5

Browse files
committed
Refactor the status prints. Change ESP32 to 15ms refresh rate.
1 parent ead6b64 commit 9b05fa5

12 files changed

+301
-416
lines changed

SerialPrograms/Source/Controllers/ControllerConnection.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
78
#include "ControllerConnection.h"
89

910
//#include <iostream>
@@ -26,17 +27,29 @@ void ControllerConnection::remove_status_listener(StatusListener& listener){
2627

2728
std::string ControllerConnection::status_text() const{
2829
SpinLockGuard lg(m_status_text_lock);
29-
return m_status_text;
30+
std::string str = m_status_line0;
31+
if (!str.empty() && !m_status_line1.empty()){
32+
str += "<br>";
33+
str += m_status_line1;
34+
}
35+
return str;
3036
}
3137

3238

33-
void ControllerConnection::set_status(const std::string& text){
39+
40+
void ControllerConnection::set_status_line0(const std::string& text, Color color){
41+
{
42+
WriteSpinLock lg(m_status_text_lock);
43+
m_status_line0 = html_color_text(text, color);
44+
}
45+
signal_status_text_changed(status_text());
46+
}
47+
void ControllerConnection::set_status_line1(const std::string& text, Color color){
3448
{
3549
WriteSpinLock lg(m_status_text_lock);
36-
m_status_text = text;
50+
m_status_line1 = html_color_text(text, color);
3751
}
38-
// cout << "ControllerConnection::set_status() = " << text << endl;
39-
signal_status_text_changed(text);
52+
signal_status_text_changed(status_text());
4053
}
4154
void ControllerConnection::declare_ready(const std::map<ControllerType, std::set<ControllerFeature>>& controllers){
4255
m_ready.store(true, std::memory_order_release);

SerialPrograms/Source/Controllers/ControllerConnection.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ class ControllerConnection{
4444
virtual std::map<ControllerType, std::set<ControllerFeature>> supported_controllers() const = 0;
4545

4646

47+
public:
48+
void set_status_line0(const std::string& text, Color color = Color());
49+
void set_status_line1(const std::string& text, Color color = Color());
50+
51+
4752
protected:
48-
void set_status(const std::string& text);
4953
void declare_ready(const std::map<ControllerType, std::set<ControllerFeature>>& controllers);
5054

5155

@@ -61,7 +65,8 @@ class ControllerConnection{
6165

6266
private:
6367
mutable SpinLock m_status_text_lock;
64-
std::string m_status_text;
68+
std::string m_status_line0;
69+
std::string m_status_line1;
6570
ListenerSet<StatusListener> m_status_listeners;
6671
};
6772

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.cpp

Lines changed: 8 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
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

174140
std::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

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Connection.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ namespace SerialPABotBase{
2424

2525
class SerialPABotBase_Connection : public ControllerConnection{
2626
public:
27-
~SerialPABotBase_Connection();
2827
SerialPABotBase_Connection(
2928
Logger& logger,
3029
const QSerialPortInfo* port
3130
);
31+
~SerialPABotBase_Connection();
3232

3333
void update_with_capabilities(const std::set<ControllerFeature>& capabilities);
3434

@@ -43,9 +43,6 @@ class SerialPABotBase_Connection : public ControllerConnection{
4343
private:
4444
std::map<ControllerType, std::set<ControllerFeature>> read_device_specs();
4545

46-
void set_label_text(const std::string& text, Color color = Color());
47-
void set_uptime_text(const std::string& text, Color color);
48-
4946
void thread_body();
5047

5148

@@ -56,9 +53,6 @@ class SerialPABotBase_Connection : public ControllerConnection{
5653
uint8_t m_program_id = 0;
5754
std::map<ControllerType, std::set<ControllerFeature>> m_controllers;
5855

59-
std::string m_label;
60-
std::string m_uptime;
61-
6256
std::thread m_status_thread;
6357
std::unique_ptr<PABotBase> m_botbase;
6458
mutable std::mutex m_lock;

0 commit comments

Comments
 (0)