fixed dpt 9.* add new value api to linux example

This commit is contained in:
Thomas Kunze 2019-05-29 22:43:21 +02:00
parent ab9644b920
commit a35c0616ac
6 changed files with 86 additions and 87 deletions

View File

@ -1,6 +1,7 @@
#include "knx_facade.h" #include "knx_facade.h"
#include "knx/bau57B0.h" #include "knx/bau57B0.h"
#include "knx/group_object_table_object.h" #include "knx/group_object_table_object.h"
#include "knx/bits.h"
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -9,9 +10,6 @@ LinuxPlatform platfrom;
Bau57B0 bau(platfrom); Bau57B0 bau(platfrom);
KnxFacade knx(bau); KnxFacade knx(bau);
float currentValue = 0;
float maxValue = 0;
float minValue = RAND_MAX;
long lastsend = 0; long lastsend = 0;
#define CURR knx.getGroupObject(1) #define CURR knx.getGroupObject(1)
@ -22,35 +20,32 @@ long lastsend = 0;
void measureTemp() void measureTemp()
{ {
long now = platfrom.millis(); long now = platfrom.millis();
if ((now - lastsend) < 2000) if ((now - lastsend) < 10000)
return; return;
lastsend = now; lastsend = now;
int r = rand(); int r = rand();
currentValue = (r * 1.0) / (RAND_MAX * 1.0); float currentValue = (r * 1.0) / (RAND_MAX * 1.0);
currentValue *= 100 * 100; currentValue *= (670433.28 + 273);
currentValue -= 273;
println(currentValue);
CURR.value(currentValue);
CURR.objectWrite(currentValue); double max = MAX.value();
if (currentValue > max)
MAX.value(currentValue);
if (currentValue > maxValue) double min = MIN.value();
{ if (currentValue < MIN.value().doubleValue())
maxValue = currentValue; MIN.value(currentValue);
MAX.objectWrite(maxValue);
}
if (currentValue < minValue)
{
minValue = currentValue;
MIN.objectWrite(minValue);
}
} }
void resetCallback(GroupObject& go) void resetCallback(GroupObject& go)
{ {
if (go.objectReadBool()) if (go.value().boolValue())
{ {
maxValue = 0; MAX.valueNoSend(-273.0);
minValue = 10000; MIN.valueNoSend(670433.28);
} }
} }
@ -72,6 +67,12 @@ void setup()
if (knx.configured()) if (knx.configured())
{ {
CURR.dataPointType(Dpt(9, 1));
MIN.dataPointType(Dpt(9, 1));
MIN.value(670433.28);
MAX.dataPointType(Dpt(9, 1));
MAX.valueNoSend(-273.0);
RESET.dataPointType(Dpt(1, 15));
RESET.callback(resetCallback); RESET.callback(resetCallback);
printf("Timeout: %d\n", bau.parameters().getWord(0)); printf("Timeout: %d\n", bau.parameters().getWord(0));
printf("Zykl. senden: %d\n", bau.parameters().getByte(2)); printf("Zykl. senden: %d\n", bau.parameters().getByte(2));

View File

@ -6,9 +6,10 @@
#define ASSERT_PAYLOAD(x) \ #define ASSERT_PAYLOAD(x) \
if (payload_length != (x)) \ if (payload_length != (x)) \
return false return false
#define ENSURE_PAYLOAD(x) \ #define ENSURE_PAYLOAD(x)
for (int pi = 0; pi < (x); ++pi) \ //\
payload[pi] = 0 // for (int pi = 0; pi < (x); ++pi) \
// payload[pi] = 0
int KNX_Decode_Value(uint8_t* payload, int payload_length, const Dpt& datatype, KNXValue& value) int KNX_Decode_Value(uint8_t* payload, int payload_length, const Dpt& datatype, KNXValue& value)
{ {
@ -1116,82 +1117,48 @@ int valueToBusValueTimeDelta(const KNXValue& value, uint8_t* payload, int payloa
int valueToBusValueFloat16(const KNXValue& value, uint8_t* payload, int payload_length, const Dpt& datatype) int valueToBusValueFloat16(const KNXValue& value, uint8_t* payload, int payload_length, const Dpt& datatype)
{ {
double numValue = value; double numValue = value;
// bigger values like 670760.0 result in 0x7FFF which denotes invalid data.
// I'm not sure if the GO shouldn't be updated to this value instead
if (numValue > 670433.28)
return false;
if (numValue < -671088.64)
return false;
switch (datatype.subGroup) switch (datatype.subGroup)
{ {
case 1: case 1:
if (numValue < -273.0 || numValue > 670760.0) if (numValue < -273.0)
return false; return false;
break; break;
case 4:
case 7:
case 8:
if (numValue < 0.0 || numValue > 670760.0)
return false;
break;
case 5:
{
if (numValue < 0.0 || numValue > 670760.0)
return false;
break;
}
case 6:
{
if (numValue < 0.0 || numValue > 670760.0)
return false;
break;
}
case 10:
{
if (numValue < -670760.0 || numValue > 670760.0)
return false;
break;
}
case 11:
{
if (numValue < -670760.0 || numValue > 670760.0)
return false;
break;
}
case 20:
{
if (numValue < -670760.0 || numValue > 670760.0)
return false;
break;
}
case 21:
{
if (numValue < -670760.0 || numValue > 670760.0)
return false;
break;
}
case 2: case 2:
case 3: case 3:
case 10:
case 11:
case 20:
case 21:
case 22: case 22:
case 23: case 23:
case 24: case 24:
if (numValue < -670760.0 || numValue > 670760.0)
return false;
break;
case 25: case 25:
{ if (numValue < -670760.0)
if (numValue < -670760.0 || numValue > 670760.0)
return false; return false;
break; break;
} case 4:
case 27: case 5:
{ case 6:
if (numValue < -459.6) case 7:
return false; case 8:
break;
}
case 28: case 28:
if (numValue < 0.0) if (numValue < 0.0)
return false; return false;
break; break;
} case 27:
if (numValue < -459.6)
if (numValue < -671088.64 || numValue > 670760.96)
return false; return false;
break;
}
float16ToPayload(payload, payload_length, 0, numValue, 0xFFFF); float16ToPayload(payload, payload_length, 0, numValue, 0xFFFF);
return true; return true;
@ -1819,9 +1786,20 @@ void signed32ToPayload(uint8_t* payload, int payload_length, int index, int32_t
void float16ToPayload(uint8_t* payload, int payload_length, int index, double value, uint16_t mask) void float16ToPayload(uint8_t* payload, int payload_length, int index, double value, uint16_t mask)
{ {
bool wasNegative = false;
if (value < 0)
{
wasNegative = true;
value *= -1;
}
value *= 100.0; value *= 100.0;
unsigned short exponent = ceil(log2(value) - 10.0); unsigned short exponent = ceil(log2(value) - 11.0);
short mantissa = roundf(value / (1 << exponent)); short mantissa = roundf(value / (1 << exponent));
if (wasNegative)
mantissa *= -1;
signed16ToPayload(payload, payload_length, index, mantissa, mask); signed16ToPayload(payload, payload_length, index, mantissa, mask);
unsigned8ToPayload(payload, payload_length, index, exponent << 3, 0x78 & (mask >> 8)); unsigned8ToPayload(payload, payload_length, index, exponent << 3, 0x78 & (mask >> 8));
} }

View File

@ -35,7 +35,7 @@ class Dpt
{ {
public: public:
Dpt() {} Dpt() {}
Dpt(short mainGroup, short subGroup, short index) Dpt(short mainGroup, short subGroup, short index = 0)
{ {
this->mainGroup = mainGroup; this->mainGroup = mainGroup;
this->subGroup = subGroup; this->subGroup = subGroup;

View File

@ -274,7 +274,7 @@ GroupObjectUpdatedHandler GroupObject::callback()
void GroupObject::value(const KNXValue& value, const Dpt& type) void GroupObject::value(const KNXValue& value, const Dpt& type)
{ {
KNX_Encode_Value(value, _data, _dataLength, type); valueNoSend(value, type);
objectWritten(); objectWritten();
} }
@ -320,3 +320,15 @@ KNXValue GroupObject::value()
{ {
return value(_datapointType); return value(_datapointType);
} }
void GroupObject::valueNoSend(const KNXValue& value)
{
valueNoSend(value, _datapointType);
}
void GroupObject::valueNoSend(const KNXValue& value, const Dpt& type)
{
KNX_Encode_Value(value, _data, _dataLength, type);
}

View File

@ -121,11 +121,14 @@ public:
GroupObjectUpdatedHandler callback(); GroupObjectUpdatedHandler callback();
KNXValue value(const Dpt& type); KNXValue value(const Dpt& type);
void value(const KNXValue& value, const Dpt& type);
bool tryValue(KNXValue& value, const Dpt& type);
KNXValue value(); KNXValue value();
void value(const KNXValue& value, const Dpt& type);
void value(const KNXValue& value); void value(const KNXValue& value);
void valueNoSend(const KNXValue& value, const Dpt& type);
void valueNoSend(const KNXValue& value);
bool tryValue(KNXValue& value, const Dpt& type);
bool tryValue(KNXValue& value); bool tryValue(KNXValue& value);
Dpt dataPointType(); Dpt dataPointType();
void dataPointType(Dpt value); void dataPointType(Dpt value);

View File

@ -488,6 +488,11 @@ void println(double num)
printf("%f\n", num); printf("%f\n", num);
} }
void println(double num, int places)
{
printf("%f\n", num);
}
void println(void) void println(void)
{ {
printf("\n"); printf("\n");