Repository navigation
Expand file tree
/
Copy pathcommand.go
More file actions
69 lines (61 loc) · 2.13 KB
/
Copy pathcommand.go
File metadata and controls
69 lines (61 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package intertechno
import (
"errors"
"fmt"
)
var (
// ErrInvalidAddress is returned if address is invalid
ErrInvalidAddress = errors.New("address has to be in range of [0..2^26-1]")
// ErrInvalidAction is returned if action is invalid
ErrInvalidAction = errors.New("action has to be ActionOff (0) => off, ActionOn (1) => on, ActionDim (2) => dim (if device is off it will be turned on and dimmed)")
// ErrInvalidDimvalue is returned if dimvalue is invalid
ErrInvalidDimvalue = errors.New("dimvalue has to be in range of [1..15] if action is set to ActionDim (2) -- if device is off it will be turned on and dimmed")
// ErrInvalidUnit is returned if unit is invalid
ErrInvalidUnit = errors.New("unit has to be in range of [0..15]")
)
// Action can be ActionOff (0) => off, ActionOn (1) => on, ActionDim (2) => dim (if device is off it will be turned on and dimmed)
type Action uint
const (
// ActionOff (0) => off
ActionOff Action = iota
// ActionOn (1) => on
ActionOn
// ActionDim (2) => dim (if device is off it will be turned on and dimmed)
ActionDim
actionEnd
)
func (ia Action) String() string {
names := [...]string{"ActionOff", "ActionOn", "ActionDim"}
if !ia.isValid() {
return fmt.Sprintf("Invalid Action: %d", ia)
}
return names[ia]
}
func (ia Action) isValid() bool {
return ia < actionEnd
}
// Command is used to store the information to send
type Command struct {
// Address Address of this transmitter [0..2^26-1]
Address int
// Dimvalue [1..15] Dim level if action is set to ActionDim. 15 for brightest level.
Dimvalue int
// Action ActionOff (0) => off, ActionOn (1) => on, ActionDim (2) => dim (dimvalue has to be set)
Action Action
// Unit [0..15] unit of the device
Unit int
// Group True to send command to the address group.
Group bool
}
func (c Command) isValid() error {
if c.Address < 0 || (c.Address>>addressBits) != 0 {
return ErrInvalidAddress
} else if !c.Action.isValid() {
return ErrInvalidAction
} else if c.Action == ActionDim && (c.Dimvalue < 1 || (c.Dimvalue>>dimvalueBits) != 0) {
return ErrInvalidDimvalue
} else if c.Unit < 0 || (c.Unit>>unitBits) != 0 {
return ErrInvalidUnit
}
return nil
}