Skip to content

Commit 962c75c

Browse files
author
BiffoBear
committed
Made the typing of the pins more explicit, so it's clear where they come from.
1 parent dbdff9b commit 962c75c

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

adafruit_rgbled.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
2121
* Adafruit's SimpleIO library: https://github.com/adafruit/Adafruit_CircuitPython_SimpleIO
2222
"""
23+
try:
24+
from typing import Union
25+
import adafruit_pca9685 as pca9685
26+
import pwmio
27+
import microcontroller
28+
except ImportError:
29+
pass
30+
2331
from pwmio import PWMOut
2432
from simpleio import map_range
2533

@@ -31,21 +39,6 @@ class RGBLED:
3139
"""
3240
Creates a RGBLED object given three physical pins or PWMOut objects.
3341
34-
:param red_pin: The physical pin connected to a red LED anode.
35-
:type ~microcontroller.Pin: Microcontroller's red_pin.
36-
:type pwmio.PWMOut: PWMOut object associated with red_pin.
37-
:type PWMChannel: PCA9685 PWM channel associated with red_pin.
38-
:param green_pin: The physical pin connected to a green LED anode.
39-
:type ~microcontroller.Pin: Microcontroller's green_pin.
40-
:type pwmio.PWMOut: PWMOut object associated with green_pin.
41-
:type PWMChannel: PCA9685 PWM channel associated with green_pin.
42-
:param blue_pin: The physical pin connected to a blue LED anode.
43-
:type ~microcontroller.Pin: Microcontroller's blue_pin.
44-
:type pwmio.PWMOut: PWMOut object associated with blue_pin.
45-
:type PWMChannel: PCA9685 PWM channel associated with blue_pin.
46-
:param bool invert_pwm: False if the RGB LED is common cathode,
47-
true if the RGB LED is common anode.
48-
4942
Example for setting a RGB LED using a RGB Tuple (Red, Green, Blue):
5043
5144
.. code-block:: python
@@ -96,7 +89,23 @@ class RGBLED:
9689
9790
"""
9891

99-
def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
92+
def __init__(
93+
self,
94+
red_pin: Union[microcontroller.Pin, pwmio.PWMOut, pca9685.PWMChannel],
95+
green_pin: Union[microcontroller.Pin, pwmio.PWMOut, pca9685.PWMChannel],
96+
blue_pin: Union[microcontroller.Pin, pwmio.PWMOut, pca9685.PWMChannel],
97+
invert_pwm: bool = False,
98+
) -> None:
99+
"""
100+
:param Union[microcontroller.Pin, pwmio.PWMOut, pca9685.PWMChannel] red_pin:
101+
The connection to the red LED.
102+
:param Union[microcontroller.Pin, pwmio.PWMOut, pca9685.PWMChannel] green_pin:
103+
The connection to the green LED.
104+
:param Union[microcontroller.Pin, pwmio.PWMOut, pca9685.PWMChannel] blue_pin:
105+
The connection to the blue LED.
106+
:param bool invert_pwm: False if the RGB LED is common cathode,
107+
True if the RGB LED is common anode.
108+
"""
100109
self._rgb_led_pins = [red_pin, green_pin, blue_pin]
101110
for i in range( # pylint: disable=consider-using-enumerate
102111
len(self._rgb_led_pins)
@@ -112,27 +121,34 @@ def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
112121
self._current_color = (0, 0, 0)
113122
self.color = self._current_color
114123

115-
def __enter__(self):
124+
def __enter__(self) -> "RGBLED":
116125
return self
117126

118-
def __exit__(self, exception_type, exception_value, traceback):
127+
def __exit__(self, exception_type, exception_value, traceback) -> None:
119128
self.deinit()
120129

121-
def deinit(self):
130+
def deinit(self) -> None:
122131
"""Turn the LEDs off, deinit pwmout and release hardware resources."""
123132
for pin in self._rgb_led_pins:
124133
pin.deinit() # pylint: disable=no-member
125134
self._current_color = (0, 0, 0)
126135

127136
@property
128-
def color(self):
129-
"""Returns the RGB LED's current color."""
137+
def color(self) -> Union[int, tuple]:
138+
"""Return the RGB LED's current color.
139+
140+
:return Union[int, tuple]: The currently set color.
141+
"""
130142
return self._current_color
131143

132144
@color.setter
133-
def color(self, value):
145+
def color(self, value: Union[int, tuple]):
134146
"""Sets the RGB LED to a desired color.
135-
:param type value: RGB LED desired value - can be a RGB tuple or a 24-bit integer.
147+
:param Union[int, tuple] value: RGB LED desired value - can be a RGB tuple of values
148+
0 - 255 or a 24-bit integer. e.g. (255, 64, 35) and 0xff4023 are equivalent.
149+
150+
:raises ValueError: If the input is an int > 0xffffff.
151+
:raises TypeError: If the input is not an integer or a tuple.
136152
"""
137153
self._current_color = value
138154
if isinstance(value, tuple):
@@ -154,4 +170,4 @@ def color(self, value):
154170
rgb[color] -= 65535
155171
self._rgb_led_pins[color].duty_cycle = abs(rgb[color])
156172
else:
157-
raise ValueError("Color must be a tuple or 24-bit integer value.")
173+
raise TypeError("Color must be a tuple or 24-bit integer value.")

0 commit comments

Comments
 (0)