hybridgroup.gobot/platforms/gpio/direct_pin_driver.go

83 lines
2.0 KiB
Go
Raw Normal View History

2014-04-28 10:34:16 +08:00
package gpio
import (
2014-06-12 08:41:04 +08:00
"strconv"
2014-07-10 09:32:27 +08:00
"github.com/hybridgroup/gobot"
2014-04-28 10:34:16 +08:00
)
type DirectPinDriver struct {
gobot.Driver
}
2014-05-23 12:29:37 +08:00
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
2014-06-12 08:41:04 +08:00
d := &DirectPinDriver{
Driver: *gobot.NewDriver(
name,
"DirectPinDriver",
a.(gobot.AdaptorInterface),
pin,
),
2014-04-28 10:34:16 +08:00
}
2014-06-12 08:41:04 +08:00
2014-07-10 09:32:27 +08:00
d.AddCommand("DigitalRead", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
return d.DigitalRead()
})
2014-07-10 09:32:27 +08:00
d.AddCommand("DigitalWrite", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
level, _ := strconv.Atoi(params["level"].(string))
d.DigitalWrite(byte(level))
return nil
})
2014-07-10 09:32:27 +08:00
d.AddCommand("AnalogRead", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
return d.AnalogRead()
})
2014-07-10 09:32:27 +08:00
d.AddCommand("AnalogWrite", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
level, _ := strconv.Atoi(params["level"].(string))
d.AnalogWrite(byte(level))
return nil
})
2014-07-10 09:32:27 +08:00
d.AddCommand("PwmWrite", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
level, _ := strconv.Atoi(params["level"].(string))
d.PwmWrite(byte(level))
return nil
})
2014-07-10 09:32:27 +08:00
d.AddCommand("ServoWrite", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
level, _ := strconv.Atoi(params["level"].(string))
d.ServoWrite(byte(level))
return nil
})
return d
2014-04-28 10:34:16 +08:00
}
func (d *DirectPinDriver) adaptor() DirectPin {
2014-07-10 09:32:27 +08:00
return d.Adaptor().(DirectPin)
}
2014-04-28 10:34:16 +08:00
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())
2014-04-28 10:34:16 +08:00
}
func (d *DirectPinDriver) DigitalWrite(level byte) {
d.adaptor().DigitalWrite(d.Pin(), level)
2014-04-28 10:34:16 +08:00
}
func (d *DirectPinDriver) AnalogRead() int {
return d.adaptor().AnalogRead(d.Pin())
2014-04-28 10:34:16 +08:00
}
func (d *DirectPinDriver) AnalogWrite(level byte) {
d.adaptor().AnalogWrite(d.Pin(), level)
2014-04-28 10:34:16 +08:00
}
func (d *DirectPinDriver) PwmWrite(level byte) {
d.adaptor().PwmWrite(d.Pin(), level)
2014-04-28 10:34:16 +08:00
}
func (d *DirectPinDriver) ServoWrite(level byte) {
d.adaptor().ServoWrite(d.Pin(), level)
2014-04-28 10:34:16 +08:00
}