1
0
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:
Karan Misra 2014-03-23 05:37:28 +05:30
parent 5a1a40d53b
commit f7b316332e
2 changed files with 28 additions and 4 deletions

17
bbb.go
View File

@ -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
View 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
}