Skip to content

Commit

Permalink
Added DPT 232.600
Browse files Browse the repository at this point in the history
  • Loading branch information
mobilarte committed May 28, 2024
1 parent abf02b5 commit e480efa
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
17 changes: 17 additions & 0 deletions knx/dpt/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
Expand Down
41 changes: 41 additions & 0 deletions knx/dpt/types_232.go
Original file line number Diff line number Diff line change
@@ -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)
}
27 changes: 27 additions & 0 deletions knx/dpt/types_232_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit e480efa

Please sign in to comment.