def vol_rotary_callback(direction): if direction is 1: if sound.volume_up(): strip.colorBlinkPos(color=Color(0,50,0), pos=sound.volume/sound.volume_step) if direction is -1: if sound.volume_down(): strip.colorBlinkPos(color=Color(0,0,50), pos=sound.volume/sound.volume_step)
def color_wipe_cycle(pixels, isCancelled, wait=0.01, color=(255, 255, 255, 255), fade_step=50, loop_forever=True): step = pixels.numPixels() * max(abs(100 - fade_step), 1) / 50 step_w = color[0] / step step_r = color[1] / step step_g = color[2] / step step_b = color[3] / step while (True): for i in range(pixels.numPixels()): for j in range(i + 1): if (j < i - 1): w = int(max(0, color[0] - (i - j) * step_w)) r = int(max(0, color[1] - (i - j) * step_r)) g = int(max(0, color[2] - (i - j) * step_g)) b = int(max(0, color[3] - (i - j) * step_b)) pixels.setPixelColor(j, Color(r, g, b, w)) else: pixels.setPixelColor( j, Color(color[1], color[2], color[3], color[0])) pixels.show() if (isCancelled()): return time.sleep(wait) if (isCancelled()): return if (not loop_forever): return
def set_leds(shutdown): LED_COUNT = 5 LED_PIN = 13 # GPIO pin connected to the pixels (18 uses PWM!). LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) LED_DMA = 12 # DMA channel to use for generating signal (try 10) LED_BRIGHTNESS = 200 # Set to 0 for darkest and 255 for brightest LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) LED_CHANNEL = 1 # set to '1' for GPIOs 13, 19, 41, 45 or 53 strip = Adafruit_NeoPixel( LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, ) # Intialize the library (must be called once before other functions). strip.begin() if shutdown: color = Color(0, 0, 0) else: color = Color(255, 0, 255) for led in range(6): strip.setPixelColor(led, color) strip.show()
def color_wipe_rainbow(pixels, isCancelled, wait=0.01, fade_step=50, color_step=30): step = pixels.numPixels() * max(abs(100 - fade_step), 1) / 50 while (True): for k in range(256): cycle_color = wheelRGB( ((256 // pixels.numPixels() + k * color_step)) % 256) step_r = cycle_color[0] / step step_g = cycle_color[1] / step step_b = cycle_color[2] / step for i in range(pixels.numPixels()): for j in range(i + 1): if (j < i - 1): r = int(max(0, cycle_color[0] - (i - j) * step_r)) g = int(max(0, cycle_color[1] - (i - j) * step_g)) b = int(max(0, cycle_color[2] - (i - j) * step_b)) pixels.setPixelColor(j, Color(r, g, b)) else: pixels.setPixelColor( j, Color(cycle_color[0], cycle_color[1], cycle_color[2])) pixels.show() if (isCancelled()): return time.sleep(wait) if (isCancelled()): return
def setRedWhitePattern(lightStrip): for i in range(lightStrip.numPixels()): if i % 2 == 0: lightStrip.setPixelColor(i, Color(255, 0, 0)) else: lightStrip.setPixelColor(i, Color(255, 255, 255)) lightStrip.show()
def americaCycle(strip, wait_ms=500): "Now with 50% more Bald Eagle!" color_red = Color(255, 0, 0) color_white = Color(100, 100, 100) color_blue = Color(0, 0, 255) start_1 = 60 # num_pixels = 300 - start_1 num_pixels = strip.numPixels() - start_1 interval = (num_pixels // 3) start_2 = start_1 + interval # 140 start_3 = start_2 + interval # 220 for i in range(0, num_pixels): pixel_red = (start_1 + i) % strip.numPixels() pixel_white = (start_2 + i) % strip.numPixels() pixel_blue = (start_3 + i) % strip.numPixels() if pixel_red < start_1: pixel_red += start_1 if pixel_white < start_2: pixel_white += start_1 if pixel_blue < start_3: pixel_blue += start_1 strip.setPixelColor(pixel_red, color_red) strip.setPixelColor(pixel_white, color_white) strip.setPixelColor(pixel_blue, color_blue) # print('i: '+ format(i) + ' red: ' + format(pixel_red) + ' white: ' + format(pixel_white) + ' blue: ' + format(pixel_blue)) strip.show() time.sleep(wait_ms / 1000.0)
def read_midi_loop(): #open midi port inport = mido.open_input('mio MIDI 1') while True: try: for msg in inport.iter_pending(): note = msg.note if (msg.type == 'note_on'): velocity = msg.velocity elif (msg.type == 'note_off'): velocity = 0 note_offset = 0 #ledoff if (int(velocity) == 0 and int(note) > 0): strip.setPixelColor(((note - 28) * 2 - note_offset), Color(0, 0, 0)) #ledon elif (int(velocity) > 0 and int(note) > 0): #strip.setPixelColor(((note - 28)*2 - note_offset), Color(0, 0, 255)) #strip.setPixelColor(((note - 28)*2 - note_offset), Color(197, 11, 226)) #strip.setPixelColor(((note - 28)*2 - note_offset), Color(204, 0, 204)) strip.setPixelColor(((note - 28) * 2 - note_offset), Color(0, 255, 255)) else: print("what else?") strip.show() except AttributeError as error: print("Error expected") pass
def blink_leds(): global blink_active global blink_targets toggle = True bleep_time = 0.5 while blink_active: if toggle: color = Color(0, 0, 0) toggle = False else: color = Color(255, 0, 0) toggle = True targetLeds = [] for target in blink_targets: if target == "internet": targetLeds.append(7) if target == "gateway": targetLeds.append(3) if target == "local": targetLeds.append(0) for targetLed in targetLeds: strip.setPixelColor(targetLed, color) strip.show() time.sleep(bleep_time)
def meet_in_the_middle(strip, colour1=None, colour2=None, wait_ms=20): """Both ends go towards the middle, then bounce back away""" clear_strip(strip) colour1 = Color(255, 0, 0) if colour1 is None else colour1 colour2 = Color(0, 120, 0) if colour2 is None else colour2 logger.debug(colour1) logger.debug(colour2) num_leds = strip.numPixels() halfway = num_leds // 2 while which_effect == "meet_in_middle": for led in range(halfway): strip.setPixelColor(led, colour1) strip.setPixelColor(num_leds - led, colour2) if which_effect != "meet_in_middle": return strip.show() time.sleep(wait_ms / 1000.0) for led in range(halfway, 0, -1): strip.setPixelColor(led, Color(0, 0, 0)) strip.setPixelColor(num_leds - led, Color(0, 0, 0)) if which_effect != "meet_in_middle": return strip.show() time.sleep(wait_ms / 1000.0)
def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'Hello, world!') query_components = parse_qs(urlparse(self.path).query) if query_components['cmnd'][0] == 'Power': if query_components['power'][0] == 'off': allOneColor(strip, Color(0, 0, 0, 0)) if query_components['power'][0] == 'on': allOneColor( strip, Color(self.last_green, self.last_red, self.last_blue, self.last_white)) if query_components['cmnd'][0] == 'color': self.last_red = int(query_components['r'][0]) self.last_green = int(query_components['g'][0]) self.last_blue = int(query_components['b'][0]) self.last_white = int(query_components['w'][0]) allOneColor( strip, Color(self.last_green, self.last_red, self.last_blue, self.last_white)) if query_components['cmnd'][0] == 'effects': if query_components['effect'][0] == 'christmas': setChristmasLights(strip)
def show_time(self, strip, fg, bg): fg_color = Color(fg["r"], fg["g"], fg["b"]) bg_color = Color(bg["r"], bg["g"], bg["b"]) clock = ClockThread(strip, fg_color=fg_color, bg_color=bg_color) clock.start() return clock
def draw_car(strip, dist, color): strip.setPixelColor(dist - 1, Color(0, 0, 0)) strip.setPixelColor(dist - 2, Color(0, 0, 0)) strip.setPixelColor(dist - 3, Color(0, 0, 0)) for i in range(5): strip.setPixelColor(i + dist, color) strip.show()
def appear_from_back(pixels, isCancelled, color=(0, 255, 0, 255), wait=0.02, size=3): clear(pixels) while (not isCancelled()): for i in range(int(pixels.numPixels() / size)): for j in reversed(range(i * size, pixels.numPixels() - size)): if (isCancelled()): return clear(pixels, False) # first set all pixels at the begin for k in range(i * size): pixels.setPixelColor( k, Color(color[1], color[2], color[3], color[0])) # set then the pixel at position j for l in range(size): pixels.setPixelColor( j + l, Color(color[1], color[2], color[3], color[0])) if (isCancelled()): return pixels.show() time.sleep(wait) if (isCancelled()): return
def seq_chaser(): global seq_number if (seq_number >= LEDCOUNT): seq_number = 0 for x in range(LEDCOUNT): strip.setPixelColor(x, Color(0, 0, 0)) strip.setPixelColor(seq_number, Color(255, 255, 255)) strip.show()
def strobe(strip): while True: fill(strip, Color(255, 255, 255)) fill(strip, Color(0, 0, 0)) time.sleep(1 / 11) #time.sleep(1/(((sin(i/10) + 1)/2 * 3) + 4)) #time.sleep((sin(i/10) + 1)/2 * 0.2) yield strip
def running_lights(strip, color=Color(255, 255, 255), dynamic_color=False, wave_delay=50, rperiod=0.5, ramplitude=127, rshift=50, gperiod=0.5, gamplitude=127, gshift=50, bperiod=0.5, bamplitude=127, bshift=50, ): """ strip = object rpi-ws281x PixelStrip color = object rpi-ws281x Color for the color of the eyes dynamic_color = boolean change the color of the waves every advance forward wave_delay = int milliseconds before moving wave forward To create a red white and blue sin wave use these settings: red = 255, period = 1.0, shift = 50 green = 255, period = 0.5, shift = 50 blue = 255, period = 0.5, shift = 50 """ def color_int(i, position, rgb, period=1, amplitude=127, shift=128): """ i = int pixel along strip position = int how many times the wave has been moved forward period = float alter period of the wave amplitude = float alter the amplitude of the wave shift = float move the entire wave up or down relative to zero line """ # val = int(round(((math.sin(i + position) * 127 + 128) / 255) * red, 0)) # period = 1 # larger is shorter period, default = 1 # amplitude = 127 # larger is bigger amplitude, default = 127 # shift = 128 # higher moves entire wave up relative to zero line, default = 128 val = int(round(((math.sin((i + position) * period) * amplitude + shift) / 255) * rgb, 0)) # always return a value between 0 and 255 if val < 0: return 0 elif val > 255: return 255 else: return val FillSolid(strip, black) red = (color & 0x00ff0000) >> 16 green = (color & 0x0000ff00) >> 8 blue = (color & 0x000000ff) position = 0 for j in range(strip.numPixels() * 1): position += 1 if dynamic_color: red = Random().randint(0, 255) green = Random().randint(0, 255) blue = Random().randint(0, 255) for i in range(strip.numPixels()): strip.setPixelColor(i, Color( color_int(i, position, red, period=rperiod, amplitude=ramplitude, shift=rshift), color_int(i, position, green, period=gperiod, amplitude=gamplitude, shift=gshift), color_int(i, position, blue, period=bperiod, amplitude=bamplitude, shift=bshift) )) strip.show() time.sleep(wave_delay / 1000)
def wheel(pos): if pos < 85: return Color(pos * 3, 255 - pos * 3, 0) elif pos < 170: pos -= 85 return Color(255 - pos * 3, 0, pos * 3) else: pos -= 170 return Color(0, pos * 3, 255 - pos * 3)
def clean_last(strip, dist, laps): if dist > 295: laps = laps + 1 dist = 5 strip.setPixelColor(295, Color(0, 0, 0)) strip.setPixelColor(296, Color(0, 0, 0)) strip.setPixelColor(297, Color(0, 0, 0)) strip.setPixelColor(298, Color(0, 0, 0)) strip.setPixelColor(299, Color(0, 0, 0))
def twinkle(strip, r, g, b, ms=10): """Twinkle twinkle little star.""" for i in range(strip.numPixels()): strip.setPixelColor(randint(0, strip.numPixels()), Color(r, g, b)) for i in range(strip.numPixels() / 2): rgb = randomRGB() strip.setPixelColor(randint(0, strip.numPixels()), Color(0, 0, 0)) strip.show() time.sleep(ms / 1000.0)
def scannerBack(strip, r, g, b, ms=50, eyeSize=10): """Simulates the eye of Cylon but going back""" setColor(strip, 0, 0, 0) for i in range(strip.numPixels() - eyeSize, 0, -1): for j in range(1, eyeSize): strip.setPixelColor(i + j, Color(r, g, b)) strip.setPixelColor(i + j, Color(0, 0, 0)) strip.show() time.sleep(ms / 1000.0)
def twilight_wheel2(pos): if pos <= 85: return Color(pos, 255 - pos * 3, 255) elif pos <= 170: pos -= 85 return Color(255 - pos * 3, 0, 255) else: pos -= 170 return Color(0, pos * 3, 255)
def setRedGreenSkipPattern(lightStrip): for i in range(lightStrip.numPixels()): if i % 4 == 0: lightStrip.setPixelColor(i, Color(255, 0, 0)) elif i % 2 == 0: lightStrip.setPixelColor(i, Color(0, 255, 0)) else: lightStrip.setPixelColor(i, Color(0, 0, 0)) lightStrip.show()
def labyrinth(pixels, wait=0.05, count=5, color=(0, 0, 255, 0), contact_color=(127, 127, 127, 0), turn_chance=2): points = [] points_location = {} points_contact = {} for i in range(count): start = randint(0, pixels.numPixels()) velocity = randint(0, 1) if (velocity == 0): velocity = -1 points.append(Point(start, velocity)) while (True): for i in range(len(points)): # Clear if (not points[i].x in points_contact): pixels.setPixelColor(points[i].x, Color(0, 0, 0, 0)) # Next move velocity = randint(0, 100) if (velocity <= turn_chance): points[i].x_v *= -1 points[i].x += points[i].x_v points[i].x %= pixels.numPixels() if points[i].x in points_location: points_location[points[i].x] += 1 else: points_location[points[i].x] = 1 if (points[i].x + points[i].x_v) in points_location: points_location[points[i].x + points[i].x_v] += 1 else: points_location[points[i].x + points[i].x_v] = 1 # Show pixels.setPixelColor(points[i].x, Color(color[0], color[1], color[2], color[3])) for key, value in points_location.items(): if (value > 1): points_contact[key] = 1 for key in list(points_contact.keys()): value = points_contact[key] pixels.setPixelColor( key, Color(int(contact_color[0] * value), int(contact_color[1] * value), int(contact_color[2] * value), int(contact_color[3] * value))) points_contact[key] = round(points_contact[key] - 0.05, 2) if (points_contact[key] < 0): points_contact.pop(key) pixels.show() points_location.clear() time.sleep(wait)
def run(self): while True: if pause_thread is False: procc = subprocess.Popen(cmd_music, stdout=subprocess.PIPE) rep = procc.communicate()[0].decode('utf-8') if ("RUNNING" in rep) : colorWipe(strip, Color(251,251,251), 0) else : colorWipe(strip, Color(0,0,0), 0) time.sleep(1)
def colorfulScannerBack(strip, ms=50, eyeSize=10): """Simulates the eye of Cylon but as if a little kid drew it but going back""" setColor(strip, 0, 0, 0) for i in range(strip.numPixels() - eyeSize, 0, -1): for j in range(1, eyeSize): rgb = randomRGB() strip.setPixelColor(i + j, Color(rgb[0], rgb[1], rgb[2])) strip.setPixelColor(i + j, Color(0, 0, 0)) strip.show() time.sleep(ms / 1000.0)
def make_button(led, value): if(value == 0): color = Color(0,0,0) if(value == 1): color = Color(0,255,0) if(value == 2): color = Color(255,0,0) button = Button(color, led, value) return button
def set_get_pixel_color_rgb(pixel_strip, pixel, r, g, b): pixel_strip.setPixelColorRGB(pixel, r, g, b) assert pixel_strip._led_buffer[pixel] == Color(r, g, b) pixel_strip.show() assert pixel_strip._leds[pixel] == Color(r, g, b) assert pixel_strip.getPixelColor(pixel) == Color(r, g, b) c = pixel_strip.getPixelColorRGB(pixel) assert c.r == r assert c.g == g assert c.b == b
def wheel(pos): """Generate rainbow colors across 0-255 positions.""" if pos < 85: return Color(pos * 3, 255 - pos * 3, 0) elif pos < 170: pos -= 85 return Color(255 - pos * 3, 0, pos * 3) else: pos -= 170 return Color(0, pos * 3, 255 - pos * 3)
def wheel(pos): """生成跨越0-255个位置的彩虹颜色.""" if pos < 85: return Color(pos * 3, 255 - pos * 3, 0) elif pos < 170: pos -= 85 return Color(255 - pos * 3, 0, pos * 3) else: pos -= 170 return Color(0, pos * 3, 255 - pos * 3)
def breath_chase(self, wait_ms=3): for i in range(0, 255, 3): now_color = Color(i, 0, 0) # 所有灯珠设置一遍 self.colorWipe(now_color, 0) # 延时 time.sleep(wait_ms / 1000.0) for i in range(255, 0, -3): now_color = Color(i, 0, 0) self.colorWipe(now_color, 0) time.sleep(wait_ms / 1000.0)