28
28
"""
29
29
from micropython import const
30
30
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
-
46
31
class Pin :
47
32
IN = const (0x00 )
48
33
OUT = const (0x01 )
49
34
LOW = const (0x00 )
50
35
HIGH = const (0x01 )
51
- id = None
52
36
_value = LOW
53
37
_mode = IN
38
+ id = None
54
39
55
40
ESP32_GPIO_PINS = set ([0 , 1 , 2 , 4 , 5 ,
56
41
12 , 13 , 14 , 15 ,
57
42
16 , 17 , 18 , 19 ,
58
43
21 , 22 , 23 , 25 ,
59
44
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.
60
51
52
+ NOTE: This class does not currently implement reading digital pins
53
+ or the use of internal pull-up resistors.
54
+ """
61
55
def __init__ (self , esp_pin , esp ):
62
56
if esp_pin in self .ESP32_GPIO_PINS :
63
57
self .id = esp_pin
@@ -67,9 +61,8 @@ def __init__(self, esp_pin, esp):
67
61
68
62
def init (self , mode = IN ):
69
63
"""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.
71
65
"""
72
- print ('pin init' )
73
66
if mode != None :
74
67
if mode == self .IN :
75
68
self ._mode = self .IN
@@ -82,7 +75,7 @@ def init(self, mode=IN):
82
75
83
76
def value (self , val = None ):
84
77
"""Sets ESP32 Pin GPIO output mode.
85
- :param val: Output level (LOW, HIGH)
78
+ :param val: Pin output level (LOW, HIGH)
86
79
"""
87
80
if val != None :
88
81
if val == self .LOW :
@@ -99,30 +92,64 @@ def value(self, val=None):
99
92
def __repr__ (self ):
100
93
return str (self .id )
101
94
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
+
102
110
class DigitalInOut ():
111
+ """Implementation of DigitalIO module for ESP32SPI.
103
112
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 .
106
115
"""
107
116
_pin = None
108
117
def __init__ (self , esp , pin ):
109
118
self ._esp = esp
110
119
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 ()
113
124
114
125
def deinit (self ):
115
126
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
116
136
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." )
119
142
120
143
@property
121
144
def direction (self ):
145
+ """Returns the pin's direction."""
122
146
return self .__direction
123
147
124
148
@direction .setter
125
149
def direction (self , dir ):
150
+ """Sets the direction of the pin.
151
+ :param Direction dir: Pin direction (Direction.OUTPUT or Direction.INPUT)
152
+ """
126
153
self .__direction = dir
127
154
if dir is Direction .OUTPUT :
128
155
self ._pin .init (mode = Pin .OUT )
@@ -135,26 +162,37 @@ def direction(self, dir):
135
162
136
163
@property
137
164
def value (self ):
165
+ """Returns the digital logic level value of the pin."""
138
166
return self ._pin .value () is 1
139
167
140
168
@value .setter
141
169
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
+ """
142
175
if self .direction is Direction .OUTPUT :
143
176
self ._pin .value (1 if val else 0 )
144
177
else :
145
178
raise AttributeError ("Not an output" )
146
179
147
180
@property
148
181
def drive_mode (self ):
182
+ """Returns pin drive mode."""
149
183
if self .direction is Direction .OUTPUT :
150
184
return self .__drive_mode
151
185
else :
152
186
raise AttributeError ("Not an output" )
153
187
154
188
@drive_mode .setter
155
189
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
+ """
156
194
self .__drive_mode = mode
157
195
if mode is DriveMode .OPEN_DRAIN :
158
196
self ._pin .init (mode = Pin .OPEN_DRAIN )
159
197
elif mode is DriveMode .PUSH_PULL :
160
- self ._pin .init (mode = Pin .OUT )
198
+ self ._pin .init (mode = Pin .OUT )
0 commit comments