You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.7 KiB

// Pin mapping support.
package embd
import (
const (
// CapDigital represents the digital IO capability.
CapDigital int = 1 << iota
// CapI2C represents pins with the I2C capability.
// CapUART represents pins with the UART capability.
// CapSPI represents pins with the SPI capability.
// CapGPMS represents pins with the GPMC capability.
// CapLCD represents pins used to carry LCD data.
// CapPWM represents pins with PWM capability.
// CapAnalog represents pins with analog IO capability.
// PinDesc represents a pin descriptor.
type PinDesc struct {
ID string
Aliases []string
Caps int
DigitalLogical int
AnalogLogical int
// PinMap type represents a collection of pin descriptors.
type PinMap []*PinDesc
// 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:
// pin1: {Aliases: [10, GPIO10], Cap: CapDigital}
// pin2: {Aliases: [10, AIN0], Cap: CapAnalog}
// Searching for 10 with CapDigital will return pin1 and searching for
// 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
switch key := k.(type) {
case int:
ks = strconv.Itoa(key)
case string:
ks = key
case fmt.Stringer:
ks = key.String()
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
return nil, false