embd/pin.go

59 lines
731 B
Go
Raw Normal View History

2014-03-22 07:53:56 +01:00
package embd
import (
"fmt"
"strconv"
)
2014-03-22 07:53:56 +01:00
const (
CapNormal int = 1 << iota
CapI2C
CapUART
CapSPI
CapGPMC
CapLCD
CapPWM
CapAnalog
2014-03-22 07:53:56 +01:00
)
type PinDesc struct {
ID string
Aliases []string
Caps int
DigitalLogical int
AnalogLogical int
2014-03-22 07:53:56 +01:00
}
type PinMap []*PinDesc
func (m PinMap) Lookup(k interface{}, cap int) (*PinDesc, bool) {
var ks string
2014-03-22 07:53:56 +01:00
switch key := k.(type) {
case int:
ks = strconv.Itoa(key)
2014-03-22 07:53:56 +01:00
case string:
ks = key
case fmt.Stringer:
ks = key.String()
default:
return nil, false
}
for i := range m {
pd := m[i]
if pd.ID == ks {
return pd, true
}
for j := range pd.Aliases {
if pd.Aliases[j] == ks && pd.Caps&cap != 0 {
return pd, true
2014-03-22 07:53:56 +01:00
}
}
}
return nil, false
}