Toggle Navigation
Hatchery
Eggs
sinenick
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
""" sinenick by Lorphos, written at cccamp19 reads nickname from nickname.txt in root directory of card10 press bottom left button to toggle LEDs """ import utime import display import light_sensor import buttons import color import math import leds import os FILENAME = 'nickname.txt' 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 render_led(offset): brightness = light_sensor.get_reading() fgcol = color.from_hsv(offset,1,0.6) for i in range(15): leds.prep(i, fgcol) dimness = 1+ int(brightness / 270); if dimness > 8: dimness = 8 leds.dim_top(dimness) leds.update() def render_nickname(title, loops): offset = 0 use_leds = True while loops != 0: wiggle = 24.0 * math.cos(offset/23) with display.open() as disp: disp.clear() num=0 for ch in title: mposx = num * 12 mposy = int(26 + wiggle * math.sin((num+offset) / 10.0 * (math.pi))) fgcol = color.from_hsv((4*num) + (3*offset),1,1) disp.print(ch, fg=fgcol, posx=mposx, posy=mposy) num += 1 disp.update() disp.close() if use_leds: render_led(offset) if buttons.read(buttons.BOTTOM_LEFT) != 0: use_leds = not use_leds # wait for button to be released while buttons.read(buttons.BOTTOM_LEFT) != 0: utime.sleep_ms(10) leds.clear() offset += 1 utime.sleep_ms(50) loops -= 1 if FILENAME not in os.listdir("."): render_error(FILENAME, '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("by Lorphos", 20) render_nickname(nick, -1)