Files
BionxControl/bcvalue.cpp

215 lines
4.1 KiB
C++
Raw Normal View History

2025-12-15 23:05:48 +01:00
/***************************************************************************
2025-12-26 14:07:55 +01:00
BionxControl
2026-01-03 23:51:14 +01:00
© 2025 -2026 christoph holzheuer
2025-12-26 14:07:55 +01:00
christoph.holzheuer@gmail.com
2025-12-15 23:05:48 +01:00
2025-12-26 14:07:55 +01:00
Using:
2025-12-15 20:57:09 +01:00
2025-12-26 14:07:55 +01:00
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
2025-12-15 20:57:09 +01:00
2025-12-26 14:07:55 +01:00
Based on Bionx data type descriptions from:
2025-12-15 20:57:09 +01:00
2025-12-26 14:07:55 +01:00
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
2025-12-15 20:57:09 +01:00
2025-12-26 14:07:55 +01:00
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
2025-12-15 20:57:09 +01:00
2025-12-15 23:05:48 +01:00
***************************************************************************/
2025-12-26 14:07:55 +01:00
2025-12-16 22:42:35 +01:00
#include <QMetaEnum>
2025-12-22 12:39:38 +01:00
2025-12-28 22:48:18 +01:00
#include <bcvalue.h>
2026-01-03 02:50:28 +01:00
///-------------------------------
2026-01-03 02:50:28 +01:00
BCValue::BCValue( BCDevice::ID deviceID_, BC::ID registerID_)
2026-01-12 23:06:36 +01:00
: _deviceID{deviceID_}, _registerID{registerID_}
2025-12-16 21:21:59 +01:00
{
2026-01-07 17:13:35 +01:00
2025-12-16 21:21:59 +01:00
}
2026-01-07 17:13:35 +01:00
QString BCValue::formatValue() const
2026-01-03 02:50:28 +01:00
{
2026-01-12 23:06:36 +01:00
if( _factor == 1 )
return QString::number( _rawValue );
2026-01-03 02:50:28 +01:00
2026-01-12 23:06:36 +01:00
double result =_rawValue * _factor;
2026-01-03 02:50:28 +01:00
return QString::number(result, 'f', 2);
}
2026-01-07 17:13:35 +01:00
bool BCValue::isWord() const
{
2026-01-12 23:06:36 +01:00
return _valueFlags.testFlag(BCValue::Flag::IsWord);
2026-01-07 17:13:35 +01:00
}
2026-01-06 18:47:08 +01:00
2026-01-08 14:55:47 +01:00
bool BCValue::isReadOnly() const
{
2026-01-12 23:06:36 +01:00
return _valueFlags.testFlag(BCValue::Flag::ReadOnly);
2026-01-08 14:55:47 +01:00
}
2026-01-13 16:29:02 +01:00
bool BCValue::testFlag( BCValue::Flag flag ) const
{
return _valueFlags.testFlag( flag );
}
void BCValue::setFlag( BCValue::Flag flag, bool state) const
{
_valueFlags.setFlag( flag, state );
}
2026-01-16 23:46:58 +01:00
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;
}
2026-01-12 23:06:36 +01:00
void BCValue::setFromDouble( double value )
{
2026-01-13 16:29:02 +01:00
//if( _isReadOnly)
switch(_valueType)
2026-01-12 23:06:36 +01:00
{
2026-01-13 16:29:02 +01:00
// wir betrachten plain
2026-01-12 23:06:36 +01:00
2026-01-13 16:29:02 +01:00
case ValueType::Bool :
_rawValue = value >0 ? 1 : 0;
break;
case ValueType::Plain :
case ValueType::Number:
case ValueType::Float:
2026-01-12 23:06:36 +01:00
2026-01-13 16:29:02 +01:00
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;
}
2026-01-12 23:06:36 +01:00
}
2026-01-08 14:55:47 +01:00
2026-01-11 14:48:51 +01:00
double BCValue::calcMinMaxRatio() const
2026-01-06 18:47:08 +01:00
{
2026-01-18 18:52:30 +01:00
double ratio = 1;
2026-01-06 18:47:08 +01:00
2026-01-12 23:06:36 +01:00
if( _optMin.has_value() && _optMax.has_value() )
2026-01-06 18:47:08 +01:00
{
2026-01-12 23:06:36 +01:00
double min = _optMin.value();
double max = _optMax.value();
2026-01-06 18:47:08 +01:00
double range = max - min;
// Safety: Division durch Null verhindern (wenn min == max)
if (std::abs(range) < 1e-9)
return ratio;
// Die eigentliche Formel
2026-01-12 23:06:36 +01:00
ratio = ((_rawValue - min) / range);
2026-01-06 18:47:08 +01:00
//ratio = (int) qBound( min,ratio, max);
}
return ratio;
}
2026-01-18 18:52:30 +01:00
uint32_t BCValue::getScaledValue() const noexcept
{
double value =_rawValue * _factor;
return (uint32_t) value * calcMinMaxRatio();
}
2026-01-03 13:15:15 +01:00
void BCValue::dumpValue() const
{
2026-01-12 23:06:36 +01:00
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();
2026-01-03 13:15:15 +01:00
qDebug();
}
2026-01-03 02:50:28 +01:00
/// ----