Skip to content

Commit c3104a9

Browse files
committed
perf: use new serial port impl
1 parent abaa1da commit c3104a9

File tree

9 files changed

+46
-22
lines changed

9 files changed

+46
-22
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
[submodule "third_party/asio"]
1717
path = third_party/asio
1818
url = [email protected]:chriskohlhoff/asio.git
19+
[submodule "modules/asio_net"]
20+
path = modules/asio_net
21+
url = [email protected]:shuai132/asio_net

App/App.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#include "App.h"
2+
3+
#include <memory>
24
#include "imgui.h"
35
#include "ui/commondef.h"
46

57
using namespace ImGui;
68

79
App::App()
8-
: comm_(std::unique_ptr<Comm>(new Comm(this)))
10+
: comm_(std::make_unique<Comm>((AppContext*)this))
911
, uiComm_(comm_.get())
1012
, uiCmd_(comm_.get())
1113
, uiVol_(comm_.get())
1214
, uiFFT_(comm_.get())
1315
{
16+
comm_->init();
1417
initGUI();
1518
}
1619

@@ -49,6 +52,10 @@ void App::post(const std::function<void()>& task) {
4952
uiContext_.post(task);
5053
}
5154

55+
void* App::nativeHandle() {
56+
return &uiContext_;
57+
}
58+
5259
void App::initGUI() {
5360
windowFlags_ |= ImGuiWindowFlags_NoMove;
5461
windowFlags_ |= ImGuiWindowFlags_NoResize;

App/App.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class App : UI, AppContext {
1919

2020
void post(const std::function<void()>& task) override;
2121

22+
void *nativeHandle() override;
23+
2224
private:
2325
void initGUI();
2426

@@ -28,10 +30,10 @@ class App : UI, AppContext {
2830
static const char* MainWindowTitle;
2931

3032
private:
31-
std::unique_ptr<Comm> comm_;
3233
asio::io_context uiContext_;
34+
std::unique_ptr<Comm> comm_;
3335

34-
uint32_t windowFlags_ = 0;
36+
int windowFlags_ = 0;
3537

3638
UIComm uiComm_;
3739
UICmd uiCmd_;

App/AppContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ class AppContext {
55
virtual ~AppContext() = default;
66
virtual void runOnUiThread(const std::function<void()>& task) = 0;
77
virtual void post(const std::function<void()>& task) = 0;
8+
virtual void* nativeHandle() = 0;
89
};

App/comm/Comm.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
#include "log.h"
55

66
Comm::Comm(AppContext* context)
7-
: scopeGui_(this), appContext_(context) {
7+
: scopeGui_(this), appContext_(context) {}
8+
9+
void Comm::init() {
10+
serialPort_ = std::make_unique<asio_net::serial_port>(*(asio::io_context*)appContext_->nativeHandle());
811
initSerial();
912
}
1013

1114
void Comm::setPortName(const std::string& name) {
12-
smartSerial_.setPortName(name);
15+
serialPort_->close();
16+
serialPort_->open(name);
1317
}
1418

1519
std::string Comm::getPortName() {
16-
return smartSerial_.getPortName();
20+
return serialPort_->config().device;
1721
}
1822

1923
bool Comm::isOpen() {
20-
return smartSerial_.isOpen();
24+
return serialPort_->is_open();
2125
}
2226

2327
void Comm::setRecvEnable(bool enable) {
@@ -33,30 +37,29 @@ void Comm::sendCmd(Cmd::Type type, Cmd::Data data) {
3337
cmdLastTime_ = now;
3438
cmdRespond_ = false;
3539

36-
if (not smartSerial_.isOpen()) {
40+
if (not serialPort_->is_open()) {
3741
LOGD("not open, cmd ignored!");
3842
return;
3943
}
4044
scopeGui_.sendCmd(type, data);
4145
}
4246

4347
void Comm::initSerial() {
44-
smartSerial_.setOnOpenHandle([this](bool isOpen) {
45-
if (isOpen) {
46-
sendCmd(Cmd::SOFTWARE_TRIGGER);
47-
}
48+
serialPort_->set_reconnect(1000);
49+
serialPort_->on_open = ([this]() {
50+
sendCmd(Cmd::SOFTWARE_TRIGGER);
4851
});
52+
serialPort_->open();
53+
// serialPort_->setVidPid(PORT_VID, PORT_PID);
4954

50-
smartSerial_.setVidPid(PORT_VID, PORT_PID);
51-
52-
smartSerial_.setOnReadHandle([this](const uint8_t* data, size_t size) {
55+
serialPort_->on_data = [this](const std::string& data) {
5356
if (not recvEnabled_) return;
54-
scopeGui_.onMcuData(data, size);
55-
});
57+
scopeGui_.onMcuData((uint8_t*)data.data(), data.size());
58+
};
5659
}
5760

5861
void Comm::sendToMcu(const uint8_t* data, size_t size) {
59-
smartSerial_.write(data, size);
62+
serialPort_->send(std::string((char*)data, size));
6063
}
6164

6265
void Comm::onMessage(Message* message, size_t size) {

App/comm/Comm.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include "base/noncopyable.h"
4-
#include "SmartSerial.h"
4+
#include "asio_net/serial_port.hpp"
55
#include "algorithm/MsgAnalyzer.h"
66
#include "AppContext.h"
77
#include "ScopeGUI.h"
@@ -19,6 +19,8 @@ class Comm : noncopyable, private ScopeGUI::Comm {
1919
void onMessage(Message* message, size_t size) override;
2020

2121
public:
22+
void init();
23+
2224
void setPortName(const std::string& name);
2325

2426
std::string getPortName();
@@ -39,9 +41,9 @@ class Comm : noncopyable, private ScopeGUI::Comm {
3941
ScopeGUI scopeGui_;
4042

4143
// Serial Port
42-
SmartSerial smartSerial_;
43-
const char* PORT_VID = "1234";
44-
const char* PORT_PID = "5740";
44+
std::unique_ptr<asio_net::serial_port> serialPort_;
45+
// const char* PORT_VID = "1234";
46+
// const char* PORT_PID = "5740";
4547

4648
std::atomic_bool recvEnabled_ {true};
4749

App/ui/UIComm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "UIComm.h"
22
#include "imgui.h"
3+
#include "serial/serial.h"
34

45
using namespace ImGui;
56

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ link_libraries(imgui)
3737
add_subdirectory(modules/SmartSerial)
3838
link_libraries(smartserial)
3939

40+
# asio_net
41+
add_subdirectory(modules/asio_net)
42+
link_libraries(asio_net)
43+
4044
# log
4145
include_directories(modules/log)
4246

modules/asio_net

Submodule asio_net added at 139d075

0 commit comments

Comments
 (0)