Skip to content

Commit 9d96275

Browse files
author
Alex Ward
committed
extract out pin number checks
1 parent 812b313 commit 9d96275

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

l293d/driver.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
threading = False
1414
from time import sleep
1515

16-
from l293d.gpio import GPIO
16+
from l293d.gpio import GPIO, pins_are_valid
1717
from l293d.config import Config
1818
Config = Config()
1919

@@ -62,7 +62,7 @@ def __init__(self, pin_a=0, pin_b=0, pin_c=0):
6262
self.reversed = False
6363

6464
# Check pins are valid
65-
if pins_are_valid(self.motor_pins):
65+
if pins_are_valid(self.motor_pins, self.pin_numbering):
6666
self.exists = True
6767
# Append to global list of pins in use
6868
for pin in self.motor_pins:
@@ -196,35 +196,6 @@ def __init__(self):
196196
'for more info')
197197

198198

199-
def pins_are_valid(pins, force_selection=True):
200-
"""
201-
Check the pins specified are valid for pin numbering in use
202-
"""
203-
# Pin numbering, used below, should be
204-
# a parameter of this function (future)
205-
if Config.pin_numbering == 'BOARD': # Set valid pins for BOARD
206-
valid_pins = [
207-
7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 33, 36, 37
208-
]
209-
elif Config.pin_numbering == 'BCM': # Set valid pins for BCM
210-
valid_pins = [
211-
4, 5, 6, 12, 13, 16, 17, 18, 22, 23, 24, 25, 26, 27
212-
]
213-
else: # pin_numbering value invalid
214-
raise ValueError("pin_numbering must be either 'BOARD' or 'BCM'.")
215-
for pin in pins:
216-
pin_int = int(pin)
217-
if pin_int not in valid_pins and force_selection is False:
218-
err_str = (
219-
"GPIO pin number must be from list of valid pins: %s"
220-
"\nTo use selected pins anyway, set force_selection=True "
221-
"in function call." % str(valid_pins))
222-
raise ValueError(err_str)
223-
if pin in pins_in_use:
224-
raise ValueError('GPIO pin {} already in use.'.format(pin))
225-
return True
226-
227-
228199
def cleanup():
229200
"""
230201
Call GPIO cleanup method

l293d/gpio/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
raspberry_pi, micropython = True, True
22

33
try:
4-
from RPi.GPIO import GPIO
4+
from l293d.gpio.raspberrypi import GPIO, pins_are_valid
55
except ImportError:
66
raspberry_pi = False
77

88
try:
9-
from machine import Pin
10-
from l293d.gpio.micropython import GPIO
9+
from l293d.gpio.micropython import GPIO, pins_are_valid
1110
except ImportError:
1211
micropython = False
1312

1413
if not raspberry_pi and not micropython:
1514
print(
1615
"Can't import a GPIO controller; test mode has been enabled:\n"
1716
"http://l293d.rtfd.io/en/latest/user-guide/configuration/#test-mode")
18-
from l293d.gpio.testgpio import GPIO
17+
from l293d.gpio.testgpio import GPIO, pins_are_valid

l293d/gpio/micropython.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
21
import machine
32

43

4+
def pins_are_valid(pins, pin_numbering, force_selection=False):
5+
"""
6+
Check the pins specified are valid for pin numbering in use
7+
"""
8+
# Micropython can be run on any number of boards, so don't do any
9+
# validation against pin numbers.
10+
return True
11+
12+
513
class GPIO(object):
614
__pins = {}
715

l293d/gpio/raspberrypi.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from RPi.GPIO import GPIO
2+
3+
4+
def pins_are_valid(pins, pin_numbering, force_selection=False):
5+
"""
6+
Check the pins specified are valid for pin numbering in use
7+
"""
8+
if pin_numbering == 'BOARD': # Set valid pins for BOARD
9+
valid_pins = [
10+
7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 33, 36, 37
11+
]
12+
elif pin_numbering == 'BCM': # Set valid pins for BCM
13+
valid_pins = [
14+
4, 5, 6, 12, 13, 16, 17, 18, 22, 23, 24, 25, 26, 27
15+
]
16+
else: # pin_numbering value invalid
17+
raise ValueError("pin_numbering must be either 'BOARD' or 'BCM'.")
18+
19+
for pin in pins:
20+
pin_int = int(pin)
21+
if pin_int not in valid_pins and force_selection is False:
22+
err_str = (
23+
"GPIO pin number must be from list of valid pins: %s"
24+
"\nTo use selected pins anyway, set force_selection=True "
25+
"in function call." % str(valid_pins))
26+
raise ValueError(err_str)
27+
if pin in pins_in_use:
28+
raise ValueError('GPIO pin {} already in use.'.format(pin))
29+
return True
30+
31+

l293d/gpio/testgpio.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

2+
def pins_are_valid(pins, pin_numbering, force_selection=False):
3+
"""
4+
Check the pins specified are valid for pin numbering in use
5+
"""
6+
# Test mode shouldn't validate pins
7+
return True
8+
29

310
class GPIO(object):
411
__pins = {}

0 commit comments

Comments
 (0)