Files
BionxControl/bcvalue.cpp
2026-01-22 22:16:19 +01:00

282 lines
5.9 KiB
C++

/***************************************************************************
BionxControl
© 2025 -2026 christoph holzheuer
christoph.holzheuer@gmail.com
Using:
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
Based on Bionx data type descriptions from:
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
Bionx Bike Info
© 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
@see www.ts-soft.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
@see https://github.com/bikemike/bionx-bikeinfo
***************************************************************************/
#include <QMetaEnum>
#include <QTextStream>
#include <bcvalue.h>
///-------------------------------
BCValue::BCValue( BCDevice::ID deviceID_, BC::ID registerID_)
: _deviceID{deviceID_}, _registerID{registerID_}
{
}
QString BCValue::formatValue() const
{
if( _factor == 1 )
return QString::number( _rawValue );
double result =_rawValue * _factor;
return QString::number(result, 'f', 2);
}
bool BCValue::isWord() const
{
return _valueFlags.testFlag(BCValue::Flag::IsWord);
}
bool BCValue::isReadOnly() const
{
return _valueFlags.testFlag(BCValue::Flag::ReadOnly);
}
bool BCValue::testFlag( BCValue::Flag flag ) const
{
return _valueFlags.testFlag( flag );
}
void BCValue::setFlag( BCValue::Flag flag, bool state) const
{
_valueFlags.setFlag( flag, state );
}
BCDevice::ID BCValue::deviceID() const noexcept
{
return _deviceID;
}
BC::ID BCValue::registerID() const noexcept
{
return _registerID;
}
uint32_t BCValue::rawValue() const noexcept
{
return _rawValue;
}
/**
* @brief Speichert einen via CAN-Bus gelesenen Wert in
* der BCValue Struktur.
*/
void BCValue::setRawValue(uint32_t newRawValue) const
{
// die per Zufallsgenerator erzeugten Werte des Dummy-Treibers
// können beliebigen Unsinn enthalten, also müssen wir sie
// auch skalieren.
if( _valueType == ValueType::Bool )
{
_rawValue = newRawValue > 0 ? 1 : 0;
return;
}
double value = newRawValue * _factor;
if( _optMin.has_value() && _optMax.has_value() )
{
double min = _optMin.value();
double max = _optMax.value();
value = (int) qBound( min,value, max);
}
_rawValue = value / _factor;
}
BCValue::ValueType BCValue::valueType() const noexcept
{
return _valueType;
}
int BCValue::indexRow() const noexcept
{
return _indexRow;
}
void BCValue::setIndexRow(int newIndexRow)
{
_indexRow = newIndexRow;
}
QString BCValue::label() const
{
return _label;
}
QString BCValue::unitLabel() const
{
return _unitLabel;
}
void BCValue::setFromDouble( double value )
{
//if( _isReadOnly)
switch(_valueType)
{
// wir betrachten plain
case ValueType::Bool :
_rawValue = value > 0 ? 1 : 0;
break;
case ValueType::Plain :
case ValueType::Number:
case ValueType::Float:
if( _optMin.has_value() && _optMax.has_value() )
{
double min = _optMin.value();
double max = _optMax.value();
value = qBound( min,value,max);
}
_rawValue = value / _factor;
default :
break;
}
}
double BCValue::calcMinMaxRatio() const
{
double ratio = 1;
if( _optMin.has_value() && _optMax.has_value() )
{
double min = _optMin.value();
double max = _optMax.value();
double range = max - min;
// Safety: Division durch Null verhindern (wenn min == max)
if (std::abs(range) < 1e-9)
return ratio;
double value = _rawValue * _factor;
// Die eigentliche Formel
ratio = ((value - min) / range);
}
return ratio;
}
bool BCValue::valuesForSlider(ValueRange& valueRange) const
{
valueRange.value = valueRange.min = valueRange.max = 0;
// min & max sind vorraussetzung für den slider
if( !_optMin.has_value() || !_optMax.has_value() )
return false;
// wir erwarten hier, das value zwischen min
// und max liegt weil wir das schon bei setRawValue
// überprüft haben.
valueRange.value = _rawValue * _factor;
valueRange.min = _optMin.value();
valueRange.max = _optMax.value();
valueRange.ratio = calcMinMaxRatio();
return true;
}
void BCValue::dumpValue() const
{
qDebug() << "DeviceID: " << _deviceID << " Register: " << _registerID << " state:" " << state << " << " label: " << _label;
qDebug() << "formattedValue: " << formatValue() << " min: " << _optMin << " max: " << _optMax << " factor: " << _factor << " ValueType: " << (char)_valueType << " ";
qDebug() << "indexRow: " << _indexRow << " isWord: " << isWord() << " isRO: " << isReadOnly();
qDebug();
}
QString BCValue::toString() const
{
QString result;
QTextStream stream(&result);
stream << *this;
/*
qDebug() << "DeviceID: " << _deviceID << " Register: " << _registerID << " state:" " << state << " << " label: " << _label;
qDebug() << "formattedValue: " << formatValue() << " min: " << _optMin << " max: " << _optMax << " factor: " << _factor << " ValueType: " << (char)_valueType << " ";
qDebug() << "indexRow: " << _indexRow << " isWord: " << isWord() << " isRO: " << isReadOnly();
qDebug();
*/
return result;
}
// Generischer Operator für ALLE Q_GADGETs
inline QTextStream& operator<<(QTextStream& out, const BCValue& obj)
{
const QMetaObject* meta = &obj.staticMetaObject;
out << meta->className() << " { ";
// Iteriere über alle Properties (Reflection)
for (int i = 0; i < meta->propertyCount(); ++i) {
QMetaProperty prop = meta->property(i);
const char* propName = prop.name();
QVariant val = prop.readOnGadget(&obj);
out << propName << ": " << val.toString();
if (i < meta->propertyCount() - 1) out << ", ";
}
out << " }";
return out;
}