18
18
* Adafruit CircuitPython firmware for the supported boards:
19
19
https://github.com/adafruit/circuitpython/releases
20
20
"""
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
+
21
30
from pwmio import PWMOut
22
31
23
32
__version__ = "0.0.0+auto.0"
26
35
27
36
class RGBLED :
28
37
"""
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.
45
39
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):
47
41
48
42
.. code-block:: python
49
43
@@ -58,7 +52,7 @@ class RGBLED:
58
52
led = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
59
53
led.color = (255, 0, 0)
60
54
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):
62
56
63
57
.. code-block:: python
64
58
@@ -69,11 +63,11 @@ class RGBLED:
69
63
GREEN_LED = board.D6
70
64
BLUE_LED = board.D7
71
65
72
- # Create a RGB LED object
66
+ # Create an RGB LED object
73
67
led = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
74
68
led.color = 0x100000
75
69
76
- Example for setting a RGB LED using a ContextManager:
70
+ Example for setting an RGB LED using a ContextManager:
77
71
78
72
.. code-block:: python
79
73
@@ -91,9 +85,23 @@ class RGBLED:
91
85
with adafruit_rgbled.RGBLED(board.D5, board.D6, board.D7, invert_pwm=True) as rgb_led:
92
86
rgb_led.color = (0, 255, 0)
93
87
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.
94
96
"""
95
97
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 :
97
105
self ._rgb_led_pins = [red_pin , green_pin , blue_pin ]
98
106
for i in range ( # pylint: disable=consider-using-enumerate
99
107
len (self ._rgb_led_pins )
@@ -109,28 +117,41 @@ def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
109
117
self ._current_color = (0 , 0 , 0 )
110
118
self .color = self ._current_color
111
119
112
- def __enter__ (self ):
120
+ def __enter__ (self ) -> "RGBLED" :
113
121
return self
114
122
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 :
116
129
self .deinit ()
117
130
118
- def deinit (self ):
131
+ def deinit (self ) -> None :
119
132
"""Turn the LEDs off, deinit pwmout and release hardware resources."""
120
133
for pin in self ._rgb_led_pins :
121
134
pin .deinit () # pylint: disable=no-member
122
135
self ._current_color = (0 , 0 , 0 )
123
136
124
137
@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
+ """
127
151
return self ._current_color
128
152
129
153
@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 ):
134
155
self ._current_color = value
135
156
if isinstance (value , tuple ):
136
157
for i in range (0 , 3 ):
@@ -151,4 +172,4 @@ def color(self, value):
151
172
rgb [color ] -= 65535
152
173
self ._rgb_led_pins [color ].duty_cycle = abs (rgb [color ])
153
174
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." )
0 commit comments