def __init__(self, num, pin, freq_hz=800000, dma=5, invert=False, channel=0): """Class to represent a NeoPixel/WS281x LED display. Num should be the number of pixels in the display, and pin should be the GPIO pin connected to the display signal line (must be a PWM pin like 18!). Optional parameters are freq, the frequency of the display signal in hertz (default 800khz), dma, the DMA channel to use (default 5), invert, a boolean specifying if the signal line should be inverted (default False), and channel, the PWM channel to use (defaults to 0). """ # Create ws2811_t structure and fill in parameters. self._leds = ws.new_ws2811_t() # Initialize the channels to zero for channum in range(2): chan = ws.ws2811_channel_get(self._leds, channum) ws.ws2811_channel_t_count_set(chan, 0) ws.ws2811_channel_t_gpionum_set(chan, 0) ws.ws2811_channel_t_invert_set(chan, 0) # Initialize the channel in use self._channel = ws.ws2811_channel_get(self._leds, channel) ws.ws2811_channel_t_count_set(self._channel, num) ws.ws2811_channel_t_gpionum_set(self._channel, pin) ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1) # Initialize the controller ws.ws2811_t_freq_set(self._leds, freq_hz) ws.ws2811_t_dmanum_set(self._leds, dma) # Grab the led data array. self._led_data = _LED_Data(self._channel, num) # Start at full brightness. self._brightness = 0
def colorChange(color): print(color) # LED configuration. LED_CHANNEL = 0 LED_COUNT = 16 # How many LEDs to light. LED_FREQ_HZ = 800000 # Frequency of the LED signal. Should be 800khz or 400khz. LED_DMA_NUM = 5 # DMA channel to use, can be 0-14. LED_GPIO = 18 # GPIO connected to the LED signal line. Must support PWM! LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest LED_INVERT = 0 # Set to 1 to invert the LED signal, good if using NPN # transistor as a 3.3V->5V level converter. Keep at 0 # for a normal/non-inverted signal. leds = ws.new_ws2811_t() # Initialize all channels to off for channum in range(2): channel = ws.ws2811_channel_get(leds, channum) ws.ws2811_channel_t_count_set(channel, 0) ws.ws2811_channel_t_gpionum_set(channel, 0) ws.ws2811_channel_t_invert_set(channel, 0) ws.ws2811_channel_t_brightness_set(channel, 0) channel = ws.ws2811_channel_get(leds, LED_CHANNEL) ws.ws2811_channel_t_count_set(channel, LED_COUNT) ws.ws2811_channel_t_gpionum_set(channel, LED_GPIO) ws.ws2811_channel_t_invert_set(channel, LED_INVERT) ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS) ws.ws2811_t_freq_set(leds, LED_FREQ_HZ) ws.ws2811_t_dmanum_set(leds, LED_DMA_NUM) # Initialize library with LED configuration. resp = ws.ws2811_init(leds) if resp != 0: raise RuntimeError('ws2811_init failed with code {0}'.format(resp)) try: DOT_COLOR = int(color[1:7],16) # Update each LED color in the buffer. for i in range(LED_COUNT): # Pick a color based on LED position and an offset for animation. # Set the LED color buffer value. ws.ws2811_led_set(channel, i, DOT_COLOR) time.sleep(.15) # Send the LED color data to the hardware. resp = ws.ws2811_render(leds) if resp != 0: raise RuntimeError('ws2811_render failed with code {0}'.format(resp)) # Delay for a small period of time. time.sleep(.25) finally: # Ensure ws2811_fini is called before the program quits. ws.ws2811_fini(leds) # Example of calling delete function to clean up structure memory. Isn't # strictly necessary at the end of the program execution here, but is good practice. ws.delete_ws2811_t(leds)
def start(self, delegate, style): print("Setting up LED environment...") try: self.LED_BRIGHTNESS = style['brightness'] or 64 self.LED_COUNT = style['led_count'] or 630 self.leds = ws.new_ws2811_t() clean_up = style['cleanup'] == 1 # Initialize all channels to off for channum in range(2): self.channel = ws.ws2811_channel_get(self.leds, channum) ws.ws2811_channel_t_count_set(self.channel, 0) ws.ws2811_channel_t_gpionum_set(self.channel, 0) ws.ws2811_channel_t_invert_set(self.channel, 0) ws.ws2811_channel_t_brightness_set(self.channel, 0) self.channel = ws.ws2811_channel_get(self.leds, self.LED_CHANNEL) ws.ws2811_channel_t_count_set(self.channel, self.LED_COUNT) ws.ws2811_channel_t_gpionum_set(self.channel, self.LED_GPIO) ws.ws2811_channel_t_invert_set(self.channel, self.LED_INVERT) ws.ws2811_channel_t_brightness_set( self.channel, self.LED_BRIGHTNESS) ws.ws2811_t_freq_set(self.leds, self.LED_FREQ_HZ) ws.ws2811_t_dmanum_set(self.leds, self.LED_DMA_NUM) # Initialize library with LED configuration. resp = ws.ws2811_init(self.leds) if resp != 0: raise RuntimeError( 'ws2811_init failed with code {0}'.format(resp)) print( "Starting {0} via {1}...".format( style['style_name'], style['method_name']) ) delegate(style) finally: if clean_up: self.cleanup() ws.ws2811_fini(self.leds) ws.delete_ws2811_t(self.leds)
def __init__(self, num, pin, freq_hz=800000, dma=5, invert=False): """Class to represent a NeoPixel/WS281x LED display. Num should be the number of pixels in the display, and pin should be the GPIO pin connected to the display signal line (must be a PWM pin like 18!). Optional parameters are freq, the frequency of the display signal in hertz (default 800khz), dma, the DMA channel to use (default 5), and invert, a boolean specifying if the signal line should be inverted (default False). """ # Create ws2811_t structure and fill in parameters. self._leds = ws.new_ws2811_t() ws.ws2811_t_count_set(self._leds, num) ws.ws2811_t_freq_set(self._leds, freq_hz) ws.ws2811_t_dmanum_set(self._leds, dma) ws.ws2811_t_gpionum_set(self._leds, pin) ws.ws2811_t_invert_set(self._leds, 0 if not invert else 1) # Create led data array. self._led_data = _LED_Data(num) # Start at full brightness. self._brightness = 0
def __init__(self, num, pin, freq_hz=800000, dma=10, invert=False, brightness=255, channel=0, strip_type=ws.WS2811_STRIP_RGB): """Class to represent a NeoPixel/WS281x LED display. Num should be the number of pixels in the display, and pin should be the GPIO pin connected to the display signal line (must be a PWM pin like 18!). Optional parameters are freq, the frequency of the display signal in hertz (default 800khz), dma, the DMA channel to use (default 10), invert, a boolean specifying if the signal line should be inverted (default False), and channel, the PWM channel to use (defaults to 0). """ # Create ws2811_t structure and fill in parameters. self._leds = ws.new_ws2811_t() # Initialize the channels to zero for channum in range(2): chan = ws.ws2811_channel_get(self._leds, channum) ws.ws2811_channel_t_count_set(chan, 0) ws.ws2811_channel_t_gpionum_set(chan, 0) ws.ws2811_channel_t_invert_set(chan, 0) ws.ws2811_channel_t_brightness_set(chan, 0) # Initialize the channel in use self._channel = ws.ws2811_channel_get(self._leds, channel) ws.ws2811_channel_t_count_set(self._channel, num) ws.ws2811_channel_t_gpionum_set(self._channel, pin) ws.ws2811_channel_t_invert_set(self._channel, 0 if not invert else 1) ws.ws2811_channel_t_brightness_set(self._channel, brightness) ws.ws2811_channel_t_strip_type_set(self._channel, strip_type) # Initialize the controller ws.ws2811_t_freq_set(self._leds, freq_hz) ws.ws2811_t_dmanum_set(self._leds, dma) # Grab the led data array. self._led_data = _LED_Data(self._channel, num) # Substitute for __del__, traps an exit condition and cleans up properly atexit.register(self._cleanup)
0x201000, # orange 0x202000, # yellow 0x002000, # green 0x002020, # lightblue 0x000020, # blue 0x100010, # purple 0x200010 ] # pink # Create a ws2811_t structure from the LED configuration. # Note that this structure will be created on the heap so you need to be careful # that you delete its memory by calling delete_ws2811_t when it's not needed. leds = ws.new_ws2811_t() ws.ws2811_t_count_set(leds, LED_COUNT) ws.ws2811_t_freq_set(leds, LED_FREQ_HZ) ws.ws2811_t_dmanum_set(leds, LED_DMA_NUM) ws.ws2811_t_gpionum_set(leds, LED_GPIO) ws.ws2811_t_invert_set(leds, LED_INVERT) # Create an array of LED data. You don't need to clean this up because the fini # function will free it automatically. led_data = ws.new_led_data(LED_COUNT) # Initialize library with LED configuration. resp = ws.ws2811_init(leds) if resp != 0: raise RuntimeError('ws2811_init failed with code {0}'.format(resp)) # Wrap following code in a try/finally to ensure cleanup functions are called # after library is initialized. try:
for channum in range(2): channel = ws.ws2811_channel_get(leds, channum) ws.ws2811_channel_t_count_set(channel, 0) ws.ws2811_channel_t_gpionum_set(channel, 0) ws.ws2811_channel_t_invert_set(channel, 0) ws.ws2811_channel_t_brightness_set(channel, 0) channel = ws.ws2811_channel_get(leds, LED_CHANNEL) ws.ws2811_channel_t_count_set(channel, LED_COUNT) ws.ws2811_channel_t_gpionum_set(channel, LED_GPIO) ws.ws2811_channel_t_invert_set(channel, LED_INVERT) ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS) ws.ws2811_t_freq_set(leds, LED_FREQ_HZ) ws.ws2811_t_dmanum_set(leds, LED_DMA_NUM) # Initialize library with LED configuration. resp = ws.ws2811_init(leds) if resp != ws.WS2811_SUCCESS: message = ws.ws2811_get_return_t_str(resp) raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, message)) # Wrap following code in a try/finally to ensure cleanup functions are called # after library is initialized. try: offset = 0 while True: # Update each LED color in the buffer. for i in range(LED_COUNT): # Pick a color based on LED position and an offset for animation.
def neopixel_write(gpio, buf): """NeoPixel Writing Function""" global _led_strip # we'll have one strip we init if its not at first if _led_strip is None: # Create a ws2811_t structure from the LED configuration. # Note that this structure will be created on the heap so you # need to be careful that you delete its memory by calling # delete_ws2811_t when it's not needed. _led_strip = ws.new_ws2811_t() # Initialize all channels to off for channum in range(2): channel = ws.ws2811_channel_get(_led_strip, channum) ws.ws2811_channel_t_count_set(channel, 0) ws.ws2811_channel_t_gpionum_set(channel, 0) ws.ws2811_channel_t_invert_set(channel, 0) ws.ws2811_channel_t_brightness_set(channel, 0) channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL) # Initialize the channel in use count = 0 if len(buf) % 3 == 0: # most common, divisible by 3 is likely RGB LED_STRIP = ws.WS2811_STRIP_RGB count = len(buf) // 3 elif len(buf) % 4 == 0: LED_STRIP = ws.SK6812_STRIP_RGBW count = len(buf) // 4 else: raise RuntimeError("We only support 3 or 4 bytes-per-pixel") ws.ws2811_channel_t_count_set( channel, count) # we manage 4 vs 3 bytes in the library ws.ws2811_channel_t_gpionum_set(channel, gpio._pin.id) ws.ws2811_channel_t_invert_set(channel, LED_INVERT) ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS) ws.ws2811_channel_t_strip_type_set(channel, LED_STRIP) # Initialize the controller ws.ws2811_t_freq_set(_led_strip, LED_FREQ_HZ) ws.ws2811_t_dmanum_set(_led_strip, LED_DMA_NUM) resp = ws.ws2811_init(_led_strip) if resp != ws.WS2811_SUCCESS: if resp == -5: raise RuntimeError( "NeoPixel support requires running with sudo, please try again!" ) message = ws.ws2811_get_return_t_str(resp) raise RuntimeError("ws2811_init failed with code {0} ({1})".format( resp, message)) atexit.register(neopixel_cleanup) channel = ws.ws2811_channel_get(_led_strip, LED_CHANNEL) if gpio._pin.id != ws.ws2811_channel_t_gpionum_get(channel): raise RuntimeError( "Raspberry Pi neopixel support is for one strip only!") if ws.ws2811_channel_t_strip_type_get(channel) == ws.WS2811_STRIP_RGB: bpp = 3 else: bpp = 4 # assign all colors! for i in range(len(buf) // bpp): r = buf[bpp * i] g = buf[bpp * i + 1] b = buf[bpp * i + 2] if bpp == 3: pixel = (r << 16) | (g << 8) | b else: w = buf[bpp * i + 3] pixel = (w << 24) | (r << 16) | (g << 8) | b ws.ws2811_led_set(channel, i, pixel) resp = ws.ws2811_render(_led_strip) if resp != ws.WS2811_SUCCESS: message = ws.ws2811_get_return_t_str(resp) raise RuntimeError("ws2811_render failed with code {0} ({1})".format( resp, message)) time.sleep(0.001 * ((len(buf) // 100) + 1)) # about 1ms per 100 bytes