forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct_pin_driver.go
123 lines (105 loc) · 3.47 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
package gpio
import (
"strconv"
"gobot.io/x/gobot/v2"
)
// DirectPinDriver represents a GPIO pin
type DirectPinDriver struct {
name string
pin string
connection gobot.Connection
gobot.Commander
}
// NewDirectPinDriver return a new DirectPinDriver given a Connection and pin.
//
// Adds the following API Commands:
//
// "DigitalRead" - See DirectPinDriver.DigitalRead
// "DigitalWrite" - See DirectPinDriver.DigitalWrite
// "PwmWrite" - See DirectPinDriver.PwmWrite
// "ServoWrite" - See DirectPinDriver.ServoWrite
func NewDirectPinDriver(a gobot.Connection, pin string) *DirectPinDriver {
d := &DirectPinDriver{
name: gobot.DefaultName("DirectPin"),
connection: a,
pin: pin,
Commander: gobot.NewCommander(),
}
d.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
val, err := d.DigitalRead()
return map[string]interface{}{"val": val, "err": err}
})
d.AddCommand("DigitalWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
return d.DigitalWrite(byte(level))
})
d.AddCommand("PwmWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
return d.PwmWrite(byte(level))
})
d.AddCommand("ServoWrite", func(params map[string]interface{}) interface{} {
level, _ := strconv.Atoi(params["level"].(string))
return d.ServoWrite(byte(level))
})
return d
}
// Name returns the DirectPinDrivers name
func (d *DirectPinDriver) Name() string { return d.name }
// SetName sets the DirectPinDrivers name
func (d *DirectPinDriver) SetName(n string) { d.name = n }
// Pin returns the DirectPinDrivers pin
func (d *DirectPinDriver) Pin() string { return d.pin }
// Connection returns the DirectPinDrivers Connection
func (d *DirectPinDriver) Connection() gobot.Connection { return d.connection }
// Start implements the Driver interface
func (d *DirectPinDriver) Start() (err error) { return }
// Halt implements the Driver interface
func (d *DirectPinDriver) Halt() (err error) { return }
// Off turn off pin
func (d *DirectPinDriver) Off() (err error) {
if writer, ok := d.Connection().(DigitalWriter); ok {
return writer.DigitalWrite(d.Pin(), byte(0))
}
err = ErrDigitalWriteUnsupported
return
}
// On turn on pin
func (d *DirectPinDriver) On() (err error) {
if writer, ok := d.Connection().(DigitalWriter); ok {
return writer.DigitalWrite(d.Pin(), byte(1))
}
err = ErrDigitalWriteUnsupported
return
}
// DigitalRead returns the current digital state of the pin
func (d *DirectPinDriver) DigitalRead() (val int, err error) {
if reader, ok := d.Connection().(DigitalReader); ok {
return reader.DigitalRead(d.Pin())
}
err = ErrDigitalReadUnsupported
return
}
// DigitalWrite writes to the pin. Acceptable values are 1 or 0
func (d *DirectPinDriver) DigitalWrite(level byte) (err error) {
if writer, ok := d.Connection().(DigitalWriter); ok {
return writer.DigitalWrite(d.Pin(), level)
}
err = ErrDigitalWriteUnsupported
return
}
// PwmWrite writes the 0-254 value to the specified pin
func (d *DirectPinDriver) PwmWrite(level byte) (err error) {
if writer, ok := d.Connection().(PwmWriter); ok {
return writer.PwmWrite(d.Pin(), level)
}
err = ErrPwmWriteUnsupported
return
}
// ServoWrite writes value to the specified pin
func (d *DirectPinDriver) ServoWrite(level byte) (err error) {
if writer, ok := d.Connection().(ServoWriter); ok {
return writer.ServoWrite(d.Pin(), level)
}
err = ErrServoWriteUnsupported
return
}