From 828f0281dd9f0f67a041edea78ab8ba5a8c56279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Sun, 15 Sep 2024 14:17:15 +0100 Subject: [PATCH] Use floats for DPT 8.003, 8.004 and 8.010 --- knx/dpt/types_8.go | 44 ++++++++++++++++++++++++----------------- knx/dpt/types_8_test.go | 23 +++++++++++---------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/knx/dpt/types_8.go b/knx/dpt/types_8.go index 3d5cb06..df20196 100644 --- a/knx/dpt/types_8.go +++ b/knx/dpt/types_8.go @@ -5,7 +5,6 @@ package dpt import ( "fmt" - "math" ) // DPT_8001 represents DPT 8.001 / Counter. @@ -47,18 +46,21 @@ func (d DPT_8002) String() string { } // DPT_8003 represents DPT 8.003 / delta time ms (range -327.68 s ... 327.67 s) -type DPT_8003 int32 +type DPT_8003 float32 func (d DPT_8003) Pack() []byte { - return packV16(int16(int32(d) / 10)) + return packV16(int16(d * 100)) } func (d *DPT_8003) Unpack(data []byte) error { - var i int16 - if err := unpackV16(data, &i); err != nil { - return nil + var value int16 + + if err := unpackV16(data, &value); err != nil { + return err } - *d = DPT_8003(int32(i) * 10) + + *d = DPT_8003(float32(value) / 100) + return nil } @@ -71,18 +73,21 @@ func (d DPT_8003) String() string { } // DPT_8004 represents DPT 8.004 / delta time ms (range -3276.8 s ... 3276.7 s) -type DPT_8004 int32 +type DPT_8004 float32 func (d DPT_8004) Pack() []byte { - return packV16(int16(int32(d) / 100)) + return packV16(int16(d * 10)) } func (d *DPT_8004) Unpack(data []byte) error { - var i int16 - if err := unpackV16(data, &i); err != nil { - return nil + var value int16 + + if err := unpackV16(data, &value); err != nil { + return err } - *d = DPT_8004(int32(i) * 100) + + *d = DPT_8004(float32(value) / 10) + return nil } @@ -155,15 +160,18 @@ func (d DPT_8007) String() string { type DPT_8010 float32 func (d DPT_8010) Pack() []byte { - return packV16(int16(math.Round(float64(d) / 0.01))) + return packV16(int16(d * 100)) } func (d *DPT_8010) Unpack(data []byte) error { - var i int16 - if err := unpackV16(data, &i); err != nil { - return nil + var value int16 + + if err := unpackV16(data, &value); err != nil { + return err } - *d = DPT_8010(float32(i) * 0.01) + + *d = DPT_8010(float32(value) / 100) + return nil } diff --git a/knx/dpt/types_8_test.go b/knx/dpt/types_8_test.go index 1fc2ae5..a6ef0c0 100644 --- a/knx/dpt/types_8_test.go +++ b/knx/dpt/types_8_test.go @@ -59,18 +59,20 @@ func TestDPT_8003(t *testing.T) { var src, dst DPT_8003 for i := 1; i <= 10; i++ { - - value := int32(rand.Uint32()%math.MaxInt16) * 10 + value := float32(int16(rand.Int31())) / 100 // Pack and unpack to test value src = DPT_8003(value) - if int32(src) != value { + + if float32(src) != value { t.Errorf("Assignment of value \"%v\" failed for source of type DPT_8003! Has value \"%s\".", value, src) } + buf = src.Pack() dst.Unpack(buf) - if int32(dst) != value { - t.Errorf("Value \"%s\" after pack/unpack different from Original value. Was \"%v\"", dst, value) + + if abs(float32(dst)-value) > 0.01 { + t.Errorf("Value \"%s\" after pack/unpack different from original value. Was \"%s\"", dst, src) } } } @@ -81,18 +83,19 @@ func TestDPT_8004(t *testing.T) { var src, dst DPT_8004 for i := 1; i <= 10; i++ { - - value := int32(rand.Uint32()%math.MaxInt16) * 100 + value := float32(int16(rand.Int31())) / 10 // Pack and unpack to test value src = DPT_8004(value) - if int32(src) != value { + if float32(src) != value { t.Errorf("Assignment of value \"%v\" failed for source of type DPT_8003! Has value \"%s\".", value, src) } + buf = src.Pack() dst.Unpack(buf) - if int32(dst) != value { - t.Errorf("Value \"%s\" after pack/unpack different from Original value. Was \"%v\"", dst, value) + + if abs(float32(dst)-value) > 0.1 { + t.Errorf("Value \"%s\" after pack/unpack different from original value. Was \"%s\"", dst, src) } } }