]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/neopixel.py
reformatted to comply with Black style
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / neopixel.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """BCM283x NeoPixel Driver Class"""
5 import time
6 import atexit
7 import _rpi_ws281x as ws
8
9 # LED configuration.
10 # pylint: disable=redefined-outer-name,too-many-branches,too-many-statements
11 # pylint: disable=global-statement,protected-access
12 LED_CHANNEL = 0
13 LED_FREQ_HZ = 800000  # Frequency of the LED signal.  We only support 800KHz
14 LED_DMA_NUM = 10  # DMA channel to use, can be 0-14.
15 LED_BRIGHTNESS = 255  # We manage the brightness in the neopixel library
16 LED_INVERT = 0  # We don't support inverted logic
17 LED_STRIP = None  # We manage the color order within the neopixel library
18
19 # a 'static' object that we will use to manage our PWM DMA channel
20 # we only support one LED strip per raspi
21 _led_strip = None
22 _buf = None
23
24
25 def neopixel_write(gpio, buf):
26     """NeoPixel Writing Function"""
27     global _led_strip  # we'll have one strip we init if its not at first
28     global _buf  # we save a reference to the buf, and if it changes we will cleanup and re-init.
29
30     if _led_strip is None or buf is not _buf:
31         # This is safe to call since it doesn't do anything if _led_strip is None
32         neopixel_cleanup()
33
34         # Create a ws2811_t structure from the LED configuration.
35         # Note that this structure will be created on the heap so you
36         # need to be careful that you delete its memory by calling
37         # delete_ws2811_t when it's not needed.
38         _led_strip = ws.new_ws2811_t()
39         _buf = buf
40
41         # Initialize all channels to off
42         for channum in range(2):
43             channel = ws.ws2811_channel_get(_led_strip, channum)
44             ws.ws2811_channel_t_count_set(channel, 0)
45             ws.ws2811_channel_t_gpionum_set(channel, 0)
46             ws.ws2811_channel_t_invert_set(channel, 0)
47             ws.ws2811_channel_t_brightness_set(channel, 0)
48
49         channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
50
51         # Initialize the channel in use
52         count = 0
53         if len(buf) % 3 == 0:
54             # most common, divisible by 3 is likely RGB
55             LED_STRIP = ws.WS2811_STRIP_RGB
56             count = len(buf) // 3
57         elif len(buf) % 4 == 0:
58             LED_STRIP = ws.SK6812_STRIP_RGBW
59             count = len(buf) // 4
60         else:
61             raise RuntimeError("We only support 3 or 4 bytes-per-pixel")
62
63         ws.ws2811_channel_t_count_set(
64             channel, count
65         )  # we manage 4 vs 3 bytes in the library
66         ws.ws2811_channel_t_gpionum_set(channel, gpio._pin.id)
67         ws.ws2811_channel_t_invert_set(channel, LED_INVERT)
68         ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS)
69         ws.ws2811_channel_t_strip_type_set(channel, LED_STRIP)
70
71         # Initialize the controller
72         ws.ws2811_t_freq_set(_led_strip, LED_FREQ_HZ)
73         ws.ws2811_t_dmanum_set(_led_strip, LED_DMA_NUM)
74
75         resp = ws.ws2811_init(_led_strip)
76         if resp != ws.WS2811_SUCCESS:
77             if resp == -5:
78                 raise RuntimeError(
79                     "NeoPixel support requires running with sudo, please try again!"
80                 )
81             message = ws.ws2811_get_return_t_str(resp)
82             raise RuntimeError(
83                 "ws2811_init failed with code {0} ({1})".format(resp, message)
84             )
85         atexit.register(neopixel_cleanup)
86
87     channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL)
88     if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel):
89         raise RuntimeError("Raspberry Pi neopixel support is for one strip only!")
90
91     if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB:
92         bpp = 3
93     else:
94         bpp = 4
95     # assign all colors!
96     for i in range(len(buf) // bpp):
97         r = buf[bpp * i]
98         g = buf[bpp * i + 1]
99         b = buf[bpp * i + 2]
100         if bpp == 3:
101             pixel = (r << 16) | (g << 8) | b
102         else:
103             w = buf[bpp * i + 3]
104             pixel = (w << 24) | (r << 16) | (g << 8) | b
105         ws.ws2811_led_set(channel, i, pixel)
106
107     resp = ws.ws2811_render(_led_strip)
108     if resp != ws.WS2811_SUCCESS:
109         message = ws.ws2811_get_return_t_str(resp)
110         raise RuntimeError(
111             "ws2811_render failed with code {0} ({1})".format(resp, message)
112         )
113     time.sleep(0.001 * ((len(buf) // 100) + 1))  # about 1ms per 100 bytes
114
115
116 def neopixel_cleanup():
117     """Cleanup when we're done"""
118     global _led_strip
119
120     if _led_strip is not None:
121         # Ensure ws2811_fini is called before the program quits.
122         ws.ws2811_fini(_led_strip)
123         # Example of calling delete function to clean up structure memory.  Isn't
124         # strictly necessary at the end of the program execution here, but is good practice.
125         ws.delete_ws2811_t(_led_strip)
126         _led_strip = None