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 3f9996e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
52 changes: 30 additions & 22 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 @@ -67,22 +69,25 @@ func (d DPT_8003) Unit() string {
}

func (d DPT_8003) String() string {
return fmt.Sprintf("%d ms", int32(d))
return fmt.Sprintf("%f ms", d)
}

// 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 All @@ -91,7 +96,7 @@ func (d DPT_8004) Unit() string {
}

func (d DPT_8004) String() string {
return fmt.Sprintf("%d ms", int32(d))
return fmt.Sprintf("%f ms", d)
}

// DPT_8005 represents DPT 8.005 / delta time seconds
Expand All @@ -113,7 +118,7 @@ func (d DPT_8005) String() string {
return fmt.Sprintf("%d s", int16(d))
}

// DPT_8006 represents DPT 8.005 / delta time minutes
// DPT_8006 represents DPT 8.006 / delta time minutes
type DPT_8006 int16

func (d DPT_8006) Pack() []byte {
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 All @@ -172,7 +180,7 @@ func (d DPT_8010) Unit() string {
}

func (d DPT_8010) String() string {
return fmt.Sprintf("%f %%", float32(d))
return fmt.Sprintf("%f %%", d)
}

// DPT_8011 represents DPT 8.011 / Rotation angle °.
Expand Down
44 changes: 22 additions & 22 deletions knx/dpt/types_8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package dpt

import (
"fmt"
"testing"

"math"
Expand Down Expand Up @@ -59,18 +58,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 +82,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 {
t.Errorf("Assignment of value \"%v\" failed for source of type DPT_8003! Has value \"%s\".", value, src)
if float32(src) != value {
t.Errorf("Assignment of value \"%v\" failed for source of type DPT_8004! 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 Expand Up @@ -166,21 +168,19 @@ func TestDPT_8010(t *testing.T) {
var src, dst DPT_8010

for i := 1; i <= 10; i++ {
value := float32(int16(rand.Int31())) / 100

iValue := int16(rand.Uint32() % math.MaxInt16)
value := float32(iValue) / 100

// Pack and unpack to test value
src = DPT_8010(value)
if abs(float32(src)-value) > epsilon {
if float32(src) != value {
t.Errorf("Assignment of value \"%v\" failed for source of type DPT_8010! Has value \"%s\".", value, src)
}

buf = src.Pack()
_ = dst.Unpack(buf)
if math.IsNaN(float64(dst)) {
t.Errorf("Value \"%s\" is not a valid number! Original value was \"%v\".", dst, value)
}
if fmt.Sprintf("%.2f", dst) != fmt.Sprintf("%.2f", value) {
t.Errorf("Value \"%f\" after pack/unpack different from Original value. Was \"%f\"", dst, value)
dst.Unpack(buf)

if abs(float32(dst)-value) > 0.01 {
t.Errorf("Value \"%s\" after pack/unpack different from original value. Was \"%s\"", dst, src)
}
}
}
Expand Down

0 comments on commit 3f9996e

Please sign in to comment.