55from abc import ABCMeta , abstractmethod
66
77from .util import StrKeyDict
8+ from enum import Enum
89
9- HIGH = 'HIGH'
10- LOW = 'LOW'
10+ State = Enum ('State' , 'LOW HIGH' )
1111
12- # TODO: 4 states implementation: IN, OUT, ANALOG, PWM
13- IN = 'IN'
14- OUT = 'OUT'
15- ANALOG = 'ANALOG'
16- PWM = 'PWM'
12+ Mode = Enum ('Mode' , 'IN OUT ANALOG PWM' )
1713
1814
1915class WrongPinMode (Exception ):
@@ -249,20 +245,20 @@ def __repr__(self):
249245
250246 @property
251247 def mode (self ):
252- """[property] Get/set pin mode to ``pingo .IN``, ``pingo .OUT``
253- ``pingo .ANALOG`` or ``pingo .PWM``"""
248+ """[property] Get/set pin mode to ``Mode .IN``, ``Mode .OUT``
249+ ``Mode .ANALOG`` or ``Mode .PWM``"""
254250 return self ._mode
255251
256252 @mode .setter
257253 def mode (self , value ):
258254 if value not in self .suported_modes :
259255 raise ModeNotSuported ()
260256
261- if value in [IN , OUT ]:
257+ if value in [Mode . IN , Mode . OUT ]:
262258 self .board ._set_digital_mode (self , value )
263- elif value == ANALOG :
259+ elif value == Mode . ANALOG :
264260 self .board ._set_analog_mode (self , value )
265- elif value == PWM :
261+ elif value == Mode . PWM :
266262 self .board ._set_pwm_mode (self , value )
267263
268264 self ._mode = value
@@ -283,60 +279,60 @@ class DigitalPin(Pin):
283279 because pins delegate all board-dependent behavior to the board.
284280 """
285281
286- suported_modes = [IN , OUT ]
282+ suported_modes = [Mode . IN , Mode . OUT ]
287283
288284 def __init__ (self , board , location , gpio_id = None ):
289285 Pin .__init__ (self , board , location , gpio_id )
290286 self ._state = None
291287
292288 @property
293289 def state (self ):
294- """[property] Get/set pin state to ``pingo .HIGH`` or ``pingo .LOW``"""
295- if self .mode not in [IN , OUT ]:
290+ """[property] Get/set pin state to ``State .HIGH`` or ``State .LOW``"""
291+ if self .mode not in [Mode . IN , Mode . OUT ]:
296292 raise WrongPinMode ()
297293
298- if self .mode == IN :
294+ if self .mode == Mode . IN :
299295 self ._state = self .board ._get_pin_state (self )
300296
301297 return self ._state
302298
303299 @state .setter
304300 def state (self , value ):
305- if self .mode != OUT :
301+ if self .mode != Mode . OUT :
306302 raise WrongPinMode ()
307303
308304 self .board ._set_pin_state (self , value )
309305 self ._state = value
310306
311307 def low (self ):
312- """Set voltage of pin to ``pingo .LOW`` (GND)."""
313- self .state = LOW
308+ """Set voltage of pin to ``State .LOW`` (GND)."""
309+ self .state = State . LOW
314310
315311 lo = low # shortcut for interactive use
316312
317313 def high (self ):
318314 """Set state of the pin to ``pingo.HIGH`` (Vcc)."""
319- self .state = HIGH
315+ self .state = State . HIGH
320316
321317 hi = high # shortcut for interactive use
322318
323319 def toggle (self ):
324320 """Change state of the pin."""
325- self .state = HIGH if self .state == LOW else LOW
321+ self .state = State . HIGH if self .state == State . LOW else State . LOW
326322
327323 def pulse (self ):
328324 """Generate a pulse in state of the pin."""
329- if self .state == LOW :
330- self .state = HIGH
331- self .state = LOW
325+ if self .state == State . LOW :
326+ self .state = State . HIGH
327+ self .state = State . LOW
332328 else :
333- self .state = LOW
334- self .state = HIGH
329+ self .state = State . LOW
330+ self .state = State . HIGH
335331
336332
337333class PwmPin (DigitalPin ):
338334
339- suported_modes = [IN , OUT , PWM ]
335+ suported_modes = [Mode . IN , Mode . OUT , Mode . PWM ]
340336
341337 def __init__ (self , board , location , gpio_id = None , frequency = None ):
342338 DigitalPin .__init__ (self , board , location , gpio_id )
@@ -348,13 +344,13 @@ def __init__(self, board, location, gpio_id=None, frequency=None):
348344
349345 @property
350346 def value (self ):
351- if self .mode != PWM :
347+ if self .mode != Mode . PWM :
352348 raise WrongPinMode ()
353349 return self .board ._get_pwm_duty_cycle (self )
354350
355351 @value .setter
356352 def value (self , value ):
357- if self .mode != PWM :
353+ if self .mode != Mode . PWM :
358354 raise WrongPinMode ()
359355 if not 0.0 <= value <= 100.0 :
360356 raise ArgumentOutOfRange ()
@@ -363,13 +359,13 @@ def value(self, value):
363359
364360 @property
365361 def frequency (self ):
366- if self .mode != PWM :
362+ if self .mode != Mode . PWM :
367363 raise WrongPinMode ()
368364 return self .board ._get_pwm_frequency (self )
369365
370366 @frequency .setter
371367 def frequency (self , new_frequency ):
372- if self .mode != PWM :
368+ if self .mode != Mode . PWM :
373369 raise WrongPinMode ()
374370 if new_frequency <= 0.0 :
375371 raise ArgumentOutOfRange ()
@@ -386,7 +382,7 @@ class AnalogPin(Pin):
386382 This pin type supports read operations only.
387383 """
388384
389- suported_modes = [IN , ANALOG ]
385+ suported_modes = [Mode . IN , Mode . ANALOG ]
390386
391387 def __init__ (self , board , location , resolution , gpio_id = None ):
392388 """
0 commit comments