6
6
import atexit
7
7
import _rpi_ws281x as ws
8
8
9
+ try :
10
+ # Used only for typing
11
+ from typing import Optional
12
+ from digitalio import DigitalInOut
13
+ except ImportError :
14
+ pass
15
+
9
16
# LED configuration.
10
17
# pylint: disable=redefined-outer-name,too-many-branches,too-many-statements
11
18
# pylint: disable=global-statement,protected-access
12
- LED_CHANNEL = 0
13
19
LED_FREQ_HZ = 800000 # Frequency of the LED signal. We only support 800KHz
14
20
LED_DMA_NUM = 10 # DMA channel to use, can be 0-14.
15
21
LED_BRIGHTNESS = 255 # We manage the brightness in the neopixel library
19
25
# a 'static' object that we will use to manage our PWM DMA channel
20
26
# we only support one LED strip per raspi
21
27
_led_strip = None
22
- _buf = None
28
+ _buf : Optional [ bytearray ] = None
23
29
24
30
25
- def neopixel_write (gpio , buf ) :
31
+ def neopixel_write (gpio : DigitalInOut , buf : bytearray ) -> None :
26
32
"""NeoPixel Writing Function"""
27
33
global _led_strip # we'll have one strip we init if its not at first
28
34
global _buf # we save a reference to the buf, and if it changes we will cleanup and re-init.
@@ -46,7 +52,7 @@ def neopixel_write(gpio, buf):
46
52
ws .ws2811_channel_t_invert_set (channel , 0 )
47
53
ws .ws2811_channel_t_brightness_set (channel , 0 )
48
54
49
- channel = ws .ws2811_channel_get (_led_strip , LED_CHANNEL )
55
+ channel = ws .ws2811_channel_get (_led_strip , _neopixel_detect_channel ( gpio ) )
50
56
51
57
# Initialize the channel in use
52
58
count = 0
@@ -84,7 +90,7 @@ def neopixel_write(gpio, buf):
84
90
)
85
91
atexit .register (neopixel_cleanup )
86
92
87
- channel = ws .ws2811_channel_get (_led_strip , LED_CHANNEL )
93
+ channel = ws .ws2811_channel_get (_led_strip , _neopixel_detect_channel ( gpio ) )
88
94
if gpio ._pin .id != ws .ws2811_channel_t_gpionum_get (channel ):
89
95
raise RuntimeError ("Raspberry Pi neopixel support is for one strip only!" )
90
96
@@ -124,3 +130,10 @@ def neopixel_cleanup():
124
130
# strictly necessary at the end of the program execution here, but is good practice.
125
131
ws .delete_ws2811_t (_led_strip )
126
132
_led_strip = None
133
+
134
+
135
+ def _neopixel_detect_channel (gpio : DigitalInOut ) -> int :
136
+ """Detect the channel for a given GPIO, added for support PWM1 pins"""
137
+ if gpio ._pin .id in (13 , 19 , 41 , 45 ):
138
+ return 1
139
+ return 0
0 commit comments