Skip to content

Commit

Permalink
Use floats for DPT 8.003, 8.004 and 8.010
Browse files Browse the repository at this point in the history
  • Loading branch information
vapourismo committed Sep 15, 2024
1 parent 1c067d1 commit 828f028
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
44 changes: 26 additions & 18 deletions knx/dpt/types_8.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package dpt

import (
"fmt"
"math"
)

// DPT_8001 represents DPT 8.001 / Counter.
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
23 changes: 13 additions & 10 deletions knx/dpt/types_8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 828f028

Please sign in to comment.