hybridgroup.gobot/drivers/gpio/direct_pin_driver.go

124 lines
3.5 KiB
Go
Raw Permalink 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
"gobot.io/x/gobot"
2014-04-28 10:34:16 +08:00
)
// DirectPinDriver represents a GPIO pin
2014-04-28 10:34:16 +08:00
type DirectPinDriver struct {
name string
pin string
connection gobot.Connection
gobot.Commander
2014-04-28 10:34:16 +08:00
}
// NewDirectPinDriver return a new DirectPinDriver given a Connection and pin.
2014-09-28 02:34:13 +08:00
//
// Adds the following API Commands:
// "DigitalRead" - See DirectPinDriver.DigitalRead
// "DigitalWrite" - See DirectPinDriver.DigitalWrite
// "AnalogWrite" - See DirectPinDriver.AnalogWrite
// "PwmWrite" - See DirectPinDriver.PwmWrite
// "ServoWrite" - See DirectPinDriver.ServoWrite
func NewDirectPinDriver(a gobot.Connection, pin string) *DirectPinDriver {
2014-06-12 08:41:04 +08:00
d := &DirectPinDriver{
name: gobot.DefaultName("DirectPin"),
2014-11-30 03:02:10 +08:00
connection: a,
pin: pin,
Commander: gobot.NewCommander(),
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{} {
val, err := d.DigitalRead()
return map[string]interface{}{"val": val, "err": err}
2014-06-12 08:41:04 +08:00
})
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))
return d.DigitalWrite(byte(level))
2014-06-12 08:41:04 +08:00
})
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))
return d.PwmWrite(byte(level))
2014-06-12 08:41:04 +08:00
})
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))
return d.ServoWrite(byte(level))
2014-06-12 08:41:04 +08:00
})
return d
2014-04-28 10:34:16 +08:00
}
// 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 }
2014-04-28 10:34:16 +08:00
// Halt implements the Driver interface
func (d *DirectPinDriver) Halt() (err error) { return }
2014-09-28 02:34:13 +08:00
2016-06-14 23:42:50 +08:00
// 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
}
// 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
}
2014-09-28 02:34:13 +08:00
// DigitalRead returns the current digital state of the pin
func (d *DirectPinDriver) DigitalRead() (val int, err error) {
2014-11-30 03:02:10 +08:00
if reader, ok := d.Connection().(DigitalReader); ok {
return reader.DigitalRead(d.Pin())
}
err = ErrDigitalReadUnsupported
return
2014-04-28 10:34:16 +08:00
}
// DigitalWrite writes to the pin. Acceptable values are 1 or 0
func (d *DirectPinDriver) DigitalWrite(level byte) (err error) {
2014-11-30 03:02:10 +08:00
if writer, ok := d.Connection().(DigitalWriter); ok {
return writer.DigitalWrite(d.Pin(), level)
}
err = ErrDigitalWriteUnsupported
return
2014-04-28 10:34:16 +08:00
}
// PwmWrite writes the 0-254 value to the specified pin
func (d *DirectPinDriver) PwmWrite(level byte) (err error) {
2014-11-30 03:02:10 +08:00
if writer, ok := d.Connection().(PwmWriter); ok {
return writer.PwmWrite(d.Pin(), level)
}
err = ErrPwmWriteUnsupported
return
2014-04-28 10:34:16 +08:00
}
// ServoWrite writes value to the specified pin
func (d *DirectPinDriver) ServoWrite(level byte) (err error) {
2014-11-30 03:02:10 +08:00
if writer, ok := d.Connection().(ServoWriter); ok {
return writer.ServoWrite(d.Pin(), level)
}
err = ErrServoWriteUnsupported
return
2014-04-28 10:34:16 +08:00
}