def leds_on(): vibra.vibrate(60) leds.set_all([[255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0], [255, 180, 0]]) disp.backlight(100) disp.clear(color.YELLOW) disp.update()
def main(): clr = color.WHITE print("main") leds.set_all([clr,clr,clr,clr,clr,clr,clr,clr,clr,clr,clr]) switch = False bpm = 120 sleep_t = sleepIntervalFromBPM(bpm) print(str(sleep_t) + " ms") disp = display.open() disp.clear() while True: if(switch): leds.dim_top(0) switch = False else: leds.dim_top(8) switch=True pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: bpm -= 10 if pressed & buttons.BOTTOM_RIGHT != 0: bpm += 10 if pressed & buttons.TOP_RIGHT != 0: r = urandom.randint(0,255) g = urandom.randint(0,255) b = urandom.randint(0,255) clr = color.Color(r,g,b) leds.dim_top(8) setRandomLEDColor(clr) switch = False bpm_str = str(bpm) + " BPM" sleep_t = sleepIntervalFromBPM(bpm) xOffset = int(round((len(bpm_str) * 20) / 2)) if(switch): disp.clear([clr.red,clr.green,clr.blue]) disp.print(bpm_str, fg=color.BLACK, bg= clr, posx=90-xOffset, posy = 40 - 20) else: disp.clear([0,0,0]) disp.print(bpm_str, fg=color.WHITE, bg= color.BLACK, posx=90-xOffset, posy = 40 - 20) disp.update() utime.sleep_ms(int(round(sleep_t)))
def flashlight(status=True): '''Turns on or off the flashlight, which is all leds at bright white Args: status (boolean): The status of the flashlight, True for on, False for off. Defaults to True ''' if status: leds.set_powersave(True) leds.set_all([color.WHITE] * 18) leds.dim_bottom(8) leds.dim_top(8) else: leds.dim_bottom(0) leds.dim_top(0) leds.update()
def show_leds(flag="rainbow", brightness=0.5, gamma=1, cutoff=12, halo=True): """ Shows pride flag on the top leds. :param str flag: Chooses flag. A list of all available flags is accessible via print_all(). Default = 'rainbow' :param float brightness: Set brightness of LEDs. Default = 0.5 :param float gamma: Apply simple gamma correction to adjust colors. Default = 1 :param int cutoff: See expand(). Default = 12 :param bool halo: Bottom LEDs copy outermost top LEDs if true. Default = True """ colors = ledfx.col_cor(flags[flag], brightness, gamma) output = expand(colors, cutoff)[::-1][0:11] if halo: output = ledfx.halo(output) leds.clear() leds.set_all(output)
def setRandomLEDColor(clr): leds.set_all([clr,clr,clr,clr,clr,clr,clr,clr,clr,clr,clr])
def main(): power_saving = False disp = display.open() disp.backlight(25) disp.clear().update() leds.clear() leds.set_powersave() leds.dim_top(1) leds.dim_bottom(1) with bme680.Bme680() as environment: while True: data = environment.get_data() disp.clear() # button toggle screen if buttons.read(buttons.TOP_RIGHT): power_saving = not power_saving if (data.iaq_accuracy >= 2): leds.set_all([iaq_color(data.iaq)] * 11) # Set green rocket if under 600 ppm: no mask required if (data.eco2 < 600): co2_text = "No masks required" co2_color = colors['green'] leds.set_rocket(2, 31) leds.set_rocket(1, 0) leds.set_rocket(0, 0) # Set blue rocket if over 600 ppm: masks required elif (data.eco2 >= 600 and data.eco2 < 900): co2_text = "Wear masks inside" co2_color = colors['blue'] leds.set_rocket(0, 31) leds.set_rocket(1, 0) leds.set_rocket(2, 0) # Set yellow rocket if over 900 ppm: dangerous even with masks on else: co2_text = "Masks won't save you!" co2_color = colors['orange'] leds.flash_rocket(1, 31, 500) leds.set_rocket(2, 0) leds.set_rocket(0, 0) # vibra.vibrate(500) # time.sleep(1) # vibra.vibrate(500) # time.sleep(1) # vibra.vibrate(500) leds_set_bottom(co2_color) if (not power_saving): disp.backlight(25) disp.print("IAQ: " + str(data.iaq)) disp.print("CO2: " + str(int(data.eco2)) + "ppm", posy=40) if (data.iaq_accuracy == 3): disp.print(iaq_string(data.iaq), posy=20, fg=iaq_color(data.iaq)) disp.print(co2_text, posy=60, fg=co2_color, font=display.FONT12) else: disp.print("(still calibrating...)", posy=60, font=display.FONT12) else: disp.clear() disp.backlight(0) disp.update() time.sleep(1) else: disp.print("Calibrating...", posy=0, font=display.FONT16) disp.print( "Place the badge both in open-air and a closed box with exhaled air for around 10min each.", posy=40, font=display.FONT8) disp.update() if (not power_saving): oldest_time = time.time() latest_time = oldest_time while (latest_time - oldest_time < 1): leds.set(rand() % 11, all_colors[rand() % len(all_colors)]) time.sleep_ms(1) # Feed watch doge latest_time = time.time() else: leds.set_all([[0, 0, 0]] * 15) time.sleep(1)
sample = samples[0] color = [255, 0, 0] if sample.status == 1: color = [255, 128, 0] elif sample.status == 2: color = [255, 255, 0] elif sample.status == 3: color = [0, 200, 0] subtractor = subtractors[sensor] colors = [abs(s) - subtractor for s in [sample.x, sample.y, sample.z]] display_colors = [0 if c < 0 else 255 for c in colors] new_color = Color(*display_colors) render_battery(disp) disp.update() leds.set_all([new_color for _ in range(11)]) # Read button v = buttons.read(buttons.BOTTOM_RIGHT) if v == 0: button_pressed = False if not button_pressed and v & buttons.BOTTOM_RIGHT != 0: button_pressed = True sensor = (sensor + 1) % len(sensors)
def main(): create_folders() disp = display.open() applist = list_apps() numapps = len(applist) current = 0 lineoffset = 0 timerscrollspeed = 1 timerstartscroll = 5 timercountpopped = 0 for ev in button_events(10): if numapps == 0: disp.clear(color.COMMYELLOW) disp.print( " No apps ", posx=17, posy=20, fg=color.COMMYELLOW_DARK, bg=color.COMMYELLOW, ) disp.print( "available", posx=17, posy=40, fg=color.COMMYELLOW_DARK, bg=color.COMMYELLOW, ) disp.update() continue if ev == buttons.BOTTOM_RIGHT: # Scroll down current = (current + 1) % numapps lineoffset = 0 timercountpopped = 0 elif ev == buttons.BOTTOM_LEFT: # Scroll up current = (current + numapps - 1) % numapps lineoffset = 0 timercountpopped = 0 elif ev == BUTTON_TIMER_POPPED: timercountpopped += 1 if (timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0): lineoffset += 1 elif ev == buttons.TOP_RIGHT: # Select & start disp.clear().update() disp.close() try: script_file = open(str(applist[current][0]), "r") script_content_list = script_file.readlines() script_content = ''.join(script_content_list) exec(script_content) except OSError as e: print("Loading failed: ", e) os.exit(1) except Exception as e: orange = [255, 106, 0] orange_bg = [50, 20, 0] black = [0, 0, 0] blink_delay = 100 with display.open() as disp: leds.set_all([orange, orange]) disp.rect(0, 0, 160, 80, col=orange) disp.update() utime.sleep_ms(blink_delay) leds.set_all([black, black]) disp.rect(0, 0, 160, 80, col=black) disp.update() utime.sleep_ms(blink_delay) leds.set_all([orange, orange]) disp.rect(0, 0, 160, 80, col=orange) disp.update() utime.sleep_ms(blink_delay) leds.set_all([black, black]) disp.rect(0, 0, 160, 80, col=black) disp.update() utime.sleep_ms(blink_delay) leds.set_all([orange, orange]) disp.rect(0, 0, 160, 80, col=orange) disp.update() utime.sleep_ms(blink_delay) leds.set_all([black, black]) disp.rect(0, 0, 160, 80, col=black) disp.update() utime.sleep_ms(blink_delay) disp.clear() disp.rect(0, 0, 160, 80, col=orange_bg) disp.print(str(e), fg=color.WHITE, bg=orange_bg, posx=0, posy=0) disp.update() disp.close() draw_menu(disp, applist, current, numapps, lineoffset)
def kitt( cycles=100, delay=80, power=10, minimum=0.3, rgb=[255, 0, 0], spectrum=[], halo=False, ): """ LED Animation. Knight rider-Style. :param int cycles: Amount of cycles for the animation :param int delay: Time in microseconds until the animation moves on (Inverse of Framerate). :param int power: Shape of your brightness curve. Bigger values make a steeper curve, smaller values make the curve wider. :param float minimum: Minimal brightness. :param [r,g,b] rgb: If you don't enter a spectrum this is the color used. :param list spectrum: A color spectrum consisting of up to 11 RGB-Value-Lists (e.g. ``[[255,255,255], [0,0,0], [255,255,255], ...]`` ). If you use less, the animation will be less wide. :param func halo: Halo function. See :py:func:`ledfx.halo`. """ # create a basic table of values for a smooth increment of the LED # brightness (if you don't understand this, don't worry, i don't either. # just paste it into the python shell and see the output). Basically # creates a negative cosinus curve. kitt_table = [((-math.cos(math.pi * (x / 10.0))) + 1) / 2.0 for x in range(21)] # adjust the values to start with a minimum brightness and the width of the # curve to the given power. kitt_table = [ math.pow(x, power) * (1 - minimum) + minimum for x in kitt_table ] # for the amount of specified cycles for i in range(cycles): # repeat every 20 steps j = i % 20 # and go backwards after 10 steps if j > 10: j = 20 - j if spectrum == []: used_leds = 11 # set the color values to the LEDs by multiplying the given color # value with the corresponding brightness value in the kitt table output = [[int(x * y) for y in rgb] for x in kitt_table[j:(j + used_leds)]] else: used_leds = len(spectrum) # multiply the color values in the corresponding spectrum tuple # with the brightness value in the kitt table output = [[int(y * kitt_table[j + x]) for y in spectrum[x]] for x in range(used_leds)] if halo: halo(output) leds.set_all(output) utime.sleep_ms(delay) leds.clear()
def leds_off(): leds.set_all([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]) disp.clear(color.BLACK) disp.update()
def playMe(self): for i in self.imageArray: leds.set_all(i)