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)
This commit is contained in:
		
							
								
								
									
										40
									
								
								kmk/common/pins.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								kmk/common/pins.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					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()
 | 
				
			||||||
@@ -8,3 +8,19 @@ class AttrDict(dict):
 | 
				
			|||||||
    '''
 | 
					    '''
 | 
				
			||||||
    def __getattr__(self, key):
 | 
					    def __getattr__(self, key):
 | 
				
			||||||
        return self[key]
 | 
					        return self[key]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Anything:
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    A stub class which will repr as a provided name
 | 
				
			||||||
 | 
					    '''
 | 
				
			||||||
 | 
					    def __init__(self, name):
 | 
				
			||||||
 | 
					        self.name = name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __repr__(self):
 | 
				
			||||||
 | 
					        return 'Anything<{}>'.format(self.name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Passthrough:
 | 
				
			||||||
 | 
					    def __getattr__(self, attr):
 | 
				
			||||||
 | 
					        return Anything(attr)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,11 @@
 | 
				
			|||||||
import machine
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
					from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
				
			||||||
from kmk.common.keycodes import KC
 | 
					from kmk.common.keycodes import KC
 | 
				
			||||||
from kmk.common.macros.unicode import unicode_sequence
 | 
					from kmk.common.macros.unicode import unicode_sequence
 | 
				
			||||||
 | 
					from kmk.common.pins import Pin as P
 | 
				
			||||||
from kmk.entrypoints.handwire.pyboard import main
 | 
					from kmk.entrypoints.handwire.pyboard import main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
p = machine.Pin.board
 | 
					cols = (P.Y12, P.Y11, P.Y10, P.Y9, P.X8, P.X7, P.X6, P.X5, P.X4, P.X3, P.X2, P.X1)
 | 
				
			||||||
 | 
					rows = (P.Y1, P.Y2, P.Y3, P.Y4)
 | 
				
			||||||
cols = (p.Y12, p.Y11, p.Y10, p.Y9, p.X8, p.X7, p.X6, p.X5, p.X4, p.X3, p.X2, p.X1)
 | 
					 | 
				
			||||||
rows = (p.Y1, p.Y2, p.Y3, p.Y4)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
diode_orientation = DiodeOrientation.COLUMNS
 | 
					diode_orientation = DiodeOrientation.COLUMNS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,13 @@
 | 
				
			|||||||
import board
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
					from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
				
			||||||
from kmk.common.keycodes import KC
 | 
					from kmk.common.keycodes import KC
 | 
				
			||||||
from kmk.common.macros.simple import send_string, simple_key_sequence
 | 
					from kmk.common.macros.simple import send_string, simple_key_sequence
 | 
				
			||||||
from kmk.common.macros.unicode import unicode_sequence
 | 
					from kmk.common.macros.unicode import unicode_sequence
 | 
				
			||||||
 | 
					from kmk.common.pins import Pin as P
 | 
				
			||||||
from kmk.entrypoints.handwire.feather_m4_express import main
 | 
					from kmk.entrypoints.handwire.feather_m4_express import main
 | 
				
			||||||
from kmk.firmware import Firmware
 | 
					from kmk.firmware import Firmware
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cols = (board.D11, board.D10, board.D9)
 | 
					cols = (P.D11, P.D10, P.D9)
 | 
				
			||||||
rows = (board.A2, board.A3, board.A4, board.A5)
 | 
					rows = (P.A2, P.A3, P.A4, P.A5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
diode_orientation = DiodeOrientation.COLUMNS
 | 
					diode_orientation = DiodeOrientation.COLUMNS
 | 
				
			||||||
unicode_mode = UnicodeModes.LINUX
 | 
					unicode_mode = UnicodeModes.LINUX
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,13 @@
 | 
				
			|||||||
import board
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
					from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
				
			||||||
from kmk.common.keycodes import KC
 | 
					from kmk.common.keycodes import KC
 | 
				
			||||||
from kmk.common.macros.simple import send_string, simple_key_sequence
 | 
					from kmk.common.macros.simple import send_string, simple_key_sequence
 | 
				
			||||||
from kmk.common.macros.unicode import unicode_sequence
 | 
					from kmk.common.macros.unicode import unicode_sequence
 | 
				
			||||||
 | 
					from kmk.common.pins import Pin as P
 | 
				
			||||||
from kmk.entrypoints.handwire.itsybitsy_m4_express import main
 | 
					from kmk.entrypoints.handwire.itsybitsy_m4_express import main
 | 
				
			||||||
from kmk.firmware import Firmware
 | 
					from kmk.firmware import Firmware
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cols = (board.A4, board.A5, board.D13)
 | 
					cols = (P.A4, P.A5, P.D13)
 | 
				
			||||||
rows = (board.D12, board.D11, board.D10)
 | 
					rows = (P.D12, P.D11, P.D10)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
diode_orientation = DiodeOrientation.COLUMNS
 | 
					diode_orientation = DiodeOrientation.COLUMNS
 | 
				
			||||||
unicode_mode = UnicodeModes.LINUX
 | 
					unicode_mode = UnicodeModes.LINUX
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,12 @@
 | 
				
			|||||||
import machine
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
					from kmk.common.consts import DiodeOrientation, UnicodeModes
 | 
				
			||||||
from kmk.common.keycodes import KC
 | 
					from kmk.common.keycodes import KC
 | 
				
			||||||
from kmk.common.macros.simple import send_string, simple_key_sequence
 | 
					from kmk.common.macros.simple import send_string, simple_key_sequence
 | 
				
			||||||
from kmk.common.macros.unicode import unicode_sequence
 | 
					from kmk.common.macros.unicode import unicode_sequence
 | 
				
			||||||
 | 
					from kmk.common.pins import Pin as P
 | 
				
			||||||
from kmk.entrypoints.handwire.pyboard import main
 | 
					from kmk.entrypoints.handwire.pyboard import main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
p = machine.Pin.board
 | 
					cols = (P.X10, P.X11, P.X12)
 | 
				
			||||||
cols = (p.X10, p.X11, p.X12)
 | 
					rows = (P.X1, P.X2, P.X3)
 | 
				
			||||||
rows = (p.X1, p.X2, p.X3)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
diode_orientation = DiodeOrientation.COLUMNS
 | 
					diode_orientation = DiodeOrientation.COLUMNS
 | 
				
			||||||
unicode_mode = UnicodeModes.LINUX
 | 
					unicode_mode = UnicodeModes.LINUX
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user