Skip to content

Commit

Permalink
extract out pin number checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Ward committed Aug 28, 2018
1 parent 812b313 commit 2ca201a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
33 changes: 2 additions & 31 deletions l293d/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
threading = False
from time import sleep

from l293d.gpio import GPIO
from l293d.gpio import GPIO, pins_are_valid
from l293d.config import Config
Config = Config()

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

# Check pins are valid
if pins_are_valid(self.motor_pins):
if pins_are_valid(self.motor_pins, self.pin_numbering):
self.exists = True
# Append to global list of pins in use
for pin in self.motor_pins:
Expand Down Expand Up @@ -196,35 +196,6 @@ def __init__(self):
'for more info')


def pins_are_valid(pins, force_selection=True):
"""
Check the pins specified are valid for pin numbering in use
"""
# Pin numbering, used below, should be
# a parameter of this function (future)
if Config.pin_numbering == 'BOARD': # Set valid pins for BOARD
valid_pins = [
7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 33, 36, 37
]
elif Config.pin_numbering == 'BCM': # Set valid pins for BCM
valid_pins = [
4, 5, 6, 12, 13, 16, 17, 18, 22, 23, 24, 25, 26, 27
]
else: # pin_numbering value invalid
raise ValueError("pin_numbering must be either 'BOARD' or 'BCM'.")
for pin in pins:
pin_int = int(pin)
if pin_int not in valid_pins and force_selection is False:
err_str = (
"GPIO pin number must be from list of valid pins: %s"
"\nTo use selected pins anyway, set force_selection=True "
"in function call." % str(valid_pins))
raise ValueError(err_str)
if pin in pins_in_use:
raise ValueError('GPIO pin {} already in use.'.format(pin))
return True


def cleanup():
"""
Call GPIO cleanup method
Expand Down
5 changes: 2 additions & 3 deletions l293d/gpio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
raspberry_pi, micropython = True, True

try:
from RPi.GPIO import GPIO
from l293d.gpio.raspberrypi import GPIO, pins_are_valid
except ImportError:
raspberry_pi = False

try:
from machine import Pin
from l293d.gpio.micropython import GPIO
from l293d.gpio.micropython import GPIO, pins_are_valid
except ImportError:
micropython = False

Expand Down
10 changes: 9 additions & 1 deletion l293d/gpio/micropython.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@

import machine


def pins_are_valid(pins, pin_numbering, force_selection=False):
"""
Check the pins specified are valid for pin numbering in use
"""
# Micropython can be run on any number of boards, so don't do any
# validation against pin numbers.
return True


class GPIO(object):
__pins = {}

Expand Down
31 changes: 31 additions & 0 deletions l293d/gpio/raspberrypi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from RPi.GPIO import GPIO


def pins_are_valid(pins, pin_numbering, force_selection=False):
"""
Check the pins specified are valid for pin numbering in use
"""
if pin_numbering == 'BOARD': # Set valid pins for BOARD
valid_pins = [
7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 33, 36, 37
]
elif pin_numbering == 'BCM': # Set valid pins for BCM
valid_pins = [
4, 5, 6, 12, 13, 16, 17, 18, 22, 23, 24, 25, 26, 27
]
else: # pin_numbering value invalid
raise ValueError("pin_numbering must be either 'BOARD' or 'BCM'.")

for pin in pins:
pin_int = int(pin)
if pin_int not in valid_pins and force_selection is False:
err_str = (
"GPIO pin number must be from list of valid pins: %s"
"\nTo use selected pins anyway, set force_selection=True "
"in function call." % str(valid_pins))
raise ValueError(err_str)
if pin in pins_in_use:
raise ValueError('GPIO pin {} already in use.'.format(pin))
return True


7 changes: 7 additions & 0 deletions l293d/gpio/testgpio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@

def pins_are_valid(pins, pin_numbering, force_selection=False):
"""
Check the pins specified are valid for pin numbering in use
"""
# Test mode shouldn't validate pins
return True


class GPIO(object):
__pins = {}
Expand Down

0 comments on commit 2ca201a

Please sign in to comment.