bl_on = True # Variables for sending Exposure Notifications intnum = 0 intnum_old = 0 tek = None tek_intnum = 0 rpi_key = None aem_key = None rpi = None aem = None ble_mac = None tx_pause = 1 while True: v_new = buttons.read() v = ~v_old & v_new v_old = v_new intnum = en_interval_number(time.unix_time()) if intnum != intnum_old: print("Interval Number changed...") if tek == None or (intnum - tek_intnum) >= 144 or ble_mac == None: print("Generating new TEK...") tek = generate_tek() rpi_key = derive_rpi_key(tek) aem_key = derive_aem_key(tek) tek_intnum = intnum write_tek_to_file(tek, tek_intnum) print("TEK:", bytes2hex(tek)) print("Generationg new BLE MAC and RPI/AEM...")
disp.print("BLE", posy=0, fg=[0, 255, 255]) if is_active(): disp.print("active", posy=20, fg=[0, 255, 255]) else: disp.print("inactive", posy=20, fg=[0, 255, 255]) def selector(): triangle(disp, 148, 46, False) disp.print("toggle", posx=25, posy=40, fg=[0, 255, 0]) disp = display.open() button_pressed = True init() while True: disp.clear() headline() v = buttons.read(buttons.TOP_RIGHT) if v == 0: button_pressed = False if not button_pressed and v & buttons.TOP_RIGHT != 0: button_pressed = True toggle() selector() disp.update() utime.sleep(0.1)
def button_events(timeout=None): """ Iterate over button presses (event-loop). This is just a helper function used internally by the menu. But you can of course use it for your own scripts as well. It works like this: .. code-block:: python import simple_menu, buttons for ev in simple_menu.button_events(): if ev == buttons.BOTTOM_LEFT: # Left pass elif ev == buttons.BOTTOM_RIGHT: # Right pass elif ev == buttons.TOP_RIGHT: # Select pass .. versionadded:: 1.4 :param float,optional timeout: Timeout after which the generator should yield in any case. If a timeout is defined, the generator will periodically yield :py:data:`simple_menu.TIMEOUT`. .. versionadded:: 1.9 """ yield 0 v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) button_pressed = True if v != 0 else False if timeout is not None: timeout = int(timeout * 1000) next_tick = utime.time_ms() + timeout while True: if timeout is not None: current_time = utime.time_ms() if current_time >= next_tick: next_tick += timeout yield TIMEOUT v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if v == 0: button_pressed = False if not button_pressed and v & buttons.BOTTOM_LEFT != 0: button_pressed = True yield buttons.BOTTOM_LEFT if not button_pressed and v & buttons.BOTTOM_RIGHT != 0: button_pressed = True yield buttons.BOTTOM_RIGHT if not button_pressed and v & buttons.TOP_RIGHT != 0: button_pressed = True yield buttons.TOP_RIGHT
degrees = sample.x 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] draw_compass(-degrees, color) if sample.status == 3: pointing_north = degrees > 360 - fov / 2 or degrees < fov / 2 vibra.set(pointing_north) utime.sleep(freq / 2) vibra.set(False) utime.sleep(freq / 2) else: utime.sleep(freq) # Check for button presses. v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) button_pressed = v != 0 if button_pressed and v & buttons.BOTTOM_LEFT != 0: freq *= 1 / 1.2 elif button_pressed and v & buttons.BOTTOM_RIGHT != 0: freq *= 1.2 elif button_pressed and v & buttons.TOP_RIGHT != 0: pass
import bhi160 import buttons import display import leds import utime # init bhi = bhi160.BHI160Accelerometer(sample_rate=4) disp = display.open() leds.clear() highest = 0 while True: # reset if button pressed pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: highest = 0 # read accel samples = bhi.read() if len(samples) == 0: utime.sleep(0.25) continue for val in samples: complete = abs(val.x) + abs(val.y) + abs(val.z) - 1 if complete > highest: highest = complete # update display disp.clear() disp.print("Slap force:", posy=0, fg=[255, 255, 255]) disp.print("<- reset", posy=60, fg=[255, 0, 0])
def toggle_seconds_mode(): button = buttons.read(buttons.BOTTOM_LEFT) pressed = button != 0 if pressed: seconds_toggle()
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 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)
def run_loop(): bat = [1, [0, 230, 00], [255, 215, 0], [255, 0, 0]] anim = 0 write_timer = 0 with display.open() as disp: disp.clear() disp.backlight(5) disp.print("@derfnull", posy=0) disp.update() disp.close() while True: pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed & buttons.BOTTOM_LEFT: anim += 1 if pressed & buttons.BOTTOM_RIGHT: anim += 2 if pressed: if anim > 4: anim = 0 if anim == 0: leds.clear() leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) if anim == 1: leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) leds.gay(0.2) if anim == 2: leds.clear() leds.set_rocket(0, 2) leds.set_rocket(1, 15) leds.set_rocket(2, 15) if anim == 3: leds.clear() leds.set_rocket(0, 15) leds.set_rocket(1, 15) leds.set_rocket(2, 15) if anim == 4: leds.clear() leds.set_rocket(0, 0) leds.set_rocket(1, 0) leds.set_rocket(2, 0) leds.set(11, [127, 127, 127]) leds.set(12, [127, 127, 127]) leds.set(13, [127, 127, 127]) leds.set(14, [127, 127, 127]) sensor_data = bme680.get_data() ambient_light = light_sensor.get_reading() battery_voltage = os.read_battery() with display.open() as disp: disp.clear() render_battery(disp, bat, battery_voltage) disp.print("@derfnull", posy=0) disp.print("{:2.1f} C {:2.0f} %".format(sensor_data[0], sensor_data[1]), posy=20) disp.print("{:5.1f} hPa ".format(sensor_data[2]), posy=40) disp.print("{:4.1f} kOhm ".format(sensor_data[3] / 1000), posy=60) disp.update() disp.close() write_timer += 1 if write_timer == 5: with open('sensorlog.txt', 'a') as f: f.write('{} {} {} {} {} {} {}\n'.format( utime.time_ms(), sensor_data[0], sensor_data[1], sensor_data[2], sensor_data[3], ambient_light, battery_voltage)) write_timer = 0 utime.sleep(2)
def main(): global pause_graph, graph_offset, pause_screen # show button layout disp.clear(COLOR_BACKGROUND) disp.print(" BUTTONS ", posx=0, posy=0, fg=COLOR_TEXT, font=display.FONT20) disp.line(0, 20, 159, 20, col=COLOR_LINE) disp.print( " Pause >", posx=0, posy=28, fg=COLOR_MODE_FINGER, font=display.FONT16 ) disp.print( " Settings >", posx=0, posy=44, fg=COLOR_MODE_USB, font=display.FONT16 ) disp.print( "< WriteLog ", posx=0, posy=64, fg=COLOR_WRITE_BG, font=display.FONT16 ) disp.update() utime.sleep(3) # start ecg open_sensor() while True: button_pressed = {"BOTTOM_LEFT": 0, "BOTTOM_RIGHT": 0, "TOP_RIGHT": 0} while True: v = buttons.read( buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT ) # TOP RIGHT # # pause # down if button_pressed["TOP_RIGHT"] == 0 and v & buttons.TOP_RIGHT != 0: button_pressed["TOP_RIGHT"] = 1 toggle_pause() # up if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0: button_pressed["TOP_RIGHT"] = 0 # BOTTOM LEFT # # on pause = shift view left # else = toggle write # down if button_pressed["BOTTOM_LEFT"] == 0 and v & buttons.BOTTOM_LEFT != 0: button_pressed["BOTTOM_LEFT"] = 1 if pause_graph: l = len(history) graph_offset += config.get_option("Rate") / 2 if l - graph_offset < WIDTH * config.get_option("Window"): graph_offset = l - WIDTH * config.get_option("Window") else: toggle_write() # up if button_pressed["BOTTOM_LEFT"] > 0 and v & buttons.BOTTOM_LEFT == 0: button_pressed["BOTTOM_LEFT"] = 0 # BOTTOM RIGHT # # on pause = shift view right # else = show settings # down if button_pressed["BOTTOM_RIGHT"] == 0 and v & buttons.BOTTOM_RIGHT != 0: button_pressed["BOTTOM_RIGHT"] = 1 if pause_graph: graph_offset -= config.get_option("Rate") / 2 graph_offset -= graph_offset % (config.get_option("Rate") / 2) if graph_offset < 0: graph_offset = 0 else: pause_screen = -1 # hide graph leds.clear() # disable all LEDs config.run() # show config menu close_sensor() # reset sensor in case mode or bias was changed TODO do not close sensor otherwise? open_sensor() pause_screen = 0 # start plotting graph again # returning from menu was by pressing the TOP_RIGHT button button_pressed["TOP_RIGHT"] = 1 # up if button_pressed["BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0: button_pressed["BOTTOM_RIGHT"] = 0
surfaces = ["Torus", "Klein bttl", "R proj pln"] ships = [ShipTorus, ShipKlein, ShipRPP] while True: text = "" for i, s in enumerate(surfaces): if i == selected: text += ">" else: text += " " text += (s + " " * 10)[:10] d.clear() d.print(text) d.update() utime.sleep(0.1) if buttons.read(buttons.TOP_RIGHT): break if buttons.read(buttons.BOTTOM_RIGHT): selected += 1 if buttons.read(buttons.BOTTOM_LEFT): selected -= 1 selected %= len(surfaces) s = ships[selected]() while True: d.clear() s.draw(d) d.update() utime.sleep(0.1) if buttons.read(buttons.BOTTOM_RIGHT):
accel_x_comp = DC accel_x_trig = 0 accel_y_comp = GT accel_y_trig = 7 accel_z_comp = DC accel_z_trig = 0 # internal variables - do not change start_time = 0 led_on = False led_time = 0 blink_on = False leds.set_powersave(eco=True) disp = display.open() pressed = buttons.read(buttons.TOP_RIGHT) gyrodev = bhi160.BHI160Gyroscope(sample_rate=10) acceldev = bhi160.BHI160Accelerometer(sample_rate=10) gyro_str_format = "% 6d" accel_str_format = "% 6.1f" gyro_str = "" accel_str = "" act_gyro_str = "" act_accel_str = "" bufferdepth = int(sumtime / looptime) gyro = ThreeAxisBuffer.ThreeAxisBuffer(bufferdepth, gyro_x_max, gyro_y_max, gyro_z_max) accel = ThreeAxisBuffer.ThreeAxisBuffer(bufferdepth, accel_x_max, accel_y_max, accel_z_max)
def main(): global pause_histogram, histogram_offset # show button layout disp.clear(COLOR_BACKGROUND) disp.print(" BUTTONS ", posx=0, posy=0, fg=COLOR_TEXT) disp.print("Finger/USB>", posx=0, posy=20, fg=COLOR_MODE_FINGER) disp.print(" Bias >", posx=0, posy=40, fg=COLOR_MODE_USB) disp.print("< Write Log", posx=0, posy=60, fg=COLOR_WRITE_BG) disp.update() utime.sleep(3) # start ecg open_sensor() while True: button_pressed = {"BOTTOM_LEFT": 0, "BOTTOM_RIGHT": 0, "TOP_RIGHT": 0} while True: v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) # BOTTOM LEFT if button_pressed[ "BOTTOM_LEFT"] == 0 and v & buttons.BOTTOM_LEFT != 0: button_pressed["BOTTOM_LEFT"] = utime.time_ms() if not pause_histogram: toggle_write() else: l = len(history) histogram_offset += ECG_RATE / 2 if l - histogram_offset < WIDTH: histogram_offset = l - WIDTH if button_pressed[ "BOTTOM_LEFT"] > 0 and v & buttons.BOTTOM_LEFT == 0: duration = utime.time_ms() - button_pressed["BOTTOM_LEFT"] button_pressed["BOTTOM_LEFT"] = 0 # BOTTOM RIGHT if button_pressed[ "BOTTOM_RIGHT"] == 0 and v & buttons.BOTTOM_RIGHT != 0: button_pressed["BOTTOM_RIGHT"] = utime.time_ms() if not pause_histogram: toggle_bias() else: histogram_offset -= ECG_RATE / 2 histogram_offset -= histogram_offset % (ECG_RATE / 2) if histogram_offset < 0: histogram_offset = 0 if button_pressed[ "BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0: duration = utime.time_ms() - button_pressed["BOTTOM_RIGHT"] button_pressed["BOTTOM_RIGHT"] = 0 # TOP RIGHT # down, and still pressed if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT != 0: duration = utime.time_ms() - button_pressed["TOP_RIGHT"] if duration > 1000: button_pressed["TOP_RIGHT"] = -1 toggle_pause() # register down event elif button_pressed[ "TOP_RIGHT"] == 0 and v & buttons.TOP_RIGHT != 0: button_pressed["TOP_RIGHT"] = utime.time_ms() # register up event but event already called if button_pressed["TOP_RIGHT"] == -1 and v & buttons.TOP_RIGHT == 0: button_pressed["TOP_RIGHT"] = 0 # register normal up event elif button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0: duration = utime.time_ms() - button_pressed["TOP_RIGHT"] button_pressed["TOP_RIGHT"] = 0 if pause_histogram: toggle_pause() else: toggle_mode()
def toggle_hint_mode(): button = buttons.read(buttons.BOTTOM_RIGHT) pressed = button != 0 if pressed: hints_toggle()
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)
def main(): global pause_histogram, histogram_offset # show button layout disp.clear(COLOR_BACKGROUND) disp.print(" BUTTONS ", posx=0, posy=0, fg=COLOR_TEXT, font=display.FONT20) disp.line(0, 20, 159, 20, col=COLOR_LINE) disp.print(" Pause >", posx=0, posy=28, fg=COLOR_MODE_FINGER, font=display.FONT16) disp.print(" Mode/Bias >", posx=0, posy=44, fg=COLOR_MODE_USB, font=display.FONT16) disp.print("< LED/WriteLog", posx=0, posy=64, fg=COLOR_WRITE_BG, font=display.FONT16) disp.update() utime.sleep(3) # start ecg open_sensor() while True: button_pressed = {"BOTTOM_LEFT": 0, "BOTTOM_RIGHT": 0, "TOP_RIGHT": 0} while True: v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) # TOP RIGHT # down if button_pressed["TOP_RIGHT"] == 0 and v & buttons.TOP_RIGHT != 0: button_pressed["TOP_RIGHT"] = utime.time_ms() toggle_pause() # up if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0: duration = utime.time_ms() - button_pressed["TOP_RIGHT"] button_pressed["TOP_RIGHT"] = 0 # BOTTOM LEFT # # on pause = shift view left # long = toggle write # short = toggle leds # down, and still pressed if (button_pressed["BOTTOM_LEFT"] > 0 and v & buttons.BOTTOM_LEFT != 0 and not pause_histogram): duration = utime.time_ms() - button_pressed["BOTTOM_LEFT"] if duration > 1000: button_pressed["BOTTOM_LEFT"] = -1 toggle_write() # register down event elif button_pressed[ "BOTTOM_LEFT"] == 0 and v & buttons.BOTTOM_LEFT != 0: button_pressed["BOTTOM_LEFT"] = utime.time_ms() # register up event but event already called if button_pressed[ "BOTTOM_LEFT"] == -1 and v & buttons.BOTTOM_LEFT == 0: button_pressed["BOTTOM_LEFT"] = 0 # register normal up event elif button_pressed[ "BOTTOM_LEFT"] > 0 and v & buttons.BOTTOM_LEFT == 0: duration = utime.time_ms() - button_pressed["BOTTOM_LEFT"] button_pressed["BOTTOM_LEFT"] = 0 if not pause_histogram: toggle_leds() else: l = len(history) histogram_offset += ECG_RATE / 2 if l - histogram_offset < WIDTH: histogram_offset = l - WIDTH # BOTTOM RIGHT # # on pause = shift view right # long = toggle bias # short = toggle mode (finger/usb) # down, and still pressed if (button_pressed["BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT != 0 and not pause_histogram): duration = utime.time_ms() - button_pressed["BOTTOM_RIGHT"] if duration > 1000: button_pressed["BOTTOM_RIGHT"] = -1 toggle_bias() # register down event elif button_pressed[ "BOTTOM_RIGHT"] == 0 and v & buttons.BOTTOM_RIGHT != 0: button_pressed["BOTTOM_RIGHT"] = utime.time_ms() # register up event but event already called if button_pressed[ "BOTTOM_RIGHT"] == -1 and v & buttons.BOTTOM_RIGHT == 0: button_pressed["BOTTOM_RIGHT"] = 0 # register normal up event elif button_pressed[ "BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0: duration = utime.time_ms() - button_pressed["BOTTOM_RIGHT"] button_pressed["BOTTOM_RIGHT"] = 0 if pause_histogram: histogram_offset -= ECG_RATE / 2 histogram_offset -= histogram_offset % (ECG_RATE / 2) if histogram_offset < 0: histogram_offset = 0 else: toggle_mode()
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 toggle_date_mode(): button = buttons.read(buttons.TOP_RIGHT) pressed = button != 0 if pressed: date_mode_toggle()