11# The MIT License (MIT)
22#
3- # Copyright (c) 2019 Brent Rubell for Adafruit
3+ # Copyright (c) 2019 Brent Rubell for Adafruit Industries
44#
55# Permission is hereby granted, free of charge, to any person obtaining a copy
66# of this software and associated documentation files (the "Software"), to deal
2929from micropython import const
3030
3131class 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 ])
4532 """
4633 Implementation of CircuitPython API Pin Handling
4734 for ESP32SPI.
@@ -52,9 +39,24 @@ class Pin:
5239 NOTE: This class does not currently implement reading digital pins
5340 or the use of internal pull-up resistors.
5441 """
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+
5557 def __init__ (self , esp_pin , esp ):
5658 if esp_pin in self .ESP32_GPIO_PINS :
57- self .id = esp_pin
59+ self .pin_id = esp_pin
5860 else :
5961 raise AttributeError ("Pin %d is not a valid ESP32 GPIO Pin." % esp_pin )
6062 self ._esp = esp
@@ -63,44 +65,46 @@ def init(self, mode=IN):
6365 """Initalizes a pre-defined pin.
6466 :param mode: Pin mode (IN, OUT, LOW, HIGH). Defaults to IN.
6567 """
66- if mode != None :
68+ if mode is not None :
6769 if mode == self .IN :
6870 self ._mode = self .IN
69- self ._esp .set_pin_mode (self .id , 0 )
71+ self ._esp .set_pin_mode (self .pin_id , 0 )
7072 elif mode == self .OUT :
7173 self ._mode = self .OUT
72- self ._esp .set_pin_mode (self .id , 1 )
74+ self ._esp .set_pin_mode (self .pin_id , 1 )
7375 else :
7476 raise RuntimeError ("Invalid mode defined" )
7577
7678 def value (self , val = None ):
7779 """Sets ESP32 Pin GPIO output mode.
7880 :param val: Pin output level (LOW, HIGH)
7981 """
80- if val != None :
82+ if val is not None :
8183 if val == self .LOW :
8284 self ._value = val
83- self ._esp .set_digital_write (self .id , 0 )
85+ self ._esp .set_digital_write (self .pin_id , 0 )
8486 elif val == self .HIGH :
8587 self ._value = val
86- self ._esp .set_digital_write (self .id , 1 )
88+ self ._esp .set_digital_write (self .pin_id , 1 )
8789 else :
8890 raise RuntimeError ("Invalid value for pin" )
8991 else :
9092 raise NotImplementedError ("digitalRead not currently implemented in esp32spi" )
9193
9294 def __repr__ (self ):
93- return str (self .id )
94-
95+ return str (self .pin_id )
9596
97+ @staticmethod
9698class DriveMode ():
99+ """DriveMode Enum."""
97100 PUSH_PULL = None
98101 OPEN_DRAIN = None
99102DriveMode .PUSH_PULL = DriveMode ()
100103DriveMode .OPEN_DRAIN = DriveMode ()
101104
102-
105+ @ staticmethod
103106class Direction ():
107+ """DriveMode Enum."""
104108 INPUT = None
105109 OUTPUT = None
106110Direction .INPUT = Direction ()
@@ -119,21 +123,25 @@ def __init__(self, esp, pin):
119123 self ._pin = Pin (pin , self ._esp )
120124 self .direction = Direction .INPUT
121125
122- def __exit__ (self ):
126+ def __enter__ (self ):
127+ return self
128+
129+ def __exit__ (self , exception_type , exception_value , traceback ):
123130 self .deinit ()
124131
125132 def deinit (self ):
133+ """De-initializes the pin object."""
126134 self ._pin = None
127135
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 ):
129137 """Set the drive mode and value and then switch to writing out digital values.
130138 :param bool value: Default mode to set upon switching.
131139 :param DriveMode drive_mode: Drive mode for the output.
132140 """
133141 self .direction = Direction .OUTPUT
134142 self .value = value
135143 self ._drive_mode = drive_mode
136-
144+
137145 def switch_to_input (self , pull = None ):
138146 """Sets the pull and then switch to read in digital values.
139147 :param Pull pull: Pull configuration for the input.
@@ -146,16 +154,16 @@ def direction(self):
146154 return self .__direction
147155
148156 @direction .setter
149- def direction (self , dir ):
157+ def direction (self , pin_dir ):
150158 """Sets the direction of the pin.
151159 :param Direction dir: Pin direction (Direction.OUTPUT or Direction.INPUT)
152160 """
153- self .__direction = dir
154- if dir is Direction .OUTPUT :
161+ self .__direction = pin_dir
162+ if pin_dir is Direction .OUTPUT :
155163 self ._pin .init (mode = Pin .OUT )
156164 self .value = False
157165 self .drive_mode = DriveMode .PUSH_PULL
158- elif dir is Direction .INPUT :
166+ elif pin_dir is Direction .INPUT :
159167 self ._pin .init (mode = Pin .IN )
160168 else :
161169 raise AttributeError ("Not a Direction" )
@@ -182,8 +190,7 @@ def drive_mode(self):
182190 """Returns pin drive mode."""
183191 if self .direction is Direction .OUTPUT :
184192 return self .__drive_mode
185- else :
186- raise AttributeError ("Not an output" )
193+ raise AttributeError ("Not an output" )
187194
188195 @drive_mode .setter
189196 def drive_mode (self , mode ):
0 commit comments