Files
BionxControl/bcvalue.cpp
2026-01-18 18:52:30 +01:00

215 lines
4.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 );
}
BCDevice::ID BCValue::getDeviceID() const noexcept
{
return _deviceID;
}
BC::ID BCValue::getRegisterID() const noexcept
{
return _registerID;
}
uint32_t BCValue::getRawValue() const noexcept
{
return _rawValue;
}
void BCValue::setRawValue(uint32_t newRawValue) const
{
_rawValue = newRawValue;
}
BCValue::ValueType BCValue::getValueType() const noexcept
{
return _valueType;
}
int BCValue::getIndexRow() const noexcept
{
return _indexRow;
}
void BCValue::setIndexRow(int newIndexRow)
{
_indexRow = newIndexRow;
}
QString BCValue::getLabel() const
{
return _label;
}
QString BCValue::getUnitLabel() const
{
return _unitLabel;
}
double BCValue::getFactor() const noexcept
{
return _factor;
}
const OptDouble BCValue::getOptMin() const
{
return _optMin;
}
const OptDouble BCValue::getOptMax() const
{
return _optMax;
}
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;
// Die eigentliche Formel
ratio = ((_rawValue - min) / range);
//ratio = (int) qBound( min,ratio, max);
}
return ratio;
}
uint32_t BCValue::getScaledValue() const noexcept
{
double value =_rawValue * _factor;
return (uint32_t) value * calcMinMaxRatio();
}
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();
}
/// ----