Skip to content

Commit 5e97cb1

Browse files
brentrubrentru
brentru
authored and
brentru
committed
add switch_to_x methods, docstrings
1 parent 00e7f97 commit 5e97cb1

File tree

1 file changed

+64
-26
lines changed

1 file changed

+64
-26
lines changed

adafruit_esp32spi/digitalio.py

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,30 @@
2828
"""
2929
from micropython import const
3030

31-
# Enums
32-
class DriveMode():
33-
PUSH_PULL = None
34-
OPEN_DRAIN = None
35-
36-
DriveMode.PUSH_PULL = DriveMode()
37-
DriveMode.OPEN_DRAIN = DriveMode()
38-
39-
class Direction:
40-
INPUT = None
41-
OUTPUT = None
42-
43-
Direction.INPUT = Direction()
44-
Direction.OUTPUT = Direction()
45-
4631
class Pin:
4732
IN = const(0x00)
4833
OUT = const(0x01)
4934
LOW = const(0x00)
5035
HIGH = const(0x01)
51-
id = None
5236
_value = LOW
5337
_mode = IN
38+
id = None
5439

5540
ESP32_GPIO_PINS = set([0, 1, 2, 4, 5,
5641
12, 13, 14, 15,
5742
16, 17, 18, 19,
5843
21, 22, 23, 25,
5944
26, 27, 32, 33])
45+
"""
46+
Implementation of CircuitPython API Pin Handling
47+
for ESP32SPI.
48+
49+
:param int esp_pin: Valid ESP32 GPIO Pin, predefined in ESP32_GPIO_PINS.
50+
:param ESP_SPIcontrol esp: The ESP object we are using.
6051
52+
NOTE: This class does not currently implement reading digital pins
53+
or the use of internal pull-up resistors.
54+
"""
6155
def __init__(self, esp_pin, esp):
6256
if esp_pin in self.ESP32_GPIO_PINS:
6357
self.id = esp_pin
@@ -67,9 +61,8 @@ def __init__(self, esp_pin, esp):
6761

6862
def init(self, mode=IN):
6963
"""Initalizes a pre-defined pin.
70-
:param mode: Pin mode (IN, OUT, LOW, HIGH).
64+
:param mode: Pin mode (IN, OUT, LOW, HIGH). Defaults to IN.
7165
"""
72-
print('pin init')
7366
if mode != None:
7467
if mode == self.IN:
7568
self._mode = self.IN
@@ -82,7 +75,7 @@ def init(self, mode=IN):
8275

8376
def value(self, val=None):
8477
"""Sets ESP32 Pin GPIO output mode.
85-
:param val: Output level (LOW, HIGH)
78+
:param val: Pin output level (LOW, HIGH)
8679
"""
8780
if val != None:
8881
if val == self.LOW:
@@ -99,30 +92,64 @@ def value(self, val=None):
9992
def __repr__(self):
10093
return str(self.id)
10194

95+
96+
class DriveMode():
97+
PUSH_PULL = None
98+
OPEN_DRAIN = None
99+
DriveMode.PUSH_PULL = DriveMode()
100+
DriveMode.OPEN_DRAIN = DriveMode()
101+
102+
103+
class Direction():
104+
INPUT = None
105+
OUTPUT = None
106+
Direction.INPUT = Direction()
107+
Direction.OUTPUT = Direction()
108+
109+
102110
class DigitalInOut():
111+
"""Implementation of DigitalIO module for ESP32SPI.
103112
104-
"""Mock DigitalIO CircuitPython API Implementation for ESP32SPI.
105-
Provides access to ESP_SPIcontrol methods.
113+
:param ESP_SPIcontrol esp: The ESP object we are using.
114+
:param int pin: Valid ESP32 GPIO Pin, predefined in ESP32_GPIO_PINS.
106115
"""
107116
_pin = None
108117
def __init__(self, esp, pin):
109118
self._esp = esp
110119
self._pin = Pin(pin, self._esp)
111-
print('id:', self._pin.id)
112-
self._direction = Direction.INPUT
120+
self.direction = Direction.INPUT
121+
122+
def __exit__(self):
123+
self.deinit()
113124

114125
def deinit(self):
115126
self._pin = None
127+
128+
def switch_to_output(self, value=False, drive_mode= DriveMode.PUSH_PULL):
129+
"""Set the drive mode and value and then switch to writing out digital values.
130+
:param bool value: Default mode to set upon switching.
131+
:param DriveMode drive_mode: Drive mode for the output.
132+
"""
133+
self.direction = Direction.OUTPUT
134+
self.value = value
135+
self._drive_mode = drive_mode
116136

117-
def __exit__(self):
118-
self.deinit()
137+
def switch_to_input(self, pull=None):
138+
"""Sets the pull and then switch to read in digital values.
139+
:param Pull pull: Pull configuration for the input.
140+
"""
141+
raise NotImplementedError("Digital reads are not currently supported in ESP32SPI.")
119142

120143
@property
121144
def direction(self):
145+
"""Returns the pin's direction."""
122146
return self.__direction
123147

124148
@direction.setter
125149
def direction(self, dir):
150+
"""Sets the direction of the pin.
151+
:param Direction dir: Pin direction (Direction.OUTPUT or Direction.INPUT)
152+
"""
126153
self.__direction = dir
127154
if dir is Direction.OUTPUT:
128155
self._pin.init(mode=Pin.OUT)
@@ -135,26 +162,37 @@ def direction(self, dir):
135162

136163
@property
137164
def value(self):
165+
"""Returns the digital logic level value of the pin."""
138166
return self._pin.value() is 1
139167

140168
@value.setter
141169
def value(self, val):
170+
"""Sets the digital logic level of the pin.
171+
:param type value: Pin logic level.
172+
:param int value: Pin logic level. 1 is logic high, 0 is logic low.
173+
:param bool value: Pin logic level. True is logic high, False is logic low.
174+
"""
142175
if self.direction is Direction.OUTPUT:
143176
self._pin.value(1 if val else 0)
144177
else:
145178
raise AttributeError("Not an output")
146179

147180
@property
148181
def drive_mode(self):
182+
"""Returns pin drive mode."""
149183
if self.direction is Direction.OUTPUT:
150184
return self.__drive_mode
151185
else:
152186
raise AttributeError("Not an output")
153187

154188
@drive_mode.setter
155189
def drive_mode(self, mode):
190+
"""Sets the pin drive mode.
191+
:param DriveMode mode: Defines the drive mode when outputting digital values.
192+
Either PUSH_PULL or OPEN_DRAIN
193+
"""
156194
self.__drive_mode = mode
157195
if mode is DriveMode.OPEN_DRAIN:
158196
self._pin.init(mode=Pin.OPEN_DRAIN)
159197
elif mode is DriveMode.PUSH_PULL:
160-
self._pin.init(mode=Pin.OUT)
198+
self._pin.init(mode=Pin.OUT)

0 commit comments

Comments
 (0)