1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00
embd/gpio.go

251 lines
5.3 KiB
Go
Raw Normal View History

2014-03-23 14:09:31 +05:30
// GPIO support.
2014-03-03 00:51:23 +05:30
package embd
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.
In Direction = iota
2014-03-23 14:09:31 +05:30
// Out represents write mode.
Out
2014-02-17 03:41:53 +05:30
)
const (
2014-03-23 14:09:31 +05:30
// Low represents 0.
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.
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.
Write(val int) error
2014-03-23 14:09:31 +05:30
// Read reads the value from the pin.
Read() (int, error)
2014-02-17 03:41:53 +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).
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.
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.
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
// 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.
type GPIODriver interface {
// Unregister unregisters the pin from the driver. Should be called when the pin is closed.
Unregister(string) error
2014-03-23 14:09:31 +05:30
// DigitalPin returns a pin capable of doing digital IO.
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.
Close() error
2014-02-17 03:41:53 +05:30
}
var gpioDriverInitialized bool
var gpioDriverInstance GPIODriver
2014-03-23 14:09:31 +05:30
// InitGPIO initializes the GPIO driver.
2014-03-03 00:51:23 +05:30
func InitGPIO() error {
if gpioDriverInitialized {
return nil
}
2014-03-03 00:51:23 +05:30
desc, err := DescribeHost()
if err != nil {
return err
}
if desc.GPIODriver == nil {
2014-03-23 06:41:51 +05:30
return ErrFeatureNotSupported
}
gpioDriverInstance = desc.GPIODriver()
gpioDriverInitialized = true
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 {
return gpioDriverInstance.Close()
}
2014-03-23 14:09:31 +05:30
// NewDigitalPin returns a DigitalPin interface which allows control over
// the digital GPIO pin.
func NewDigitalPin(key interface{}) (DigitalPin, error) {
if err := InitGPIO(); err != nil {
return nil, err
}
return gpioDriverInstance.DigitalPin(key)
}
2014-03-23 14:09:31 +05:30
// DigitalWrite writes val to the pin.
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.
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).
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.
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) {
if err := InitGPIO(); err != nil {
return nil, err
}
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) {
if err := InitGPIO(); err != nil {
return nil, err
}
2014-03-28 08:24:42 +05:30
return gpioDriverInstance.PWMPin(key)
}