def firefly(strip, duration=10, steps_up=10, steps_total=60, step_delay_ms=500, percent_on=0.15): """blinks lights randomly at percent of whole strip but with ramp up/down of randomized light color to simulate fireflies""" start = time.time() lights_on = [] lights_color_hue = {} lights_step = {} max_on = strip.numPixels() * percent_on while start + duration > time.time(): if len(lights_on) < max_on: next_on = randrange(strip.numPixels()) if next_on not in lights_on: lights_on.append(next_on) lights_color_hue[next_on] = random() lights_step[next_on] = 0 else: for i in range(strip.numPixels()): if i in lights_on: if lights_step[i] == steps_total: del lights_on[i] brightness = 0 if lights_step[i] <= steps_up: brightness = lights_step[i] / steps_up else: brightness = (steps_total - steps_up - lights_step[i]) / steps_total current_color = hsv_to_rgb(lights_color_hue[i], 1, brightness) current_int_color = Color(int(current_color[0]*255), int(current_color[1]*255), int(current_color[2]*255)) strip.setPixelColor(i, current_int_color) lights_step[next_on] = lights_step[next_on] + 1 else: strip.setPixelColor(i, Color(0,0,0)) strip.show() time.sleep(step_delay_ms/1000)
def Bin2Led_inferior(self, hexa, posicion, color, direccion, dif): ConvBin = bin(hexa) if len(ConvBin) != 10: self.ConvBin2 = ['0', '0', '0', '0', '0', '0', '0', '0'] for j in range(2, len(ConvBin)): self.ConvBin2[10 - len(ConvBin) + j - 2] = ConvBin[j] else: for i in range(0, 7): self.ConvBin2[i] = ConvBin[i + 2] # Los leds en la misma dirección if direccion == 0: for i in range(0, dif): #print(8 - dif + i) if self.ConvBin2[i] == '0': self.strip.setPixelColor(posicion + 8 - i, Color(0, 0, 0)) else: self.strip.setPixelColor(posicion + 8 - i, color) # Los leds en dirección opuesta else: for i in range(0, dif): if self.ConvBin2[i] == '0': self.strip.setPixelColor(posicion - 8 + i, Color(0, 0, 0)) else: self.strip.setPixelColor(posicion - 8 + i, color)
def drawFrame(self): # for y in range(self.clock_height-1, -1, -1): for y in range(self.clock_height - 1): for x in range(self.clock_width): nextv = ((((100.0 - self.pcnt) * self.matrixValue[y][x] + self.pcnt * self.matrixValue[y + 1][x]) / 100.0) - self.valueMask[y][x]) rgb = colorsys.hsv_to_rgb(self.hueMask[y][x] / 255.0, 1.0, max(0, nextv / 255.0)) r = int(rgb[0] * 255) g = int(rgb[1] * 255) b = int(rgb[2] * 255) self.stripFunctions.setColorBy2DCoordinates( x, y, Color(r, g, b)) for x in range(self.clock_width): nextv = ((((100.0 - self.pcnt) * self.matrixValue[self.clock_height - 1][x] + self.pcnt * self.line[x]) / 100.0) / 255) rgb = colorsys.hsv_to_rgb(self.hueMask[0][x] / 255.0, 1.0, nextv) r = int(rgb[0] * 255) g = int(rgb[1] * 255) b = int(rgb[2] * 255) self.stripFunctions.setColorBy2DCoordinates( x, self.clock_height - 1, Color(r, g, b))
def run_advent(): strip = get_strip() month = datetime.now().month # month = 12 # uncomment to test global stop_flag stop_flag = False log.info('advent started, stop_flag = ' + str(stop_flag)) for i in range(0, strip.numPixels(), 1): strip.setPixelColor(i, Color(0, 0, 0)) try: if month != 12: log.warn( 'Wrong month for xmas/advent animation, it\'s {0}!'.format( time.strftime("%B"))) while not stop_flag: theater_chase(strip, Color(0, 15, 0)) else: december_cycle(month, strip) except KeyboardInterrupt: log.warn("KeyboardInterrupt") color_wipe_full(strip, Color(0, 0, 0), 0) exit() except Exception as e: log.error("Any error occurs: " + str(e)) exit() log.info('advent run stopped') reset_strip(strip) return
def run(self): color = Color(self.r, self.g, self.b) off = Color(0, 0, 0) pixels = self.led_strip.num_pixels led_sleep_duration = float(self.duration) / float(pixels) i = 0 forwards = True while not self.should_stop: if i == 0 and not forwards: self.led_strip.strip.setPixelColor(i + 1, off) self.led_strip.strip.setPixelColor(i, color) forwards = True i += 1 elif i == pixels - 1: self.led_strip.strip.setPixelColor(i - 1, off) self.led_strip.strip.setPixelColor(i, color) forwards = False i -= 1 else: if forwards: self.led_strip.strip.setPixelColor(i - 1, off) self.led_strip.strip.setPixelColor(i, color) i += 1 else: self.led_strip.strip.setPixelColor(i + 1, off) self.led_strip.strip.setPixelColor(i, color) i -= 1 self.led_strip.strip.show() sleep(led_sleep_duration)
def createGradient(self, startColor, endColor, wait_sec=.05): ''' Gradient styling ''' rStep = 0 gStep = 0 bStep = 0 if (endColor['r'] - startColor['r']) > 0: rStep = int(floor((endColor['r'] - startColor['r']) / self.numLEDs())) else: rStep = int((endColor['r'] - startColor['r']) / self.numLEDs()) + 1 if (endColor['g'] - startColor['g']) > 0: gStep = int(floor((endColor['g'] - startColor['g']) / self.numLEDs())) else: gStep = int((endColor['g'] - startColor['g']) / self.numLEDs()) + 1 if (endColor['b'] - startColor['b']) > 0: bStep = int(floor((endColor['b'] - startColor['b']) / self.numLEDs())) else: bStep = int((endColor['b'] - startColor['b']) / self.numLEDs()) + 1 self._obj.setPixelColor(0, Color(startColor['g'], startColor['r'], startColor['b'])) for i in range(1, self.numLEDs() - 1): self._obj.setPixelColor(i,\ Color(startColor['g'] + (gStep * i), startColor['r'] + (rStep * i), startColor['b'] + (bStep * i))) self._obj.setPixelColor(self.numLEDs() - 1, Color(endColor['g'], endColor['r'], endColor['b'])) self._obj.show() time.sleep(wait_sec)
def light_keys_in_range(self, location): fastColorWipe(self.ledstrip.strip, True, self) color_counter = 0 for i in self.multicolor: start = self.multicolor_range[int(color_counter)][0] end = self.multicolor_range[int(color_counter)][1] if start > 92: note_offset_start = 2 elif start > 55: note_offset_start = 1 else: note_offset_start = 0 if end > 92: note_offset_end = 2 elif end > 55: note_offset_end = 1 else: note_offset_end = 0 red = self.multicolor[int(color_counter)][0] green = self.multicolor[int(color_counter)][1] blue = self.multicolor[int(color_counter)][2] self.ledstrip.strip.setPixelColor(int(((start - 20) * 2 - note_offset_start)), Color(int(green), int(red), int(blue))) self.ledstrip.strip.setPixelColor(int(((end - 20) * 2 - note_offset_end)), Color(int(green), int(red), int(blue))) color_counter += 1
def binaryTime(self, hue): # foreground color colorForeground = self.helpers.getRainbowColor( self.configuration.rainbow, hue) self.curr_fg_color = colorForeground backgroundNoSeconds = [1, 2, 3, 4, 7, 8, 9, 10, 13, 14, 15, 16, 19, 20, 21, 22] # set background color if self.configuration.background == 1: colorBackground = Color(self.configuration.background_value, self.configuration.background_value, self.configuration.background_value) else: colorBackground = Color(0, 0, 0) self.curr_bg_color = colorBackground # show / hide seconds if self.configuration.show_seconds == 1: for i in range(self.strip.numPixels()): self.stripFunctions.setColorBy1DCoordinate( i, colorBackground, flag_forClock=self.flag_clockIndent) else: for i in range(len(backgroundNoSeconds)): self.stripFunctions.setColorBy1DCoordinate( backgroundNoSeconds[i], colorBackground, flag_forClock=self.flag_clockIndent) # get current Time curTime = datetime.datetime.now() # separate date elements year = curTime.year month = curTime.month day = curTime.day # separate time elements hour = curTime.hour minute = curTime.minute second = curTime.second # new year countdown if day == 31 and month == 12 and hour == 23 and minute == 59 and second = 50: open(os.path.join(self.basePath, 'other', 'customText.txt'), 'w').close() with open(os.path.join(self.basePath, 'other', 'customText.txt'), "a") as myfile: myfile.write("10 9 8 7 6 5 4 3 2 1 0 Frohes Neues Jahr " + str(year + 1)) self.plugin2.showTimeAsText(fg_color=self.curr_fg_color, bg_color=self.curr_bg_color, fps=10)
def turnOff4(strip, wait_ms=50): """Turn off all LEDs.""" for i in range(strip.numPixels()): strip.setPixelColor(12, Color(0, 0, 0)) strip.setPixelColor(13, Color(0, 0, 0)) strip.setPixelColor(14, Color(0, 0, 0)) strip.setPixelColor(15, Color(0, 0, 0)) strip.show()
def turnOff5(strip, wait_ms=50): """Turn off all LEDs.""" for i in range(strip.numPixels()): strip.setPixelColor(16, Color(0, 0, 0)) strip.setPixelColor(17, Color(0, 0, 0)) strip.setPixelColor(18, Color(0, 0, 0)) strip.setPixelColor(19, Color(0, 0, 0)) strip.show()
def turnOff2(strip, wait_ms=50): """Turn off all LEDs.""" for i in range(strip.numPixels()): strip.setPixelColor(4, Color(0, 0, 0)) strip.setPixelColor(5, Color(0, 0, 0)) strip.setPixelColor(6, Color(0, 0, 0)) strip.setPixelColor(7, Color(0, 0, 0)) strip.show()
def turnOff3(strip, wait_ms=50): """Turn off all LEDs.""" for i in range(strip.numPixels()): strip.setPixelColor(8, Color(0, 0, 0)) strip.setPixelColor(9, Color(0, 0, 0)) strip.setPixelColor(10, Color(0, 0, 0)) strip.setPixelColor(11, Color(0, 0, 0)) strip.show()
def draw(self, strip): snake_len = len(self.snake) for i, pos in enumerate(self.snake): _set_pixel(strip, pos, Color(255 - (snake_len - i) * 20, 0, 0)) if self.to_clear: _set_pixel(strip, self.to_clear, Color(0, 0, 0)) if self.reward: _set_pixel(strip, self.reward, REWARD_COLORS[self._frame % len(REWARD_COLORS)])
def whiteLight5(strip, wait_ms=50): """Turn off all LEDs.""" for i in range(strip.numPixels()): strip.setPixelColor(16, Color(200, 255, 255)) strip.setPixelColor(17, Color(200, 255, 255)) strip.setPixelColor(18, Color(200, 255, 255)) strip.setPixelColor(19, Color(200, 255, 255)) strip.show() time.sleep(wait_ms / 1000.0)
def wheel(wheel_pos): wheel_pos = 255 - wheel_pos if wheel_pos < 85: return Color(255 - wheel_pos * 3, 0, wheel_pos * 3) if wheel_pos < 170: wheel_pos -= 85 return Color(0, wheel_pos * 3, 255 - wheel_pos * 3) wheel_pos -= 170 return Color(wheel_pos * 3, 255 - wheel_pos * 3, 0)
def twinkle_selector(self): global cur_mode if cur_mode != "Twinkle": return self._pixel.color_wipe( Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) self._pixel.twinkle( Color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
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 wall_route_on(): try: percent = int(request.args.get('p')) if (percent >= 0 and percent <= 100): intensity = int(percent * 255.0 / 100.0) neopixel_control.oneColor(Color(intensity, intensity, intensity)) else: neopixel_control.oneColor(Color(255, 255, 255)) except: neopixel_control.oneColor(Color(255, 255, 255)) return "ok"
def lightsOn(strip, four_on=False, wait_ms=50): if four_on: for i, four in zip(range(strip.numPixels()), four_on): print(i, four, 'on') strip.setPixelColor(four, Color(200, 255, 255)) strip.setPixelColor(four, Color(200, 255, 255)) strip.setPixelColor(four, Color(200, 255, 255)) strip.setPixelColor(four, Color(200, 255, 255)) strip.show() time.sleep(wait_ms / 1000.0)
def whiteLight(strip, wait_ms=50): """Turn off all LEDs.""" for i in range(strip.numPixels()): # strip.setPixelColor(0, Color(200, 255, 255)) # strip.setPixelColor(1, Color(200, 255, 255)) # strip.setPixelColor(2, Color(200, 255, 255)) # strip.setPixelColor(3, Color(200, 255, 255)) strip.setPixelColor(0, Color(0, 255, 0)) strip.setPixelColor(1, Color(0, 255, 0)) strip.setPixelColor(2, Color(0, 255, 0)) strip.setPixelColor(3, Color(0, 255, 0)) strip.show() time.sleep(wait_ms / 1000.0)
def no_run(self): num_points = 4 bulge = [] for i in reversed(range(1, num_points * 10, 10)): bulge.append(Color(self.r / i, self.g / i, self.b / i)) bulge.extend(bulge[1::-1]) for _ in range(self.led_strip.num_pixels - len(bulge)): bulge.append(Color(0, 0, 0)) self.led_strip.set_pattern(bulge)
def run_theater(): strip = get_strip() global stop_flag stop_flag = False log.info('theater started, stop_flag = ' + str(stop_flag)) for i in range(0, strip.numPixels(), 1): strip.setPixelColor(i, Color(0, 0, 0)) while not stop_flag: try: color_wipe_full(strip, Color(127, 0, 0)) # Green wipe if not stop_flag: color_wipe_full(strip, Color(0, 127, 0)) # Red wipe if not stop_flag: color_wipe_full(strip, Color(0, 0, 127)) # Blue wipe if not stop_flag: theater_chase(strip, Color(127, 127, 127)) # White theater chase if not stop_flag: theater_chase(strip, Color(0, 0, 127)) # Blue theater chase if not stop_flag: theater_chase(strip, Color(80, 0, 0)) # Green theater chase except KeyboardInterrupt: log.warn("KeyboardInterrupt") color_wipe_full(strip, Color(0, 0, 0), 0) exit() except Exception as e: log.error("Any error occurs: " + str(e)) exit() log.info('theater run stopped') reset_strip(strip) return
def magic_update(message, namespace): try: if 'mode' in message and 'element' in message: mode = message['mode'] if mode == "staticColor" or mode == "colorWheel" or mode == "rainbow" or mode == "rainbowCycle": data['element'] = message['element'] else: return if 'color' in message: data['color'] = message['color'] else: data['color'] = "none" if mode == "setGPIO": if 'pin' in message and 'pinValue' in message: setGPIO(message.pin, message.pinValue) elif mode == "power": if 'state' in message: if (message['state'] == 'on'): mysensors.uplightOn() elif (message['state'] == "off"): mysensors.uplightOff() if 'relay' in message: data['relays'][message['relay']] = message['state'] elif mode == "rainbow": neopixel_control.rainbow() elif mode == "rainbowCycle": neopixel_control.rainbowCycleThreaded() else: color = message['color'] color = color.lstrip('#') lv = len(color) rgbColor = tuple( int(color[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)) if mode == "staticColor": neopixel_control.oneColor( Color(rgbColor[0], rgbColor[1], rgbColor[2])) elif mode == "colorWheel": neopixel_control.theaterChaseThreaded( Color(rgbColor[0], rgbColor[1], rgbColor[2])) updateWebClients('mode', data) except: print "ERROR: updating the magic light failed"
def __init__(self, *args, **kwargs): super(LEDs, self).__init__(int(end_idx), 18, strip_type=ws.WS2811_STRIP_RGB) self.begin() self.brightness = 0.5 #0=black, 0.5=color, 1=white this is the absolute brightness, which is scaled by the routine self.color_nums = [ Color(0, 0, 0), Color(23, 45, 67), Color(0, 45, 1), Color(100, 45, 1) ] #color 0 should never change self.color_hues = [0, 77, 40, 99 ] #not actually used, defaults to color nums above
def run(self, wcd, wci): """ Displays time until aborted by user interaction on pin button_return """ while True: """Reset all LEDs""" wcd.setColorToAll(Color(0,0,0), includeMinutes=True) """Color wipe each LED to find the error""" for i in range(wcd.get_led_count()): wcd.setPixelColor(i, Color(255,0,0)) wcd.show() time.sleep(0.1) if wci.waitForEvent(0.02) >= 0: return time.sleep(10)
def drop(strip, R_color, G_color, B_color, wait_ms=100): for i in range(strip.numPixels() + 3): strip.setPixelColor(i, Color(R_color, G_color, B_color)) count = i fade_factor = 0.8 while (count > 0): count -= 1 strip.setPixelColor( count, Color(int(R_color * fade_factor), int(G_color * fade_factor), int(B_color * fade_factor))) fade_factor *= 0.6 strip.show() speed_factor = random.gauss(1, 0.2) time.sleep(speed_factor * wait_ms / 1000.0)
def lightsOff(strip, four_off=False, all_off=False, wait=50): if four_off: for i, off in zip(range(strip.numPixels()), four_off): print(i, off, 'off') strip.setPixelColor(off, Color(0, 0, 0)) strip.setPixelColor(off, Color(0, 0, 0)) strip.setPixelColor(off, Color(0, 0, 0)) strip.setPixelColor(off, Color(0, 0, 0)) strip.show() if all_off: for i in range(strip.numPixels()): strip.setPixelColor(i, Color(0, 0, 0)) strip.show()
def candle(strip, leds): for turns in range(leds): for i in range(leds): p = percent() strip.setPixelColor(i, Color(int(green * p), int(red * p), int(blue * p))) strip.show() time.sleep(randint(13, 15) / 100.0)
def fadeToBlack(self, pixel, fadeValue, pixelColor): oldColor = pixelColor if oldColor > 0: w = (oldColor & 0xff000000) >> 24 r = (oldColor & 0x00ff0000) >> 16 g = (oldColor & 0x0000ff00) >> 8 b = (oldColor & 0x000000ff) r = 0 if (r <= 10) else int(r - (r * fadeValue / 256)) g = 0 if (g <= 10) else int(g - (g * fadeValue / 256)) b = 0 if (b <= 10) else int(b - (b * fadeValue / 256)) return (pixel, Color(r, g, b)) else: return (pixel, Color(0, 0, 0))
def __init__(self, pixelstart, pixelstop, direction, amp=10): self.__name = "MeteorRainYellow" self.__color = Color(255, 255, 0) MeteorRain.__init__(self, pixelstart, pixelstop, direction, amp, self.__name, self.__color)