Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dpt 8.xxx (attempt 2) #93

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions knx/dpt/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ func unpackV8(data []byte, i *int8) error {
return nil
}

func packV16(i int16) []byte {
b := make([]byte, 3)

b[0] = 0
b[1] = byte((i >> 8) & 0xff)
b[2] = byte(i & 0xff)

return b
}

func unpackV16(data []byte, i *int16) error {
if len(data) != 3 {
return ErrInvalidLength
}

*i = int16(data[1])<<8 | int16(data[2])

return nil
}

func packV32(i int32) []byte {
b := make([]byte, 5)

Expand Down
195 changes: 195 additions & 0 deletions knx/dpt/types_8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright 2017 Ole Krüger.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt

import (
"fmt"
"math"
)

// DPT_8001 represents DPT 8.001 / Counter.
type DPT_8001 int16

func (d DPT_8001) Pack() []byte {
return packV16(int16(d))
}

func (d *DPT_8001) Unpack(data []byte) error {
return unpackV16(data, (*int16)(d))
}

func (d DPT_8001) Unit() string {
return "pulses"
}

func (d DPT_8001) String() string {
return fmt.Sprintf("%d pulses", int16(d))
}

// DPT_8002 represents DPT 8.002 / delta time ms.
type DPT_8002 int16

func (d DPT_8002) Pack() []byte {
return packV16(int16(d))
}

func (d *DPT_8002) Unpack(data []byte) error {
return unpackV16(data, (*int16)(d))
}

func (d DPT_8002) Unit() string {
return "ms"
}

func (d DPT_8002) String() string {
return fmt.Sprintf("%d ms", int16(d))
}

// DPT_8003 represents DPT 8.003 / delta time ms (range -327.68 s ... 327.67 s)
type DPT_8003 int32

func (d DPT_8003) Pack() []byte {
return packV16(int16(int32(d) / 10))
}

func (d *DPT_8003) Unpack(data []byte) error {
var i int16
if err := unpackV16(data, &i); err != nil {
return nil
}
*d = DPT_8003(int32(i) * 10)
return nil
}

func (d DPT_8003) Unit() string {
return "ms"
}

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

// DPT_8004 represents DPT 8.004 / delta time ms (range -3276.8 s ... 3276.7 s)
type DPT_8004 int32

func (d DPT_8004) Pack() []byte {
return packV16(int16(int32(d) / 100))
}

func (d *DPT_8004) Unpack(data []byte) error {
var i int16
if err := unpackV16(data, &i); err != nil {
return nil
}
*d = DPT_8004(int32(i) * 100)
return nil
}

func (d DPT_8004) Unit() string {
return "ms"
}

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

// DPT_8005 represents DPT 8.005 / delta time seconds
type DPT_8005 int16

func (d DPT_8005) Pack() []byte {
return packV16(int16(d))
}

func (d *DPT_8005) Unpack(data []byte) error {
return unpackV16(data, (*int16)(d))
}

func (d DPT_8005) Unit() string {
return "s"
}

func (d DPT_8005) String() string {
return fmt.Sprintf("%d s", int16(d))
}

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

func (d DPT_8006) Pack() []byte {
return packV16(int16(d))
}

func (d *DPT_8006) Unpack(data []byte) error {
return unpackV16(data, (*int16)(d))
}

func (d DPT_8006) Unit() string {
return "min"
}

func (d DPT_8006) String() string {
return fmt.Sprintf("%d min", int16(d))
}

// DPT_8007 represents DPT 8.007 / delta time hours
type DPT_8007 int16

func (d DPT_8007) Pack() []byte {
return packV16(int16(d))
}

func (d *DPT_8007) Unpack(data []byte) error {
return unpackV16(data, (*int16)(d))
}

func (d DPT_8007) Unit() string {
return "h"
}

func (d DPT_8007) String() string {
return fmt.Sprintf("%d h", int16(d))
}

// DPT_8010 represents DPT 8.010 / percentage difference
type DPT_8010 float32

func (d DPT_8010) Pack() []byte {
return packV16(int16(math.Round(float64(d) / 0.01)))
}

func (d *DPT_8010) Unpack(data []byte) error {
var i int16
if err := unpackV16(data, &i); err != nil {
return nil
}
*d = DPT_8010(float32(i) * 0.01)
return nil
}

func (d DPT_8010) Unit() string {
return "%"
}

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

// DPT_8011 represents DPT 8.011 / 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))
}
Loading
Loading