Skip to content

Commit 929eb5e

Browse files
authored
Merge pull request #931 from Roman3349/feature/neopixel_rpi_pwm1
neopixel_write: add support for PWM1 pins on Raspberry Pis
2 parents 2454308 + 75033f1 commit 929eb5e

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/adafruit_blinka/microcontroller/bcm283x/neopixel.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
import atexit
77
import _rpi_ws281x as ws
88

9+
try:
10+
# Used only for typing
11+
from typing import Optional
12+
from digitalio import DigitalInOut
13+
except ImportError:
14+
pass
15+
916
# LED configuration.
1017
# pylint: disable=redefined-outer-name,too-many-branches,too-many-statements
1118
# pylint: disable=global-statement,protected-access
12-
LED_CHANNEL = 0
1319
LED_FREQ_HZ = 800000 # Frequency of the LED signal. We only support 800KHz
1420
LED_DMA_NUM = 10 # DMA channel to use, can be 0-14.
1521
LED_BRIGHTNESS = 255 # We manage the brightness in the neopixel library
@@ -19,10 +25,10 @@
1925
# a 'static' object that we will use to manage our PWM DMA channel
2026
# we only support one LED strip per raspi
2127
_led_strip = None
22-
_buf = None
28+
_buf: Optional[bytearray] = None
2329

2430

25-
def neopixel_write(gpio, buf):
31+
def neopixel_write(gpio: DigitalInOut, buf: bytearray) -> None:
2632
"""NeoPixel Writing Function"""
2733
global _led_strip # we'll have one strip we init if its not at first
2834
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):
4652
ws.ws2811_channel_t_invert_set(channel, 0)
4753
ws.ws2811_channel_t_brightness_set(channel, 0)
4854

49-
channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
55+
channel = ws.ws2811_channel_get(_led_strip, _neopixel_detect_channel(gpio))
5056

5157
# Initialize the channel in use
5258
count = 0
@@ -84,7 +90,7 @@ def neopixel_write(gpio, buf):
8490
)
8591
atexit.register(neopixel_cleanup)
8692

87-
channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
93+
channel = ws.ws2811_channel_get(_led_strip, _neopixel_detect_channel(gpio))
8894
if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
8995
raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")
9096

@@ -124,3 +130,10 @@ def neopixel_cleanup():
124130
# strictly necessary at the end of the program execution here, but is good practice.
125131
ws.delete_ws2811_t(_led_strip)
126132
_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

Comments
 (0)