kmk_firmware/kmk/common/pins.py
Josh Klar 392f8d7e51
Abstract away platform-dependent pin references
Quote taken straight from the docstring of get_pin:

>Cross-platform method to find a pin by string.
>
>The pin definitions are platform-dependent, but this provides
>a way to say "I'm using pin D20" without rolling a D20 and
>having to actually learn MicroPython/CircuitPython and the
>differences in how they handle pinouts.
>
>This also makes the keymap sanity checker actually work for
>CircuitPython boards, since it's not possible in CPY to
>define a module stub for `board` that uses Passthrough
>natively (which is how the MicroPython stub worked originally)
2018-10-07 01:40:09 -07:00

41 lines
1020 B
Python

try:
import board
PLATFORM = 'CircuitPython'
PIN_SOURCE = board
except ImportError:
import machine
PLATFORM = 'MicroPython'
PIN_SOURCE = machine.Pin.board
except ImportError:
from kmk.common.types import Passthrough
PLATFORM = 'Unit Testing'
PIN_SOURCE = Passthrough()
def get_pin(pin):
'''
Cross-platform method to find a pin by string.
The pin definitions are platform-dependent, but this provides
a way to say "I'm using pin D20" without rolling a D20 and
having to actually learn MicroPython/CircuitPython and the
differences in how they handle pinouts.
This also makes the keymap sanity checker actually work for
CircuitPython boards, since it's not possible in CPY to
define a module stub for `board` that uses Passthrough
natively (which is how the MicroPython stub worked originally)
'''
return getattr(PIN_SOURCE, pin)
class PinLookup:
def __getattr__(self, attr):
return get_pin(attr)
Pin = PinLookup()