/
ep0099.go
110 lines (88 loc) · 2.02 KB
/
ep0099.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2021 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package ep0099
import (
"errors"
"periph.io/x/conn/v3/i2c"
)
var errInvalidAddress = errors.New("invalid EP-0099 address")
var errInvalidChannel = errors.New("invalid EP-0099 channel")
type State byte
const (
StateOff State = 0x00
StateOn State = 0xFF
)
type Dev struct {
i2c i2c.Dev
state [4]State
}
func New(bus i2c.Bus, address uint16) (*Dev, error) {
if err := isValidAddress(address); err != nil {
return nil, err
}
d := &Dev{
i2c: i2c.Dev{Bus: bus, Addr: address},
}
if err := d.reset(); err != nil {
return nil, err
}
return d, nil
}
func (d *Dev) Halt() error {
return d.reset()
}
func (d *Dev) On(channel uint8) error {
if !isValidChannel(channel) {
return errInvalidChannel
}
_, err := d.i2c.Write([]byte{channel, byte(StateOn)})
d.state[channel-1] = StateOn
return err
}
func (d *Dev) Off(channel uint8) error {
if !isValidChannel(channel) {
return errInvalidChannel
}
_, err := d.i2c.Write([]byte{channel, byte(StateOff)})
d.state[channel-1] = StateOff
return err
}
func (d *Dev) State(channel uint8) (State, error) {
if !isValidChannel(channel) {
return 0, errInvalidChannel
}
return d.state[channel-1], nil
}
func (d *Dev) AvailableChannels() []uint8 {
return []uint8{0x01, 0x02, 0x03, 0x04}
}
func (s State) String() string {
if s == StateOff {
return "off"
}
return "on"
}
func (d *Dev) reset() error {
for _, channel := range d.AvailableChannels() {
err := d.Off(channel)
if err != nil {
return err
}
}
return nil
}
// Addresses in EP0099 are configured via DIP Switches on the board.
// Up to 4 HATs can be stacked and each one need a different address to
// work.
func isValidAddress(address uint16) error {
switch address {
case 0x10, 0x11, 0x12, 0x13:
return nil
default:
return errInvalidAddress
}
}
func isValidChannel(channel uint8) bool {
return channel >= 1 && channel <= 4
}