mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
add uart
This commit is contained in:
@@ -2,13 +2,23 @@
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <magic_enum/magic_enum.hpp>
|
||||
|
||||
CAN_Interface::CAN_Interface(QString dev, QObject *parent) : QObject(parent)
|
||||
CAN_Interface::CAN_Interface(QObject *parent) : QObject(parent)
|
||||
{
|
||||
if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan")))
|
||||
{
|
||||
SPDLOG_CRITICAL("Runtime error: CAN Interface: socketcan plugin not found");
|
||||
throw std::runtime_error("CAN Interface: socketcan plugin not found");
|
||||
}
|
||||
}
|
||||
|
||||
CAN_Interface::~CAN_Interface()
|
||||
{
|
||||
m_canDevice->disconnectDevice();
|
||||
delete m_canDevice;
|
||||
}
|
||||
|
||||
void CAN_Interface::connect_to_dev(QString dev)
|
||||
{
|
||||
QString errorString;
|
||||
m_canDevice = QCanBus::instance()->createDevice(QStringLiteral("socketcan"), dev, &errorString);
|
||||
if (!m_canDevice)
|
||||
@@ -30,12 +40,6 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent) : QObject(parent)
|
||||
});
|
||||
}
|
||||
|
||||
CAN_Interface::~CAN_Interface()
|
||||
{
|
||||
m_canDevice->disconnectDevice();
|
||||
delete m_canDevice;
|
||||
}
|
||||
|
||||
void CAN_Interface::send_frame(const QCanBusFrame frame)
|
||||
{
|
||||
m_canDevice->writeFrame(frame);
|
||||
|
||||
@@ -8,8 +8,9 @@ class CAN_Interface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
CAN_Interface(QString dev, QObject *parent = nullptr);
|
||||
CAN_Interface(QObject *parent = nullptr);
|
||||
~CAN_Interface();
|
||||
void connect_to_dev(QString dev);
|
||||
|
||||
void send_frame(const QCanBusFrame frame);
|
||||
signals:
|
||||
|
||||
+9
-3
@@ -37,20 +37,26 @@ int main(int argc, char *argv[])
|
||||
QCoreApplication::translate("main", "port"), QLatin1String("9002"));
|
||||
QCommandLineOption can_port_option(QStringList() << "c"
|
||||
<< "can",
|
||||
QCoreApplication::translate("main", "CAN device [default: can0]."),
|
||||
QCoreApplication::translate("main", "can device"), QLatin1String("can0"));
|
||||
QCoreApplication::translate("main", "CAN device."),
|
||||
QCoreApplication::translate("main", "can device"));
|
||||
QCommandLineOption uart_port_option(QStringList() << "u"
|
||||
<< "uart",
|
||||
QCoreApplication::translate("main", "uart device."),
|
||||
QCoreApplication::translate("main", "uart device"));
|
||||
parser.addOption(port_option);
|
||||
parser.addOption(can_port_option);
|
||||
parser.addOption(uart_port_option);
|
||||
parser.process(a);
|
||||
uint16_t port = parser.value(port_option).toUShort();
|
||||
QString can_port = parser.value(can_port_option);
|
||||
QString uart_port = parser.value(uart_port_option);
|
||||
|
||||
// Initialize the logger
|
||||
init_logger();
|
||||
|
||||
// The server starts in this thread
|
||||
SPDLOG_INFO("Starting server");
|
||||
Simulators simulators(port, can_port);
|
||||
Simulators simulators(port, can_port, uart_port);
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "simulator_base.hpp"
|
||||
|
||||
Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval, QObject *parent)
|
||||
: QObject(parent), m_name(name), m_timer(new QTimer(this)), m_interval(interval)
|
||||
Simulator_base::Simulator_base(std::string name, CAN_Wrapper *can_wrapper, QSerialPort *serial,
|
||||
std::chrono::milliseconds interval, QObject *parent)
|
||||
: QObject(parent), m_name(name), m_timer(new QTimer(this)), m_interval(interval), m_can_wrapper(can_wrapper),
|
||||
m_serial(serial)
|
||||
{
|
||||
connect(m_timer, &QTimer::timeout, this, [this] { timer(); });
|
||||
m_UI_items.reserve(50);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "UI.hpp"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <QSerialPort>
|
||||
#include <QTimer>
|
||||
|
||||
class Simulator_base : public QObject
|
||||
@@ -14,8 +15,8 @@ class Simulator_base : public QObject
|
||||
* @param name The name of the simulator
|
||||
* @param interval The interval of the timer (default is 100ms)
|
||||
*/
|
||||
Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100),
|
||||
QObject *parent = nullptr);
|
||||
Simulator_base(std::string name, CAN_Wrapper *can_wrapper = nullptr, QSerialPort *serial = nullptr,
|
||||
std::chrono::milliseconds interval = std::chrono::milliseconds(100), QObject *parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Returns the name of the simulator
|
||||
@@ -104,4 +105,6 @@ class Simulator_base : public QObject
|
||||
std::vector<UI_item *> m_UI_items;
|
||||
QTimer *m_timer;
|
||||
std::chrono::milliseconds m_interval;
|
||||
CAN_Wrapper *m_can_wrapper;
|
||||
QSerialPort *m_serial;
|
||||
};
|
||||
|
||||
+21
-3
@@ -3,12 +3,30 @@
|
||||
#include "test_sim.hpp"
|
||||
#include <unordered_set>
|
||||
|
||||
Simulators::Simulators(uint16_t port, QString can_dev, QObject *parent)
|
||||
: QObject(parent), m_server_thread(new QThread), m_server(new Websocket(port, parent)),
|
||||
m_can_wrapper(new CAN_Wrapper(new CAN_Interface(can_dev, parent), parent))
|
||||
Simulators::Simulators(uint16_t port, QString can_dev, QString uart_dev, QObject *parent)
|
||||
: QObject(parent), m_server_thread(new QThread), m_server(new Websocket(port, this)),
|
||||
m_can_wrapper(new CAN_Wrapper(new CAN_Interface(this), this)), m_serial(new QSerialPort(this))
|
||||
{
|
||||
m_server->moveToThread(m_server_thread);
|
||||
m_server_thread->start();
|
||||
if (!can_dev.isEmpty())
|
||||
{
|
||||
m_can_wrapper->connect_to_dev(can_dev);
|
||||
}
|
||||
if (!uart_dev.isEmpty())
|
||||
{
|
||||
m_serial->setPortName(uart_dev);
|
||||
m_serial->setBaudRate(QSerialPort::Baud115200);
|
||||
m_serial->setDataBits(QSerialPort::Data8);
|
||||
m_serial->setParity(QSerialPort::NoParity);
|
||||
m_serial->setStopBits(QSerialPort::OneStop);
|
||||
m_serial->setFlowControl(QSerialPort::NoFlowControl);
|
||||
|
||||
if (!m_serial->open(QIODevice::ReadWrite))
|
||||
{
|
||||
SPDLOG_CRITICAL("Error {}", m_serial->errorString().toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
INSERT_SIMULATOR(Test_Sim);
|
||||
INSERT_SIMULATOR(Can_Debugger);
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
#include "simulator_base.hpp"
|
||||
#include "websocket.hpp"
|
||||
|
||||
#define INSERT_SIMULATOR(type) m_simulators.insert(std::make_pair(type().name(), std::make_unique<type>()));
|
||||
#define INSERT_SIMULATOR(type) \
|
||||
m_simulators.insert(std::make_pair(type().name(), std::make_unique<type>(m_can_wrapper, m_serial, this)));
|
||||
|
||||
class Simulators : public QObject
|
||||
{
|
||||
@@ -38,7 +39,7 @@ class Simulators : public QObject
|
||||
begin = 0
|
||||
};
|
||||
|
||||
Simulators(uint16_t port, QString can_dev, QObject *parent = nullptr);
|
||||
Simulators(uint16_t port, QString can_dev, QString uart_dev, QObject *parent = nullptr);
|
||||
std::string active_simulator_name() const;
|
||||
void run();
|
||||
void stop();
|
||||
@@ -55,6 +56,8 @@ class Simulators : public QObject
|
||||
private:
|
||||
QThread *m_server_thread;
|
||||
Websocket *m_server;
|
||||
CAN_Wrapper *m_can_wrapper;
|
||||
QSerialPort *m_serial;
|
||||
std::map<std::string, std::unique_ptr<Simulator_base>> m_simulators;
|
||||
std::string m_current_simulator = "";
|
||||
std::mutex m_mutex;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "can_debugger.hpp"
|
||||
|
||||
Can_Debugger::Can_Debugger(CAN_Wrapper *can_wrapper, QObject *parent)
|
||||
: Simulator_base("Can Debugger", can_wrapper, std::chrono::milliseconds(100), parent), m_wrapper(can_wrapper),
|
||||
m_can_ui(Color::Black, Color::White)
|
||||
Can_Debugger::Can_Debugger(CAN_Wrapper *can_wrapper, QSerialPort *serial, QObject *parent)
|
||||
: Simulator_base("Can Debugger", can_wrapper, serial, std::chrono::milliseconds(100), parent),
|
||||
m_wrapper(can_wrapper), m_can_ui(Color::Black, Color::White)
|
||||
{
|
||||
this->push_ui_item(&this->m_can_ui);
|
||||
|
||||
|
||||
@@ -5,10 +5,11 @@ class Can_Debugger : public Simulator_base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Can_Debugger(QObject* parent = nullptr);
|
||||
explicit Can_Debugger(CAN_Wrapper *can_wrapper = nullptr, QSerialPort *serial = nullptr, QObject *parent = nullptr);
|
||||
void timer() override;
|
||||
void run_at_startup() override;
|
||||
void run_at_stop() override;
|
||||
|
||||
private:
|
||||
CAN_Wrapper *m_wrapper;
|
||||
UI_can m_can_ui;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "test_sim.hpp"
|
||||
|
||||
Test_Sim::Test_Sim(CAN_Wrapper *can_wrapper, QObject *parent)
|
||||
: Simulator_base("Test Sim", can_wrapper, std::chrono::milliseconds(1000), parent),
|
||||
Test_Sim::Test_Sim(CAN_Wrapper *can_wrapper, QSerialPort *serial, QObject *parent)
|
||||
: Simulator_base("Test Sim", can_wrapper, serial, std::chrono::milliseconds(1000), parent),
|
||||
m_button("Off", Color::White, Color::Red),
|
||||
m_combobox(Color::White, Color::Black, {"Option 1", "Option 2", "Option 3"}, 0),
|
||||
m_label("Label", Color::White, Color::Black), m_checkbox("Option 1", Color::Default, Color::Default),
|
||||
|
||||
@@ -5,7 +5,7 @@ class Test_Sim : public Simulator_base
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Test_Sim(QObject* parent = nullptr);
|
||||
explicit Test_Sim(CAN_Wrapper *can_wrapper = nullptr, QSerialPort *serial = nullptr, QObject *parent = nullptr);
|
||||
void timer() override;
|
||||
void run_at_startup() override;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user