Toggle Navigation
Hatchery
Eggs
Random LED
random_led.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
random_led.py
raw
Content
from urandom import * import leds import light_sensor import utime import display 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_message(msg): with display.open() as disp: disp.clear() disp.print(msg, posx=80 - round(len(msg) / 2 * 14), posy=18) disp.update() disp.close() def randrange(start, stop=None): if stop is None: stop = start start = 0 upper = stop - start bits = 0 pwr2 = 1 while upper > pwr2: pwr2 <<= 1 bits += 1 while True: r = getrandbits(bits) if r < upper: break return r + start def randint(start, stop): return randrange(start, stop + 1) def shuffle(seq): l = len(seq) for i in range(l): j = randrange(l) seq[i], seq[j] = seq[j], seq[i] try: words = ['having', 'fun', 'at', 'ccc 2019', 'hydrate', 'hack', 'eat', 'sleep', 'visit', 'help', 'repeat'] disp = display.open() light_sensor.start() seed(light_sensor.get_reading()) while True: position = randrange(0, 10) leds.clear() leds.set(position, [255, 255, 255]) render_message(words[position]) utime.sleep(0.3) except Exception as e: render_error('Error: ', str(e))