def wifi_select(): msg("Please select your wifi\nConfirm with button A") sl = ugfx.List(5, 110, 228, 204) aps = {} while not Buttons.is_pressed(Buttons.BTN_A): for s in (wifi.scan() or []): if s[0] not in aps: sl.add_item(s[0]) aps[s[0]] = s time.sleep(0.01) ugfx.poll() ssid = sl.selected_text() sl.destroy() msg("Wifi: %s\nPlease enter your password\nConfirm with button A" % ssid) kb = ugfx.Keyboard(0, 160, 240, 170) e = ugfx.Textbox(5, 130, 228, 25, text="") while not Buttons.is_pressed(Buttons.BTN_A): time.sleep(0.01) ugfx.poll() pw = e.text() e.destroy() kb.destroy() result = {"ssid": ssid, "pw": pw} with open("wifi.json", "wt") as file: file.write(json.dumps(result)) file.flush() os.sync() return result
def prompt_text(description, init_text="", true_text="OK", false_text="Back", font=FONT_MEDIUM_BOLD, style=default_style_badge): """Shows a dialog and keyboard that allows the user to input/change a string Returns None if user aborts with button B """ window = ugfx.Container(0, 0, ugfx.width(), ugfx.height()) if false_text: true_text = "A: " + true_text false_text = "B: " + false_text ugfx.set_default_font(FONT_MEDIUM_BOLD) kb = ugfx.Keyboard(0, ugfx.height()//2, ugfx.width(), ugfx.height()//2, parent=window) edit = ugfx.Textbox(2, ugfx.height()//2-60, ugfx.width()-7, 25, text = init_text, parent=window) ugfx.set_default_font(FONT_SMALL) button_yes = ugfx.Button(2, ugfx.height()//2-30, ugfx.width()//2-6, 25 , true_text, parent=window) button_no = ugfx.Button(ugfx.width()//2+2, ugfx.height()//2-30, ugfx.width()//2-6, 25 , false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(ugfx.width()//10, ugfx.height()//10, ugfx.width()*4//5, ugfx.height()*2//5-90, description, parent=window) try: window.show() # edit.set_focus() todo: do we need this? while True: sleep.wfi() ugfx.poll() if buttons.is_triggered(buttons.Buttons.BTN_A): return edit.text() if buttons.is_triggered(buttons.Buttons.BTN_B): return None if buttons.is_triggered(buttons.Buttons.BTN_Menu): return edit.text() if buttons.is_triggered(buttons.Buttons.BTN_0): edit.text(edit.text() + "0") if buttons.is_triggered(buttons.Buttons.BTN_1): edit.text(edit.text() + "1") if buttons.is_triggered(buttons.Buttons.BTN_2): edit.text(edit.text() + "2") if buttons.is_triggered(buttons.Buttons.BTN_3): edit.text(edit.text() + "3") if buttons.is_triggered(buttons.Buttons.BTN_4): edit.text(edit.text() + "4") if buttons.is_triggered(buttons.Buttons.BTN_5): edit.text(edit.text() + "5") if buttons.is_triggered(buttons.Buttons.BTN_6): edit.text(edit.text() + "6") if buttons.is_triggered(buttons.Buttons.BTN_7): edit.text(edit.text() + "7") if buttons.is_triggered(buttons.Buttons.BTN_8): edit.text(edit.text() + "8") if buttons.is_triggered(buttons.Buttons.BTN_9): edit.text(edit.text() + "9") if buttons.is_triggered(buttons.Buttons.BTN_Hash): edit.text(edit.text() + "#") if buttons.is_triggered(buttons.Buttons.BTN_Star): edit.text(edit.text() + "*") finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() kb.destroy() edit.destroy(); return
def prompt_text(description, init_text = "", true_text="OK", false_text="Back", width = 300, height = 200, font=ugfx.FONT_MEDIUM_BOLD, style=default_style_badge): """Shows a dialog and keyboard that allows the user to input/change a string Returns None if user aborts with button B """ window = ugfx.Container(int((ugfx.width()-width)/2), int((ugfx.height()-height)/2), width, height, style=style) if false_text: true_text = "M: " + true_text false_text = "B: " + false_text if buttons.has_interrupt("BTN_MENU"): buttons.disable_interrupt("BTN_MENU") ugfx.set_default_font(ugfx.FONT_MEDIUM) kb = ugfx.Keyboard(0, int(height/2), width, int(height/2), parent=window) edit = ugfx.Textbox(5, int(height/2)-30, int(width*4/5)-10, 25, text = init_text, parent=window) ugfx.set_default_font(ugfx.FONT_SMALL) button_yes = ugfx.Button(int(width*4/5), int(height/2)-30, int(width*1/5)-3, 25 , true_text, parent=window) button_no = ugfx.Button(int(width*4/5), int(height/2)-30-30, int(width/5)-3, 25 , false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(int(width/10), int(height/10), int(width*4/5), int(height*2/5)-60, description, parent=window) try: buttons.init() button_yes.attach_input(ugfx.BTN_MENU,0) if button_no: button_no.attach_input(ugfx.BTN_B,0) window.show() edit.set_focus() while True: pyb.wfi() ugfx.poll() #if buttons.is_triggered("BTN_A"): return edit.text() if buttons.is_triggered("BTN_B"): return None if buttons.is_triggered("BTN_MENU"): return edit.text() finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() kb.destroy() edit.destroy(); return
def get_input(self, label, callback): ugfx.input_attach(ugfx.BTN_B, util.reboot) w = self.create_window() edit_size = 40 ugfx.Textbox(5, 0, w.width() - 12, edit_size, text=label, parent=w).enabled(False) self.kb_input = ugfx.Textbox(5, 7 + edit_size, w.width() - 12, edit_size, parent=w) self.kb = ugfx.Keyboard(5, edit_size * 2 + 12, w.width() - 12, w.height() - edit_size * 2 - 20, parent=w) self.kb_callback = callback self.kb.set_keyboard_callback(self.kb_handler)
def prompt_text(description, init_text="", true_text="OK", false_text="Back", width=ugfx.width(), height=ugfx.height(), font="Roboto_BlackItalic24", cb=None): """Shows a dialog and keyboard that allows the user to input/change a string Calls the 'cb' callback or return None if user aborts with button B. Using a callback is highly recommended as it's not possible to process events inside an event callback. The caller is responsible for flushing the display after processing the response. """ global wait_for_interrupt, button_pushed window = ugfx.Container(int((ugfx.width() - width) / 2), int((ugfx.height() - height) / 2), width, height) window.show() ugfx.set_default_font("Roboto_Regular12") kb_height = int(height / 2) + 30 kb = ugfx.Keyboard(0, height - kb_height, width, kb_height, parent=window) ugfx.set_default_font("Roboto_Regular18") edit_height = 25 edit = ugfx.Textbox(5, height - kb_height - 5 - edit_height, int(width * 4 / 5) - 10, edit_height, text=init_text, parent=window) ugfx.set_default_font("Roboto_Regular12") button_height = 25 def done(result): window.destroy() if cb: cb(result) return result def syncSuccess(evt): if evt: # We'd like promises here, but for now this should do global wait_for_interrupt, button_pushed button_pushed = "A" wait_for_interrupt = False def syncCancel(evt): if evt: # We'd like promises here, but for now this should do global wait_for_interrupt, button_pushed button_pushed = "B" wait_for_interrupt = False def asyncSuccess(evt): if evt: done(edit.text()) def asyncCancel(evt): if evt: done(None) button_yes = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height, int(width * 1 / 5) - 3, button_height, true_text, parent=window, cb=asyncSuccess if cb else syncSuccess) button_no = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height - button_height, int(width / 5) - 3, button_height, false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(5, 1, int(width * 4 / 5), height - kb_height - 5 - edit_height - 5, description, parent=window) def vkey_backspace(): edit.backspace() ugfx.flush() focus = 0 def toggle_focus(pressed): if pressed: if focus == 0: edit.set_focus() kb.enabled(1) ugfx.input_attach( version.btn_cancel, lambda pressed: vkey_backspace() if pressed else 0) ugfx.input_attach( version.btn_ok, lambda pressed: 0 if pressed else ugfx.flush()) focus = 1 elif focus == 1 or not button_no: button_yes.set_focus() kb.enabled(0) ugfx.input_attach(version.btn_ok, asyncSuccess if cb else syncSuccess) ugfx.input_attach(version.btn_cancel, asyncCancel if cb else syncCancel) focus = (2 if button_no else 0) else: button_no.set_focus() kb.enabled(0) ugfx.input_attach(version.btn_ok, asyncCancel if cb else syncCancel) ugfx.input_attach(version.btn_cancel, asyncCancel if cb else syncCancel) focus = 0 ugfx.flush() ugfx.input_init() ugfx.input_attach(ugfx.BTN_SELECT, toggle_focus) ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_UP, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: ugfx.flush() if pressed else 0) toggle_focus(True) ugfx.set_lut(ugfx.LUT_NORMAL) ugfx.flush() wait_for_interrupt = True if cb: return else: while wait_for_interrupt: time.sleep(0.2) if (focus == 0 and no_button) or button_pushed == "B": return done(False) return done(edit.text())
ugfx.set_default_font(ugfx.FONT_FIXED) ugfx.Label(5, 180, 240, 15, "Press A to scan, MENU to exit") # while (not Buttons.is_pressed(Buttons.BTN_A)) and (not Buttons.is_pressed(Buttons.BTN_B)) and (not Buttons.is_pressed(Buttons.BTN_Menu)): while not Buttons.is_pressed(Buttons.BTN_Menu): if not Buttons.is_pressed(Buttons.BTN_A) and not Buttons.is_pressed( Buttons.BTN_B): ugfx.poll() continue if Buttons.is_pressed(Buttons.BTN_B): ugfx.clear() ugfx.Label(0, 0, 240, 25, "SSID:") ssid_box = ugfx.Textbox(0, 25, 240, 25, text=ssid) ugfx.Keyboard(0, ugfx.height() // 2, ugfx.width(), ugfx.height() // 2) ssid_box.set_focus() while not Buttons.is_pressed(Buttons.BTN_A): ugfx.poll() continue ssid = ssid_box.text() ugfx.clear() wifi.nic().active(False) wifi.nic().active(True) # networks = [{ "ssid": ap[0], "mac": ap[1], "channel": ap[2], "signal": ap[3] } for ap in wifi.nic().scan()] networks = sorted([net for net in wifi.nic().scan() if net[0] == ssid], key=lambda n: n[3], reverse=True)
def prompt_text(description, init_text="", true_text="OK", false_text="Back", width=300, height=200, font="Roboto_BlackItalic24"): """Shows a dialog and keyboard that allows the user to input/change a string Returns None if user aborts with button B The caller is responsible for flushing the display after processing the response. """ global wait_for_interrupt, button_pushed window = ugfx.Container(int((ugfx.width() - width) / 2), int((ugfx.height() - height) / 2), width, height) window.show() ugfx.set_default_font("Roboto_Regular12") kb_height = int(height / 2) + 30 kb = ugfx.Keyboard(0, height - kb_height, width, kb_height, parent=window) ugfx.set_default_font("Roboto_Regular18") edit_height = 25 edit = ugfx.Textbox(5, height - kb_height - 5 - edit_height, int(width * 4 / 5) - 10, edit_height, text=init_text, parent=window) ugfx.set_default_font("Roboto_Regular12") button_height = 25 def okay(evt): # We'd like promises here, but for now this should do global wait_for_interrupt button_pushed = "A" wait_for_interrupt = False button_yes = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height, int(width * 1 / 5) - 3, button_height, true_text, parent=window, cb=okay) button_no = ugfx.Button(int(width * 4 / 5), height - kb_height - button_height - button_height, int(width / 5) - 3, button_height, false_text, parent=window) if false_text else None ugfx.set_default_font(font) label = ugfx.Label(5, 1, int(width * 4 / 5), height - kb_height - 5 - edit_height - 5, description, parent=window) def vkey_backspace(): edit.backspace() ugfx.flush() focus = 0 def toggle_focus(pressed): if pressed: if focus == 0: edit.set_focus() kb.enabled(1) ugfx.input_attach( ugfx.BTN_B, lambda pressed: vkey_backspace() if pressed else 0) ugfx.input_attach( ugfx.BTN_A, lambda pressed: 0 if pressed else ugfx.flush()) focus = 1 elif focus == 1 or not button_no: button_yes.set_focus() kb.enabled(0) ugfx.input_attach(ugfx.BTN_A, pressed_a) ugfx.input_attach(ugfx.BTN_B, pressed_b) focus = (2 if button_no else 0) else: button_no.set_focus() kb.enabled(0) ugfx.input_attach(ugfx.BTN_A, pressed_a) ugfx.input_attach(ugfx.BTN_B, pressed_b) focus = 0 ugfx.flush() ugfx.input_init() ugfx.input_attach(ugfx.BTN_SELECT, toggle_focus) ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_UP, lambda pressed: ugfx.flush() if pressed else 0) ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: ugfx.flush() if pressed else 0) toggle_focus(True) ugfx.set_lut(ugfx.LUT_NORMAL) ugfx.flush() wait_for_interrupt = True while wait_for_interrupt: time.sleep(0.2) def done(value): window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy() kb.destroy() edit.destroy() return value if (focus == 0 and no_button) or button_pushed == "B": return done(False) return done(edit.text())