Skip to content

Commit

Permalink
Updated DPTs, clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
mobilarte committed Nov 24, 2024
1 parent 054a8b3 commit 616d7c9
Show file tree
Hide file tree
Showing 32 changed files with 250 additions and 42 deletions.
14 changes: 0 additions & 14 deletions knx/cemi/ldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ func makeRandBuffer(n int) []byte {
return buffer
}

/* unused!
func makeRandTPDUSegment() []byte {
n := util.Randint64() % 256
buffer := make([]byte, n+2)
buffer[0] = byte(n)
rand.Read(buffer[1:])
return buffer
}
*/

func makeRandLData() []byte {
return bytes.Join([][]byte{
makeRandInfoSegment(),
Expand Down
2 changes: 1 addition & 1 deletion knx/describe.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package knx
Expand Down
2 changes: 1 addition & 1 deletion knx/diagnostic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

// Described in 03_08_07 KNXnetIP Remote Configuration and Diagnosis v01.01.02 AS.pdf
Expand Down
1 change: 1 addition & 0 deletions knx/dpt/formats.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type DatapointValue interface {
Unpack(data []byte) error
}

// DatapointMeta gives meta information about a datapoint.
// DatapointMeta gives meta information about a datapoint
type DatapointMeta interface {
// Unit returns the unit of this datapoint type or empty string if it doesn't have a unit.
Unit() string
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_10.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_10_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_11.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_11_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_12.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_12_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_13_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_14_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_16.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_16_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
10 changes: 7 additions & 3 deletions knx/dpt/types_17.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
type DPT_17001 uint8

func (d DPT_17001) Pack() []byte {
if d > 63 {
return packU8(uint8(63))
if d.IsValid() {
return packU8(d)
} else {
return packU8(uint8(d))
return packU8(uint8(63))
}
}

Expand All @@ -38,6 +38,10 @@ func (d DPT_17001) Unit() string {
return ""
}

func (d DPT_17001) IsValid() bool {
return d <= 0x3F
}

func (d DPT_17001) String() string {
return fmt.Sprintf("%d", uint8(d))
}
2 changes: 1 addition & 1 deletion knx/dpt/types_18.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_18_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2017 Ole Krüger.
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_19.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_19_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_21.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
77 changes: 77 additions & 0 deletions knx/dpt/types_242.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dpt

import (
"fmt"
)

// DPT_242600 represents DPT 242.600 (DPT_Colour_xyY)
// Colour xyY - x: 0-1 (= 0 - 65535) y: 0-1 (= 0 - 65535)
// U16 U16 U8 r6B2
type DPT_242600 struct {
X uint16
Y uint16
YBrightness uint8
ColorValid bool
BrightnessValid bool
}

func (d DPT_242600) Pack() []byte {
validBits := packB2(d.ColorValid, d.BrightnessValid)

x := packU16(uint16(d.X))
y := packU16(uint16(d.Y))

return []byte{0, x[1], x[2], y[1], y[2], d.YBrightness, validBits[0]}
}

func (d *DPT_242600) Unpack(data []byte) error {
if len(data) != 7 {
return ErrInvalidLength
}

var colorValid, brightnessValid bool

err := unpackB2([]byte{data[6]}, &colorValid, &brightnessValid)

if err != nil {
return ErrInvalidData
}

var x, y uint16

xData := []byte{0}
xData = append(xData, data[1], data[2])

err = unpackU16(xData, &x)

if err != nil {
return ErrInvalidData
}

yData := []byte{0}
yData = append(yData, data[3], data[4])

err = unpackU16(yData, &y)

if err != nil {
return ErrInvalidData
}

*d = DPT_242600{
X: x,
Y: y,
YBrightness: uint8(data[5]),
ColorValid: colorValid,
BrightnessValid: brightnessValid,
}

return nil
}

func (d DPT_242600) Unit() string {
return ""
}

func (d DPT_242600) String() string {
return fmt.Sprintf("x: %d y: %d Y: %d ColorValid: %t, BrightnessValid: %t", d.X, d.Y, d.YBrightness, d.ColorValid, d.BrightnessValid)
}
33 changes: 33 additions & 0 deletions knx/dpt/types_242_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dpt

import (
"fmt"
"reflect"
"testing"
)

func TestDPT_242600(t *testing.T) {
var buf []byte
var dst DPT_242600
sources := []DPT_242600{
{X: 0, Y: 0, YBrightness: 1, ColorValid: true, BrightnessValid: true},
{X: 65535, Y: 1, YBrightness: 255, ColorValid: true, BrightnessValid: true},
{X: 32767, Y: 32767, YBrightness: 127, ColorValid: true, BrightnessValid: true},
{X: 6553, Y: 58981, YBrightness: 127, ColorValid: true, BrightnessValid: true},
}

for _, src := range sources {
buf = src.Pack()
err := dst.Unpack(buf)

if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(src, dst) {
fmt.Printf("%+v\n", src)
fmt.Printf("%+v\n", dst)
t.Errorf("Value [%s] after pack/unpack for DPT_242600 differs. Original value was [%v]!", dst, src)
}
}
}
3 changes: 3 additions & 0 deletions knx/dpt/types_251_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt

import (
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_2_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_3.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_3_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
2 changes: 1 addition & 1 deletion knx/dpt/types_6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Martin Müller.
// Copyright 2024 Martin Müller.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt
Expand Down
Loading

0 comments on commit 616d7c9

Please sign in to comment.