def fuse(self, style): """ Perform a fuse burn, then explode! """ explode = False cycle_counter = 1 colors = GRB_Parser().convert(style['fuse_fire']) explode_colors = style['explosion'] or ['red', 'orange'] speed = style['speed'] offset = style['offset'] iterations = style['iterations'] direction = style['direction'] trail_colors = GRB_Parser().convert( style['fuse_unlit'] * (self.LED_COUNT - len(colors))) afuse = colors + trail_colors while iterations > 0: if cycle_counter % self.LED_COUNT != 0: cycle_counter += 1 # Set the LED color buffer value. for i in range(self.LED_COUNT): color = afuse[ (i + offset) % len(afuse) ] ws.ws2811_led_set(self.channel, i, color) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) time.sleep(speed) afuse = deque(afuse) afuse.rotate(direction) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) else: explode = True cycle_counter += 1 if explode: print('Exploding!') explode = False self.LED_BRIGHTNESS = style['explosion_brightness'], explosion = { 'style_name': style['style_name'], 'method_name': style['method_name'], 'css3_colors': explode_colors, 'speed': style['explosion_speed'], 'offset': 1, 'reverse_after': 500, 'iterations': 1, 'direction': 1, 'cleanup': 1, } self.LED_BRIGHTNESS = style['explosion_brightness'] self.party(explosion, True) cycle_counter += 1 iterations -= 1
def show(self, output_array): import _rpi_ws281x as ws # pylint: disable=import-error # Typecast the array to int output_array = output_array.clip(0, 255).astype(int) # sort the colors. grb g = np.left_shift(output_array[1][:].astype(int), 16) # pylint: disable=assignment-from-no-return r = np.left_shift(output_array[0][:].astype(int), 8) # pylint: disable=assignment-from-no-return b = output_array[2][:].astype(int) rgb = np.bitwise_or(np.bitwise_or(r, g), b).astype(int) # You can only use ws2811_leds_set with the custom version. #ws.ws2811_leds_set(self.channel, rgb) for i in range(self._led_count): ws.ws2811_led_set(self.channel, i, rgb[i].item()) resp = ws.ws2811_render(self._leds) 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))
def disp_image(duration, matrix, loop=False): thfactor = 360 // THDIV # Angle per division elapsed_time = 0 # Time elapsed in each iteration theta = 0 # Current angle of blades while duration > 0: theta += int(RPS * elapsed_time * 360) theta %= 360 elapsed_time = time.perf_counter() for i in range(LED_COUNT//2, LED_COUNT): color = matrix[((theta//thfactor) + (180//thfactor)) % THDIV][i - LED_COUNT//2] ws.ws2811_led_set(channel, i, color) for i in range(0, LED_COUNT//2): color = matrix[theta//thfactor][LED_COUNT//2 - i - 1] ws.ws2811_led_set(channel, i, color) render() time.sleep(0.002) elapsed_time = time.perf_counter() - elapsed_time if not loop: duration -= elapsed_time
def WipeColorLED(channel, color): """Wipe color across display a pixel at a time.""" for i in range(LED_COUNT): # Set the LED color buffer value. ws.ws2811_led_set(channel, i, color) # Send the LED color data to the hardware. ws.ws2811_render(leds)
def comet(self, style): print('COMET') cycle_counter = 0 colors = GRB_Parser().convert(style['comet_head']) speed = style['speed'] or random() offset = style['offset'] or 1 iterations = style['iterations'] or 1 direction = style['direction'] or 1 trail_colors = GRB_Parser().convert(style['comet_tail'] * (self.LED_COUNT - len(colors))) comet = colors + trail_colors while iterations > 0: for i in range(self.LED_COUNT): color = comet[(i + offset) % len(comet)] # Set the LED color buffer value. ws.ws2811_led_set(self.channel, i, color) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) # shift things comet = deque(comet) comet.rotate(direction) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) # Delay for a small period of time. time.sleep(speed) cycle_counter += 1 if cycle_counter % self.LED_COUNT == 0: iterations -= 1
def comet(self, style): print('COMET') cycle_counter = 0 colors = GRB_Parser().convert(style['comet_head']) speed = style['speed'] or random() offset = style['offset'] or 1 iterations = style['iterations'] or 1 direction = style['direction'] or 1 trail_colors = GRB_Parser().convert( style['comet_tail'] * (self.LED_COUNT - len(colors))) comet = colors + trail_colors while iterations > 0: for i in range(self.LED_COUNT): color = comet[ (i + offset) % len(comet) ] # Set the LED color buffer value. ws.ws2811_led_set(self.channel, i, color) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) # shift things comet = deque(comet) comet.rotate(direction) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) # Delay for a small period of time. time.sleep(speed) cycle_counter += 1 if cycle_counter % self.LED_COUNT == 0: iterations -= 1
def fuse(self, style): """ Perform a fuse burn, then explode! """ explode = False cycle_counter = 1 colors = GRB_Parser().convert(style['fuse_fire']) explode_colors = style['explosion'] or ['red', 'orange'] speed = style['speed'] offset = style['offset'] iterations = style['iterations'] direction = style['direction'] trail_colors = GRB_Parser().convert(style['fuse_unlit'] * (self.LED_COUNT - len(colors))) afuse = colors + trail_colors while iterations > 0: if cycle_counter % self.LED_COUNT != 0: cycle_counter += 1 # Set the LED color buffer value. for i in range(self.LED_COUNT): color = afuse[(i + offset) % len(afuse)] ws.ws2811_led_set(self.channel, i, color) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) time.sleep(speed) afuse = deque(afuse) afuse.rotate(direction) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) else: explode = True cycle_counter += 1 if explode: print('Exploding!') explode = False self.LED_BRIGHTNESS = style['explosion_brightness'], explosion = { 'style_name': style['style_name'], 'method_name': style['method_name'], 'css3_colors': explode_colors, 'speed': style['explosion_speed'], 'offset': 1, 'reverse_after': 500, 'iterations': 1, 'direction': 1, 'cleanup': 1, } self.LED_BRIGHTNESS = style['explosion_brightness'] self.party(explosion, True) cycle_counter += 1 iterations -= 1
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 setPixelColor(pixelIndex, color, brightness=100): global CHANNEL color['R'] = math.floor(color['R'] * brightness / 100) color['G'] = math.floor(color['G'] * brightness / 100) color['B'] = math.floor(color['B'] * brightness / 100) chan = CHANNEL[0] if pixelIndex < 420 else CHANNEL[1] pix = pixelIndex if pixelIndex < 420 else pixelIndex - 420 col = rgb2hex(color['R'], color['G'], color['B']) ws.ws2811_led_set(chan, pix, col)
def neopixel_write(gpio, buf): 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 ws.ws2811_channel_t_count_set(channel, math.ceil(len(buf)/3)) # 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: 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!") # assign all colors! for i in range(len(buf) // 3): r = buf[3*i] g = buf[3*i+1] b = buf[3*i+2] pixel = (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
def room_lighting(self, style): colors = GRB_Parser().convert(style['color']) # self.LED_BRIGHTNESS = style['brightness'] print('brightness: ', self.LED_BRIGHTNESS) print('senselight: ', style['senselight']) print('color: ', style['color']) for i in range(self.LED_COUNT): ws.ws2811_led_set(self.channel, i, colors[0]) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp))
def room_lighting_on(self, style): colors = GRB_Parser().convert(style['color']) # self.LED_BRIGHTNESS = style['brightness'] print('brightness: ', self.LED_BRIGHTNESS) print('senselight: ', style['senselight']) print('color: ', style['color']) for i in range(self.LED_COUNT): ws.ws2811_led_set(self.channel, i, colors[0]) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp))
def __setitem__(self, pos, value): """Set the 24-bit RGB color value at the provided position or slice of positions. """ # Handle if a slice of positions are passed in by setting the appropriate # LED data values to the provided values. if isinstance(pos, slice): index = 0 for n in range(pos.indices(self.size)): ws.ws2811_led_set(self.channel, n, value[index]) index += 1 # Else assume the passed in value is a number to the position. else: return ws.ws2811_led_set(self.channel, pos, value)
def fade(self, style): length = style['led_count'] speed = style['speed'] iterations = style['iterations'] or 1 fade_out_brightness, fade_in_brightness = style['fade_dim'], style[ 'fade_bright'] in_color, out_color = GRB_Parser().convert( style['fade_in']), GRB_Parser().convert(style['fade_out']) chain = [] div = int((out_color[0] - in_color[0]) / length) if div < 0: div *= -1 cnt = 0 for n in range(length): cnt += 1 chain.append(div * cnt) if n % length == 0: if n < 0: n = n * -1 brightness_direction = 1 brightness = 0 returns = 0 while iterations > 0: brightness += brightness_direction if (brightness >= fade_in_brightness - 1 and brightness_direction == 1) or (brightness <= fade_out_brightness + 1 and brightness_direction == -1): brightness_direction *= -1 returns += 1 for i in range(self.LED_COUNT): ws.ws2811_led_set(self.channel, i, chain[i]) ws.ws2811_channel_t_brightness_set(self.channel, brightness) # print(brightness) resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) time.sleep(speed) if returns == 1: iterations -= 1 returns = 0
def fade(self, style): length = style['led_count'] speed = style['speed'] iterations = style['iterations'] or 1 fade_out_brightness, fade_in_brightness = style[ 'fade_dim'], style['fade_bright'] in_color, out_color = GRB_Parser().convert( style['fade_in']), GRB_Parser().convert(style['fade_out']) chain = [] div = int((out_color[0] - in_color[0]) / length) if div < 0: div *= -1 cnt = 0 for n in range(length): cnt += 1 chain.append(div * cnt) if n % length == 0: if n < 0: n = n * -1 brightness_direction = 1 brightness = 0 returns = 0 while iterations > 0: brightness += brightness_direction if (brightness >= fade_in_brightness - 1 and brightness_direction == 1) or ( brightness <= fade_out_brightness + 1 and brightness_direction == -1): brightness_direction *= -1 returns += 1 for i in range(self.LED_COUNT): ws.ws2811_led_set(self.channel, i, chain[i]) ws.ws2811_channel_t_brightness_set(self.channel, brightness) # print(brightness) resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) time.sleep(speed) if returns == 1: iterations -= 1 returns = 0
def set(channel, leds, colorArray): position = 0 if not isinstance(colorArray[0], list): colorArray = [colorArray] for row in colorArray: for element in row: ws.ws2811_led_set(channel, position, ctypes.c_uint32(element).value) position += 1 resp = ws.ws2811_render(leds) if resp != ws.WS2811_SUCCESS: message = ws.ws2811_get_return_t_str(resp) raise RuntimeError('ws2811_render failed : {0} ({1})'.format( resp, message))
def typewriter(self, style): head = GRB_Parser().convert(style['head'] or ['red']) empty = GRB_Parser().convert(style['empty'] or ['black']) base = GRB_Parser().convert(style['base'] or ['blue']) length = style['led_count'] or 630 speed = style['speed'] or 0.05 iterations = style['iterations'] or 1 chain = head + (empty * (length - len(head))) head_pos = len(head) base_color = 0 direction = style['direction'] or 1 offset = style['offset'] or 1 cycle_counter = 1 while iterations > 0: for i in range(self.LED_COUNT): color = chain[ (i + offset) % len(list(chain)) ] ws.ws2811_led_set(self.channel, i, color) base_trail = [base[base_color]] * \ (length - len(list(chain)[0:head_pos])) chain = empty * \ (length - (len(head) + len(base_trail) - 2)) + \ head + base_trail time.sleep(speed) # print(list(chain)) resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) # print(' Head: ', head_pos, ' ', chain, end="\r", flush=True) chain = deque(chain) chain.rotate(direction) if head_pos == length - 1: head_pos = 0 head.reverse() direction *= -1 if base_color == len(base) - 1: base_color = 0 else: base_color += 1 else: head_pos += 1 if cycle_counter % length == 0: iterations -= 1
def party(self, style, internal=False): temp_led_count = 480 if internal: temp_led_count = int(self.LED_COUNT / 15) cycle_counter = 0 colors = GRB_Parser().convert(style['css3_colors']) speed = style['speed'] or random() offset = style['offset'] or 1 reverse_after = style['reverse_after'] or 0 iterations = style['iterations'] or 1 direction = style['direction'] or 1 while iterations > 0: # Update each LED color in the buffer. for i in range(self.LED_COUNT): # Pick a color based on LED position and an offset for # animation. color = colors[ (i + offset) % len(colors)] # Set the LED color buffer value. ws.ws2811_led_set(self.channel, i, color) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) # Delay for a small period of time. time.sleep(speed) # Increase offset to animate colors moving. Will eventually overflow, which # is fine. if offset > reverse_after or offset < (reverse_after * -1): direction *= -1 offset += direction # used to determine end of a task cycle_counter += 1 if not internal: if cycle_counter % self.LED_COUNT == 0: iterations -= 1 else: if cycle_counter % temp_led_count == 0: iterations -= 1
def party(self, style, internal=False): temp_led_count = 480 if internal: temp_led_count = int(self.LED_COUNT / 15) cycle_counter = 0 colors = GRB_Parser().convert(style['css3_colors']) speed = style['speed'] or random() offset = style['offset'] or 1 reverse_after = style['reverse_after'] or 0 iterations = style['iterations'] or 1 direction = style['direction'] or 1 while iterations > 0: # Update each LED color in the buffer. for i in range(self.LED_COUNT): # Pick a color based on LED position and an offset for # animation. color = colors[(i + offset) % len(colors)] # Set the LED color buffer value. ws.ws2811_led_set(self.channel, i, color) # Send the LED color data to the hardware. resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) # Delay for a small period of time. time.sleep(speed) # Increase offset to animate colors moving. Will eventually overflow, which # is fine. if offset > reverse_after or offset < (reverse_after * -1): direction *= -1 offset += direction # used to determine end of a task cycle_counter += 1 if not internal: if cycle_counter % self.LED_COUNT == 0: iterations -= 1 else: if cycle_counter % temp_led_count == 0: iterations -= 1
def typewriter(self, style): head = GRB_Parser().convert(style['head'] or ['red']) empty = GRB_Parser().convert(style['empty'] or ['black']) base = GRB_Parser().convert(style['base'] or ['blue']) length = style['led_count'] or 630 speed = style['speed'] or 0.05 iterations = style['iterations'] or 1 chain = head + (empty * (length - len(head))) head_pos = len(head) base_color = 0 direction = style['direction'] or 1 offset = style['offset'] or 1 cycle_counter = 1 while iterations > 0: for i in range(self.LED_COUNT): color = chain[(i + offset) % len(list(chain))] ws.ws2811_led_set(self.channel, i, color) base_trail = [base[base_color]] * \ (length - len(list(chain)[0:head_pos])) chain = empty * \ (length - (len(head) + len(base_trail) - 2)) + \ head + base_trail time.sleep(speed) # print(list(chain)) resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) # print(' Head: ', head_pos, ' ', chain, end="\r", flush=True) chain = deque(chain) chain.rotate(direction) if head_pos == length - 1: head_pos = 0 head.reverse() direction *= -1 if base_color == len(base) - 1: base_color = 0 else: base_color += 1 else: head_pos += 1 if cycle_counter % length == 0: iterations -= 1
def __setitem__(self, pos, value): """Set the 24-bit RGB color value at the provided position or slice of positions. """ # Handle if a slice of positions are passed in by setting the appropriate # LED data values to the provided values. if isinstance(pos, slice): pixels = pos.indices(self.size) ws.ws2811_led_set_multi_colors(pixels, value, 0, self.channum) # Else assume the passed in value is a number to the position. else: return ws.ws2811_led_set(pos, value, 0, self.channum)
def gradient(self, style): cycle_counter = 1 iterations = style['iterations'] length = style['led_count'] speed = style['speed'] direction = style['direction'] crange = style['color_range'] crange = GRB_Parser().convert(crange) div = (crange[1] - crange[0]) / self.LED_COUNT offset = 0 cnt = 0 chain = [] for n in range(length): cnt += 1 chain.append(int(div * cnt)) if n % length == 0: if n < 0: n = n * -1 while iterations > 0: cycle_counter += 1 chain = deque(chain) for i in range(self.LED_COUNT): color = chain[ (i + offset) % len(list(chain)) ] ws.ws2811_led_set(self.channel, i, color) resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) chain.rotate(direction) # print(list(chain), end="\r", flush=True) time.sleep(speed) if cycle_counter % length == 0: iterations -= 1
def gradient(self, style): cycle_counter = 1 iterations = style['iterations'] length = style['led_count'] speed = style['speed'] direction = style['direction'] crange = style['color_range'] crange = GRB_Parser().convert(crange) div = (crange[1] - crange[0]) / self.LED_COUNT offset = 0 cnt = 0 chain = [] for n in range(length): cnt += 1 chain.append(int(div * cnt)) if n % length == 0: if n < 0: n = n * -1 while iterations > 0: cycle_counter += 1 chain = deque(chain) for i in range(self.LED_COUNT): color = chain[(i + offset) % len(list(chain))] ws.ws2811_led_set(self.channel, i, color) resp = ws.ws2811_render(self.leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) chain.rotate(direction) # print(list(chain), end="\r", flush=True) time.sleep(speed) if cycle_counter % length == 0: iterations -= 1
def show(self, output_array): import _rpi_ws281x as ws # pylint: disable=import-error # Typecast the array to int. output_array = output_array.clip(0, 255).astype(int) # Check if we have a white channel or not. if len(output_array[:]) == 4 and "SK6812" in self._led_strip: # Sort the colors as RGB type. g = np.left_shift(output_array[1][:].astype(int), 24) # pylint: disable=assignment-from-no-return r = np.left_shift(output_array[0][:].astype(int), 16) # pylint: disable=assignment-from-no-return b = np.left_shift(output_array[2][:].astype(int), 8) # pylint: disable=assignment-from-no-return w = output_array[3][:].astype(int) grbw = np.bitwise_or(np.bitwise_or(np.bitwise_or(r, g), b), w).astype(int) # You can only use ws2811_leds_set with the custom version. for i in range(self._led_count): ws.ws2811_led_set(self.channel, i, int(grbw[i].item())) else: # Sort the colors as RGB type. g = np.left_shift(output_array[1][:].astype(int), 16) # pylint: disable=assignment-from-no-return r = np.left_shift(output_array[0][:].astype(int), 8) # pylint: disable=assignment-from-no-return b = output_array[2][:].astype(int) grb = np.bitwise_or(np.bitwise_or(r, g), b).astype(int) # You can only use ws2811_leds_set with the custom version. for i in range(self._led_count): ws.ws2811_led_set(self.channel, i, grb[i].item()) resp = ws.ws2811_render(self._leds) if resp != ws.WS2811_SUCCESS: message = ws.ws2811_get_return_t_str(resp) raise RuntimeError( f'ws2811_render failed with code {resp} ({message})')
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)
def cleanup(self): self.LED_BRIGHTNESS = 0 print('Cleaning up...') for i in range(self.LED_COUNT): ws.ws2811_led_set(self.channel, i, 0x000000) ws.ws2811_render(self.leds)
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: 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:
def ws2811_led_set(*args): return _rpi_ws281x.ws2811_led_set(*args)
def set_led(self, n, v): return ws.ws2811_led_set(self._channel, n % self.size, v)
def ws2811_led_set(channel, lednum, color): return _rpi_ws281x.ws2811_led_set(channel, lednum, color)
resp = ws.ws2811_init(leds) if resp != 0: raise RuntimeError('ws2811_init failed with code {0}'.format(resp)) try: offset = 0 while True: (fractionOfMinute, dummy) = modf(time() / 60.0) for i in range(LED_COUNT): p = i / float(LED_COUNT) # 0.0..1.0 by position on string q = p + fractionOfMinute while q > 1: q = q - 1.0 # normalize for overflow (r, g, b) = colorsys.hsv_to_rgb(q, 1.0, 1.0) ws.ws2811_led_set(channel, i, to_neopixel_color(r, g, b)) resp = ws.ws2811_render(leds) if resp != 0: raise RuntimeError( 'ws2811_render failed with code {0}'.format(resp)) sleep(0.2) finally: for i in range(LED_COUNT): ws.ws2811_led_set(channel, i, 0) resp = ws.ws2811_render(leds) if resp != 0: raise RuntimeError('ws2811_render failed with code {0}'.format(resp)) ws.ws2811_fini(leds)
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
resp = ws.ws2811_init(leds) if resp != 0: raise RuntimeError('ws2811_init failed with code {0}'.format(resp)) try: offset = 0 while True: (fractionOfMinute, dummy) = modf(time() / 60.0) for i in range(LED_COUNT): p = i / float(LED_COUNT) # 0.0..1.0 by position on string q = p + fractionOfMinute while(q > 1): q = q - 1.0 # normalize for overflow (r, g, b) = colorsys.hsv_to_rgb(q, 1.0, 1.0) ws.ws2811_led_set(channel, i, toNeoPixelColor(r, g, b)) resp = ws.ws2811_render(leds) if resp != 0: raise RuntimeError('ws2811_render failed with code {0}'. format(resp)) sleep(0.2) finally: for i in range(LED_COUNT): ws.ws2811_led_set(channel, i, 0) resp = ws.ws2811_render(leds) if resp != 0: raise RuntimeError('ws2811_render failed with code {0}'.format(resp)) ws.ws2811_fini(leds)
def set_pixel(self, x, y, color): color = ((color & 0xFF00) << 8) | ( (color & 0xFF0000) >> 8) | color & 0xFF ws.ws2811_led_set(self.channel, self.compute_index(x, y), color)
ws.ws2811_channel_t_brightness_set(channel, LED_BRIGHTNESS) ws.ws2811_channel_t_strip_type_set(channel, 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)) #Turn all LEDS off for i in range(LED_COUNT): ws.ws2811_led_set(channel, i, 0) resp = ws.ws2811_render(leds) time.sleep(0.25) #input = GPIO.input(17) #while not input # print ("waiting for button") # Wrap following code in a try/finally to ensure cleanup functions are called # after library is initialized. # Datagram (udp) socket try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
import _rpi_ws281x as ws 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, 0) ws.ws2811_channel_t_count_set(channel, 2) ws.ws2811_channel_t_gpionum_set(channel, 18) ws.ws2811_channel_t_invert_set(channel, 0) ws.ws2811_channel_t_brightness_set(channel, 128) ws.ws2811_t_freq_set(leds, 800000) ws.ws2811_t_dmanum_set(leds, 10) # Initialize library with LED configuration. resp = ws.ws2811_init(leds) if resp != 0: raise RuntimeError('ws2811_init failed with code {0}'.format(resp)) while True: ws.ws2811_led_set(channel, 0, 0x202020) ws.ws2811_led_set(channel, 1, 0x202020)
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. 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 != ws.WS2811_SUCCESS: message = ws.ws2811_get_return_t_str(resp) raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, message)) # 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:
def color_wipe(color, wait): for i in range(LED_COUNT//2): ws.ws2811_led_set(channel, LED_COUNT//2 - i - 1, color) ws.ws2811_led_set(channel, i + LED_COUNT//2, color) render() time.sleep(wait)
print("accepted connection") while True: data = conn.recv( 6120 ) # 6120 is number of bytes for an entire frame of the LED wall. if not data: break else: for i in range(0, len(data), 3): # Write to appropriate strip if index < LED_COUNT: ws.ws2811_led_set( channel, index, (ord(data[i + 1]) << 16) | (ord(data[i]) << 8) | ord(data[i + 2])) else: ws.ws2811_led_set(channel2, index - LED_COUNT, (ord(data[i + 1]) << 16) | (ord(data[i]) << 8) | ord(data[i + 2])) # Increment the pixel index index += 1 # Check whether end of frame has been reached and if so reset pixel index and show pixels if index == LED_COUNT + LED_2_COUNT: index = 0 # Send the LED color data to the hardware. resp = ws.ws2811_render(leds)