20
20
21
21
* Adafruit's SimpleIO library: https://github.com/adafruit/Adafruit_CircuitPython_SimpleIO
22
22
"""
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
+
23
31
from pwmio import PWMOut
24
32
from simpleio import map_range
25
33
@@ -31,21 +39,6 @@ class RGBLED:
31
39
"""
32
40
Creates a RGBLED object given three physical pins or PWMOut objects.
33
41
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
-
49
42
Example for setting a RGB LED using a RGB Tuple (Red, Green, Blue):
50
43
51
44
.. code-block:: python
@@ -96,7 +89,23 @@ class RGBLED:
96
89
97
90
"""
98
91
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
+ """
100
109
self ._rgb_led_pins = [red_pin , green_pin , blue_pin ]
101
110
for i in range ( # pylint: disable=consider-using-enumerate
102
111
len (self ._rgb_led_pins )
@@ -112,27 +121,34 @@ def __init__(self, red_pin, green_pin, blue_pin, invert_pwm=False):
112
121
self ._current_color = (0 , 0 , 0 )
113
122
self .color = self ._current_color
114
123
115
- def __enter__ (self ):
124
+ def __enter__ (self ) -> "RGBLED" :
116
125
return self
117
126
118
- def __exit__ (self , exception_type , exception_value , traceback ):
127
+ def __exit__ (self , exception_type , exception_value , traceback ) -> None :
119
128
self .deinit ()
120
129
121
- def deinit (self ):
130
+ def deinit (self ) -> None :
122
131
"""Turn the LEDs off, deinit pwmout and release hardware resources."""
123
132
for pin in self ._rgb_led_pins :
124
133
pin .deinit () # pylint: disable=no-member
125
134
self ._current_color = (0 , 0 , 0 )
126
135
127
136
@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
+ """
130
142
return self ._current_color
131
143
132
144
@color .setter
133
- def color (self , value ):
145
+ def color (self , value : Union [ int , tuple ] ):
134
146
"""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.
136
152
"""
137
153
self ._current_color = value
138
154
if isinstance (value , tuple ):
@@ -154,4 +170,4 @@ def color(self, value):
154
170
rgb [color ] -= 65535
155
171
self ._rgb_led_pins [color ].duty_cycle = abs (rgb [color ])
156
172
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