def step(self): # set every LED to its current color value for i in range(0, self.LED_COUNT): leds.prep_hsv(i, [self.states[i], 1, 0.5]) leds.update() leds.dim_top(3) # wait a certain amount of time utime.sleep(0.1) # prepare the next color for each LED for i in range(0, self.LED_COUNT): self.states[i] = (self.states[i] + self.STEP) % self.SMTH
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 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 render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg): anim = 0 posy = 30 if sub != '': posy = 18 r = 255 g = 0 b = 0 r_sub = sub last_btn_poll = utime.time() - 2 while True: sleep = 0.5 if sub == '#time': r_sub = get_time() dark = 0 if light_sensor.get_reading() < 40: dark = 1 r_fg_color = fg[dark] r_bg_color = bg[dark] r_fg_sub_color = fg_sub[dark] r_bg_sub_color = bg_sub[dark] r_bg = main_bg[dark] # Button handling pressed = buttons.read( buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT ) if utime.time() - last_btn_poll >= 1: last_btn_poll = utime.time() if pressed & buttons.BOTTOM_RIGHT != 0: anim = anim + 1 if anim >= len(ANIM_TYPES): anim = 0 if pressed & buttons.BOTTOM_LEFT != 0: anim = anim - 1 if anim < 0: anim = len(ANIM_TYPES) - 1 # Animations if ANIM_TYPES[anim] == 'fade': sleep = 0.1 leds.clear() toggle_rockets(False) if r > 0 and b == 0: r = r - 1 g = g + 1 if g > 0 and r == 0: g = g - 1 b = b + 1 if b > 0 and g == 0: r = r + 1 b = b - 1 r_bg = [r, g, b] r_bg_color = r_bg r_bg_sub_color = r_bg if ANIM_TYPES[anim] == 'led': if dark == 1: for i in range(0, 11): leds.prep(i, r_bg) leds.update() leds.dim_top(4) toggle_rockets(True) else: leds.clear() toggle_rockets(False) if ANIM_TYPES[anim] == 'gay': toggle_rockets(False) leds.gay(0.4) if ANIM_TYPES[anim] == 'none': leds.clear() toggle_rockets(False) with display.open() as disp: disp.rect(0, 0, 160, 80, col=r_bg, filled=True) disp.print(title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy) if r_sub != '': disp.print(r_sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(r_sub) / 2 * 14), posy=42) disp.update() disp.close() utime.sleep(sleep)
def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): """ Main function to render the nickname on screen. Pretty ugly but not time for cleanup right now (and some APIs missing) :param title: first row of text :param sub: second row of text :param fg: tuple of (day, night) rgb for title text color :param bg: tuple of (day, night) rgb for title background color :param fg_sub: tuple of (day, night) rgb for subtitle text color :param bg_sub: tuple of (day, night) rgb for subtitle background color :param main_bg: tuple of (day, night) rgb for general background color :param mode: default animation to start in (index of ANIM_TYPES array) :param bat: battery config tuple (boolean: indicator on/off, array: good rgb, array: ok rgb, array: bad rgb) """ anim = mode posy = 30 if sub != "": posy = 18 r = 255 g = 0 b = 0 rainbow_led_pos = 0 r_sub = sub last_btn_poll = utime.time() - 2 while True: sleep = 0.5 if sub == "#time": r_sub = get_time() dark = 0 if light_sensor.get_reading() < 30: dark = 1 r_fg_color = fg[dark] r_bg_color = bg[dark] r_fg_sub_color = fg_sub[dark] r_bg_sub_color = bg_sub[dark] r_bg = main_bg[dark] # Button handling pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if utime.time() - last_btn_poll >= 1: last_btn_poll = utime.time() if pressed & buttons.BOTTOM_RIGHT != 0: anim = anim + 1 if anim >= len(ANIM_TYPES): anim = 0 blink_led(0) if pressed & buttons.BOTTOM_LEFT != 0: anim = anim - 1 if anim < 0: anim = len(ANIM_TYPES) - 1 blink_led(0) # Animations if ANIM_TYPES[anim] == "fade": sleep = 0.1 leds.clear() toggle_rockets(False) if r > 0 and b == 0: r -= 1 g += 1 if g > 0 and r == 0: g -= 1 b += 1 if b > 0 and g == 0: r += 1 b -= 1 r_bg = [r, g, b] r_bg_color = r_bg r_bg_sub_color = r_bg if ANIM_TYPES[anim] == "led": if dark == 1: for i in range(0, 11): leds.prep(i, r_bg) leds.update() leds.dim_top(4) toggle_rockets(True) else: leds.clear() toggle_rockets(False) if ANIM_TYPES[anim] == "rnd_led": if dark == 1: for i in range(0, 11): leds.prep(i, random_rgb()) leds.update() leds.dim_top(4) toggle_rockets(True) else: leds.clear() toggle_rockets(False) if ANIM_TYPES[anim] == "gay": toggle_rockets(False) leds.gay(0.4) if ANIM_TYPES[anim] == "rainbow": for i in range(0, 11): lr, lg, lb = wheel(rainbow_led_pos + i * 3) leds.prep(i, [lr, lg, lb]) rainbow_led_pos += 1 if rainbow_led_pos > 255: rainbow_led_pos = 0 leds.update() leds.dim_top(3) toggle_rockets(True) if ANIM_TYPES[anim] == "none": leds.clear() toggle_rockets(False) with display.open() as disp: disp.rect(0, 0, 160, 80, col=r_bg, filled=True) if bat[0]: render_battery(disp, bat) disp.print( title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy, ) if r_sub != "": disp.print( r_sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(r_sub) / 2 * 14), posy=42, ) disp.update() disp.close() utime.sleep(sleep)
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)
}), ]) led_mode = next(modes)[0] history = [] filebuffer = bytearray() write = 0 bias = True update_screen = 0 pause_screen = 0 pause_histogram = False histogram_offset = 0 sensor = 0 disp = display.open() last_sample_count = 1 leds.dim_top(1) COLORS = [((23 + (15 * i)) % 360, 1.0, 1.0) for i in range(11)] # variables for high-pass filter moving_average = 0 alpha = 2 beta = 3 def update_history(datasets): global history, moving_average, alpha, beta, last_sample_count last_sample_count = len(datasets) for val in datasets: history.append(val - moving_average) moving_average = (alpha * moving_average + beta * val) / (alpha + beta)
chargeCurrent = power.read_chargein_current() chargeVoltage = power.read_chargein_voltage() disp.print("Charging:", posy=0) if chargeCurrent < 0.01: disp.print("NEIN", posy=20, fg=[255, 0, 0]) else: disp.print("%fA" % chargeCurrent, posy=20) disp.print("Voltage:", posy=40) disp.print("%fV" % chargeVoltage, posy=60) if actualMode == 2: batteryCurrent = power.read_battery_current() disp.print("Drawing:", posy=0) disp.print("%fA" % batteryCurrent, posy=20) if actualMode == 3: leds.dim_top(math.floor(1 + mode / 4)) disp.backlight(0) update_leds(batteryLevel) update_rockets(batteryLevel) leds.update() disp.update() if waitAfterUpdate == True: waitAfterUpdate = False utime.sleep_ms(500) else: utime.sleep_ms(10)
def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg): anim = 'led' posy = 30 if sub != '': posy = 18 r = 255 g = 0 b = 0 while True: dark = 0 if light_sensor.get_reading() < 30: dark = 1 r_fg_color = fg[dark] r_bg_color = bg[dark] r_fg_sub_color = fg_sub[dark] r_bg_sub_color = bg_sub[dark] r_bg = main_bg[dark] if anim == 'fade': if r > 0 and b == 0: r = r - 1 g = g + 1 if g > 0 and r == 0: g = g - 1 b = b + 1 if b > 0 and g == 0: r = r + 1 b = b - 1 r_bg = [r, g, b] if anim == 'led': for i in range(0, 11): leds.prep(i, r_bg) leds.update() leds.dim_top(3) leds.set_rocket(0, 15) leds.set_rocket(1, 15) leds.set_rocket(2, 15) if anim == 'none': leds.clear() leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) with display.open() as disp: disp.rect(0, 0, 160, 80, col=r_bg, filled=True) disp.print(title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy) if sub != '': disp.print(sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(sub) / 2 * 14), posy=42) disp.update() disp.close() pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: anim = ANIM_TYPES[1] if pressed & buttons.BOTTOM_RIGHT != 0: anim = ANIM_TYPES[0] utime.sleep(0.3)