embd/pin.go

87 lines
1.7 KiB
Go
Raw Permalink Normal View History

2014-03-23 09:39:31 +01:00
// Pin mapping support.
2014-03-22 07:53:56 +01:00
package embd
import (
"fmt"
"strconv"
)
2014-03-22 07:53:56 +01:00
const (
2014-03-23 09:41:09 +01:00
// CapDigital represents the digital IO capability.
CapDigital int = 1 << iota
2014-03-23 09:39:31 +01:00
// CapI2C represents pins with the I2C capability.
2014-03-22 07:53:56 +01:00
CapI2C
2014-03-23 09:39:31 +01:00
// CapUART represents pins with the UART capability.
2014-03-22 07:53:56 +01:00
CapUART
2014-03-23 09:39:31 +01:00
// CapSPI represents pins with the SPI capability.
2014-03-22 07:53:56 +01:00
CapSPI
2014-03-23 09:39:31 +01:00
// CapGPMS represents pins with the GPMC capability.
2014-03-22 07:53:56 +01:00
CapGPMC
2014-03-23 09:39:31 +01:00
// CapLCD represents pins used to carry LCD data.
2014-03-22 07:53:56 +01:00
CapLCD
2014-03-23 09:39:31 +01:00
// CapPWM represents pins with PWM capability.
2014-03-22 07:53:56 +01:00
CapPWM
2014-03-23 09:39:31 +01:00
// CapAnalog represents pins with analog IO capability.
CapAnalog
2014-03-22 07:53:56 +01:00
)
2014-03-23 09:39:31 +01:00
// PinDesc represents a pin descriptor.
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
}
2014-03-23 09:39:31 +01:00
// PinMap type represents a collection of pin descriptors.
2014-03-22 07:53:56 +01:00
type PinMap []*PinDesc
2014-03-23 09:39:31 +01:00
// Lookup returns a pin descriptor matching the provided key and capability
// combination. This allows the same keys to be used across pins with differing
// capabilities. For example, it is perfectly fine to have:
//
2014-03-23 09:41:09 +01:00
// pin1: {Aliases: [10, GPIO10], Cap: CapDigital}
2014-03-23 09:39:31 +01:00
// pin2: {Aliases: [10, AIN0], Cap: CapAnalog}
//
2014-03-23 09:41:09 +01:00
// Searching for 10 with CapDigital will return pin1 and searching for
2014-03-23 09:39:31 +01:00
// 10 with CapAnalog will return pin2. This makes for a very pleasant to use API.
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
}