Toggle Navigation
Hatchery
Eggs
benvtest
hometext.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
hometext.py
raw
Content
import ugfx, badge import appglue import dialogs def action_home(pressed): if (pressed): appglue.home() def action_tekst(pressed): if (pressed): global serviceText #load() print("[HOMETEXT] Prompt started.") # WHY DOES THIS CRASH?! # serviceText = dialogs.prompt_text("Enter text:", serviceText) serviceText = dialogs.prompt_text("Enter text:", '') print("[HOMETEXT] Prompt finished.") store() draw() def store(): global serviceText global serviceEnabled global serviceFont print("[HOMETEXT] Storing to NVRAM.") badge.nvs_set_str("hometext", "text", serviceText) badge.nvs_set_str("hometext", "font", serviceFont) badge.nvs_set_u8('hometext', 'service', serviceEnabled) print("[HOMETEXT] Store complete.") def load(): global serviceText global serviceEnabled global serviceFont print("[HOMETEXT] Load.") serviceText = badge.nvs_get_str('hometext', 'text', '') serviceFont = badge.nvs_get_str("hometext", "font", "PermanentMarker22") serviceEnabled = badge.nvs_get_u8('hometext', 'service', 0) def action_enable(pressed): if (pressed): print("[HOMETEXT] Enable.") global serviceEnabled serviceEnabled = 1 store() draw() def action_disable(pressed): if (pressed): print("[HOMETEXT] Disable.") global serviceEnabled serviceEnabled = 0 store() draw() def action_font(pressed): if (pressed): print("[HOMETEXT] Font cycle.") global serviceFont try: fonts = ugfx.fonts_list() fix = fonts.index(serviceFont) if len(fonts) == (fix + 1): serviceFont = fonts[0] else: serviceFont = fonts[fix + 1] except Exception as e: print("[HOMETEXT] Exception. Resetting font.") serviceFont = "PermanentMarker22" store() print("[HOMETEXT] Font is now " + serviceFont) draw() def draw(): ugfx.clear(ugfx.WHITE) ugfx.string(0, 0, "Homescreen text", "PermanentMarker22", ugfx.BLACK) global serviceText global serviceFont if (len(serviceText)>0): ugfx.string(0, 25, "Service is " + ("enabled!" if serviceEnabled else "disabled."), "Roboto_Regular12", ugfx.BLACK) ugfx.string(0, 38, "Press start to " + ("" if serviceEnabled else "not ") + "see it in action.", "Roboto_Regular12", ugfx.BLACK) else: ugfx.string(0, 25, "No text! Setup with SELECT", "Roboto_Regular12", ugfx.BLACK) ugfx.string(0, 38+13*1, "UP: Enable service", "Roboto_Regular12", ugfx.BLACK) ugfx.string(0, 38+13*2, "DOWN: Disable service", "Roboto_Regular12", ugfx.BLACK) ugfx.string(0, 38+13*3, "START: Go to homescreen", "Roboto_Regular12", ugfx.BLACK) ugfx.string(0, 38+13*4, "SELECT: Enter text", "Roboto_Regular12", ugfx.BLACK) ugfx.string(0, 38+13*5, "A: Font: " + serviceFont, "Roboto_Regular12", ugfx.BLACK) ugfx.set_lut(ugfx.LUT_FASTER) ugfx.flush() def program_main(): ugfx.init() ugfx.input_init() badge.init() ugfx.input_attach(ugfx.BTN_A, action_font) ugfx.input_attach(ugfx.BTN_SELECT, action_tekst) ugfx.input_attach(ugfx.BTN_START, action_home) ugfx.input_attach(ugfx.JOY_UP, action_enable) ugfx.input_attach(ugfx.JOY_DOWN, action_disable) load() draw() print("[HOMETEXT] program_main loop.") # Start main application program_main()