1
1
# The MIT License (MIT)
2
2
#
3
- # Copyright (c) 2019 Brent Rubell for Adafruit
3
+ # Copyright (c) 2019 Brent Rubell for Adafruit Industries
4
4
#
5
5
# Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
# of this software and associated documentation files (the "Software"), to deal
29
29
from micropython import const
30
30
31
31
class Pin :
32
- IN = const (0x00 )
33
- OUT = const (0x01 )
34
- LOW = const (0x00 )
35
- HIGH = const (0x01 )
36
- _value = LOW
37
- _mode = IN
38
- id = None
39
-
40
- ESP32_GPIO_PINS = set ([0 , 1 , 2 , 4 , 5 ,
41
- 12 , 13 , 14 , 15 ,
42
- 16 , 17 , 18 , 19 ,
43
- 21 , 22 , 23 , 25 ,
44
- 26 , 27 , 32 , 33 ])
45
32
"""
46
33
Implementation of CircuitPython API Pin Handling
47
34
for ESP32SPI.
@@ -52,9 +39,24 @@ class Pin:
52
39
NOTE: This class does not currently implement reading digital pins
53
40
or the use of internal pull-up resistors.
54
41
"""
42
+ #pylint: disable=invalid-name
43
+ IN = const (0x00 )
44
+ OUT = const (0x01 )
45
+ LOW = const (0x00 )
46
+ HIGH = const (0x01 )
47
+ _value = LOW
48
+ _mode = IN
49
+ pin_id = None
50
+
51
+ ESP32_GPIO_PINS = set ([0 , 1 , 2 , 4 , 5 ,
52
+ 12 , 13 , 14 , 15 ,
53
+ 16 , 17 , 18 , 19 ,
54
+ 21 , 22 , 23 , 25 ,
55
+ 26 , 27 , 32 , 33 ])
56
+
55
57
def __init__ (self , esp_pin , esp ):
56
58
if esp_pin in self .ESP32_GPIO_PINS :
57
- self .id = esp_pin
59
+ self .pin_id = esp_pin
58
60
else :
59
61
raise AttributeError ("Pin %d is not a valid ESP32 GPIO Pin." % esp_pin )
60
62
self ._esp = esp
@@ -63,44 +65,46 @@ def init(self, mode=IN):
63
65
"""Initalizes a pre-defined pin.
64
66
:param mode: Pin mode (IN, OUT, LOW, HIGH). Defaults to IN.
65
67
"""
66
- if mode != None :
68
+ if mode is not None :
67
69
if mode == self .IN :
68
70
self ._mode = self .IN
69
- self ._esp .set_pin_mode (self .id , 0 )
71
+ self ._esp .set_pin_mode (self .pin_id , 0 )
70
72
elif mode == self .OUT :
71
73
self ._mode = self .OUT
72
- self ._esp .set_pin_mode (self .id , 1 )
74
+ self ._esp .set_pin_mode (self .pin_id , 1 )
73
75
else :
74
76
raise RuntimeError ("Invalid mode defined" )
75
77
76
78
def value (self , val = None ):
77
79
"""Sets ESP32 Pin GPIO output mode.
78
80
:param val: Pin output level (LOW, HIGH)
79
81
"""
80
- if val != None :
82
+ if val is not None :
81
83
if val == self .LOW :
82
84
self ._value = val
83
- self ._esp .set_digital_write (self .id , 0 )
85
+ self ._esp .set_digital_write (self .pin_id , 0 )
84
86
elif val == self .HIGH :
85
87
self ._value = val
86
- self ._esp .set_digital_write (self .id , 1 )
88
+ self ._esp .set_digital_write (self .pin_id , 1 )
87
89
else :
88
90
raise RuntimeError ("Invalid value for pin" )
89
91
else :
90
92
raise NotImplementedError ("digitalRead not currently implemented in esp32spi" )
91
93
92
94
def __repr__ (self ):
93
- return str (self .id )
94
-
95
+ return str (self .pin_id )
95
96
97
+ @staticmethod
96
98
class DriveMode ():
99
+ """DriveMode Enum."""
97
100
PUSH_PULL = None
98
101
OPEN_DRAIN = None
99
102
DriveMode .PUSH_PULL = DriveMode ()
100
103
DriveMode .OPEN_DRAIN = DriveMode ()
101
104
102
-
105
+ @ staticmethod
103
106
class Direction ():
107
+ """DriveMode Enum."""
104
108
INPUT = None
105
109
OUTPUT = None
106
110
Direction .INPUT = Direction ()
@@ -119,21 +123,25 @@ def __init__(self, esp, pin):
119
123
self ._pin = Pin (pin , self ._esp )
120
124
self .direction = Direction .INPUT
121
125
122
- def __exit__ (self ):
126
+ def __enter__ (self ):
127
+ return self
128
+
129
+ def __exit__ (self , exception_type , exception_value , traceback ):
123
130
self .deinit ()
124
131
125
132
def deinit (self ):
133
+ """De-initializes the pin object."""
126
134
self ._pin = None
127
135
128
- def switch_to_output (self , value = False , drive_mode = DriveMode .PUSH_PULL ):
136
+ def switch_to_output (self , value = False , drive_mode = DriveMode .PUSH_PULL ):
129
137
"""Set the drive mode and value and then switch to writing out digital values.
130
138
:param bool value: Default mode to set upon switching.
131
139
:param DriveMode drive_mode: Drive mode for the output.
132
140
"""
133
141
self .direction = Direction .OUTPUT
134
142
self .value = value
135
143
self ._drive_mode = drive_mode
136
-
144
+
137
145
def switch_to_input (self , pull = None ):
138
146
"""Sets the pull and then switch to read in digital values.
139
147
:param Pull pull: Pull configuration for the input.
@@ -146,16 +154,16 @@ def direction(self):
146
154
return self .__direction
147
155
148
156
@direction .setter
149
- def direction (self , dir ):
157
+ def direction (self , pin_dir ):
150
158
"""Sets the direction of the pin.
151
159
:param Direction dir: Pin direction (Direction.OUTPUT or Direction.INPUT)
152
160
"""
153
- self .__direction = dir
154
- if dir is Direction .OUTPUT :
161
+ self .__direction = pin_dir
162
+ if pin_dir is Direction .OUTPUT :
155
163
self ._pin .init (mode = Pin .OUT )
156
164
self .value = False
157
165
self .drive_mode = DriveMode .PUSH_PULL
158
- elif dir is Direction .INPUT :
166
+ elif pin_dir is Direction .INPUT :
159
167
self ._pin .init (mode = Pin .IN )
160
168
else :
161
169
raise AttributeError ("Not a Direction" )
@@ -182,8 +190,7 @@ def drive_mode(self):
182
190
"""Returns pin drive mode."""
183
191
if self .direction is Direction .OUTPUT :
184
192
return self .__drive_mode
185
- else :
186
- raise AttributeError ("Not an output" )
193
+ raise AttributeError ("Not an output" )
187
194
188
195
@drive_mode .setter
189
196
def drive_mode (self , mode ):
0 commit comments