def rainbow(self): spacing = 360.0 / 16.0 hue = 0 blinkt.set_brightness(0.1) blinkt.set_all(0, 0, 0) blinkt.show() self._running = True blinkt.set_pixel(4,255,255,0) blinkt.show() blinkt.set_pixel(4,0,0,0) blinkt.show() blinkt.set_pixel(3,255,255,0) blinkt.show() blinkt.set_pixel(3,0,0,0) blinkt.show() blinkt.set_pixel(2,255,255,0) blinkt.show() blinkt.set_pixel(2,0,0,0) blinkt.show() time.sleep(0.04) while self._running: hue = int(time.time() * 1000) % 360 for x in range(8): offset = x * spacing h = ((hue + offset) % 360) / 360.0 r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)] blinkt.set_pixel(x, r, g, b) blinkt.show() time.sleep(0.001) print("Rainbow LED thread ended...") blinkt.set_all(0, 0, 0) blinkt.show()
def showWeather(self): blinkt.set_all(0, 0, 0) blinkt.show() self._running = True while self._running: self.update_weather() alert = self.draw_thermo(self._temp) if alert: print("We have a weather alert. Threat level = ",alert) weatherAlert = Thread(target = a.alert, args = (self.X,self.R,self.G,self.B,alert)) weatherAlert.start() else: print("No weather alert. Threat level = ",alert) for i in range(120): if self._running: time.sleep(1) if alert: weatherAlertStop = Thread(target = a.stop) weatherAlertStop.start() weatherAlert.join() weatherAlertStop.join() if alert: weatherAlertStop = Thread(target = a.stop) weatherAlertStop.start() weatherAlert.join() weatherAlertStop.join() print("Weather LED thread ended...") blinkt.set_all(0, 0, 0) blinkt.show()
def flash(self): blinkt.set_all(0, 0, 0) blinkt.show() self._running = True while self._running: for z in list(range(1, 10)[::-1]) + list(range(1, 10)): fwhm = 5.0 / z gauss = self.make_gaussian(fwhm) start = time.time() y = 4 for x in range(blinkt.NUM_PIXELS): h = 0.5 s = 1.0 v = gauss[x, y] rgb = colorsys.hsv_to_rgb(h, s, v) r, g, b = [int(255.0 * i) for i in rgb] blinkt.set_pixel(x, r, g, b) blinkt.show() end = time.time() t = end - start if t < 0.04: time.sleep(0.04 - t) print("Flash LED thread ended...") blinkt.set_all(0, 0, 0) blinkt.show()
def runway(self): blinkt.set_all(0, 0, 0) blinkt.show() self._running = True blinkt.set_pixel(4,255,255,0) blinkt.show() blinkt.set_pixel(4,0,0,0) blinkt.show() blinkt.set_pixel(3,255,255,0) blinkt.show() blinkt.set_pixel(3,0,0,0) blinkt.show() blinkt.set_pixel(2,255,255,0) blinkt.show() blinkt.set_pixel(2,0,0,0) blinkt.show() time.sleep(0.04) while self._running: for i in range(8): blinkt.clear() blinkt.set_pixel(i, 119, 255, 0) blinkt.show() blinkt.time.sleep(0.05) print("Runway LED thread ended...") blinkt.set_all(0, 0, 0) blinkt.show()
def lightsequence(r, g, b, fade_time): set_all(r, g, b, brightness=0.1) for x in range(7, -1, -1): for i in np.arange(0.1, 0, -0.01): set_pixel(x, r, g, b, brightness=i) show() time.sleep(fade_time)
def rain(): forecast = darksky.Forecast(api_key, latitude, longitude, units=units) hourly = forecast.hourly rain = hourly[1].precipProbability*100 try: if rain <=19: print("The chance of rain is very low") blinkt.set_all(0, 215, 0) # Green ( very low) blinkt.show() elif (rain >=20) and (rain <=39): print("The chance of rain is low") blinkt.set_all(255, 155, 0) # Yellow (low) blinkt.show() elif (rain >=40) and (rain <=59): print("The chance of rain is moderate!") blinkt.set_all(255, 35, 0) # Orange (moderate) blinkt.show() elif (rain >=60) and (rain <=79): print("The chance of rain is very high!\nDon't forget your umbrella!") blinkt.set_all(205, 0, 0) # Red (high) blinkt.show() else: print("The chance of rain is extreme!\nIt's probably raining now!") blinkt.set_all(0, 10, 255) # Blue (extreme) blinkt.show() except: print("Connection Error")
def OnStartup(self): print(f"Running server on {self.ipAddress} port {PORT}, brightness={LED_BRIGHTNESS_PERCENT}%, press CTRL-C to exit") blinkt.set_all(255, 255, 255) blinkt.show() time.sleep(1) blinkt.set_all(0, 0, 0) blinkt.show()
def display_color(price): '''Display LED colors based on price''' colors = { 'blue': [0, 97, 255], 'green': [0, 255, 0], 'lime green': [128, 255, 0], 'yellow': [255, 255, 0], 'orange': [255, 29, 0], 'red': [255, 0, 0] } blinkt.set_brightness(0.1) blinkt.set_clear_on_exit() if price < -999: color = colors['blue'] elif price < 2.5: color = colors['green'] elif price < 3: color = colors['lime green'] elif price < 4: color = colors['yellow'] elif price < 5: color = colors['orange'] else: color = colors['red'] blinkt.set_all(color[0], color[1], color[2]) blinkt.show()
def make_light(start_time, filename): # getting time.ctime() beginnAt = time.mktime(time.strptime(start_time)) time.sleep(max(0, beginnAt - time.time())) # set_clear_on_exit() datalist = [] with open(filename) as json_file: data = json.load(json_file) for frame in data: val = frame["y"] * 255 datalist.append(val) period = 0.04 t = time.time() for frame in datalist: t += period # print(frame["y"]) # for px in range(8): # set_pixel(px, val,val, val) # show() set_all(frame, frame, frame) show() time.sleep(max(0, t - time.time())) clear() show()
def doBlink(r, g, b): global debounce, args if debounce != True: debounce = True blinkt.set_all(r, g, b) blinkt.show() debounce = False
def blinkt_on(c): global status r, g, b = hex_to_rgb(c) blinkt.set_all(r, g, b) blinkt.show() status = 1 return True
def make_light(start_time, filename): # getting time.ctime() beginnAt = time.mktime(time.strptime(start_time)) time.sleep(max(0, beginnAt-time.time())) # set_clear_on_exit() datalist = [] with open(filename) as json_file: data = json.load(json_file) for frame in data: # json structure: list of dict(keyframeX,KeyframeY,r,g,b) val = [frame["r"], frame["g"], frame["b"], frame["y"]] datalist.append(val) period = 0.04 t = time.time() for frame in datalist: t += period # print(frame["y"]) # for px in range(8): # set_pixel(px, val,val, val) # show() ###set_all(r,g, b, brightness) set_all(frame[0], frame[1], frame[2], frame[3]) #set_all(frame, frame, frame) show() time.sleep(max(0, t-time.time())) clear() show()
def OnExit(self): print("Exiting") blinkt.set_all(255, 0, 0) blinkt.show() time.sleep(1) blinkt.set_all(0, 0, 0) blinkt.show()
def show(r, g, b): blinkt.set_brightness(0.05) blinkt.set_all(r, g, b) blinkt.show() time.sleep(2) blinkt.clear() blinkt.show()
def flashOn(): # # Set the Blinkt LED's to all white # blinkt.set_all(255, 255, 255, 0.3) # Set the Blinkt LED's all on and white blinkt.show() clearScrollPhatHD() # Clear the Display # # Show a bright rectangle as a flash # for i in range(0, 17): # loop through the numbers 0 to 10 scrollphathd.set_pixel(i, 0, 1) # set the pixel on the row for i in range(0, 7): # loop through the numbers 0 to 10 scrollphathd.set_pixel(0, i, 1) # set the pixel on the row for i in range(0, 17): # loop through the numbers 0 to 10 scrollphathd.set_pixel(i, 6, 1) # set the pixel on the row for i in range(0, 7): # loop through the numbers 0 to 10 scrollphathd.set_pixel(16, i, 1) # set the pixel on the row scrollphathd.show()
def colorrotate(runSeconds: int = 5, clear: bool = True, decreaseBrightness: bool = False): hue = 0 blinkt.set_clear_on_exit() start_time = datetime.datetime.now() tSeconds = (datetime.datetime.now() - start_time).total_seconds() while (tSeconds < runSeconds) : brightness = math.ceil((tSeconds/runSeconds)*10)/10 if(decreaseBrightness): brightness = 1 - brightness hue = int(time.time() * 100) % 360 h = (hue % 360) / 360.0 r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)] blinkt.set_all(r, g, b, brightness) blinkt.show() time.sleep(0.005) tSeconds = (datetime.datetime.now() - start_time).total_seconds() if clear: blinkt.clear() blinkt.show()
def on_event(self, event, payload): self._logger.info("Received event " + event) if event == "CaptureStart": clear() set_all(255, 255, 255, 0.1) show() if event == "CaptureDone": clear() # Restore the progress self._set_progress(self._blinkt_progress) show() if event == "PrintDone": clear() for x in range(120): for n in range(8): if x % 2 == 0: set_pixel(n,0,0,0) if x % 2 == 1: set_pixel(n,0,255,0) show() time.sleep(0.05) clear() show()
def play_sound(filename): color_provider = ColorProvider(20) f = wave.open(filename, 'rb') periodsize = int(f.getframerate() / 16) print("framerate: %f, periodsize: %f" % (f.getframerate(), periodsize)) data = f.readframes(periodsize) period_length = 1 / 16 counter = 1 next_timestamp = time.time() while data: if time.time() >= next_timestamp: device.write(data) next_timestamp = time.time() + period_length vu = (math.log(float(max(audioop.max(data, 2), 1))) - log_lo) / (log_hi - log_lo) volume = (min(max(int(vu * 100), 0), 100)) print("%i: %f" % (counter, volume)) r, g, b = color_provider.give_color() blinkt.set_all(r, g, b, brightness=volume / 200.0) blinkt.show() counter += 1 data = f.readframes(periodsize) f.close() blinkt.clear() blinkt.show()
def custom_alert(r, g, b, current_status): """Consistent pulse""" clear() brightness = 0.0 direction = True running = True while running: if direction: brightness += 0.1 if brightness >= 0.9: direction = False else: if brightness > 0.15: brightness -= 0.1 else: brightness = 0.0 if brightness <= 0.000001: direction = True set_all(r, g, b, brightness) show() if brightness == 0.0: custom_sleep(0.3, current_status) elif brightness >= 0.9: custom_sleep(0.3, current_status) else: custom_sleep(0.05, current_status)
def loop(): global tom_iphone_time global iphone_time global iphone2_time global brookes_air_time global zacks_air_time start_time = datetime.now().strftime("%A, %B %d - %I:%M:%S %p") while True: os.system('clear') print('----------------------------------------------------') print('Starting wifi scan -', start_time) print('Searching for signals...') print() print('----------------------------------------------------') print() if tom_iphone_time != 0: print('Tom iPhone -', tom_iphone_time) print() blinkt.set_all(128, 0, 0) if iphone_time != 0: print('Brooke iPhone -', iphone_time) print() if iphone2_time != 0: print('Zack iPhone -', iphone2_time) print() if zacks_air_time != 0: print('Zack Air -', zacks_air_time) print() if brookes_air_time != 0: print('Brooke Air-2 -', brookes_air_time) print() blinkt.show() nm.scan('192.168.1.*', '62078-62079') for host in nm.all_hosts(): x = datetime.now().strftime("%A, %B %d - %I:%M:%S %p") try: if 'B4:8B:19:88:47:07' in nm[host]['addresses']['mac']: tom_iphone_time = x if '18:81:0E:7E:60:71' in nm[host]['addresses']['mac']: iphone_time = x if 'FC:E9:98:B7:5C:45' in nm[host]['addresses']['mac']: iphone2_time = x if '84:38:35:48:6D:4C' in nm[host]['addresses']['mac']: zacks_air_time = x if '84:38:35:67:42:22' in nm[host]['addresses']['mac']: brookes_air_time = x except (KeyError): continue
def set_all(status): if BLINKT: blinkt.set_all(BlinktHelper.colors[status]["r"], BlinktHelper.colors[status]["g"], BlinktHelper.colors[status]["b"], brightness=BlinktHelper.colors[status]["brightness"]) blinkt.show()
def set_all(self, color, brightness=255, retain=True): if retain: self.color = color self.brightness = brightness blinkt.set_all(color.red(), color.green(), color.blue(), brightness / 255) blinkt.show()
def set_all(status): if BLINKT: blinkt.set_all( BlinktHelper.colors[status]["r"], BlinktHelper.colors[status]["g"], BlinktHelper.colors[status]["b"], brightness=BlinktHelper.colors[status]["brightness"]) blinkt.show()
def lightsUp(times, brightness, name, r, g, b): print(name) blinkt.set_brightness(brightness) for _ in range(times): blinkt.set_all(r, g, b) blinkt.show() time.sleep(3) blinkt.clear() return
def all_rgb(self, **kwargs): ''' Set all the LEDs to a color ''' red = kwargs.get('red') green = kwargs.get('green') blue = kwargs.get('blue') blinkt.set_all(r, g, b) blinkt.show()
def flashing(r, g, b, number_of_flashes, time_gap): # make the lights flash for x in range(number_of_flashes): set_all(r, g, b, 1) show() time.sleep(time_gap) clear() show() time.sleep(time_gap / 1.3)
def sun(r, g, b): i = 0.0 while i <= 0.8: blinkt.set_all(r, g, b, i) blinkt.show() time.sleep(0.2) i += 0.1 if i > 0.8: i = 0.8
def flashing(r, g, b, number_of_flashes, time_gap, current_status): """Make lights to flash""" for x in range(number_of_flashes): set_all(r, g, b, 1) show() custom_sleep(time_gap, current_status) clear() show() custom_sleep(time_gap / 1.3, current_status)
def solid_colour(r, g, b, current_status): """Show a solid_colour beginning with LED_line animation""" custom_sleep(1, current_status) for x in range(8): led_line(0.08, 1.0, x, r, g, b, current_status) for x in range(7, -1, -1): led_line(0.08, 1.0, x, r, g, b, current_status) clear() set_all(r, g, b, 1.0) show()
def main(): db = init() while True: time.sleep(30) evt = db.find_current_event() if evt is not None: blinkt.set_all(evt.R, evt.G, rgb.B, 0.5) else: blink.clear()
def led(): if request.method == 'POST': var1 = request.get_json() print var1 blinkt.set_all(var1['color']['r'], var1['color']['g'], var1['color']['b'], var1['brightness']) blinkt.show() return "OK" else: return "colors"
def BlinktManager(): #time.sleep(20) localBrightness=globalBrightness while not globalShutdown: spacing = 360.0 / 16.0 hue = 0 if localBrightness!=globalBrightness: blinkt.set_brightness(globalBrightness) localBrightness=globalBrightness if globalResponse=='pulse': for z in list(range(1, 10)[::-1]) + list(range(1, 10)): fwhm = 5.0/z gauss = make_gaussian(fwhm) start = time.time() y = 4 for x in range(blinkt.NUM_PIXELS): h = 0.5 s = 1.0 v = gauss[x, y] rgb = colorsys.hsv_to_rgb(h, s, v) r, g, b = [int(255.0 * i) for i in rgb] blinkt.set_pixel(x, r, g, b) blinkt.show() end = time.time() t = end - start if t < 0.04: time.sleep(0.04 - t) time.sleep(0.15) elif globalResponse=='rainbow': hue = int(time.time() * 100) % 360 for x in range(blinkt.NUM_PIXELS): offset = x * spacing h = ((hue + offset) % 360) / 360.0 r, g, b = [int(c*255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)] blinkt.set_pixel(x,r,g,b) blinkt.show() time.sleep(0.15) elif globalResponse=='solid': blinkt.set_all(255, 255, 255) print "solid" time.sleep(3) elif globalResponse=='off': blinkt.set_all(0, 0, 0) time.sleep(3) blinkt.show()
def on_ble_write(self, *args, **kwargs): try: # bytes=[0x07, 0x02, 0x00, 0x01, 0x00, 0x0FF, 0x00] bytes = args[1]["Value"] if len(bytes) > 2: cmd = (bytes[0] << 8) + (bytes[1] & 0xff) if cmd == 0x0702: if len(bytes) >= 7: set_pixel(bytes[3] - 1, bytes[4], bytes[5], bytes[6]) elif cmd == 0x0601: if len(bytes) >= 5: set_all(bytes[2], bytes[3], bytes[4]) show() except Exception as inst: print(type(inst)) print(inst.args) print(inst) return 0
def ledstage( stage ): #Prevent the LED's clearing after script execution and reboot. set_clear_on_exit(False) #Colours! colours = [] colours.append([255,25,0]) colours.append([200,50,0]) colours.append([150,75,0]) colours.append([125,100,0]) colours.append([100,125,0]) colours.append([50,175,0]) colours.append([25,200,0]) colours.append([0,255,0]) #Loop var x = 0 #Clear LED's if 0 if stage == 0: set_all(0,0,0) show() elif stage == 8: #Party! counter = 50 while x < counter: for i in range(8): set_pixel(i, random.randint(0,255), random.randint(0,255), random.randint(0,255)) show() time.sleep(0.05) x += 1 else: while x < stage: set_pixel(x,colours[x][0],colours[x][1],colours[x][2],0.1) show() x += 1
def ledclear(): set_all(0,0,0) show()
def process_event(event, device_id): """Pretty prints events. Prints all events that occur with two spaces between each new conversation and a single space between turns of a conversation. Args: event(event.Event): The current event to process. """ if event.type == EventType.ON_CONVERSATION_TURN_STARTED: subprocess.Popen(["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) #Uncomment the following after starting the Kodi #status=mutevolstatus() #vollevel=status[1] #with open('/home/pi/.volume.json', 'w') as f: #json.dump(vollevel, f) #kodi.Application.SetVolume({"volume": 0}) GPIO.output(5,GPIO.HIGH) led.ChangeDutyCycle(100) print() print(event) if (event.type == EventType.ON_RESPONDING_STARTED and event.args and not event.args['is_error_response']): GPIO.output(5,GPIO.LOW) GPIO.output(6,GPIO.HIGH) led.ChangeDutyCycle(50) if event.type == EventType.ON_RESPONDING_FINISHED: GPIO.output(6,GPIO.LOW) GPIO.output(5,GPIO.HIGH) led.ChangeDutyCycle(100) print(event) if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and event.args and not event.args['with_follow_on_turn']): GPIO.output(5,GPIO.LOW) led.ChangeDutyCycle(0) #Uncomment the following after starting the Kodi #with open('/home/pi/.volume.json', 'r') as f: #vollevel = json.load(f) #kodi.Application.SetVolume({"volume": vollevel}) print() if event.type == EventType.ON_DEVICE_ACTION: for command, params in process_device_actions(event, device_id): print('Do command', command, 'with params', str(params)) if command=='action.devices.commands.OnOff': if params['on']: blinkt.set_all(255, 255, 255,brightness=0.05) blinkt.show() else: blinkt.clear() blinkt.show() if command=='action.devices.commands.ColorAbsolute': r,g,b=colourconv(params['color']['spectrumRGB']) print(r,g,b) blinkt.set_all(r,g,b) blinkt.show() if command=='action.devices.commands.BrightnessAbsolute': bright=(params['brightness'])/100 blinkt.set_brightness(bright) blinkt.show()
import sys import time from blinkt import set_all, set_clear_on_exit, show def usage(): print("Usage: {} <r> <g> <b>".format(sys.argv[0])) sys.exit(1) if len(sys.argv) != 4: usage() # Exit if non integer value. int() will raise a ValueError try: r, g, b = [int(x) for x in sys.argv[1:]] except ValueError: usage() # Exit if any of r, g, b are greater than 255 if max(r,g,b) > 255: usage() print("Setting Blinkt to {r},{g},{b}".format(r=r,g=g,b=b)) set_clear_on_exit(False) set_all(r, g, b) show()
def reset_lights(): if BLINKT: blinkt.set_all(0, 0, 0, 0) blinkt.show()
def button_a(button, pressed): buttonshim.set_pixel(128, 0, 0) blinkt.set_all(128, 0, 0)
def button_c(button, pressed): buttonshim.set_pixel(0, 0, 128) blinkt.set_all(0, 0, 128)
#!/usr/bin/env python import time from blinkt import set_clear_on_exit, set_all, show set_clear_on_exit() step = 0 while True: if step == 0: set_all(128,0,0) if step == 1: set_all(0,128,0) if step == 2: set_all(0,0,128) step+=1 step%=3 show() time.sleep(0.5)
def button_b(button, pressed): buttonshim.set_pixel(0, 128, 0) blinkt.set_all(0, 128, 0)