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

181 lines
3.7 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
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
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-23 14:09:31 +05:30
// GPIODriver implements a generic GPIO driver.
type GPIODriver interface {
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-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 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 {
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()
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) {
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) {
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()
}