diff --git a/knx/cemi/ldata_test.go b/knx/cemi/ldata_test.go index 15fce4d..7e13942 100644 --- a/knx/cemi/ldata_test.go +++ b/knx/cemi/ldata_test.go @@ -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(), diff --git a/knx/describe.go b/knx/describe.go index bea5ba4..32b7049 100644 --- a/knx/describe.go +++ b/knx/describe.go @@ -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 diff --git a/knx/diagnostic.go b/knx/diagnostic.go index b316249..4a88846 100644 --- a/knx/diagnostic.go +++ b/knx/diagnostic.go @@ -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 diff --git a/knx/dpt/formats.go b/knx/dpt/formats.go index 49ab116..883a61e 100644 --- a/knx/dpt/formats.go +++ b/knx/dpt/formats.go @@ -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 diff --git a/knx/dpt/types.go b/knx/dpt/types.go index b92ef76..c225ee7 100644 --- a/knx/dpt/types.go +++ b/knx/dpt/types.go @@ -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 diff --git a/knx/dpt/types_10.go b/knx/dpt/types_10.go index 60aa539..506d56d 100644 --- a/knx/dpt/types_10.go +++ b/knx/dpt/types_10.go @@ -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 diff --git a/knx/dpt/types_10_test.go b/knx/dpt/types_10_test.go index 8c88632..009061e 100644 --- a/knx/dpt/types_10_test.go +++ b/knx/dpt/types_10_test.go @@ -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 diff --git a/knx/dpt/types_11.go b/knx/dpt/types_11.go index f8e91ff..ac14314 100644 --- a/knx/dpt/types_11.go +++ b/knx/dpt/types_11.go @@ -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 diff --git a/knx/dpt/types_11_test.go b/knx/dpt/types_11_test.go index 07a8fbc..d65d3b8 100644 --- a/knx/dpt/types_11_test.go +++ b/knx/dpt/types_11_test.go @@ -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 diff --git a/knx/dpt/types_12.go b/knx/dpt/types_12.go index 7e2c33b..0dbd1dd 100644 --- a/knx/dpt/types_12.go +++ b/knx/dpt/types_12.go @@ -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 diff --git a/knx/dpt/types_12_test.go b/knx/dpt/types_12_test.go index 8af360a..8a554e1 100644 --- a/knx/dpt/types_12_test.go +++ b/knx/dpt/types_12_test.go @@ -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 diff --git a/knx/dpt/types_13_test.go b/knx/dpt/types_13_test.go index a2c455b..aa94a57 100644 --- a/knx/dpt/types_13_test.go +++ b/knx/dpt/types_13_test.go @@ -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 diff --git a/knx/dpt/types_14_test.go b/knx/dpt/types_14_test.go index 2d47299..8dd0080 100644 --- a/knx/dpt/types_14_test.go +++ b/knx/dpt/types_14_test.go @@ -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 diff --git a/knx/dpt/types_16.go b/knx/dpt/types_16.go index c82f31c..62d323e 100644 --- a/knx/dpt/types_16.go +++ b/knx/dpt/types_16.go @@ -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 diff --git a/knx/dpt/types_16_test.go b/knx/dpt/types_16_test.go index 7e472b2..8887f24 100644 --- a/knx/dpt/types_16_test.go +++ b/knx/dpt/types_16_test.go @@ -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 diff --git a/knx/dpt/types_17.go b/knx/dpt/types_17.go index e96dd26..266f2bb 100644 --- a/knx/dpt/types_17.go +++ b/knx/dpt/types_17.go @@ -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)) } } @@ -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)) } diff --git a/knx/dpt/types_18.go b/knx/dpt/types_18.go index f52b250..9eace3f 100644 --- a/knx/dpt/types_18.go +++ b/knx/dpt/types_18.go @@ -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 diff --git a/knx/dpt/types_18_test.go b/knx/dpt/types_18_test.go index 7c7a906..3e9b8c8 100644 --- a/knx/dpt/types_18_test.go +++ b/knx/dpt/types_18_test.go @@ -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 diff --git a/knx/dpt/types_19.go b/knx/dpt/types_19.go index 3a0c98b..6c62116 100644 --- a/knx/dpt/types_19.go +++ b/knx/dpt/types_19.go @@ -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 diff --git a/knx/dpt/types_19_test.go b/knx/dpt/types_19_test.go index e6e8527..7dd5d9a 100644 --- a/knx/dpt/types_19_test.go +++ b/knx/dpt/types_19_test.go @@ -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 diff --git a/knx/dpt/types_2.go b/knx/dpt/types_2.go index 322c20a..b62c9dc 100644 --- a/knx/dpt/types_2.go +++ b/knx/dpt/types_2.go @@ -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 diff --git a/knx/dpt/types_21.go b/knx/dpt/types_21.go index cafa38a..bf67718 100644 --- a/knx/dpt/types_21.go +++ b/knx/dpt/types_21.go @@ -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 diff --git a/knx/dpt/types_242.go b/knx/dpt/types_242.go new file mode 100644 index 0000000..1b14886 --- /dev/null +++ b/knx/dpt/types_242.go @@ -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) +} diff --git a/knx/dpt/types_242_test.go b/knx/dpt/types_242_test.go new file mode 100644 index 0000000..dff0d08 --- /dev/null +++ b/knx/dpt/types_242_test.go @@ -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) + } + } +} diff --git a/knx/dpt/types_251_test.go b/knx/dpt/types_251_test.go index d5bfee8..a4b3266 100644 --- a/knx/dpt/types_251_test.go +++ b/knx/dpt/types_251_test.go @@ -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 ( diff --git a/knx/dpt/types_2_test.go b/knx/dpt/types_2_test.go index 3566962..6a4cd61 100644 --- a/knx/dpt/types_2_test.go +++ b/knx/dpt/types_2_test.go @@ -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 diff --git a/knx/dpt/types_3.go b/knx/dpt/types_3.go index 699de46..183f61c 100644 --- a/knx/dpt/types_3.go +++ b/knx/dpt/types_3.go @@ -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 diff --git a/knx/dpt/types_3_test.go b/knx/dpt/types_3_test.go index ff2e4dc..9b0c27d 100644 --- a/knx/dpt/types_3_test.go +++ b/knx/dpt/types_3_test.go @@ -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 diff --git a/knx/dpt/types_6.go b/knx/dpt/types_6.go index 84b7e8a..a00317c 100644 --- a/knx/dpt/types_6.go +++ b/knx/dpt/types_6.go @@ -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 diff --git a/knx/dpt/types_8.go b/knx/dpt/types_8.go index a45e310..5e96bd2 100644 --- a/knx/dpt/types_8.go +++ b/knx/dpt/types_8.go @@ -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 @@ -49,6 +49,65 @@ func (d DPT_8002) String() string { return fmt.Sprintf("%d ms", d) } +// DPT_8003 represents DPT 8.002 (G) / DPT_DeltaTime10Msec. +type DPT_8003 float32 + +func (d DPT_8003) Pack() []byte { + return packV16(int16(d * 100)) +} + +func (d *DPT_8003) Unpack(data []byte) error { + var value int16 + + if err := unpackV16(data, &value); err != nil { + return err + } + + *d = DPT_8003(float32(value) / 100) + + return nil +} + +func (d DPT_8003) Unit() string { + return "ms" +} + +func (d DPT_8003) IsValid() bool { + // TBD + return true +} + +func (d DPT_8003) String() string { + return fmt.Sprintf("%f ms", d) +} + +// DPT_8004 represents DPT 8.004 (G) / DPT_DeltaTime100Msec. +type DPT_8004 float32 + +func (d DPT_8004) Pack() []byte { + return packV16(int16(d * 10)) +} + +func (d *DPT_8004) Unpack(data []byte) error { + var value int16 + + if err := unpackV16(data, &value); err != nil { + return err + } + + *d = DPT_8004(float32(value) / 10) + + return nil +} + +func (d DPT_8004) Unit() string { + return "ms" +} + +func (d DPT_8004) String() string { + return fmt.Sprintf("%f ms", d) +} + // DPT_8005 represents DPT 8.005 (G) / DPT_DeltaTimeSec. type DPT_8005 int16 @@ -68,6 +127,25 @@ func (d DPT_8005) String() string { return fmt.Sprintf("%d s", d) } +// DPT_8006 represents DPT 8.005 (G) / DPT_DeltaTimeMin. +type DPT_8006 int16 + +func (d DPT_8006) Pack() []byte { + return packV16(d) +} + +func (d *DPT_8006) Unpack(data []byte) error { + return unpackV16(data, d) +} + +func (d DPT_8006) Unit() string { + return "min" +} + +func (d DPT_8006) String() string { + return fmt.Sprintf("%d min", d) +} + // DPT_8007 represents DPT 8.007 (G) / DPT_DeltaTimeHour. type DPT_8007 int16 @@ -125,6 +203,25 @@ func (d DPT_8010) String() string { return fmt.Sprintf("%.2f %%", d) } +// DPT_8011 represents DPT 8.011 (FB) / DPT_Rotation_Angle. +type DPT_8011 int16 + +func (d DPT_8011) Pack() []byte { + return packV16(int16(d)) +} + +func (d *DPT_8011) Unpack(data []byte) error { + return unpackV16(data, (*int16)(d)) +} + +func (d DPT_8011) Unit() string { + return "°" +} + +func (d DPT_8011) String() string { + return fmt.Sprintf("%d°", int16(d)) +} + // DPT_8012 represents DPT 8.012 (FB) / DPT_Length_m. type DPT_8012 int16 diff --git a/knx/dpt/types_8_test.go b/knx/dpt/types_8_test.go index 02a78d4..55f9197 100644 --- a/knx/dpt/types_8_test.go +++ b/knx/dpt/types_8_test.go @@ -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 diff --git a/knx/dpt/types_registry.go b/knx/dpt/types_registry.go index 013d854..cd849d2 100644 --- a/knx/dpt/types_registry.go +++ b/knx/dpt/types_registry.go @@ -71,9 +71,13 @@ var dptTypes = map[string]Datapoint{ // 8.xxx "8.001": new(DPT_8001), "8.002": new(DPT_8002), + "8.003": new(DPT_8003), + "8.004": new(DPT_8004), "8.005": new(DPT_8005), + "8.006": new(DPT_8006), "8.007": new(DPT_8007), "8.010": new(DPT_8010), + "8.011": new(DPT_8010), "8.012": new(DPT_8012), // 9.xxx @@ -247,6 +251,9 @@ var dptTypes = map[string]Datapoint{ // 232.xxx "232.600": new(DPT_232600), + // 242.xxx + "242.600": new(DPT_242600), + // 251.xxx "251.600": new(DPT_251600), }