From e480efa519da8702d2e737c4f4b94eefc05d5246 Mon Sep 17 00:00:00 2001 From: mobilarte Date: Tue, 28 May 2024 22:33:52 +0200 Subject: [PATCH] Added DPT 232.600 --- knx/dpt/formats.go | 17 ++++++++++++++++ knx/dpt/types_232.go | 41 +++++++++++++++++++++++++++++++++++++++ knx/dpt/types_232_test.go | 27 ++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 knx/dpt/types_232.go create mode 100644 knx/dpt/types_232_test.go diff --git a/knx/dpt/formats.go b/knx/dpt/formats.go index df887d9..49ab116 100644 --- a/knx/dpt/formats.go +++ b/knx/dpt/formats.go @@ -121,6 +121,23 @@ func unpackU8[T ~uint8](data []byte, i *T) error { return nil } +// func pack3U8[T ~uint8](u2 T, u1 T, u0 T) []byte { +func pack3U8(u2 uint8, u1 uint8, u0 uint8) []byte { + return []byte{0, uint8(u2), uint8(u1), uint8(u0)} +} + +// func unpack3U8[T ~uint8](data []byte, u []T) error { +func unpack3U8(data []byte, u2 *uint8, u1 *uint8, u0 *uint8) error { + if len(data) != 4 { + return ErrInvalidLength + } + *u2 = uint8(data[1]) + *u1 = uint8(data[2]) + *u0 = uint8(data[3]) + + return nil +} + func packV8[T ~int8](i T) []byte { return []byte{0, byte(i)} } diff --git a/knx/dpt/types_232.go b/knx/dpt/types_232.go new file mode 100644 index 0000000..cf2a966 --- /dev/null +++ b/knx/dpt/types_232.go @@ -0,0 +1,41 @@ +package dpt + +import ( + "fmt" +) + +// DPT_232600 represents DPT 232.600 (G) / DPT_Colour_RGB. +// Colour RGB - RGB value 4x(0..100%) / U8 U8 U8 +type DPT_232600 struct { + Red uint8 + Green uint8 + Blue uint8 +} + +func (d DPT_232600) Pack() []byte { + + return pack3U8(d.Red, d.Green, d.Blue) +} + +func (d *DPT_232600) Unpack(data []byte) error { + + if len(data) != 4 { + return ErrInvalidLength + } + + err := unpack3U8(data, &d.Red, &d.Green, &d.Blue) + + if err != nil { + return ErrInvalidLength + } + + return nil +} + +func (d DPT_232600) Unit() string { + return "" +} + +func (d DPT_232600) String() string { + return fmt.Sprintf("%d %d %d", d.Red, d.Green, d.Blue) +} diff --git a/knx/dpt/types_232_test.go b/knx/dpt/types_232_test.go new file mode 100644 index 0000000..f527fc7 --- /dev/null +++ b/knx/dpt/types_232_test.go @@ -0,0 +1,27 @@ +package dpt + +import ( + "fmt" + "reflect" + "testing" +) + +func TestDPT_232600(t *testing.T) { + var buf []byte + var dst DPT_232600 + sources := []DPT_232600{ + {Red: 255, Green: 96, Blue: 0}, + {Red: 0, Green: 128, Blue: 128}, + } + + for _, src := range sources { + buf = src.Pack() + _ = dst.Unpack(buf) + + if !reflect.DeepEqual(src, dst) { + fmt.Printf("%+v\n", src) + fmt.Printf("%+v\n", dst) + t.Errorf("Value [%s] after pack/unpack for DPT_232600 differs. Original value was [%v].", dst, src) + } + } +}