mirror of
https://github.com/kidoman/embd
synced 2025-07-04 20:37:46 +02:00
bring in the idea of a hardware abstraction layer
This commit is contained in:
parent
b4de382833
commit
b5e2d0acc7
35 changed files with 994 additions and 971 deletions
114
host/generic/linux/gpio/digitalpin.go
Normal file
114
host/generic/linux/gpio/digitalpin.go
Normal file
|
@ -0,0 +1,114 @@
|
|||
package gpio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/kidoman/embd/gpio"
|
||||
)
|
||||
|
||||
type digitalPin struct {
|
||||
n int
|
||||
|
||||
dir *os.File
|
||||
val *os.File
|
||||
activeLow *os.File
|
||||
edge *os.File
|
||||
}
|
||||
|
||||
func newDigitalPin(n int) (p *digitalPin, err error) {
|
||||
p = &digitalPin{n: n}
|
||||
err = p.init()
|
||||
return
|
||||
}
|
||||
|
||||
func (p *digitalPin) init() (err error) {
|
||||
if p.dir, err = p.directionFile(); err != nil {
|
||||
return
|
||||
}
|
||||
if p.val, err = p.valueFile(); err != nil {
|
||||
return
|
||||
}
|
||||
if p.activeLow, err = p.activeLowFile(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (p *digitalPin) basePath() string {
|
||||
return fmt.Sprintf("/sys/class/gpio/gpio%v", p.n)
|
||||
}
|
||||
|
||||
func (p *digitalPin) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_RDWR, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (p *digitalPin) directionFile() (*os.File, error) {
|
||||
return p.openFile(path.Join(p.basePath(), "direction"))
|
||||
}
|
||||
|
||||
func (p *digitalPin) valueFile() (*os.File, error) {
|
||||
return p.openFile(path.Join(p.basePath(), "value"))
|
||||
}
|
||||
|
||||
func (p *digitalPin) activeLowFile() (*os.File, error) {
|
||||
return p.openFile(path.Join(p.basePath(), "active_low"))
|
||||
}
|
||||
|
||||
func (p *digitalPin) SetDir(dir gpio.Direction) (err error) {
|
||||
str := "in"
|
||||
if dir == gpio.Out {
|
||||
str = "out"
|
||||
}
|
||||
_, err = p.dir.WriteString(str)
|
||||
return
|
||||
}
|
||||
|
||||
func (p *digitalPin) Read() (val int, err error) {
|
||||
buf := make([]byte, 1)
|
||||
if _, err = p.val.Read(buf); err != nil {
|
||||
return
|
||||
}
|
||||
val = 0
|
||||
if buf[0] == '1' {
|
||||
val = 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *digitalPin) Write(val int) (err error) {
|
||||
str := "0"
|
||||
if val == gpio.High {
|
||||
str = "1"
|
||||
}
|
||||
_, err = p.val.WriteString(str)
|
||||
return
|
||||
}
|
||||
|
||||
func (p *digitalPin) ActiveLow(b bool) (err error) {
|
||||
str := "0"
|
||||
if b {
|
||||
str = "1"
|
||||
}
|
||||
_, err = p.activeLow.WriteString(str)
|
||||
return
|
||||
}
|
||||
|
||||
func (p *digitalPin) Close() error {
|
||||
if err := p.dir.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.val.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.activeLow.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.edge.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
149
host/generic/linux/gpio/gpio.go
Normal file
149
host/generic/linux/gpio/gpio.go
Normal file
|
@ -0,0 +1,149 @@
|
|||
package gpio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/kidoman/embd/gpio"
|
||||
)
|
||||
|
||||
const (
|
||||
Normal int = 1 << iota
|
||||
I2C
|
||||
UART
|
||||
SPI
|
||||
)
|
||||
|
||||
type PinDesc struct {
|
||||
N int
|
||||
IDs []string
|
||||
Caps int
|
||||
}
|
||||
|
||||
type PinMap []*PinDesc
|
||||
|
||||
func (m PinMap) Lookup(k interface{}) (*PinDesc, bool) {
|
||||
switch key := k.(type) {
|
||||
case int:
|
||||
for i := range m {
|
||||
if m[i].N == key {
|
||||
return m[i], true
|
||||
}
|
||||
}
|
||||
case string:
|
||||
for i := range m {
|
||||
for j := range m[i].IDs {
|
||||
if m[i].IDs[j] == key {
|
||||
return m[i], true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
type GPIO struct {
|
||||
exporter, unexporter *os.File
|
||||
|
||||
initialized bool
|
||||
|
||||
pinMap PinMap
|
||||
initializedPins map[int]*digitalPin
|
||||
}
|
||||
|
||||
func New(pinMap PinMap) *GPIO {
|
||||
return &GPIO{
|
||||
pinMap: pinMap,
|
||||
initializedPins: map[int]*digitalPin{},
|
||||
}
|
||||
}
|
||||
|
||||
func (io *GPIO) init() (err error) {
|
||||
if io.initialized {
|
||||
return
|
||||
}
|
||||
|
||||
if io.exporter, err = os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, os.ModeExclusive); err != nil {
|
||||
return
|
||||
}
|
||||
if io.unexporter, err = os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
io.initialized = true
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (io *GPIO) lookupKey(key interface{}) (*PinDesc, bool) {
|
||||
return io.pinMap.Lookup(key)
|
||||
}
|
||||
|
||||
func (io *GPIO) export(n int) (err error) {
|
||||
_, err = io.exporter.WriteString(strconv.Itoa(n))
|
||||
return
|
||||
}
|
||||
|
||||
func (io *GPIO) unexport(n int) (err error) {
|
||||
_, err = io.unexporter.WriteString(strconv.Itoa(n))
|
||||
return
|
||||
}
|
||||
|
||||
func (io *GPIO) digitalPin(key interface{}) (p *digitalPin, err error) {
|
||||
pd, found := io.lookupKey(key)
|
||||
if !found {
|
||||
err = fmt.Errorf("gpio: could not find pin matching %q", key)
|
||||
return
|
||||
}
|
||||
|
||||
n := pd.N
|
||||
|
||||
var ok bool
|
||||
if p, ok = io.initializedPins[n]; ok {
|
||||
return
|
||||
}
|
||||
|
||||
if pd.Caps&Normal == 0 {
|
||||
err = fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key)
|
||||
return
|
||||
}
|
||||
|
||||
if pd.Caps != Normal {
|
||||
glog.Infof("gpio: pin %q is not a dedicated GPIO pin. please refer to the system reference manual for more details", key)
|
||||
}
|
||||
|
||||
if err = io.export(n); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if p, err = newDigitalPin(n); err != nil {
|
||||
io.unexport(n)
|
||||
return
|
||||
}
|
||||
|
||||
io.initializedPins[n] = p
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (io *GPIO) DigitalPin(key interface{}) (gpio.DigitalPin, error) {
|
||||
if err := io.init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return io.digitalPin(key)
|
||||
}
|
||||
|
||||
func (io *GPIO) Close() error {
|
||||
for n := range io.initializedPins {
|
||||
io.unexport(n)
|
||||
}
|
||||
|
||||
io.exporter.Close()
|
||||
io.unexporter.Close()
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue