Skip to content

Commit 7772894

Browse files
brentrubrentru
brentru
authored and
brentru
committed
add valid pin tupleset check before initialization, raise error
1 parent 4df8144 commit 7772894

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

adafruit_esp32spi/digitalio.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,32 @@ class Pull:
4949
Pull.UP = Pull()
5050
Pull.DOWN = Pull()
5151

52+
# ESP32-WROOM GPIO Pins
53+
54+
5255
class Pin:
5356
IN = const(0x00)
5457
OUT = const(0x01)
5558
LOW = const(0x00)
5659
HIGH = const(0x01)
5760
id = None
58-
_value = LOW
61+
_value = LOW
5962
_mode = IN
6063

61-
def __init__(self, esp_pin_number, esp):
62-
self.id = esp_pin_number
64+
ESP32_GPIO_PINS = set([0, 1, 2, 4, 5,
65+
12, 13, 14, 15,
66+
16, 17, 18, 19,
67+
21, 22, 23, 25,
68+
26, 27, 32, 33])
69+
70+
71+
def __init__(self, esp_pin, esp):
72+
if esp_pin in self.ESP32_GPIO_PINS:
73+
self.id = esp_pin
74+
else:
75+
raise AttributeError("Pin %d is not a valid ESP32 GPIO Pin."%esp_pin)
6376
self._esp = esp
64-
77+
6578
def init(self, mode=IN, pull=None):
6679
"""Initalizes a pre-defined pin.
6780
:param mode: Pin mode (IN, OUT, LOW, HIGH)
@@ -79,7 +92,7 @@ def init(self, mode=IN, pull=None):
7992
raise RuntimeError("Invalid mode defined")
8093
if pull != None:
8194
raise RuntimeError("ESP32 does not have pull-up resistors defined.")
82-
95+
8396
def value(self, val=None):
8497
"""Sets ESP32 Pin GPIO output mode.
8598
:param val: Output level (LOW, HIGH)

0 commit comments

Comments
 (0)