2014-03-23 14:09:31 +05:30
|
|
|
// GPIO support.
|
|
|
|
|
2014-03-03 00:51:23 +05:30
|
|
|
package embd
|
2014-03-01 20:19:44 +05:30
|
|
|
|
2014-04-05 04:06:40 +05:30
|
|
|
import "time"
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// The Direction type indicates the direction of a GPIO pin.
|
2014-02-17 03:41:53 +05:30
|
|
|
type Direction int
|
|
|
|
|
|
|
|
const (
|
2014-03-23 14:09:31 +05:30
|
|
|
// In represents read mode.
|
2014-02-27 04:24:53 +05:30
|
|
|
In Direction = iota
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// Out represents write mode.
|
2014-02-27 04:24:53 +05:30
|
|
|
Out
|
2014-02-17 03:41:53 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-03-23 14:09:31 +05:30
|
|
|
// Low represents 0.
|
2014-02-27 04:24:53 +05:30
|
|
|
Low int = iota
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// High represents 1.
|
2014-02-17 03:41:53 +05:30
|
|
|
High
|
|
|
|
)
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// DigitalPin implements access to a digital IO capable GPIO pin.
|
2014-02-27 04:24:53 +05:30
|
|
|
type DigitalPin interface {
|
2014-03-23 14:09:31 +05:30
|
|
|
// N returns the logical GPIO number.
|
2014-03-23 04:59:35 +05:30
|
|
|
N() int
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Write writes the provided value to the pin.
|
2014-02-27 04:24:53 +05:30
|
|
|
Write(val int) error
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// Read reads the value from the pin.
|
2014-02-27 04:24:53 +05:30
|
|
|
Read() (int, error)
|
2014-02-17 03:41:53 +05:30
|
|
|
|
2014-04-05 04:06:40 +05:30
|
|
|
// TimePulse measures the duration of a pulse on the pin.
|
|
|
|
TimePulse(state int) (time.Duration, error)
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// SetDirection sets the direction of the pin (in/out).
|
2014-03-01 20:19:44 +05:30
|
|
|
SetDirection(dir Direction) error
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// ActiveLow makes the pin active low. A low logical state is represented by
|
|
|
|
// a high state on the physical pin, and vice-versa.
|
2014-02-27 04:24:53 +05:30
|
|
|
ActiveLow(b bool) error
|
2014-02-17 03:41:53 +05:30
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// PullUp pulls the pin up.
|
2014-03-02 12:09:57 +05:30
|
|
|
PullUp() error
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// PullDown pulls the pin down.
|
2014-03-02 12:09:57 +05:30
|
|
|
PullDown() error
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Close releases the resources associated with the pin.
|
2014-02-27 04:24:53 +05:30
|
|
|
Close() error
|
2014-02-17 03:41:53 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// AnalogPin implements access to a analog IO capable GPIO pin.
|
2014-03-23 04:59:35 +05:30
|
|
|
type AnalogPin interface {
|
2014-03-23 14:09:31 +05:30
|
|
|
// N returns the logical GPIO number.
|
2014-03-23 04:59:35 +05:30
|
|
|
N() int
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Read reads the value from the pin.
|
2014-03-23 04:59:35 +05:30
|
|
|
Read() (int, error)
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Close releases the resources associated with the pin.
|
2014-03-23 04:59:35 +05:30
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:24:42 +05:30
|
|
|
// The Polarity type indicates the polarity of a pwm pin.
|
|
|
|
type Polarity int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Positive represents (default) positive polarity.
|
|
|
|
Positive Polarity = iota
|
|
|
|
|
|
|
|
// Negative represents negative polarity.
|
|
|
|
Negative
|
|
|
|
)
|
|
|
|
|
|
|
|
// PWMPin implements access to a pwm capable GPIO pin.
|
|
|
|
type PWMPin interface {
|
|
|
|
// N returns the logical PWM id.
|
|
|
|
N() string
|
|
|
|
|
|
|
|
// SetPeriod sets the period of a pwm pin.
|
|
|
|
SetPeriod(ns int) error
|
|
|
|
|
|
|
|
// SetDuty sets the duty of a pwm pin.
|
|
|
|
SetDuty(ns int) error
|
|
|
|
|
|
|
|
// SetPolarity sets the polarity of a pwm pin.
|
|
|
|
SetPolarity(pol Polarity) error
|
|
|
|
|
2014-04-02 17:25:28 +05:30
|
|
|
// SetMicroseconds sends a command to the PWM driver to generate a us wide pulse.
|
|
|
|
SetMicroseconds(us int) error
|
|
|
|
|
|
|
|
// SetAnalog allows easy manipulation of the PWM based on a (0-255) range value.
|
|
|
|
SetAnalog(value byte) error
|
|
|
|
|
2014-03-28 08:24:42 +05:30
|
|
|
// Close releases the resources associated with the pin.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// GPIODriver implements a generic GPIO driver.
|
2014-03-23 06:32:24 +05:30
|
|
|
type GPIODriver interface {
|
2014-03-23 14:09:31 +05:30
|
|
|
// DigitalPin returns a pin capable of doing digital IO.
|
2014-02-27 04:24:53 +05:30
|
|
|
DigitalPin(key interface{}) (DigitalPin, error)
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// AnalogPin returns a pin capable of doing analog IO.
|
2014-03-23 04:59:35 +05:30
|
|
|
AnalogPin(key interface{}) (AnalogPin, error)
|
2014-02-17 03:41:53 +05:30
|
|
|
|
2014-03-28 08:24:42 +05:30
|
|
|
// PWMPin returns a pin capable of generating PWM.
|
|
|
|
PWMPin(key interface{}) (PWMPin, error)
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// Close releases the resources associated with the driver.
|
2014-02-27 04:24:53 +05:30
|
|
|
Close() error
|
2014-02-17 03:41:53 +05:30
|
|
|
}
|
2014-03-01 20:19:44 +05:30
|
|
|
|
2014-03-23 06:32:24 +05:30
|
|
|
var gpioDriverInstance GPIODriver
|
2014-03-01 20:19:44 +05:30
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// InitGPIO initializes the GPIO driver.
|
2014-03-03 00:51:23 +05:30
|
|
|
func InitGPIO() error {
|
|
|
|
desc, err := DescribeHost()
|
2014-03-01 20:19:44 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-23 06:32:24 +05:30
|
|
|
if desc.GPIODriver == nil {
|
2014-03-23 06:41:51 +05:30
|
|
|
return ErrFeatureNotSupported
|
2014-03-23 06:25:32 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 06:32:24 +05:30
|
|
|
gpioDriverInstance = desc.GPIODriver()
|
2014-03-01 20:19:44 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// CloseGPIO releases resources associated with the GPIO driver.
|
2014-03-03 00:51:23 +05:30
|
|
|
func CloseGPIO() error {
|
2014-03-23 06:32:24 +05:30
|
|
|
return gpioDriverInstance.Close()
|
2014-03-01 20:19:44 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// NewDigitalPin returns a DigitalPin interface which allows control over
|
|
|
|
// the digital GPIO pin.
|
2014-03-01 20:19:44 +05:30
|
|
|
func NewDigitalPin(key interface{}) (DigitalPin, error) {
|
2014-03-23 06:32:24 +05:30
|
|
|
return gpioDriverInstance.DigitalPin(key)
|
2014-03-01 20:19:44 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// DigitalWrite writes val to the pin.
|
2014-03-01 20:19:44 +05:30
|
|
|
func DigitalWrite(key interface{}, val int) error {
|
|
|
|
pin, err := NewDigitalPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.Write(val)
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// DigitalRead reads a value from the pin.
|
2014-03-01 20:19:44 +05:30
|
|
|
func DigitalRead(key interface{}) (int, error) {
|
|
|
|
pin, err := NewDigitalPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.Read()
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// SetDirection sets the direction of the pin (in/out).
|
2014-03-01 20:19:44 +05:30
|
|
|
func SetDirection(key interface{}, dir Direction) error {
|
|
|
|
pin, err := NewDigitalPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.SetDirection(dir)
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// ActiveLow makes the pin active low. A low logical state is represented by
|
|
|
|
// a high state on the physical pin, and vice-versa.
|
2014-03-01 20:19:44 +05:30
|
|
|
func ActiveLow(key interface{}, b bool) error {
|
|
|
|
pin, err := NewDigitalPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.ActiveLow(b)
|
|
|
|
}
|
2014-03-23 04:59:35 +05:30
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// PullUp pulls the pin up.
|
|
|
|
func PullUp(key interface{}) error {
|
|
|
|
pin, err := NewDigitalPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.PullUp()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PullDown pulls the pin down.
|
|
|
|
func PullDown(key interface{}) error {
|
|
|
|
pin, err := NewDigitalPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.PullDown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAnalogPin returns a AnalogPin interface which allows control over
|
|
|
|
// the analog GPIO pin.
|
2014-03-23 04:59:35 +05:30
|
|
|
func NewAnalogPin(key interface{}) (AnalogPin, error) {
|
2014-03-23 06:32:24 +05:30
|
|
|
return gpioDriverInstance.AnalogPin(key)
|
2014-03-23 04:59:35 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// AnalogWrite reads a value from the pin.
|
2014-03-23 04:59:35 +05:30
|
|
|
func AnalogRead(key interface{}) (int, error) {
|
|
|
|
pin, err := NewAnalogPin(key)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pin.Read()
|
|
|
|
}
|
2014-03-28 08:24:42 +05:30
|
|
|
|
|
|
|
// NewPWMPin returns a PWMPin interface which allows PWM signal
|
|
|
|
// generation over a the PWM pin.
|
|
|
|
func NewPWMPin(key interface{}) (PWMPin, error) {
|
|
|
|
return gpioDriverInstance.PWMPin(key)
|
|
|
|
}
|