mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-26 21:51:13 +01:00
server extensions
move gpio and visa extensions to extensions folder
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#include "CAN_interface.hpp"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
CAN_Interface::CAN_Interface(QObject *parent) : QObject(parent), m_canDevice(nullptr)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// Error handling goes here
|
||||
SPDLOG_CRITICAL("Runtime error: CAN Interface: {}", errorString.toStdString());
|
||||
throw std::runtime_error("CAN Interface: " + errorString.toStdString());
|
||||
}
|
||||
|
||||
connect(m_canDevice, &QCanBusDevice::errorOccurred, this,
|
||||
[=, this](QCanBusDevice::CanBusError error) { processErrors(error); });
|
||||
|
||||
m_canDevice->connectDevice();
|
||||
connect(m_canDevice, &QCanBusDevice::framesReceived, this, [this] {
|
||||
for (QCanBusFrame &frame : m_canDevice->readAllFrames())
|
||||
{
|
||||
emit frame_received(frame);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void CAN_Interface::send_frame(const QCanBusFrame frame)
|
||||
{
|
||||
if (m_canDevice == nullptr)
|
||||
{
|
||||
SPDLOG_CRITICAL("CAN dev = nullptr");
|
||||
return;
|
||||
}
|
||||
m_canDevice->writeFrame(frame);
|
||||
emit frame_send(frame);
|
||||
}
|
||||
|
||||
void CAN_Interface::processErrors(QCanBusDevice::CanBusError error) const
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case QCanBusDevice::ReadError:
|
||||
case QCanBusDevice::WriteError:
|
||||
case QCanBusDevice::ConnectionError:
|
||||
case QCanBusDevice::ConfigurationError:
|
||||
case QCanBusDevice::UnknownError:
|
||||
SPDLOG_CRITICAL("CAN Interface: {} {}", qt_getEnumName(error), m_canDevice->errorString().toStdString());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
#include <QCanBus>
|
||||
#include <QCanBusDevice>
|
||||
#include <QCanBusFrame>
|
||||
#include <QObject>
|
||||
|
||||
/**
|
||||
* @brief Class representing a CAN interface
|
||||
*
|
||||
*/
|
||||
class CAN_Interface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new CAN Interface object
|
||||
*
|
||||
* @param parent The parent QObject
|
||||
*/
|
||||
CAN_Interface(QObject *parent = nullptr);
|
||||
/**
|
||||
* @brief Destroy the CAN Interface object
|
||||
*
|
||||
*/
|
||||
~CAN_Interface();
|
||||
|
||||
/**
|
||||
* @brief Connect to a CAN device
|
||||
*
|
||||
* @param dev The device to connect to (e.g. "can0")
|
||||
*/
|
||||
void connect_to_dev(QString dev);
|
||||
|
||||
/**
|
||||
* @brief Send a CAN frame
|
||||
*
|
||||
* @param frame The frame to send
|
||||
*/
|
||||
void send_frame(const QCanBusFrame frame);
|
||||
signals:
|
||||
/**
|
||||
* @brief Signal emitted when a CAN frame is received
|
||||
*
|
||||
* @param frame The received CAN frame
|
||||
*/
|
||||
void frame_received(QCanBusFrame frame);
|
||||
/**
|
||||
* @brief Signal emitted when a CAN frame is sent
|
||||
*
|
||||
* @param frame The sent CAN frame
|
||||
*/
|
||||
void frame_send(QCanBusFrame frame);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Private method to process CAN bus errors
|
||||
*
|
||||
* @param error The CAN bus error
|
||||
*/
|
||||
void processErrors(QCanBusDevice::CanBusError error) const;
|
||||
QCanBusDevice *m_canDevice;
|
||||
};
|
||||
Reference in New Issue
Block a user