1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00
embd/pin.go

87 lines
1.7 KiB
Go
Raw Normal View History

2014-03-23 14:09:31 +05:30
// Pin mapping support.
2014-03-22 12:23:56 +05:30
package embd
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.
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 {
ID string
Aliases []string
Caps int
DigitalLogical int
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.
func (m PinMap) Lookup(k interface{}, cap int) (*PinDesc, bool) {
var ks string
2014-03-22 12:23:56 +05:30
switch key := k.(type) {
case int:
ks = strconv.Itoa(key)
2014-03-22 12:23:56 +05:30
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 12:23:56 +05:30
}
}
}
return nil, false
}