def strip_fade(r1, g1, b1, r2, g2, b2, frames=51): r_delta = (r2 - r1) // frames g_delta = (g2 - g1) // frames b_delta = (b2 - b1) // frames for i in range(frames): red = r1 + (r_delta * i) green = g1 + (g_delta * i) blue = b1 + (b_delta * i) strip_set(Color(red, green, blue)) time.sleep(wait_ms / 1000.0) strip_set(Color(r2, g2, b2))
def nowifi(): while True: for j in range(1): for q in reversed(range(7)): for i in range(strip.numPixels()): strip.setPixelColor(i + q, Color(251, 251, 251)) strip.show() time.sleep(50 / 500.0) for i in range(strip.numPixels()): strip.setPixelColor(i + q, Color(0, 0, 0, 0)) time.sleep(0.1)
def randomTwinkle(strip, ms=50): """Twinkle twinkle little star but with random colors!""" for i in range(strip.numPixels()): rgb = randomRGB() strip.setPixelColor(randint(0, strip.numPixels()), Color(rgb[0], rgb[1], rgb[2])) 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 pong_init(self): # reset for led in self: self.strip.setPixelColor(led, Color(0, 0, 0)) self.pointer = random.randint(0, len(self)) self.color = util.random_color() self.strip.setPixelColor(self[self.pointer], Color(255, 0, 0)) self.strip.show() self.direction = -1 if random.randint(0, 10) < 5 else 1
def _wheel(self, 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 _color_wheel(self, pos, offset=0): if offset != 0: pos = (pos + offset) & 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 thread_load_indicator(self, sharedVariable): """ Thread task, use utils.sharedVariable to indicate finish """ if (self.CONFIG["TOP_INSTALLED"]): accentColor = Color(136, 176, 75) # 2017 Greenery mainColor = Color(255, 0, 0) secondColor = Color(140, 0, 0) thirdColor = Color(60, 0, 0) baseValue = sharedVariable.get_storage() + 1 lowerBound = baseValue current_position = lowerBound upwards = True upperBound = 5 while (not sharedVariable.get_task_done()): if (sharedVariable.has_changed()): self.ilog(__name__ + " thread, new value!") baseValue = sharedVariable.get_storage() + 1 lowerBound = baseValue current_position = lowerBound for idx in range(self.STRING_CNT): if (idx < baseValue): self.top_row[idx] = accentColor else: self.top_row[idx] = Color(0, 0, 0) if (upwards): delta = current_position - baseValue if (delta > 0): self.top_row[current_position - 1] = secondColor if (delta > 1): self.top_row[current_position - 2] = thirdColor else: delta = upperBound - current_position if (delta > 0): self.top_row[current_position + 1] = secondColor if (delta > 1): self.top_row[current_position + 2] = thirdColor self.top_row[current_position] = mainColor self.applytop() if (upwards): #Todo inc or dec counter if (current_position == upperBound): upwards = False else: current_position = current_position + 1 else: if (current_position == lowerBound): upwards = True else: current_position = current_position - 1 else: self.colorwipeTop()
def phase(pos): #color = Color ( 128 * pos / strip.numPixels () , 0, 128 - (pos / strip.numPixels() * 255) ) if pos < 74: color = Color ( 0 , 0, 128 ) elif pos < 76: color = Color ( 0,255, 0 ) elif pos < 148: color = Color ( 128 , 0, 100 ) else: color = Color ( 0,255, 0 ) return color
def twinkler(): base_brightness = int(LED_BRIGHTNESS/4) delta_brightness = LED_BRIGHTNESS - base_brightness while True: for i in range(10): c = base_brightness + int(delta_brightness/10.0*i) yield Color(c, c, c) for i in range(9, -1, -1): c = base_brightness + int(delta_brightness/10.0*i) yield Color(c, c, c) for i in range(int(random.random()*300)): yield Color(base_brightness, base_brightness, base_brightness)
def candycane(strip, wait_ms=200): """Candycane Stripes/Barber Pole""" for j in range(256): for i in range(strip.numPixels()): move = i + j pos = move % 4 if pos == 0 or pos == 1: strip.setPixelColor(i, Color(255, 0, 0)) # red else: strip.setPixelColor(i, Color(255, 255, 255)) # white strip.show() time.sleep(wait_ms / 1000.0)
def colorBlinkPos(self, color, blink_ms=30, nblinks=1, pos=0): for i in range(nblinks): for i in range(self.strip.numPixels()): if ((i + pos) % 7 == 0): self.strip.setPixelColor(i, color) else: self.strip.setPixelColor(i, Color(0, 0, 0, 0)) self.strip.show() time.sleep(blink_ms / 1000.0) self.colorFill(Color(0, 0, 0, 0)) if i < (nblinks - 1): time.sleep(blink_ms / 1000.0)
def wheel(pos): """ Generate rainbow colors across 0-255 positions. Extracted from https://github.com/rpi-ws281x/rpi-ws281x-python/blob/master/examples/strandtest.py """ 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 run(self): while quit_thread_wifi == False: for j in range(1): for q in reversed(range(7)): for i in range(strip.numPixels()): strip.setPixelColor(i + q, Color(251, 251, 251)) strip.show() time.sleep(50 / 500.0) for i in range(strip.numPixels()): strip.setPixelColor(i + q, Color(0, 0, 0, 0)) time.sleep(0.1) if quit_thread_wifi: break
def cylon(strip, wait_ms=50): print "The Cylons are here!" dir = 1 i = 10 i1 = 9 i2 = 8 i3 = 7 i4 = 6 i5 = 5 i6 = 4 i7 = 3 i8 = 2 i9 = 1 #while GPIO.input(pinButtonMode) == 0: while checkModeExt() == False: strip.setPixelColor(i, Color(16,16,16)) strip.setPixelColor(i1, Color(0,32,0)) strip.setPixelColor(i2, Color(0,64,0)) strip.setPixelColor(i3, Color(0,200,0)) strip.setPixelColor(i4, Color(0,255,0)) strip.setPixelColor(i5, Color(0,255,0)) strip.setPixelColor(i6, Color(0,200,0)) strip.setPixelColor(i7, Color(0,64,0)) strip.setPixelColor(i8, Color(0,32,0)) strip.setPixelColor(i9, Color(16,16,16)) strip.show() i9 = i8 i8 = i7 i7 = i6 i6 = i5 i5 = i4 i4 = i3 i3 = i2 i2 = i1 i1 = i if i == 1: dir = 1 i+=1 elif i == strip.numPixels()-1: dir = 0 i-=1 elif dir == 1: i+=1 else: i-=1 time.sleep(wait_ms/1000.0) time.sleep(1)
def utdColor(): for i in range(strip.numPixels()): even = i % 2 if(even == 0): #official burnt orange color from ut color guidlines strip.setPixelColor(i, Color(191,87,0)) #but we dont want that because we are utd and we use a slightly different orange #here is utds official orange: #strip.setPixelColor(i, Color(232,117,0)) strip.show() else: #utds official green color: is too blue for the leds strip.setPixelColor(i, Color(55,200,10)) strip.show()
def color(self, c_name, led=False): self.stop_rainbow = True # Change RGB to GRB if needed: color = Color( *colors[c_name]) if not self.CONVERT_RGB_TO_GRB else Color( colors[c_name][1], colors[c_name][0], colors[c_name][2]) try: if led: self.strip.setPixelColor(led, color) self.strip.show() else: self.colorWipe(color) except KeyError: self.log_warning("Color name not found: {}".format(c_name))
def del_cmd(self, paras): self._mod = paras["mod"] if (paras["mod"] == 1): # 全体显示 if (paras["function"] == 0): self._symbolLeftToRight(wan, Color(100, 100, 0), 500) elif (paras["function"] == 1): self._symbolRightToLeft(wan, Color(100, 100, 0), 500) elif (paras["function"] == 2): self._leftToRight(Color(100, 100, 0), 50) elif (paras["function"] == 3): self._symbolLeftToRight(zhong, Color(100, 100, 0), 500) elif (paras["function"] == 4): self._leftToRight(Color(100, 100, 0), 50) elif (paras["function"] == 5): self._rightToLeft(Color(100, 100, 0), 50) elif (paras["function"] == 6): self._bottomToTop(Color(100, 100, 0), 50) elif (paras["function"] == 7): self._topToBottom(Color(100, 100, 0), 50) elif (paras["mod"] == 0): #单个显示 row = paras["led_row"] - 1 col = paras["led_column"] - 1 led_index = self.led_index[row][col] self.leds[led_index]._set_color(0) self.leds[led_index]._set_cycle(paras["cycle"]) self.leds[led_index]._set_delay(paras["delay"])
def run(self): while True: for j in range(1): for q in reversed(range(6)): for i in range(strip.numPixels()): strip.setPixelColor(i + q, Color(251, 251, 251)) strip.show() time.sleep(0.3) for i in range(strip.numPixels()): strip.setPixelColor(i + q, Color(0, 0, 0, 0)) # time.sleep(0.1) global quit_thread_wifi if quit_thread_wifi: break
def appear_from_back(pixels, color=(0, 255, 0, 255), size=3): clear(pixels) for i in range(int(pixels.numPixels() / size)): for j in reversed(range(i * size, pixels.numPixels() - size)): 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])) pixels.show() time.sleep(0.02)
def flash(count=3, delay=1, color=Color(255,255,255)): """Flashes pixels with the color specified""" i = 0 while i < count: for pixel in range(strip.numPixels()): strip.setPixelColor(pixel, color) strip.show() time.sleep(delay) for pixel in range(strip.numPixels()): strip.setPixelColor(pixel, Color(0,0,0)) strip.show() time.sleep(delay) i+=1
def lightbox(strip, wait_ms=50): """Fill first and last 14 LEDs with white.""" for i in range(14): strip.setPixelColor(i, Color(255, 255, 255)) strip.show() time.sleep(wait_ms / 1000.0) for i in range(14): strip.setPixelColor(i + 14, Color(0, 0, 0)) strip.show() for i in range(14): strip.setPixelColor(i + 28, Color(255, 255, 255)) strip.show() time.sleep(wait_ms / 1000.0)
def pulse(strip, colour1=None, colour2=None, wait_ms=10): 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("pulse option") logger.debug(colour1) logger.debug(colour2) while which_effect == "pulse": logger.debug("loop") for led in range(strip.numPixels()): strip.setPixelColor(led, colour1) _pulse_brightness(strip, wait_ms) for led in range(strip.numPixels()): strip.setPixelColor(led, colour2) _pulse_brightness(strip, wait_ms)
def updateNeopixels(): global position, old_position, pixelpos, old_pixel, timestamp, fadeoutCounter change = (position - old_position) / 4 old_position = position pixelpos += change if (pixelpos >= LED_COUNT): pixelpos -= LED_COUNT if (pixelpos < 0): pixelpos += LED_COUNT act_pixel = int(pixelpos) if (act_pixel != old_pixel): if (change > 0): strip.setPixelColor(act_pixel, Color(255, 0, 0)) else: strip.setPixelColor(act_pixel, Color(0, 0, 255)) old_pixel = act_pixel period = time.time() - timestamp timestamp = time.time() fadeoutCounter = 0 period = 1.0 - (period * 3) if (period < 0): period = 0 if (change < 0): period = -period # print (period); rate = statistics.mean(buf) # if (abs(rate) < 0.15): # if (period > 0.0): # buf.append(period+3.0); # else: # buf.append(period-3.0); # # else: # buf.append(period); buf.append(period) buf.pop(0) rate = statistics.mean(buf) if (abs(rate) < 0.15): rate = 0 print("fadeout:" + str(fadeoutCounter) + " speed:" + str(rate)) server.send_message_to_all(str(rate * 2.5)) return
def fadePixels(): global timestamp, fadeoutCounter for i in range(0, LED_COUNT): r = (strip.getPixelColor(i) >> 16) & 0xff g = (strip.getPixelColor(i) >> 8) & 0xff b = (strip.getPixelColor(i) >> 0) & 0xff r = int(r * 0.8) g = int(g * 0.8) b = int(b * 0.8) strip.setPixelColor(i, Color(r, g, b)) strip.show() if (time.time() - timestamp > 0.01): timestamp = time.time() buf.pop(0) buf.append(0.0) fadeoutCounter += 1 rate = statistics.mean(buf) if (abs(rate) < 0.15): rate = 0 print("fadeout:" + str(fadeoutCounter) + " speed:" + str(rate)) try: server except NameError: print("server not defined by now .... ") else: server.send_message_to_all(str(rate * 2.5)) threading.Timer(0.05, fadePixels).start()
def main(agent_id): # 根据agent_id点亮不同位置的灯区分树莓派 1 2 3 print('suceess0:the number of this pi is %d:', agent_id) strip = Adafruit_NeoPixel(32, 18, 800000, 10, False, 10) strip.begin() strip.setPixelColor(agent_id, Color(255, 0, 0)) strip.show() context = zmq.Context() publisher = context.socket(zmq.PUB) publisher.connect("tcp://%s:5556" % broker_ip) print("success1: begin zmq") subscriber = context.socket(zmq.SUB) subscriber.connect("tcp://%s:5555" % broker_ip) # subscriber.setsockopt_string(zmq.SUBSCRIBE, str(agent_id)) while True: flag = 0 #message = subscriber.recv() r = scan_wifi() print("success2:scan_wifi") info = r.readlines() print("success3:read lines") for line in info: line = line.strip('\r\n') # print(line) mac = line[0:17] # 移动终端的mac地址 if (mac == '54:25:ea:4c:22:c0') | (mac == '74:60:fa:81:a8:d8'): print('%d: %s' % (agent_id, line)) publisher.send_string('%d: %s' % (13, line)) flag = 1 if flag == 0: print('haha') time.sleep(0.2)
def alter_brightness(color: Colors, brightness: float = 1.0) -> int: """Modifies the brightness of 'color' by a factor of 'brightness'.""" return Color( int(((color.value >> 16) & 0xFF) * brightness), int(((color.value >> 8) & 0xFF) * brightness), int((color.value & 0xFF) * brightness), )
def play(self, alarm): # Set volume start volume alarm.volume.current = alarm.volume.start self.sound.set_volume(alarm.volume.current) # Set timestamps current_timestamp = currentseconds() alarm.volume.tstart = current_timestamp alarm.volume.tstop = current_timestamp + alarm.volume.ramptime * 60 alarm.weather.timestamp = current_timestamp + alarm.weather.delay * 60 # Reset flags alarm.weather.executed = False # Determine source if alarm.source.type == 'spotify': print('[alarm] Alarm ' + alarm.name + ' triggered! (spotify)') # Kill previous playbacks if exist self.radio.kill_radio() self.spotify.kill_spotify() self.spotify.play_URI(URI=alarm.source.item, shuffle=alarm.source.randomize) self.queue.put('spotify') elif alarm.source.type == 'radio': print('[alarm] Alarm ' + alarm.name + ' triggered! (radio)') # Kill previous playbacks if exist self.radio.kill_radio() self.spotify.kill_spotify() self.radio.tune_freq(freq=alarm.source.item) self.queue.put('radio') # Put light ON self.strip.colorWipe(Color(30, 5, 0))
def stop_program(shutdown=False): if shutdown: lcd_display.lcd_clear() lcd_display.lcd_display_string("Shutting down...", 2, 2) else: lcd_display.lcd_clear() lcd_display.lcd_display_string("Program stopped!", 2, 2) stop_motors() strip_thread.running = False strip_thread.colorWipe(strip, Color(0, 0, 0)) LStrip.join() interface.running = False interface_thread.join() if shutdown: sleep(2) lcd_display.lcd_clear() GPIO.cleanup() lcd_display.lcd_clear() lcd_display.lcd_display_string("Wait at least 10 sec", 2) lcd_display.lcd_display_string("before restarting!", 3, 1) sleep(3) lcd_display.lcd_clear() lcd_display.backlight(0) call("sudo shutdown -h now", shell=True) else: GPIO.cleanup() print("Exiting...") exit()
def breathing_effect(self): while responding: duty = self.map_range(math.sin(self._start), -1, 1, 0, 150) self._start += self._step #self.color_wipe(Color(0,0,0,int(duty)),0) self.color_wipe(Color(int(duty), int(duty), int(duty)), 0) time.sleep(0.05)
def Off(self): """ Turn the leds off. """ color = Color(0, 0, 0, 0) for i in range(self._strip.numPixels()): self._strip.setPixelColor(i, color) self._strip.show() self._lightState = False