Add docs to gpio package

This commit is contained in:
Adrian Zankich 2014-09-27 11:34:13 -07:00
parent 8e1d9e3a1d
commit 09bc76d501
7 changed files with 112 additions and 24 deletions

View File

@ -4,10 +4,15 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents an Analog Sensor
type AnalogSensorDriver struct {
gobot.Driver
}
// NewAnalogSensorDriver returns a new AnalogSensorDriver given an AnalogReader, name and pin.
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
d := &AnalogSensorDriver{
Driver: *gobot.NewDriver(
@ -30,6 +35,10 @@ func (a *AnalogSensorDriver) adaptor() AnalogReader {
return a.Adaptor().(AnalogReader)
}
// Starts the AnalogSensorDriver and reads the Analog Sensor at the given Driver.Interval().
// Returns true on successful start of the driver.
// Emits the Events:
// "data" int - Event is emitted on change and represents the current reading from the sensor.
func (a *AnalogSensorDriver) Start() bool {
value := 0
gobot.Every(a.Interval(), func() {
@ -41,9 +50,11 @@ func (a *AnalogSensorDriver) Start() bool {
})
return true
}
func (a *AnalogSensorDriver) Init() bool { return true }
// Halt returns true on a successful halt of the driver
func (a *AnalogSensorDriver) Halt() bool { return true }
// Read returns the current reading from the Analog Sensor
func (a *AnalogSensorDriver) Read() int {
return a.adaptor().AnalogRead(a.Pin())
}

View File

@ -4,11 +4,13 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents a digital Button
type ButtonDriver struct {
gobot.Driver
Active bool
}
// NewButtonDriver return a new ButtonDriver given a DigitalReader, name and pin
func NewButtonDriver(a DigitalReader, name string, pin string) *ButtonDriver {
b := &ButtonDriver{
Driver: *gobot.NewDriver(
@ -30,6 +32,12 @@ func (b *ButtonDriver) adaptor() DigitalReader {
return b.Adaptor().(DigitalReader)
}
// Starts the ButtonDriver and reads the state of the button at the given Driver.Interval().
// Returns true on successful start of the driver.
//
// Emits the Events:
// "push" int - On button push
// "release" int - On button release
func (b *ButtonDriver) Start() bool {
state := 0
gobot.Every(b.Interval(), func() {
@ -41,8 +49,9 @@ func (b *ButtonDriver) Start() bool {
})
return true
}
// Halt returns true on a successful halt of the driver
func (b *ButtonDriver) Halt() bool { return true }
func (b *ButtonDriver) Init() bool { return true }
func (b *ButtonDriver) readState() int {
return b.adaptor().DigitalRead(b.Pin())

View File

@ -6,10 +6,20 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents a raw GPIO pin
type DirectPinDriver struct {
gobot.Driver
}
// NewDirectPinDriver return a new DirectPinDriver given a DirectPin, name and pin.
//
// Adds the following API Commands:
// "DigitalRead" - See DirectPinDriver.DigitalRead
// "DigitalWrite" - See DirectPinDriver.DigitalWrite
// "AnalogRead" - See DirectPinDriver.AnalogRead
// "AnalogWrite" - See DirectPinDriver.AnalogWrite
// "PwmWrite" - See DirectPinDriver.PwmWrite
// "ServoWrite" - See DirectPinDriver.ServoWrite
func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
d := &DirectPinDriver{
Driver: *gobot.NewDriver(
@ -53,30 +63,39 @@ func NewDirectPinDriver(a DirectPin, name string, pin string) *DirectPinDriver {
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 }
// Starts the DirectPinDriver. Returns true on successful start of the driver
func (d *DirectPinDriver) Start() bool { return true }
// Halts the DirectPinDriver. Returns true on successful halt of the driver
func (d *DirectPinDriver) Halt() bool { return true }
// DigitalRead returns the current digital state of the pin
func (d *DirectPinDriver) DigitalRead() int {
return d.adaptor().DigitalRead(d.Pin())
}
// DigitalWrite writes to the pin
func (d *DirectPinDriver) DigitalWrite(level byte) {
d.adaptor().DigitalWrite(d.Pin(), level)
}
// AnalogRead reads the current analog reading of the pin
func (d *DirectPinDriver) AnalogRead() int {
return d.adaptor().AnalogRead(d.Pin())
}
// AnalogWrite writes to the pin
func (d *DirectPinDriver) AnalogWrite(level byte) {
d.adaptor().AnalogWrite(d.Pin(), level)
}
// PwmWrite writes to the pin
func (d *DirectPinDriver) PwmWrite(level byte) {
d.adaptor().PwmWrite(d.Pin(), level)
}
// ServoWrite writes to the pin
func (d *DirectPinDriver) ServoWrite(level byte) {
d.adaptor().ServoWrite(d.Pin(), level)
}

View File

@ -4,13 +4,20 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents a digital Led
type LedDriver struct {
gobot.Driver
High bool
high bool
}
// NewLedDriver return a new LedDriver given a PwmDigitalWriter, name and pin.
//
// Adds the following API Commands:
// "Brightness" - See LedDriver.Brightness
// "Toggle" - See LedDriver.Toggle
// "On" - See LedDriver.On
// "Off" - See LedDriver.Off
func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
l := &LedDriver{
Driver: *gobot.NewDriver(
name,
@ -18,7 +25,7 @@ func NewLedDriver(a PwmDigitalWriter, name string, pin string) *LedDriver {
pin,
a.(gobot.AdaptorInterface),
),
High: false,
high: false,
}
l.AddCommand("Brightness", func(params map[string]interface{}) interface{} {
@ -49,38 +56,41 @@ func (l *LedDriver) adaptor() PwmDigitalWriter {
return l.Adaptor().(PwmDigitalWriter)
}
// Start starts the LedDriver. Returns true on successful start of the driver
func (l *LedDriver) Start() bool { return true }
func (l *LedDriver) Halt() bool { return true }
func (l *LedDriver) Init() bool { return true }
func (l *LedDriver) IsOn() bool {
return l.High
}
func (l *LedDriver) IsOff() bool {
return !l.IsOn()
// Halt halts the LedDriver. Returns true on successful halt of the driver
func (l *LedDriver) Halt() bool { return true }
// State return true if the led is On and false if the led is Off
func (l *LedDriver) State() bool {
return l.high
}
// On sets the led to a high state. Returns true on success
func (l *LedDriver) On() bool {
l.changeState(1)
l.High = true
l.high = true
return true
}
// Off sets the led to a low state. Returns true on success
func (l *LedDriver) Off() bool {
l.changeState(0)
l.High = false
l.high = false
return true
}
// Toggle sets the led to the opposite of it's current state
func (l *LedDriver) Toggle() {
if l.IsOn() {
if l.State() {
l.Off()
} else {
l.On()
}
}
// Brightness sets the led to the specified level of brightness
func (l *LedDriver) Brightness(level byte) {
l.adaptor().PwmWrite(l.Pin(), level)
}

View File

@ -4,12 +4,14 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents a Makey Button
type MakeyButtonDriver struct {
gobot.Driver
Active bool
data []int
}
// NewMakeyButtonDriver returns a new MakeyButtonDriver given a DigitalRead, name and pin.
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
m := &MakeyButtonDriver{
Driver: *gobot.NewDriver(
@ -31,6 +33,12 @@ func (b *MakeyButtonDriver) adaptor() DigitalReader {
return b.Adaptor().(DigitalReader)
}
// Starts the MakeyButtonDriver and reads the state of the button at the given Driver.Interval().
// Returns true on successful start of the driver.
//
// Emits the Events:
// "push" int - On button push
// "release" int - On button release
func (m *MakeyButtonDriver) Start() bool {
state := 0
gobot.Every(m.Interval(), func() {
@ -42,8 +50,9 @@ func (m *MakeyButtonDriver) Start() bool {
})
return true
}
// Halt returns true on a successful halt of the driver
func (m *MakeyButtonDriver) Halt() bool { return true }
func (m *MakeyButtonDriver) Init() bool { return true }
func (m *MakeyButtonDriver) readState() int {
return m.adaptor().DigitalRead(m.Pin())

View File

@ -4,6 +4,7 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents a Motor
type MotorDriver struct {
gobot.Driver
SpeedPin string
@ -17,6 +18,7 @@ type MotorDriver struct {
CurrentDirection string
}
// NewMotorDriver return a new MotorDriver given a PwmDigitalWriter, name and pin
func NewMotorDriver(a PwmDigitalWriter, name string, pin string) *MotorDriver {
return &MotorDriver{
Driver: *gobot.NewDriver(
@ -35,10 +37,13 @@ func (m *MotorDriver) adaptor() PwmDigitalWriter {
return m.Adaptor().(PwmDigitalWriter)
}
// Start starts the MotorDriver. Returns true on successful start of the driver
func (m *MotorDriver) Start() bool { return true }
func (m *MotorDriver) Halt() bool { return true }
func (m *MotorDriver) Init() bool { return true }
// Halt halts the MotorDriver. Returns true on successful halt of the driver
func (m *MotorDriver) Halt() bool { return true }
// Off turns the motor off or sets the motor to a 0 speed
func (m *MotorDriver) Off() {
if m.isDigital() {
m.changeState(0)
@ -47,6 +52,7 @@ func (m *MotorDriver) Off() {
}
}
// On turns the motor on or sets the motor to a maximum speed
func (m *MotorDriver) On() {
if m.isDigital() {
m.changeState(1)
@ -58,14 +64,17 @@ func (m *MotorDriver) On() {
}
}
// Min sets the motor to the minimum speed
func (m *MotorDriver) Min() {
m.Off()
}
// Max sets the motor to the maximum speed
func (m *MotorDriver) Max() {
m.Speed(255)
}
// InOn returns true if the motor is on
func (m *MotorDriver) IsOn() bool {
if m.isDigital() {
return m.CurrentState == 1
@ -73,10 +82,12 @@ func (m *MotorDriver) IsOn() bool {
return m.CurrentSpeed > 0
}
// InOff returns true if the motor is off
func (m *MotorDriver) IsOff() bool {
return !m.IsOn()
}
// Toggle sets the motor to the opposite of it's current state
func (m *MotorDriver) Toggle() {
if m.IsOn() {
m.Off()
@ -85,22 +96,26 @@ func (m *MotorDriver) Toggle() {
}
}
// Speed sets the speed of the motor
func (m *MotorDriver) Speed(value byte) {
m.CurrentMode = "analog"
m.CurrentSpeed = value
m.adaptor().PwmWrite(m.SpeedPin, value)
}
// Forward sets the forward pin to the specified speed
func (m *MotorDriver) Forward(speed byte) {
m.Direction("forward")
m.Speed(speed)
}
// Backward sets the backward pin to the specified speed
func (m *MotorDriver) Backward(speed byte) {
m.Direction("backward")
m.Speed(speed)
}
// Direction sets the direction pin to the specified speed
func (m *MotorDriver) Direction(direction string) {
m.CurrentDirection = direction
if m.DirectionPin != "" {

View File

@ -4,11 +4,19 @@ import (
"github.com/hybridgroup/gobot"
)
// Represents a Servo
type ServoDriver struct {
gobot.Driver
CurrentAngle byte
}
// NewSerovDriver return a new ServoDriver given a Servo, name and pin.
//
// Adds the following API Commands:
// "Move" - See ServoDriver.Move
// "Min" - See ServoDriver.Min
// "Center" - See ServoDriver.Center
// "Max" - See ServoDriver.Max
func NewServoDriver(a Servo, name string, pin string) *ServoDriver {
s := &ServoDriver{
Driver: *gobot.NewDriver(
@ -46,14 +54,18 @@ func (s *ServoDriver) adaptor() Servo {
return s.Adaptor().(Servo)
}
// Start starts the ServoDriver. Returns true on successful start of the driver.
func (s *ServoDriver) Start() bool { return true }
func (s *ServoDriver) Halt() bool { return true }
func (s *ServoDriver) Init() bool { return true }
// Halt halts the ServoDriver. Returns true on successful halt of the driver.
func (s *ServoDriver) Halt() bool { return true }
// InitServo initializes the ServoDriver on platforms which require an explicit initialization.
func (s *ServoDriver) InitServo() {
s.adaptor().InitServo()
}
// Move sets the servo to the specified angle
func (s *ServoDriver) Move(angle uint8) {
if !(angle >= 0 && angle <= 180) {
panic("Servo angle must be an integer between 0-180")
@ -62,14 +74,17 @@ func (s *ServoDriver) Move(angle uint8) {
s.adaptor().ServoWrite(s.Pin(), s.angleToSpan(angle))
}
// Min sets the servo to it's minimum position
func (s *ServoDriver) Min() {
s.Move(0)
}
// Center sets the servo to it's center position
func (s *ServoDriver) Center() {
s.Move(90)
}
// Max sets the servo to its maximum position
func (s *ServoDriver) Max() {
s.Move(180)
}