Files
BionxControl/bcvalue.cpp
Christoph Holzheuer 95dd9d18e6 Added setFromDouble
2026-01-13 16:29:02 +01:00

145 lines
3.1 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 <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 );
}
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 = 0;
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;
// Die eigentliche Formel
ratio = ((_rawValue - min) / range);
//ratio = (int) qBound( min,ratio, max);
}
return ratio;
}
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();
}
/// ----