mirror of
https://github.com/kidoman/embd
synced 2024-12-22 12:50:19 +01:00
gpio: make analog pin support more versatile on the bbb
This commit is contained in:
parent
5a1a40d53b
commit
f7b316332e
17
bbb.go
17
bbb.go
@ -123,7 +123,11 @@ func (p *bbbAnalogPin) init() error {
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) ensureEnabled() error {
|
||||
file := "/sys/devices/bone_capemgr.8/slots"
|
||||
pattern := "/sys/devices/bone_capemgr.*/slots"
|
||||
file, err := findFirstMatchingFile(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -142,8 +146,9 @@ func (p *bbbAnalogPin) ensureEnabled() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) valueFilePath() string {
|
||||
return fmt.Sprintf("/sys/devices/ocp.2/helper.14/AIN%v", p.n)
|
||||
func (p *bbbAnalogPin) valueFilePath() (string, error) {
|
||||
pattern := fmt.Sprintf("/sys/devices/ocp.*/helper.*/AIN%v", p.n)
|
||||
return findFirstMatchingFile(pattern)
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) openFile(path string) (*os.File, error) {
|
||||
@ -151,7 +156,11 @@ func (p *bbbAnalogPin) openFile(path string) (*os.File, error) {
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) valueFile() (*os.File, error) {
|
||||
return p.openFile(p.valueFilePath())
|
||||
path, err := p.valueFilePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.openFile(path)
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) Read() (int, error) {
|
||||
|
15
utils.go
Normal file
15
utils.go
Normal file
@ -0,0 +1,15 @@
|
||||
package embd
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
// Inspiration: https://github.com/mrmorphic/hwio/blob/master/hwio.go#L451
|
||||
func findFirstMatchingFile(glob string) (string, error) {
|
||||
matches, err := filepath.Glob(glob)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(matches) >= 1 {
|
||||
return matches[0], nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user