2014-03-23 14:09:31 +05:30
|
|
|
// Pin mapping support.
|
|
|
|
|
2014-03-22 12:23:56 +05:30
|
|
|
package embd
|
|
|
|
|
2014-03-23 01:11:24 +05:30
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2014-03-22 12:23:56 +05:30
|
|
|
const (
|
2014-03-23 14:11:09 +05:30
|
|
|
// CapDigital represents the digital IO capability.
|
|
|
|
CapDigital int = 1 << iota
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapI2C represents pins with the I2C capability.
|
2014-03-22 12:23:56 +05:30
|
|
|
CapI2C
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapUART represents pins with the UART capability.
|
2014-03-22 12:23:56 +05:30
|
|
|
CapUART
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapSPI represents pins with the SPI capability.
|
2014-03-22 12:23:56 +05:30
|
|
|
CapSPI
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapGPMS represents pins with the GPMC capability.
|
2014-03-22 12:23:56 +05:30
|
|
|
CapGPMC
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapLCD represents pins used to carry LCD data.
|
2014-03-22 12:23:56 +05:30
|
|
|
CapLCD
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapPWM represents pins with PWM capability.
|
2014-03-22 12:23:56 +05:30
|
|
|
CapPWM
|
2014-03-23 14:09:31 +05:30
|
|
|
|
|
|
|
// CapAnalog represents pins with analog IO capability.
|
2014-03-23 01:36:24 +05:30
|
|
|
CapAnalog
|
2014-03-22 12:23:56 +05:30
|
|
|
)
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// PinDesc represents a pin descriptor.
|
2014-03-22 12:23:56 +05:30
|
|
|
type PinDesc struct {
|
2014-03-23 01:11:24 +05:30
|
|
|
ID string
|
|
|
|
Aliases []string
|
|
|
|
Caps int
|
|
|
|
|
|
|
|
DigitalLogical int
|
2014-03-23 01:36:24 +05:30
|
|
|
AnalogLogical int
|
2014-03-22 12:23:56 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// PinMap type represents a collection of pin descriptors.
|
2014-03-22 12:23:56 +05:30
|
|
|
type PinMap []*PinDesc
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// 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 14:11:09 +05:30
|
|
|
// pin1: {Aliases: [10, GPIO10], Cap: CapDigital}
|
2014-03-23 14:09:31 +05:30
|
|
|
// pin2: {Aliases: [10, AIN0], Cap: CapAnalog}
|
|
|
|
//
|
2014-03-23 14:11:09 +05:30
|
|
|
// Searching for 10 with CapDigital will return pin1 and searching for
|
2014-03-23 14:09:31 +05:30
|
|
|
// 10 with CapAnalog will return pin2. This makes for a very pleasant to use API.
|
2014-03-23 01:36:24 +05:30
|
|
|
func (m PinMap) Lookup(k interface{}, cap int) (*PinDesc, bool) {
|
2014-03-23 01:11:24 +05:30
|
|
|
var ks string
|
2014-03-22 12:23:56 +05:30
|
|
|
switch key := k.(type) {
|
|
|
|
case int:
|
2014-03-23 01:11:24 +05:30
|
|
|
ks = strconv.Itoa(key)
|
2014-03-22 12:23:56 +05:30
|
|
|
case string:
|
2014-03-23 01:11:24 +05:30
|
|
|
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 {
|
2014-03-23 01:36:24 +05:30
|
|
|
if pd.Aliases[j] == ks && pd.Caps&cap != 0 {
|
2014-03-23 01:11:24 +05:30
|
|
|
return pd, true
|
2014-03-22 12:23:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|