Toggle Navigation
Hatchery
Eggs
Knightrider Nickname
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
""" Improvement ideas - animations - dvd - rainbow - led control - fade effekt - led nick writing """ import utime import display import leds import ledfx import buttons import light_sensor import ujson import os import knightrider_nickname.kitty FILENAME = 'nickname.txt' FILENAME_ADV = 'nickname.json' ANIM_TYPES = ['none', 'led', 'fade', 'gay'] def render_error(err1, err2): with display.open() as disp: disp.clear() disp.print(err1, posx=80 - round(len(err1) / 2 * 14), posy=18) disp.print(err2, posx=80 - round(len(err2) / 2 * 14), posy=42) disp.update() disp.close() def get_time(): timestamp = '' if utime.localtime()[3] < 10: timestamp = timestamp + '0' timestamp = timestamp + str(utime.localtime()[3]) + ':' if utime.localtime()[4] < 10: timestamp = timestamp + '0' timestamp = timestamp + str(utime.localtime()[4]) + ':' if utime.localtime()[5] < 10: timestamp = timestamp + '0' timestamp = timestamp + str(utime.localtime()[5]) return timestamp def toggle_rockets(state): brightness = 15 if not state: brightness = 0 leds.set_rocket(0, brightness) leds.set_rocket(1, brightness) leds.set_rocket(2, brightness) 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 knightrider = kitty.KnightRider() r_sub = sub last_btn_poll = utime.time() dark = 0 while True: sleep = 0.5 if sub == '#time': r_sub = get_time() if dark and light_sensor.get_reading() > 45: dark = 0 if not dark and light_sensor.get_reading() < 35: 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] 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() pressed = buttons.read( buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT ) if utime.time() - last_btn_poll >= 1: if pressed: last_btn_poll = utime.time() if pressed & buttons.TOP_RIGHT != 0: knightrider.toggle_enabled() if pressed & buttons.BOTTOM_LEFT != 0: knightrider.toggle_halo() if pressed & buttons.BOTTOM_RIGHT != 0: knightrider.cycle_speeds() knightrider.work() def get_key(json, key, default): try: return json[key] except KeyError: return default leds.clear() with display.open() as disp: disp.clear().update() disp.close() if FILENAME_ADV in os.listdir("."): f = open(FILENAME_ADV, 'r') try: c = ujson.loads(f.read()) f.close() # parse config nick = get_key(c, 'nickname', 'no nick') sub = get_key(c, 'subtitle', '') # daytime values background = get_key(c, 'background', [0, 0, 0]) fg_color = get_key(c, 'fg_color', [255, 255, 255]) bg_color = get_key(c, 'bg_color', background) fg_sub_color = get_key(c, 'fg_sub_color', [255, 255, 255]) bg_sub_color = get_key(c, 'bg_sub_color', background) # nighttime values background_night = get_key(c, 'background_night', [0, 0, 0]) fg_color_night = get_key(c, 'fg_color_night', [255, 255, 255]) bg_color_night = get_key(c, 'bg_color_night', background_night) fg_sub_color_night = get_key(c, 'fg_sub_color_night', [255, 255, 255]) bg_sub_color_night = get_key(c, 'bg_sub_color_night', background_night) # render nickname render_nickname(nick, sub, (fg_color, fg_color_night), (bg_color, bg_color_night), (fg_sub_color, fg_sub_color_night), (bg_sub_color, bg_sub_color_night), (background, background_night)) except ValueError: render_error('invalid', 'json') else: if FILENAME not in os.listdir("."): render_error('file not', 'found') else: f = open(FILENAME, 'r') nick = f.read() f.close() if len(nick) > 11: render_error('name too', 'long') if len(nick) < 1: render_error('nick file', 'empty') else: render_nickname(nick, '', ([255, 255, 255], [255, 255, 255]), ([0, 0, 0], [0, 0, 0]), ([255, 255, 255], [255, 255, 255]), ([0, 0, 0], [0, 0, 0]), ([0, 0, 0], [0, 0, 0]))