Skip to content

Commit 6fb99e4

Browse files
authored
Merge pull request #23 from BiffoBear/Add_typing
Add typing
2 parents ba44689 + f5d9e06 commit 6fb99e4

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

adafruit_rgbled.py

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
* Adafruit CircuitPython firmware for the supported boards:
1919
https://github.com/adafruit/circuitpython/releases
2020
"""
21+
try:
22+
from typing import Union, Optional, Type
23+
from types import TracebackType
24+
from microcontroller import Pin
25+
from adafruit_pca9685 import PWMChannel
26+
from circuitpython_typing.led import ColorBasedColorUnion
27+
except ImportError:
28+
pass
29+
2130
from pwmio import PWMOut
2231

2332
__version__ = "0.0.0+auto.0"
@@ -26,24 +35,9 @@
2635

2736
class RGBLED:
2837
"""
29-
Creates a RGBLED object given three physical pins or PWMOut objects.
30-
31-
:param red_pin: The physical pin connected to a red LED anode.
32-
:type ~microcontroller.Pin: Microcontroller's red_pin.
33-
:type pwmio.PWMOut: PWMOut object associated with red_pin.
34-
:type PWMChannel: PCA9685 PWM channel associated with red_pin.
35-
:param green_pin: The physical pin connected to a green LED anode.
36-
:type ~microcontroller.Pin: Microcontroller's green_pin.
37-
:type pwmio.PWMOut: PWMOut object associated with green_pin.
38-
:type PWMChannel: PCA9685 PWM channel associated with green_pin.
39-
:param blue_pin: The physical pin connected to a blue LED anode.
40-
:type ~microcontroller.Pin: Microcontroller's blue_pin.
41-
:type pwmio.PWMOut: PWMOut object associated with blue_pin.
42-
:type PWMChannel: PCA9685 PWM channel associated with blue_pin.
43-
:param bool invert_pwm: False if the RGB LED is common cathode,
44-
true if the RGB LED is common anode.
38+
Create an RGBLED object given three physical pins or PWMOut objects.
4539
46-
Example for setting a RGB LED using a RGB Tuple (Red, Green, Blue):
40+
Example for setting an RGB LED using an RGB Tuple (Red, Green, Blue):
4741
4842
.. code-block:: python
4943
@@ -58,7 +52,7 @@ class RGBLED:
5852
led = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
5953
led.color = (255, 0, 0)
6054
61-
Example for setting a RGB LED using a 24-bit integer (hex syntax):
55+
Example for setting an RGB LED using a 24-bit integer (hex syntax):
6256
6357
.. code-block:: python
6458
@@ -69,11 +63,11 @@ class RGBLED:
6963
GREEN_LED = board.D6
7064
BLUE_LED = board.D7
7165
72-
# Create a RGB LED object
66+
# Create an RGB LED object
7367
led = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
7468
led.color = 0x100000
7569
76-
Example for setting a RGB LED using a ContextManager:
70+
Example for setting an RGB LED using a ContextManager:
7771
7872
.. code-block:: python
7973
@@ -91,9 +85,23 @@ class RGBLED:
9185
with adafruit_rgbled.RGBLED(board.D5, board.D6, board.D7, invert_pwm=True) as rgb_led:
9286
rgb_led.color = (0, 255, 0)
9387
88+
:param Union[Pin, PWMOut, "PWMChannel"] red_pin:
89+
The connection to the red LED.
90+
:param Union[Pin, PWMOut, "PWMChannel"] green_pin:
91+
The connection to the green LED.
92+
:param Union[Pin, PWMOut, "PWMChannel"] blue_pin:
93+
The connection to the blue LED.
94+
:param bool invert_pwm: False if the RGB LED is common cathode,
95+
True if the RGB LED is common anode. Defaults to False.
9496
"""
9597

96-
def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
98+
def __init__(
99+
self,
100+
red_pin: Union[Pin, PWMOut, PWMChannel],
101+
green_pin: Union[Pin, PWMOut, PWMChannel],
102+
blue_pin: Union[Pin, PWMOut, PWMChannel],
103+
invert_pwm: bool = False,
104+
) -> None:
97105
self._rgb_led_pins = [red_pin, green_pin, blue_pin]
98106
for i in range( # pylint: disable=consider-using-enumerate
99107
len(self._rgb_led_pins)
@@ -109,28 +117,41 @@ def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
109117
self._current_color = (0, 0, 0)
110118
self.color = self._current_color
111119

112-
def __enter__(self):
120+
def __enter__(self) -> "RGBLED":
113121
return self
114122

115-
def __exit__(self, exception_type, exception_value, traceback):
123+
def __exit__(
124+
self,
125+
exception_type: Optional[Type[type]],
126+
exception_value: Optional[BaseException],
127+
traceback: Optional[TracebackType],
128+
) -> None:
116129
self.deinit()
117130

118-
def deinit(self):
131+
def deinit(self) -> None:
119132
"""Turn the LEDs off, deinit pwmout and release hardware resources."""
120133
for pin in self._rgb_led_pins:
121134
pin.deinit() # pylint: disable=no-member
122135
self._current_color = (0, 0, 0)
123136

124137
@property
125-
def color(self):
126-
"""Returns the RGB LED's current color."""
138+
def color(self) -> ColorBasedColorUnion:
139+
"""
140+
Sets the RGB LED to a desired color.
141+
142+
:param ColorBasedColorUnion value: RGB LED desired value - can be a RGB
143+
tuple of values 0 - 255 or a 24-bit integer. e.g. (255, 64, 35) and 0xff4023
144+
are equivalent.
145+
146+
:returns Union[int, Tuple[int, int, int]]: The current LED color setting.
147+
148+
:raises ValueError: If the input is an int > 0xffffff.
149+
:raises TypeError: If the input is not an integer or a tuple.
150+
"""
127151
return self._current_color
128152

129153
@color.setter
130-
def color(self, value):
131-
"""Sets the RGB LED to a desired color.
132-
:param type value: RGB LED desired value - can be a RGB tuple or a 24-bit integer.
133-
"""
154+
def color(self, value: ColorBasedColorUnion):
134155
self._current_color = value
135156
if isinstance(value, tuple):
136157
for i in range(0, 3):
@@ -151,4 +172,4 @@ def color(self, value):
151172
rgb[color] -= 65535
152173
self._rgb_led_pins[color].duty_cycle = abs(rgb[color])
153174
else:
154-
raise ValueError("Color must be a tuple or 24-bit integer value.")
175+
raise TypeError("Color must be a tuple or 24-bit integer value.")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
# SPDX-License-Identifier: Unlicense
44

55
Adafruit-Blinka
6+
adafruit-circuitpython-typing~=1.4
7+
adafruit-circuitpython-pca9685

0 commit comments

Comments
 (0)