Toggle Navigation
Hatchery
Eggs
Fieldphonebook
fieldphonebook.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
fieldphonebook.py
raw
Content
import ugfx, wifi, badge, deepsleep from . import urequests_pmenke as requests from time import sleep ugfx.init() ugfx.input_init() # Make sure WiFi is connected wifi.init() def set_message(text): ugfx.clear(ugfx.WHITE); ugfx.string(10,10,text,"Roboto_Regular12", 0) ugfx.flush() badge.eink_busy_wait() set_message("Phonebook is loading. Please be patient") debug_enabled = badge.nvs_get_str("fieldphonebook", "debug") == "True" def debug(message): if debug_enabled: set_message(message) limit = 11 offset= 0 entries = [] def load_entries(limit, offset): debug("Waiting for wifi...") # Wait for WiFi connection while not wifi.sta_if.isconnected(): sleep(0.1) pass debug("Got wifi. Loading data. Limit: "+str(limit)+", Offset: "+str(offset)) data=None try: url = "http://217.115.15.115/sha2017.csv" r = requests.get(url, headers={"Host": "badge.chaosvermittlung.de"}, host_override="badge.chaosvermittlung.de") data = r.text r.close() except Exception as e: set_message("Unexpected error: "+str(e)) sleep(10) deepsleep.reboot() try: for entry in data.split('\n'): if entry == "": continue name = entry entries.append(name) debug("Loaded "+str(len(entries))+" entries") except Exception as e: set_message("Parsing error: "+str(e)) sleep(10) def draw(entries): debug("Will now draw") y = 10 ugfx.clear(ugfx.WHITE) if offset+limit < len(entries): for i in range(offset, limit+offset): name = entries[i] ugfx.string(10, y, name, "Roboto_Regular12", 0) y += 10 ugfx.flush() badge.eink_busy_wait() up_state = False down_state = False def up_pressed(pressed): global up_state, offset if up_state and not pressed and offset +11 < len(entries)-1: offset += 1 draw(entries) up_state = pressed def down_pressed(pressed): global down_state, offset if down_state and not pressed and offset > 0: offset -= 1 draw(entries) down_state = pressed def set_debug(pressed): if not pressed: if debug_enabled: set_message("Debug disabled") badge.nvs_set_str("fieldphonebook", "debug", "False") else: set_message("Debug enabled") badge.nvs_set_str("fieldphonebook", "debug", "True") try: load_entries(limit, offset) draw(entries) except Exception as e: set_message("Error: "+str(e)) sleep(10) deepsleep.reboot() ugfx.input_attach(ugfx.JOY_UP, down_pressed) ugfx.input_attach(ugfx.JOY_DOWN, up_pressed) ugfx.input_attach(ugfx.BTN_SELECT, lambda pressed: deepsleep.reboot()) ugfx.input_attach(ugfx.BTN_START, set_debug) while True: sleep(0.1)