Fix issues with api handling, use shared_ptr to clean up multiplexer structs

This commit is contained in:
Carl Philipp Klemm 2025-06-30 16:59:48 +02:00
parent 278db0b23f
commit d36d5e563a
4 changed files with 18 additions and 13 deletions

View file

@ -26,6 +26,7 @@ add_executable(eismultiplexer-qt
channelwidget.cpp
channelwidget.h
)
target_compile_options(eismultiplexer-qt PUBLIC "-Wall")
# Link Qt Widgets
target_link_libraries(eismultiplexer-qt Qt6::Widgets eismultiplexer)

View file

@ -56,18 +56,20 @@ void ChannelWidget::onChannelToggled(bool checked)
channel_t channelFlag = static_cast<channel_t>(1 << channelNumber);
if (checked) {
if (eismultiplexer_connect_channel(multiplexer, channelFlag) < 0) {
if (eismultiplexer_connect_channel(multiplexer.get(), channelFlag) < 0) {
QMessageBox::warning(this, tr("Connection Failed"),
tr("Failed to connect channel %1 on device %2").arg(channelNumber).arg(deviceSerial));
qWarning() << "Failed to connect channel" << channelNumber << "on device" << deviceSerial;
checkbox->blockSignals(true);
checkbox->setChecked(false);
setEnabled(false); // Gray out the widget
}
} else {
if (eismultiplexer_disconnect_channel(multiplexer, channelFlag) < 0) {
if (eismultiplexer_disconnect_channel(multiplexer.get(), channelFlag) < 0) {
QMessageBox::warning(this, tr("Disconnection Failed"),
tr("Failed to disconnect channel %1 on device %2").arg(channelNumber).arg(deviceSerial));
qWarning() << "Failed to disconnect channel" << channelNumber << "on device" << deviceSerial;
checkbox->blockSignals(true);
checkbox->setChecked(true);
setEnabled(false); // Gray out the widget
}

View file

@ -1,6 +1,3 @@
#ifndef CHANNELWIDGET_H
#define CHANNELWIDGET_H
@ -9,6 +6,7 @@
#include <QLabel>
#include <QHBoxLayout>
#include <eismultiplexer.h>
#include <memory>
class ChannelWidget : public QWidget
{
@ -32,7 +30,7 @@ private slots:
private:
uint16_t deviceSerial;
uint16_t channelNumber;
struct eismultiplexer* multiplexer;
std::shared_ptr<struct eismultiplexer> multiplexer;
QCheckBox* checkbox;
QLabel* label;
};

View file

@ -1,9 +1,8 @@
#include "mainwindow.h"
#include <QDebug>
#include <QMessageBox>
#include <eismultiplexer.h>
#include <memory>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@ -44,6 +43,9 @@ void MainWindow::setupUi()
setWindowTitle("EIS Multiplexer Controller");
}
void MainWindow::onChannelAboutToBeTurnedOn(uint16_t deviceSerial, uint16_t channelNumber)
{}
void MainWindow::enumerateDevices()
{
size_t count = 0;
@ -53,22 +55,24 @@ void MainWindow::enumerateDevices()
QMessageBox::warning(this, tr("No Devices Found"),
tr("No EIS multiplexer devices were found. Please connect a device and try again."));
qWarning() << "No EIS multiplexer devices found";
close();
return;
}
for (size_t i = 0; i < count; i++) {
uint16_t serial = serials[i];
struct eismultiplexer multiplexer;
if (eismultiplexer_connect(&multiplexer, serial) == 0) {
std::shared_ptr<struct eismultiplexer> multiplexer(new struct eismultiplexer);
if (eismultiplexer_connect(multiplexer.get(), serial) >= 0) {
uint16_t channelCount = 0;
if (eismultiplexer_get_channel_count(&multiplexer, &channelCount) == 0) {
qDebug()<<"Adding channels from device "<<serial;
if (eismultiplexer_get_channel_count(multiplexer.get(), &channelCount) >= 0) {
for (uint16_t channel = 0; channel < channelCount; channel++) {
ChannelWidget* widget = new ChannelWidget(serial, channel, &multiplexer);
ChannelWidget* widget = new ChannelWidget(serial, channel, multiplexer.get());
qDebug()<<"Added widget from device "<<serial<<" channel "<<channel;
channelWidgets.push_back(widget);
scrollLayout->addWidget(widget);
}
}
eismultiplexer_disconnect(&multiplexer);
} else {
QMessageBox::warning(this, tr("Connection Failed"),
tr("Failed to connect to device with serial %1").arg(serial));