Skip to content

Commit

Permalink
perf: use new serial port impl
Browse files Browse the repository at this point in the history
  • Loading branch information
shuai132 committed Oct 25, 2023
1 parent abaa1da commit 5966cf5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "third_party/asio"]
path = third_party/asio
url = git@github.com:chriskohlhoff/asio.git
[submodule "modules/asio_net"]
path = modules/asio_net
url = git@github.com:shuai132/asio_net
9 changes: 8 additions & 1 deletion App/App.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#include "App.h"

#include <memory>
#include "imgui.h"
#include "ui/commondef.h"

using namespace ImGui;

App::App()
: comm_(std::unique_ptr<Comm>(new Comm(this)))
: comm_(std::make_unique<Comm>((AppContext*)this))
, uiComm_(comm_.get())
, uiCmd_(comm_.get())
, uiVol_(comm_.get())
, uiFFT_(comm_.get())
{
comm_->init();
initGUI();
}

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

void* App::nativeHandle() {
return &uiContext_;
}

void App::initGUI() {
windowFlags_ |= ImGuiWindowFlags_NoMove;
windowFlags_ |= ImGuiWindowFlags_NoResize;
Expand Down
2 changes: 2 additions & 0 deletions App/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class App : UI, AppContext {

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

void *nativeHandle() override;

private:
void initGUI();

Expand Down
1 change: 1 addition & 0 deletions App/AppContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class AppContext {
virtual ~AppContext() = default;
virtual void runOnUiThread(const std::function<void()>& task) = 0;
virtual void post(const std::function<void()>& task) = 0;
virtual void* nativeHandle() = 0;
};
30 changes: 16 additions & 14 deletions App/comm/Comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
#include "log.h"

Comm::Comm(AppContext* context)
: scopeGui_(this), appContext_(context) {
: scopeGui_(this), appContext_(context) {}

void Comm::init() {
serialPort_ = std::make_unique<asio_net::serial_port>(*(asio::io_context*)appContext_->nativeHandle());
initSerial();
}

void Comm::setPortName(const std::string& name) {
smartSerial_.setPortName(name);
serialPort_->config().device = name;
}

std::string Comm::getPortName() {
return smartSerial_.getPortName();
return serialPort_->config().device;
}

bool Comm::isOpen() {
return smartSerial_.isOpen();
return serialPort_->is_open();
}

void Comm::setRecvEnable(bool enable) {
Expand All @@ -33,30 +36,29 @@ void Comm::sendCmd(Cmd::Type type, Cmd::Data data) {
cmdLastTime_ = now;
cmdRespond_ = false;

if (not smartSerial_.isOpen()) {
if (not serialPort_->is_open()) {
LOGD("not open, cmd ignored!");
return;
}
scopeGui_.sendCmd(type, data);
}

void Comm::initSerial() {
smartSerial_.setOnOpenHandle([this](bool isOpen) {
if (isOpen) {
sendCmd(Cmd::SOFTWARE_TRIGGER);
}
serialPort_->set_reconnect(1000);
serialPort_->on_open = ([this]() {
sendCmd(Cmd::SOFTWARE_TRIGGER);
});

smartSerial_.setVidPid(PORT_VID, PORT_PID);
// serialPort_->setVidPid(PORT_VID, PORT_PID);

smartSerial_.setOnReadHandle([this](const uint8_t* data, size_t size) {
serialPort_->on_data = [this](const std::string& data) {
if (not recvEnabled_) return;
scopeGui_.onMcuData(data, size);
});
scopeGui_.onMcuData((uint8_t*)data.data(), data.size());
};
}

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

void Comm::onMessage(Message* message, size_t size) {
Expand Down
10 changes: 6 additions & 4 deletions App/comm/Comm.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

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

public:
void init();

void setPortName(const std::string& name);

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

// Serial Port
SmartSerial smartSerial_;
const char* PORT_VID = "1234";
const char* PORT_PID = "5740";
std::unique_ptr<asio_net::serial_port> serialPort_;
// const char* PORT_VID = "1234";
// const char* PORT_PID = "5740";

std::atomic_bool recvEnabled_ {true};

Expand Down
1 change: 1 addition & 0 deletions App/ui/UIComm.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UIComm.h"
#include "imgui.h"
#include "serial/serial.h"

using namespace ImGui;

Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ link_libraries(imgui)
add_subdirectory(modules/SmartSerial)
link_libraries(smartserial)

# asio_net
add_subdirectory(modules/asio_net)
link_libraries(asio_net)

# log
include_directories(modules/log)

Expand Down
1 change: 1 addition & 0 deletions modules/asio_net
Submodule asio_net added at 6cbac5

0 comments on commit 5966cf5

Please sign in to comment.