forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct_pin_driver.go
82 lines (69 loc) · 2.02 KB
/
direct_pin_driver.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
package gpio
import (
"strconv"
"github.com/hybridgroup/gobot"
)
type DirectPinDriver struct {
gobot.Driver
}
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
d := &DirectPinDriver{
Driver: *gobot.NewDriver(
name,
"DirectPinDriver",
a.(gobot.AdaptorInterface),
pin,
),
}
d.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
return d.DigitalRead()
})
d.AddCommand("DigitalWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.DigitalWrite(byte(level))
return nil
})
d.AddCommand("AnalogRead", func(params map[string]interface{}) interface{} {
return d.AnalogRead()
})
d.AddCommand("AnalogWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.AnalogWrite(byte(level))
return nil
})
d.AddCommand("PwmWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.PwmWrite(byte(level))
return nil
})
d.AddCommand("ServoWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
d.ServoWrite(byte(level))
return nil
})
return d
}
func (d *DirectPinDriver) adaptor() DirectPin {
return d.Adaptor().(DirectPin)
}
func (d *DirectPinDriver) Start() bool { return true }
func (d *DirectPinDriver) Halt() bool { return true }
func (d *DirectPinDriver) Init() bool { return true }
func (d *DirectPinDriver) DigitalRead() int {
return d.adaptor().DigitalRead(d.Pin())
}
func (d *DirectPinDriver) DigitalWrite(level byte) {
d.adaptor().DigitalWrite(d.Pin(), level)
}
func (d *DirectPinDriver) AnalogRead() int {
return d.adaptor().AnalogRead(d.Pin())
}
func (d *DirectPinDriver) AnalogWrite(level byte) {
d.adaptor().AnalogWrite(d.Pin(), level)
}
func (d *DirectPinDriver) PwmWrite(level byte) {
d.adaptor().PwmWrite(d.Pin(), level)
}
func (d *DirectPinDriver) ServoWrite(level byte) {
d.adaptor().ServoWrite(d.Pin(), level)
}