add converstions to KnxValue and remove now obsolet methods

This commit is contained in:
Thomas Kunze 2019-06-12 00:01:21 +02:00
parent 57db264603
commit 378385ee97
10 changed files with 435 additions and 138 deletions

View File

@ -45,7 +45,7 @@ void measureTemp()
// callback from reset-GO
void resetCallback(GroupObject& go)
{
if (go.value().boolValue())
if (go.value())
{
maxValue = 0;
minValue = 10000;

View File

@ -14,7 +14,7 @@
// callback from switch-GO
void switchCallback(GroupObject& go)
{
if (goBlock.value().boolValue())
if (goBlock.value())
return;
bool value = goSwitch.value();

View File

@ -24,7 +24,7 @@ void measureTemp()
lastsend = now;
int r = rand();
float currentValue = (r * 1.0) / (RAND_MAX * 1.0);
double currentValue = (r * 1.0) / (RAND_MAX * 1.0);
currentValue *= (670433.28 + 273);
currentValue -= 273;
println(currentValue);
@ -35,13 +35,13 @@ void measureTemp()
MAX.value(currentValue);
double min = MIN.value();
if (currentValue < MIN.value().doubleValue())
if (currentValue < (double)MIN.value())
MIN.value(currentValue);
}
void resetCallback(GroupObject& go)
{
if (go.value().boolValue())
if (go.value())
{
MAX.valueNoSend(-273.0);
MIN.valueNoSend(670433.28);

View File

@ -82,7 +82,7 @@ void BauSystemB::updateGroupObject(GroupObject & go, uint8_t * data, uint8_t len
memcpy(goData, data, length);
go.commFlag(Update);
go.commFlag(Updated);
GroupObjectUpdatedHandler handler = go.callback();
if (handler)
handler(go);

View File

@ -417,11 +417,11 @@ int busValueToUnsigned8(const uint8_t* payload, int payload_length, const Dpt& d
switch (datatype.subGroup)
{
case 1:
value.ucharValue(unsigned8FromPayload(payload, 0) * 100.0 / 255.0);
value = (uint8_t)(unsigned8FromPayload(payload, 0) * 100.0 / 255.0);
return true;
case 3:
value.ucharValue(unsigned8FromPayload(payload, 0) * 360.0 / 255.0);
value = (uint8_t)unsigned8FromPayload(payload, 0) * 360.0 / 255.0;
return true;
case 6:
@ -434,14 +434,14 @@ int busValueToUnsigned8(const uint8_t* payload, int payload_length, const Dpt& d
}
}
value.ucharValue(unsigned8FromPayload(payload, 0));
value = unsigned8FromPayload(payload, 0);
return true;
}
int busValueToSigned8(const uint8_t* payload, int payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(1);
value.ucharValue(unsigned8FromPayload(payload, 0));
value = (uint8_t)(unsigned8FromPayload(payload, 0));
return true;
}
@ -455,7 +455,7 @@ int busValueToStatusAndMode(const uint8_t* payload, int payload_length, const Dp
}
else if (datatype.index == 5)
{
value.ucharValue(unsigned8FromPayload(payload, 0) & 0x07);
value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x07);
return true;
}
return false;
@ -514,7 +514,7 @@ int busValueToTime(const uint8_t* payload, int payload_length, const Dpt& dataty
switch (datatype.index)
{
case 0:
value.ucharValue((unsigned8FromPayload(payload, 0) >> 5) & 0x07);
value = (uint8_t)((unsigned8FromPayload(payload, 0) >> 5) & 0x07);
return true;
case 1:
{
@ -598,7 +598,7 @@ int busValueToAccess(const uint8_t* payload, int payload_length, const Dpt& data
return false;
digits += digit * factor;
}
value.intValue(digits);
value = digits;
return true;
}
case 1:
@ -633,7 +633,7 @@ int busValueToString(const uint8_t* payload, int payload_length, const Dpt& data
int busValueToScene(const uint8_t* payload, int payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(1);
value.ucharValue(unsigned8FromPayload(payload, 0) & 0x3F);
value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x3F);
return true;
}
@ -649,7 +649,7 @@ int busValueToSceneControl(const uint8_t* payload, int payload_length, const Dpt
}
case 1:
{
value.ucharValue(unsigned8FromPayload(payload, 0) & 0x3F);
value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x3F);
return true;
}
}
@ -669,7 +669,7 @@ int busValueToSceneInfo(const uint8_t* payload, int payload_length, const Dpt& d
}
case 1:
{
value.ucharValue(unsigned8FromPayload(payload, 0) & 0x3F);
value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x3F);
return true;
}
}
@ -684,7 +684,7 @@ int busValueToSceneConfig(const uint8_t* payload, int payload_length, const Dpt&
{
case 0:
{
value.ucharValue(unsigned8FromPayload(payload, 0) & 0x3F);
value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x3F);
return true;
}
case 1:
@ -743,7 +743,7 @@ int busValueToDateTime(const uint8_t* payload, int payload_length, const Dpt& da
if (bitFromPayload(payload, 53))
return false;
value.ucharValue((unsigned8FromPayload(payload, 3) >> 5) & 0x07);
value = (uint8_t)((unsigned8FromPayload(payload, 3) >> 5) & 0x07);
return true;
}
case 2:
@ -799,7 +799,7 @@ int busValueToAlarmInfo(const uint8_t* payload, int payload_length, const Dpt& d
case 0:
case 2:
case 3:
value.ucharValue(unsigned8FromPayload(payload, datatype.index));
value = unsigned8FromPayload(payload, datatype.index);
return true;
case 4:
case 5:
@ -837,13 +837,13 @@ int busValueToVersion(const uint8_t* payload, int payload_length, const Dpt& dat
switch (datatype.index)
{
case 0:
value.ucharValue((unsigned8FromPayload(payload, 0) >> 3) & 0x1F);
value = (uint8_t)((unsigned8FromPayload(payload, 0) >> 3) & 0x1F);
return true;
case 1:
value.ushortValue((unsigned16FromPayload(payload, 0) >> 6) & 0x1F);
value = (uint16_t)((unsigned16FromPayload(payload, 0) >> 6) & 0x1F);
return true;
case 2:
value.ucharValue(unsigned8FromPayload(payload, 1) & 0x3F);
value = (uint8_t)(unsigned8FromPayload(payload, 1) & 0x3F);
return true;
}
@ -859,7 +859,7 @@ int busValueToScaling(const uint8_t* payload, int payload_length, const Dpt& dat
value = unsigned16FromPayload(payload, 0);
return true;
case 1:
value.ucharValue(unsigned8FromPayload(payload, 2) * 100.0 / 255.0);
value = (uint8_t)(unsigned8FromPayload(payload, 2) * 100.0 / 255.0);
return true;
}
@ -917,7 +917,7 @@ int busValueToFlaggedScaling(const uint8_t* payload, int payload_length, const D
switch (datatype.index)
{
case 0:
value.ucharValue(unsigned8FromPayload(payload, 0) * 100.0 / 255.0);
value = (uint8_t)(unsigned8FromPayload(payload, 0) * 100.0 / 255.0);
return true;
case 1:
value = bitFromPayload(payload, 15);
@ -935,7 +935,7 @@ int busValueToActiveEnergy(const uint8_t* payload, int payload_length, const Dpt
value = signed32FromPayload(payload, 0);
return true;
case 1:
value.ucharValue(unsigned8FromPayload(payload, 4));
value = unsigned8FromPayload(payload, 4);
return true;
case 2:
case 3:
@ -1602,8 +1602,8 @@ int valueToBusValueLocale(const KNXValue& value, uint8_t* payload, int payload_l
if (!datatype.index || (datatype.mainGroup == 231 && datatype.index == 1))
{
ENSURE_PAYLOAD(datatype.mainGroup == 231 ? 4 : 2);
unsigned8ToPayload(payload, payload_length, datatype.index * 2, ((char*)value)[0], 0xff);
unsigned8ToPayload(payload, payload_length, datatype.index * 2 + 1, ((char*)value)[1], 0xff);
unsigned8ToPayload(payload, payload_length, datatype.index * 2, ((const char*)value)[0], 0xff);
unsigned8ToPayload(payload, payload_length, datatype.index * 2 + 1, ((const char*)value)[1], 0xff);
return true;
}

View File

@ -195,7 +195,7 @@ void GroupObject::value(const KNXValue& value, const Dpt& type)
KNXValue GroupObject::value(const Dpt& type)
{
KNXValue value;
KNXValue value = 0;
KNX_Decode_Value(_data, _dataLength, type, value);
return value;
}

View File

@ -9,7 +9,7 @@ class GroupObjectTableObject;
enum ComFlag
{
Update = 0, //!< Group object was updated
Updated = 0, //!< Group object was updated
ReadRequest = 1, //!< Read was requested but was not processed
WriteRequest = 2, //!< Write was requested but was not processed
Transmitting = 3, //!< Group Object is processed a the moment (read or write)

View File

@ -68,7 +68,7 @@ GroupObject& GroupObjectTableObject::nextUpdatedObject(bool& valid)
{
GroupObject& go = get(asap);
if (go.commFlag() == Update)
if (go.commFlag() == Updated)
{
go.commFlag(Ok);
startIdx = asap + 1;

View File

@ -1,250 +1,531 @@
#include "knx_value.h"
KNXValue::KNXValue()
{}
#include <cstring>
#include <cstdlib>
#include <ctime>
KNXValue::KNXValue(bool value)
{
_value.boolValue = value;
_type = BoolType;
}
KNXValue::KNXValue(uint8_t value)
{
_value.ucharValue = value;
_type = UCharType;
}
KNXValue::KNXValue(uint16_t value)
{
_value.ushortValue = value;
_type = UShortType;
}
KNXValue::KNXValue(uint32_t value)
{
_value.uintValue = value;
_type = UIntType;
}
KNXValue::KNXValue(uint64_t value)
{
_value.ulongValue = value;
_type = ULongType;
}
KNXValue::KNXValue(int8_t value)
{
_value.charValue = value;
_type = CharType;
}
KNXValue::KNXValue(int16_t value)
{
_value.shortValue = value;
_type = ShortType;
}
KNXValue::KNXValue(int32_t value)
{
_value.intValue = value;
_type = IntType;
}
KNXValue::KNXValue(int64_t value)
{
_value.longValue = value;
_type = LongType;
}
KNXValue::KNXValue(double value)
{
_value.doubleValue = value;
_type = DoubleType;
}
KNXValue::KNXValue(char* value)
KNXValue::KNXValue(const char* value)
{
_value.stringValue = value;
_type = StringType;
}
KNXValue::KNXValue(struct tm value)
{
_value.timeValue = value;
_type = TimeType;
}
KNXValue::operator bool() const
{
return _value.boolValue;
return boolValue();
}
KNXValue::operator uint8_t() const
{
return _value.ucharValue;
return ucharValue();
}
KNXValue::operator uint16_t() const
{
return _value.ushortValue;
return ushortValue();
}
KNXValue::operator uint32_t() const
{
return _value.uintValue;
return uintValue();
}
KNXValue::operator uint64_t() const
{
return _value.ulongValue;
return ulongValue();
}
KNXValue::operator int8_t() const
{
return _value.charValue;
return charValue();
}
KNXValue::operator int16_t() const
{
return _value.shortValue;
return shortValue();
}
KNXValue::operator int32_t() const
{
return _value.intValue;
return intValue();
}
KNXValue::operator int64_t() const
{
return _value.longValue;
return longValue();
}
KNXValue::operator double() const
{
return _value.doubleValue;
return doubleValue();
}
KNXValue::operator char*() const
KNXValue::operator const char*() const
{
return _value.stringValue;
return stringValue();
}
KNXValue::operator struct tm() const
{
return _value.timeValue;
return timeValue();
}
KNXValue& KNXValue::operator=(const bool value)
{
_value.boolValue = value;
_type = BoolType;
return *this;
}
KNXValue& KNXValue::operator=(const uint8_t value)
{
_value.ucharValue = value;
_type = UCharType;
return *this;
}
KNXValue& KNXValue::operator=(const uint16_t value)
{
_value.ushortValue = value;
_type = UShortType;
return *this;
}
KNXValue& KNXValue::operator=(const uint32_t value)
{
_value.uintValue = value;
_type = UIntType;
return *this;
}
KNXValue& KNXValue::operator=(const uint64_t value)
{
_value.ulongValue = value;
_type = ULongType;
return *this;
}
KNXValue& KNXValue::operator=(const int8_t value)
{
_value.charValue = value;
_type = CharType;
return *this;
}
KNXValue& KNXValue::operator=(const int16_t value)
{
_value.shortValue = value;
_type = ShortType;
return *this;
}
KNXValue& KNXValue::operator=(const int32_t value)
{
_value.boolValue = value;
_type = IntType;
return *this;
}
KNXValue& KNXValue::operator=(const int64_t value)
{
_value.longValue = value;
_type = LongType;
return *this;
}
KNXValue& KNXValue::operator=(const double value)
{
_value.doubleValue = value;
_type = DoubleType;
return *this;
}
KNXValue& KNXValue::operator=(const char* value)
{
_value.stringValue = value;
_type = StringType;
return *this;
}
KNXValue& KNXValue::operator=(const struct tm value)
{
_value.timeValue = value;
_type = TimeType;
return *this;
}
bool KNXValue::boolValue() const
{
return _value.boolValue;
switch (_type)
{
case BoolType:
return _value.boolValue;
case UCharType:
case UShortType:
case UIntType:
case ULongType:
case CharType:
case ShortType:
case IntType:
case LongType:
case TimeType:
return longValue() != 0;
case DoubleType:
return _value.doubleValue != 0;
case StringType:
return strcmp(_value.stringValue, "true") == 0 || strcmp(_value.stringValue, "True") == 0 || longValue() != 0 || doubleValue() != 0;
}
return 0;
}
uint8_t KNXValue::ucharValue() const
{
return _value.ucharValue;
switch (_type)
{
case UCharType:
return _value.ucharValue;
case BoolType:
case UShortType:
case UIntType:
case ULongType:
case TimeType:
return (uint8_t)ulongValue();
case CharType:
case ShortType:
case IntType:
case LongType:
case DoubleType:
case StringType:
return (uint8_t)longValue();
}
return 0;
}
uint16_t KNXValue::ushortValue() const
{
return _value.ushortValue;
switch (_type)
{
case UShortType:
return _value.ushortValue;
case BoolType:
case UCharType:
case UIntType:
case ULongType:
case TimeType:
return (uint16_t)ulongValue();
case CharType:
case ShortType:
case IntType:
case LongType:
case DoubleType:
case StringType:
return (uint16_t)longValue();
}
return 0;
}
uint32_t KNXValue::uintValue() const
{
return _value.uintValue;
switch (_type)
{
case UIntType:
return _value.uintValue;
case BoolType:
case UCharType:
case UShortType:
case ULongType:
case TimeType:
return (uint32_t)ulongValue();
case CharType:
case ShortType:
case IntType:
case LongType:
case DoubleType:
case StringType:
return (uint32_t)longValue();
}
return 0;
}
uint64_t KNXValue::ulongValue() const
{
return _value.ulongValue;
switch (_type)
{
case ULongType:
return _value.uintValue;
case BoolType:
return _value.boolValue ? 1 : 0;
case UCharType:
return (uint64_t)_value.ucharValue;
case UShortType:
return (uint64_t)_value.ushortValue;
case UIntType:
return (uint64_t)_value.uintValue;
case TimeType:
{
struct tm* timeptr = const_cast<struct tm*>(&_value.timeValue);
return (uint64_t)mktime(timeptr);
}
case CharType:
return (uint64_t)_value.charValue;
case ShortType:
return (uint64_t)_value.shortValue;
case IntType:
return (uint64_t)_value.intValue;
case LongType:
return (uint64_t)_value.longValue;
case DoubleType:
return (uint64_t)_value.doubleValue;
case StringType:
return (uint64_t)strtoul(_value.stringValue, NULL, 0);
}
return 0;
}
int8_t KNXValue::charValue() const
{
return _value.charValue;
switch (_type)
{
case CharType:
return _value.charValue;
case BoolType:
case UCharType:
case UShortType:
case UIntType:
case ULongType:
case TimeType:
return (int8_t)ulongValue();
case ShortType:
case IntType:
case LongType:
case DoubleType:
case StringType:
return (int8_t)longValue();
}
return 0;
}
int16_t KNXValue::shortValue() const
{
return _value.shortValue;
switch (_type)
{
case ShortType:
return _value.shortValue;
case BoolType:
case UCharType:
case UShortType:
case UIntType:
case ULongType:
case TimeType:
return (int16_t)ulongValue();
case CharType:
case IntType:
case LongType:
case DoubleType:
case StringType:
return (int16_t)longValue();
}
return 0;
}
int32_t KNXValue::intValue() const
{
return _value.intValue;
switch (_type)
{
case IntType:
return _value.shortValue;
case BoolType:
case UCharType:
case UShortType:
case UIntType:
case ULongType:
case TimeType:
return (int32_t)ulongValue();
case CharType:
case ShortType:
case LongType:
case DoubleType:
case StringType:
return (int32_t)longValue();
}
return 0;
}
int64_t KNXValue::longValue() const
{
return _value.longValue;
switch (_type)
{
case LongType:
return _value.longValue;
case BoolType:
return _value.boolValue ? 1 : 0;
case UCharType:
return (int64_t)_value.ucharValue;
case UShortType:
return (int64_t)_value.ushortValue;
case UIntType:
return (int64_t)_value.uintValue;
case ULongType:
return (int64_t)_value.uintValue;
case TimeType:
return (int64_t)ulongValue();
case CharType:
return (int64_t)_value.charValue;
case ShortType:
return (int64_t)_value.shortValue;
case IntType:
return (int64_t)_value.intValue;
case DoubleType:
return (int64_t)_value.doubleValue;
case StringType:
return strtol(_value.stringValue, NULL, 0);
}
return 0;
}
double KNXValue::doubleValue() const
{
return _value.doubleValue;
switch (_type)
{
case DoubleType:
return _value.doubleValue;
case BoolType:
return _value.boolValue ? 1 : 0;
case UCharType:
return _value.ucharValue;
case UShortType:
return _value.ushortValue;
case UIntType:
return _value.uintValue;
case ULongType:
return _value.uintValue;
case TimeType:
return ulongValue();
case CharType:
return _value.charValue;
case ShortType:
return _value.shortValue;
case IntType:
return _value.intValue;
case LongType:
return _value.longValue;
case StringType:
return strtod(_value.stringValue, NULL);
}
return 0;
}
char* KNXValue::stringValue() const
const char* KNXValue::stringValue() const
{
return _value.stringValue;
switch (_type)
{
case DoubleType:
case BoolType:
case UCharType:
case UShortType:
case UIntType:
case ULongType:
case TimeType:
case CharType:
case ShortType:
case IntType:
case LongType:
return ""; // we would have to manage the memory for the string otherwise. Maybe later.
case StringType:
return _value.stringValue;
}
return 0;
}
struct tm KNXValue::timeValue() const
{
return _value.timeValue;
}
void KNXValue::boolValue(bool value)
{
_value.boolValue = value;
}
void KNXValue::ucharValue(uint8_t value)
{
_value.ucharValue = value;
}
void KNXValue::ushortValue(uint16_t value)
{
_value.ushortValue = value;
}
void KNXValue::uintValue(uint32_t value)
{
_value.uintValue = value;
}
void KNXValue::ulongValue(uint64_t value)
{
_value.ulongValue = value;
}
void KNXValue::charValue(int8_t value)
{
_value.charValue = value;
}
void KNXValue::shortValue(int16_t value)
{
_value.shortValue = value;
}
void KNXValue::intValue(int32_t value)
{
_value.intValue = value;
}
void KNXValue::longValue(int64_t value)
{
_value.longValue = value;
}
void KNXValue::doubleValue(double value)
{
_value.doubleValue = value;
}
void KNXValue::stringValue(char* value)
{
_value.stringValue = value;
}
void KNXValue::timeValue(struct tm value)
{
_value.timeValue = value;
}
switch (_type)
{
case TimeType:
return _value.timeValue;
case BoolType:
case UCharType:
case UShortType:
case UIntType:
case ULongType:
case CharType:
case ShortType:
case IntType:
case LongType:
case DoubleType:
case StringType:
{
time_t timeVal = ulongValue();
struct tm* timePtr = gmtime(&timeVal);
return *timePtr;
}
}
struct tm tmp;
return tmp;
}

View File

@ -6,7 +6,6 @@
class KNXValue
{
public:
KNXValue();
KNXValue(bool value);
KNXValue(uint8_t value);
KNXValue(uint16_t value);
@ -17,7 +16,7 @@ class KNXValue
KNXValue(int32_t value);
KNXValue(int64_t value);
KNXValue(double value);
KNXValue(char* value);
KNXValue(const char* value);
KNXValue(struct tm value);
operator bool() const;
@ -30,11 +29,24 @@ class KNXValue
operator int32_t() const;
operator int64_t() const;
operator double() const;
operator char*() const;
operator const char*() const;
operator struct tm() const;
KNXValue& operator=(const bool value);
KNXValue& operator=(const uint8_t value);
KNXValue& operator=(const uint16_t value);
KNXValue& operator=(const uint32_t value);
KNXValue& operator=(const uint64_t value);
KNXValue& operator=(const int8_t value);
KNXValue& operator=(const int16_t value);
KNXValue& operator=(const int32_t value);
KNXValue& operator=(const int64_t value);
KNXValue& operator=(const double value);
KNXValue& operator=(const char* value);
KNXValue& operator=(const struct tm value);
private:
bool boolValue() const;
uint8_t ucharValue() const;
uint16_t ushortValue() const;
@ -45,23 +57,10 @@ class KNXValue
int32_t intValue() const;
int64_t longValue() const;
double doubleValue() const;
char* stringValue() const;
const char* stringValue() const;
struct tm timeValue() const;
void boolValue(bool value);
void ucharValue(uint8_t value);
void ushortValue(uint16_t value);
void uintValue(uint32_t value);
void ulongValue(uint64_t value);
void charValue(int8_t value);
void shortValue(int16_t value);
void intValue(int32_t value);
void longValue(int64_t value);
void doubleValue(double value);
void stringValue(char* value);
void timeValue(struct tm value);
private:
union Value
{
bool boolValue;
@ -74,8 +73,25 @@ class KNXValue
int32_t intValue;
int64_t longValue;
double doubleValue;
char* stringValue;
const char* stringValue;
struct tm timeValue;
};
enum ValueType
{
BoolType,
UCharType,
UShortType,
UIntType,
ULongType,
CharType,
ShortType,
IntType,
LongType,
DoubleType,
StringType,
TimeType
};
ValueType _type;
Value _value;
};