Cleanup datatypes.

This commit is contained in:
2025-12-23 14:58:54 +01:00
parent 01912c75ef
commit b219a71cba
10 changed files with 80 additions and 42 deletions

View File

@@ -29,6 +29,7 @@ SOURCES += \
bcdataitem.cpp \ bcdataitem.cpp \
bcdatamodel.cpp \ bcdatamodel.cpp \
bcitemdelegate.cpp \ bcitemdelegate.cpp \
bclegacy.cpp \
bctransmitter.cpp \ bctransmitter.cpp \
bcvdatamanager.cpp \ bcvdatamanager.cpp \
lib/can_drv_win.c \ lib/can_drv_win.c \

6
bc.h
View File

@@ -35,7 +35,7 @@
#include <QDebug> #include <QDebug>
#include <QObject> // Nötig für Q_GADGET/Q_ENUM Makros #include <QObject> // Nötig für Q_GADGET/Q_ENUM Makros
using bcdata_t = uint8_t; //uint8_t;
/** /**
@@ -69,11 +69,11 @@ namespace bc
// misc // misc
//#define cbc::Version "CanBusControl 0.0.01 / 02.07.2022" //#define cbc::Version "CanBusControl 0.0.01 / 02.07.2022"
[[maybe_unused]] constexpr static const char* Version = "CanBusControl 0.1.00 / 08.11.2022 © 2022 chris@sourceworx.org"; [[maybe_unused]] constexpr static const char* Version = "BionxControl 0.1.00 / 08.11.2022 © 2022 chris@sourceworx.org";
[[maybe_unused]] constexpr static const char* OrgName = "source::worx"; [[maybe_unused]] constexpr static const char* OrgName = "source::worx";
[[maybe_unused]] constexpr static const char* DomainName = "sourceworx.org"; [[maybe_unused]] constexpr static const char* DomainName = "sourceworx.org";
[[maybe_unused]] constexpr static const char* AppName = "CanBusControl"; [[maybe_unused]] constexpr static const char* AppName = "BionxControl";
// timer // timer

View File

@@ -77,8 +77,8 @@ public:
virtual DriverState loadDriver() = 0; virtual DriverState loadDriver() = 0;
virtual DriverState initDriver() = 0; virtual DriverState initDriver() = 0;
virtual bcdata_t readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const = 0; virtual uint32_t readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const = 0;
virtual void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const = 0; virtual void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, uint8_t value ) const = 0;
signals: signals:

View File

@@ -83,7 +83,7 @@ BCCanDriver::DriverState BCCanDriverTinyCan::initDriver()
unsigned int retry = _timeOuts; unsigned int retry = _timeOuts;
emit statusHint( "Driver Init: putting BCDevice::ID::Console in slave mode ... " ); emit statusHint( "Driver Init: putting BCDevice::ID::Console in slave mode ... " );
bcdata_t isSlave = 0; uint32_t isSlave = 0;
do do
{ {
writeRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave, 1 ); writeRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave, 1 );
@@ -105,18 +105,17 @@ BCCanDriver::DriverState BCCanDriverTinyCan::initDriver()
} }
uint32_t BCCanDriverTinyCan::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const
bcdata_t BCCanDriverTinyCan::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const
{ {
if( getState() != DriverState::Ready) if( getState() != DriverState::Ready)
throw BCException( "readRawValue error: driver not loaded." ); throw BCException( "readRawValue error: driver not loaded." );
//bcdata_t result = transmitter.readRawValue( deviceID, registerID ); //uint32_t result = transmitter.readRawValue( deviceID, registerID );
bcdata_t myRandomByte = static_cast<quint8>(QRandomGenerator::global()->bounded(256)); uint32_t myRandomByte = static_cast<uint32_t>(QRandomGenerator::global()->bounded(256));
return myRandomByte; return myRandomByte;
//value.fromValue<bcdata_t>( myRandomByte ); //value.fromValue<uint32_t>( myRandomByte );
TCanMsg msg; TCanMsg msg;
@@ -174,13 +173,13 @@ retry:
if( !timeOuts ) if( !timeOuts )
throw BCException( "CAN --response errror" ); throw BCException( "CAN --response errror" );
return (bcdata_t) msg.MsgData[3]; return (uint32_t) msg.MsgData[3];
} }
// void BCCanDriverTinyCan::setValue( unsigned char receipient, unsigned char reg, unsigned char value ) // void BCCanDriverTinyCan::setValue( unsigned char receipient, unsigned char reg, unsigned char value )
void BCCanDriverTinyCan::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const void BCCanDriverTinyCan::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, uint8_t value ) const
{ {
if( getState() != DriverState::Ready) if( getState() != DriverState::Ready)

View File

@@ -15,8 +15,8 @@ public:
DriverState loadDriver() override; DriverState loadDriver() override;
DriverState initDriver() override; DriverState initDriver() override;
bcdata_t readRawValue ( BCDevice::ID deviceID, BC::ID registerID ) const override; uint32_t readRawValue ( BCDevice::ID deviceID, BC::ID registerID ) const override;
void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const override; void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, uint8_t value ) const override;
QString getNodeName( unsigned char id ); QString getNodeName( unsigned char id );

View File

@@ -54,7 +54,7 @@ BCDataItem::BCDataItem(const BCDataType& valueType_, BCDevice::ID deviceID_, BC:
void BCDataItem::readRawValue( const BCAbstractTransmitter& transmitter ) const void BCDataItem::readRawValue( const BCAbstractTransmitter& transmitter ) const
{ {
bcdata_t rawValue = transmitter.readRawValue( deviceID, registerID ); uint32_t rawValue = transmitter.readRawValue( deviceID, registerID );
} }
void BCDataItem::writeRawValue( const BCAbstractTransmitter& transmitter ) const void BCDataItem::writeRawValue( const BCAbstractTransmitter& transmitter ) const

View File

@@ -61,7 +61,7 @@ class BCAbstractTransmitter
public: public:
// //
virtual bcdata_t readRawValue( BCDevice::ID deviceID_, BC::ID registerID_ ) const = 0; virtual uint32_t readRawValue( BCDevice::ID deviceID_, BC::ID registerID_ ) const = 0;
virtual void writeRawValue( BCDevice::ID deviceID_, BC::ID registerID_, uint8_t value_ ) const = 0; virtual void writeRawValue( BCDevice::ID deviceID_, BC::ID registerID_, uint8_t value_ ) const = 0;
}; };
@@ -136,7 +136,7 @@ public:
bool inSync{false}; bool inSync{false};
bool readOnly{false}; bool readOnly{false};
mutable std::optional<bcdata_t> rawValue; mutable std::optional<uint32_t> rawValue;

View File

@@ -17,7 +17,7 @@ void BCTransmitter::onToggleConnectionState( bool connect )
if( _canDriver.getState() != BCCanDriver::DriverState::Ready ) if( _canDriver.getState() != BCCanDriver::DriverState::Ready )
_canDriver.onStartDriver(); _canDriver.onStartDriver();
bcdata_t hwVersion = _canDriver.readRawValue( BCDevice::ID::Console, BC::ID::Cons_Rev_Hw); uint32_t hwVersion = _canDriver.readRawValue( BCDevice::ID::Console, BC::ID::Cons_Rev_Hw);
if(!hwVersion) if(!hwVersion)
{ {
@@ -106,7 +106,7 @@ void BCTransmitter::processValueOp( BC::OpID opID )
} }
} }
bcdata_t BCTransmitter::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const uint32_t BCTransmitter::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const
{ {
try try
{ {
@@ -119,7 +119,7 @@ bcdata_t BCTransmitter::readRawValue( BCDevice::ID deviceID, BC::ID registerID )
} }
void BCTransmitter::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const void BCTransmitter::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, uint8_t value ) const
{ {
try try
{ {

View File

@@ -18,8 +18,8 @@ public:
explicit BCTransmitter(QObject *parent = nullptr); explicit BCTransmitter(QObject *parent = nullptr);
bcdata_t readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const override; uint32_t readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const override;
void writeRawValue(BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const override; void writeRawValue(BCDevice::ID deviceID, BC::ID registerID, uint8_t value ) const override;
public slots: public slots:

View File

@@ -31,6 +31,8 @@
#include <QApplication> #include <QApplication>
#include <QMetaEnum> #include <QMetaEnum>
#include <QFile> #include <QFile>
#include <QLabel>
#include <QProgressBar>
// main.cpp // main.cpp
#include <QCoreApplication> #include <QCoreApplication>
@@ -41,25 +43,8 @@
#include <bcdataitem.h> #include <bcdataitem.h>
#include <bcvdatamanager.h> #include <bcvdatamanager.h>
/* #include <variant>
#include <QMetaEnum> #include <string>
void parseString(const QString &inputString) {
QMetaEnum metaEnum = QMetaEnum::fromType<BCData::ID>();
bool ok = false;
// keyToValue parst den String ("x1") und liefert den int-Wert
int intVal = metaEnum.keyToValue(inputString.toLatin1().constData(), &ok);
if (ok) {
BCData::ID id = static_cast<BCData::ID>(intVal);
// Erfolg!
} else {
// Fehler: String existiert nicht im Enum
qWarning() << "Unbekannter Enum String:" << inputString;
}
}
*/
@@ -81,6 +66,59 @@ bool setApplicationStyleSheet( QAnyStringView path )
return true; return true;
} }
struct moo
{
QString moo_str {"irgendwas"};
};
// Verschiedene Datentypen für die Sensoren
struct Temperature : public moo{ float celsius; };
struct CpuLoad : public moo{ int percentage; };
struct Status : public moo{ std::string message; };
// Die Variant als universeller Datencontainer
using SensorReading = std::variant<Temperature, CpuLoad, Status>;
struct UIUpdateVisitor
{
QLabel* tempLabel;
QProgressBar* cpuBar;
QLabel* statusLabel;
// Überladene operator() für jeden Typ in der Variant
void operator()(const Temperature& t) const
{
tempLabel->setText(QString("Temp: %1°C").arg(t.celsius, 0, 'f', 1));
tempLabel->setStyleSheet(t.celsius > 40 ? "color: red;" : "color: black;");
}
void operator()(const CpuLoad& c) const
{
cpuBar->setValue(c.percentage);
qDebug() << c.moo_str;
}
void operator()(const Status& s) const
{
statusLabel->setText(QString::fromStdString(s.message));
}
};
void onNewDataReceived(const SensorReading& data)
{
QLabel* labelTemp = nullptr;
QProgressBar* progressCpu = nullptr;
QLabel* labelStatus = nullptr;
// Wir erstellen den Visitor mit Referenzen auf unsere UI-Elemente
UIUpdateVisitor visitor { labelTemp, progressCpu, labelStatus };
// Die Magie: std::visit wählt zur Kompilierzeit die richtige Methode
std::visit(visitor, data);
}
// 2. Datei öffnen und lesen // 2. Datei öffnen und lesen
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {