def __del__(self): # Clean up memory used by the library when not needed anymore. if self._leds is not None: ws.ws2811_fini(self._leds) ws.delete_ws2811_t(self._leds) self._leds = None self._channel = None
def _cleanup(self): # Clean up memory used by the library when not needed anymore. if self._leds is not None: print("Exiting cleanly") ws.ws2811_fini(self._leds) ws.delete_ws2811_t(self._leds) self._leds = None self._channel = None
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 _cleanup(self): # Clean up memory used by the library when not needed anymore. if self._leds is not None: if self._init_successful: ws.ws2811_fini(self._leds) ws.delete_ws2811_t(self._leds) self._leds = None self._channel = None
def init(maxBrightness=10): global LED_COUNT global LED_BRIGHTNESS global LED_GPIO global CHANNEL global LEDS if (LEDS == False): LEDS = ws.new_ws2811_t() else: # 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) LEDS = ws.new_ws2811_t() SPLIT_LED_COUNT = [420, 402] LED_COUNT = 420 + 402 LED_BRIGHTNESS = maxBrightness LED_GPIO = [18, 13] CHANNEL = [] LED_CHANNEL = [0, 1] # 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. # 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.append(ws.ws2811_channel_get(LEDS, channum)) ws.ws2811_channel_t_count_set(CHANNEL[channum], SPLIT_LED_COUNT[channum]) ws.ws2811_channel_t_gpionum_set(CHANNEL[channum], LED_GPIO[channum]) ws.ws2811_channel_t_invert_set(CHANNEL[channum], LED_INVERT) ws.ws2811_channel_t_brightness_set(CHANNEL[channum], LED_BRIGHTNESS) ws.ws2811_channel_t_strip_type_set(CHANNEL[channum], LED_STRIP) 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)) fill({"R": 0, "G": 0, "B": 0}) render() print('Initialized ✓')
def neopixel_cleanup(): global _led_strip if _led_strip is not None: # Ensure ws2811_fini is called before the program quits. ws.ws2811_fini(_led_strip) # 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(_led_strip) _led_strip = None
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 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 set_leds(self, status): """ Set the color according to the humidity threshold In the optimal state when no threshold is reached the led is turned off. @param status: An integer array with 4 integers between 0 and 4 @type status: int[4] @return: None """ leds = ws.new_ws2811_t() channel = ws.ws2811_channel_get(leds, self.get_channel()) ws.ws2811_channel_t_count_set(channel, self.get_count()) ws.ws2811_channel_t_gpionum_set(channel, self.get_gpio()) ws.ws2811_channel_t_invert_set(channel, self.get_invert()) ws.ws2811_channel_t_brightness_set(channel, self.get_brightness()) ws.ws2811_t_freq_set(leds, self.get_freq()) ws.ws2811_t_dmanum_set(leds, self.get_dma()) try: resp = ws.ws2811_init(leds) except Exception as err: raise RuntimeError('ws2811_init failed with code {0} ({1}) {}', resp, err) finally: if resp != ws.WS2811_SUCCESS: ws.ws2811_get_return_t_str(resp) try: for i in range(self.get_count()): ws.ws2811_led_set(channel, i, self.get_color(status[i])) resp = ws.ws2811_render(leds) if resp != ws.WS2811_SUCCESS: ws.ws2811_get_return_t_str(resp) time.sleep(0.015) except Exception as err: raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, err)) finally: logging.getLogger().info("LED status:\t\t\t{}".format(status)) ws.ws2811_fini(leds) ws.delete_ws2811_t(leds)
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. color = DOT_COLORS[(i + offset) % len(DOT_COLORS)] # Set the LED color buffer value. ws.ws2811_led_set(channel, i, color) # 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(0.25) # Increase offset to animate colors moving. Will eventually overflow, which # is fine. offset += 1 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)
# Wrap following code in a try/finally to ensure cleanup functions are called # after library is initialized. try: # Set LED data array on the ws2811_t structure. Be sure to do this AFTER the # init function is called as it clears out the LEDs. ws.ws2811_t_leds_set(leds, led_data) # Loop forever or until ctrl-c is pressed. 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. color = DOT_COLORS[(i + offset) % len(DOT_COLORS)] # Set the LED color buffer value. ws.led_data_setitem(led_data, i, color) # 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(0.25) # Increase offset to animate colors moving. Will eventually overflow, which # is fine. offset += 1 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 ws2811_fini(*args): return _rpi_ws281x.ws2811_fini(*args)
def stop(self): self.colorwipe() self.render() ws.ws2811_fini(self.leds) ws.delete_ws2811_t(self.leds)
def ws2811_fini(ws2811): return _rpi_ws281x.ws2811_fini(ws2811)
def shutdown(leds): ws.ws2811_fini(leds) ws.delete_ws2811_t(leds)