Skip to content

Commit

Permalink
Merge branch 'feature/dpt-8'
Browse files Browse the repository at this point in the history
  • Loading branch information
vapourismo committed Sep 15, 2024
2 parents 3b325e3 + 3f9996e commit a6ab434
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 0 deletions.
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
203 changes: 203 additions & 0 deletions knx/dpt/types_8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// Copyright 2017 Ole Krüger.
// Licensed under the MIT license which can be found in the LICENSE file.

package dpt

import (
"fmt"
)

// 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 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) String() string {
return fmt.Sprintf("%f ms", d)
}

// DPT_8004 represents DPT 8.004 / delta time ms (range -3276.8 s ... 3276.7 s)
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 / 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.006 / 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(d * 100))
}

func (d *DPT_8010) Unpack(data []byte) error {
var value int16

if err := unpackV16(data, &value); err != nil {
return err
}

*d = DPT_8010(float32(value) / 100)

return nil
}

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

func (d DPT_8010) String() string {
return fmt.Sprintf("%f %%", 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

0 comments on commit a6ab434

Please sign in to comment.