-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapdigital.go
60 lines (48 loc) · 857 Bytes
/
wrapdigital.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
package main
import (
"strings"
)
type wrappedDI struct {
DigitalInputPin
}
func WrapDigitalInput(p DigitalInputPin) GenericInputPin {
return &wrappedDI{p}
}
func (p *wrappedDI) Read() (string, error) {
v, err := p.ReadBool()
if err != nil {
return "n/a", nil
}
if v {
return "1", nil
}
return "0", nil
}
type wrappedDO struct {
DigitalOutputPin
}
func WrapDigitalOutput(p DigitalOutputPin) GenericOutputPin {
return &wrappedDO{p}
}
func (p *wrappedDO) Write(value string) error {
var v bool
switch {
case strings.HasPrefix(value, "false"),
strings.HasPrefix(value, "low"),
strings.HasPrefix(value, "0"):
v = false
default:
v = true
}
return p.WriteBool(v)
}
func (p *wrappedDO) Read() (string, error) {
v, err := p.ReadBool()
if err != nil {
return "n/a", nil
}
if v {
return "1", nil
}
return "0", nil
}