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