def populate_options(): global options if orientation.isLandscape(): options = ugfx.List(0, 0, int(ugfx.width() / 2), ugfx.height()) else: options = ugfx.List(0, 0, ugfx.width(), int(ugfx.height() - 18 * 4)) global currentListTitles for title in currentListTitles: options.add_item(title)
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 list_categories(): global options global text global categories try: categories except: ugfx.input_init() draw_msg('Getting categories') try: f = urequests.get("https://badge.team/eggs/categories/json", timeout=30) categories = f.json() except: draw_msg('Failed!') draw_msg('Returning to launcher :(') appglue.start_app('launcher', False) f.close() draw_msg('Done!') options = ugfx.List(0, 0, int(ugfx.width() / 2), ugfx.height()) text = ugfx.Textbox(int(ugfx.width() / 2), 0, int(ugfx.width() / 2), ugfx.height()) ugfx.input_attach(ugfx.JOY_UP, lambda pushed: ugfx.flush() if pushed else False) ugfx.input_attach(ugfx.JOY_DOWN, lambda pushed: ugfx.flush() if pushed else False) ugfx.input_attach(ugfx.BTN_A, select_category) ugfx.input_attach( ugfx.BTN_B, lambda pushed: appglue.start_app("launcher", False) if pushed else False) ugfx.input_attach( ugfx.BTN_START, lambda pushed: appglue.start_app("") if pushed else False) ugfx.clear(ugfx.WHITE) ugfx.flush() while options.count() > 0: options.remove_item(0) for category in categories: options.add_item("%s (%d) >" % (category["name"], category["eggs"])) options.selected_index(0) text.text("Install or update eggs from the hatchery here\n\n\n\n") ugfx.string_box(148, 0, 148, 26, "Hatchery", "Roboto_BlackItalic24", ugfx.BLACK, ugfx.justifyCenter) ugfx.line(148, 78, 296, 78, ugfx.BLACK) ugfx.string_box(148, 78, 148, 18, " A: Open category", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.string_box(148, 92, 148, 18, " B: Return to home", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.line(148, 110, 296, 110, ugfx.BLACK) ugfx.string_box(148, 110, 148, 18, " badge.team", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.flush(ugfx.LUT_FULL) gc.collect()
def __init__(self, manager): super().__init__(manager) self.set_title('Select Game') self.game_list = ugfx.List(10, 40, self.container.width() - 20, self.container.height() - 40, up=ugfx.JOY_UP, down=ugfx.JOY_DOWN, parent=self.container)
def prompt_option(options, index=0, text = "Please select one of the following:", title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ global wait_for_interrupt, button_pushed ugfx.set_default_font("Roboto_Regular12") window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, ugfxBLACK) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) window.text(5, 30, text, ugfx.BLACK) list_y = 50 else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 180 - list_y, parent = window) for option in options: if isinstance(option, dict) and option["title"]: options_list.add_item(option["title"]) else: options_list.add_item(str(option)) options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 140 if none_text else ugfx.width() - 25, 30 , select_text, parent=window) button_none = ugfx.Button(ugfx.width() - 160, ugfx.height() - 50, 140, 30 , none_text, parent=window) if none_text else None try: ugfx.input_init() wait_for_interrupt = True while wait_for_interrupt: if button_pushed == "A": return options[options_list.selected_index()] if button_pushed == "B": return None if button_none and button_pushed == "START": return None time.sleep(0.2) finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
def prompt_option(options, index=0, text = "Please select one of the following:", title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ ugfx.set_default_font(ugfx.FONT_SMALL) window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, TILDA_COLOR) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) window.text(5, 30, text, ugfx.BLACK) list_y = 50 else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 180 - list_y, parent = window) for option in options: if isinstance(option, dict) and option["title"]: options_list.add_item(option["title"]) else: options_list.add_item(str(option)) options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 140 if none_text else ugfx.width() - 25, 30 , select_text, parent=window) button_none = ugfx.Button(ugfx.width() - 160, ugfx.height() - 50, 140, 30 , none_text, parent=window) if none_text else None try: buttons.init() while True: pyb.wfi() ugfx.poll() if buttons.is_triggered("BTN_A"): return options[options_list.selected_index()] if button_none and buttons.is_triggered("BTN_B"): return None if button_none and buttons.is_triggered("BTN_MENU"): return None finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
def __init__(self, manager): super().__init__(manager) self.set_title('Select Action') self.action_list = ugfx.List(10, 40, self.container.width() - 20, self.container.height() - 40, up=ugfx.JOY_UP, down=ugfx.JOY_DOWN, parent=self.container) self.actions = ['Rock', 'Paper', 'Scissors', 'Leave'] for action in self.actions: self.action_list.add_item(action)
def list_network(self): w = self.create_window() self.scan() self.scan_list = ugfx.List(10, 10, w.width() - 20, w.height() - 20, parent=w) self.scan_list.visible(False) for scan_config in self.scan_configs: ap_name = '{} {}'.format('@' if scan_config[4] else ' ', scan_config[0].decode('utf-8')) self.scan_list.add_item(ap_name) print(scan_config) self.scan_list.visible(True) ugfx.input_attach(ugfx.BTN_A, self.network_selected) ugfx.input_attach(ugfx.BTN_B, util.reboot)
def show_shift_list(): ugfx.clear(ugfx.WHITE) ugfx.flush() global shift_list global shifts shift_list = ugfx.List(0, 0, ugfx.width(), ugfx.height()) if not shifts: shifts = get_shifts(api_key) for shift in shifts: shift_list.add_item("{} - {}, {}".format( generate_timedelta_text(int(shift['start']), int(shift['end'])), shift['name'], shift["Name"])) ugfx.flush(ugfx.LUT_NORMAL) ugfx.input_attach(ugfx.JOY_UP, nothing) ugfx.input_attach(ugfx.JOY_DOWN, nothing) ugfx.input_attach(ugfx.JOY_LEFT, nothing) ugfx.input_attach(ugfx.JOY_RIGHT, nothing) ugfx.input_attach(ugfx.BTN_A, lambda pressed: show_shift_detail() if pressed else None) ugfx.input_attach(ugfx.BTN_B, lambda pressed: appglue.home() if pressed else None)
def list_categories(): global options global categories try: categories except: ugfx.input_init() easydraw.msg('Getting categories', "Loading...", True) try: f = urequests.get("https://badge.disobey.fi/eggs/categories/json", timeout=30) categories = f.json() except: easydraw.msg('Failed :(') appglue.start_app('launcher', False) f.close() easydraw.msg('Success :)') options = ugfx.List(0, 0, int(ugfx.width()), ugfx.height()) ugfx.input_attach(ugfx.JOY_UP, dummy_button_cb) ugfx.input_attach(ugfx.JOY_DOWN, dummy_button_cb) ugfx.input_attach(ugfx.BTN_START, select_category) ugfx.input_attach( ugfx.BTN_B, lambda pushed: appglue.start_app("launcher", False) if pushed else False) ugfx.clear(ugfx.WHITE) ugfx.flush() while options.count() > 0: options.remove_item(0) for category in categories: options.add_item("%s (%d) >" % (category["name"], category["eggs"])) options.selected_index(0) ugfx.flush(ugfx.LUT_FULL) gc.collect()
def list_apps(self): self.create_status_box() self.set_status('Waiting for network') if not util.wait_network(): self.set_status('Cannot connect WiFi') raise Exception('Cannot connect WiFi') self.set_status('Downloading app list') apps = ota.download_json(self.LIST_URL) self.close_status_box() w = self.window ugfx.set_default_font('IBMPlexSans_Regular18') self.app_list = ugfx.List(5, 50, 150, w.height() - 60, parent = w) self.app_list.visible(False) self.apps = [] for slug, app in apps.items(): app['installed'] = False app['slug'] = slug app['upgrade'] = False app['ver_string'] = '{}'.format(app['version']) try: with open('/apps/{}/app.json'.format(slug)) as fp: data = json.load(fp) app['installed'] = data['version'] if app['version'] != app['installed']: app['upgrade'] = True app['ver_string'] = '{} -> {}'.format( app['installed'], app['version']) except Exception as e: print(e) self.apps.append(app) self.app_list.add_item(app['name'] if 'name' in app else slug) self.app_list.visible(True) #ugfx.input_attach(ugfx.BTN_A, self.network_selected) ugfx.input_attach(ugfx.BTN_B, util.reboot) self.widgets.append(self.app_list)
import easydraw, network, machine, system, keyboard, ugfx easydraw.messageCentered("Scanning...", True, "/media/wifi.png") sta_if = network.WLAN(network.STA_IF); sta_if.active(True) scanResults = sta_if.scan() ssidList = [] for AP in scanResults: ssidList.append(AP[0]) ssidSet = set(ssidList) options = ugfx.List(0,0,ugfx.width(),ugfx.height()) for ssid in ssidSet: try: ssidStr = ssid.decode("ascii") options.add_item(ssidStr) except: pass chosenSsid = "" def connectClick(pushed): global chosenSsid if pushed: selected = options.selected_text().encode() ssidType = scanResults[ssidList.index(selected)][4] if ssidType == 5: easydraw.messageCentered("WPA Enterprise is not supported yet.", True, "/media/alert.png") system.reboot()
def file_loader(): width = ugfx.width() height = ugfx.height() buttons.disable_menu_reset() # Create visual elements win_header = ugfx.Container(0, 0, width, 30) win_files = ugfx.Container(0, 33, int(width / 2), height - 33) win_preview = ugfx.Container( int(width / 2) + 2, 33, int(width / 2) - 2, height - 33) components = [win_header, win_files, win_preview] ugfx.set_default_font(ugfx.FONT_TITLE) components.append( ugfx.Label(3, 3, width - 10, 29, "Choose App", parent=win_header)) ugfx.set_default_font(ugfx.FONT_MEDIUM) options = ugfx.List(0, 30, win_files.width(), win_files.height() - 30, parent=win_files) btnl = ugfx.Button(5, 3, 20, 20, "<", parent=win_files) btnr = ugfx.Button(win_files.width() - 7 - 20, 3, 20, 20, ">", parent=win_files) btnr.attach_input(ugfx.JOY_RIGHT, 0) btnl.attach_input(ugfx.JOY_LEFT, 0) components.append(options) components.append(btnr) components.append(btnl) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) l_cat = ugfx.Label(30, 3, 100, 20, "Built-in", parent=win_files) components.append(l_cat) components.append( ugfx.Button(10, win_preview.height() - 25, 20, 20, "A", parent=win_preview)) components.append( ugfx.Label(35, win_preview.height() - 25, 50, 20, "Run", parent=win_preview)) components.append( ugfx.Button(80, win_preview.height() - 25, 20, 20, "B", parent=win_preview)) components.append( ugfx.Label(105, win_preview.height() - 25, 100, 20, "Back", parent=win_preview)) components.append( ugfx.Button(10, win_preview.height() - 50, 20, 20, "M", parent=win_preview)) components.append( ugfx.Label(35, win_preview.height() - 50, 100, 20, "Pin/Unpin", parent=win_preview)) ugfx.set_default_font(ugfx.FONT_SMALL) author = ugfx.Label(1, win_preview.height() - 78, win_preview.width() - 3, 20, "by: ", parent=win_preview) desc = ugfx.Label(3, 1, win_preview.width() - 10, win_preview.height() - 83, "", parent=win_preview, justification=ugfx.Label.LEFTTOP) components.append(author) components.append(desc) app_to_load = None pinned = database_get("pinned_apps", []) catergories = get_local_app_categories() c_ptr = 0 try: win_header.show() win_files.show() win_preview.show() pinned = database_get("pinned_apps", []) # apps = [] apps_path = [] if is_dir("apps"): for app in os.listdir("apps"): path = "apps/" + app if is_dir(path) and is_file(path + "/main.py"): apps_path.append(path + "/main.py") if is_dir("examples"): for app in os.listdir("examples"): path = "examples/" + app if is_file(path) and path.endswith(".py"): apps_path.append(path) displayed_apps = update_options(options, catergories[c_ptr], pinned) index_prev = -1 while True: pyb.wfi() ugfx.poll() if index_prev != options.selected_index(): if options.selected_index() < len(displayed_apps): author.text("by: %s" % displayed_apps[options.selected_index()].user) desc.text( displayed_apps[options.selected_index()].description) index_prev = options.selected_index() if buttons.is_triggered("JOY_LEFT"): if c_ptr > 0: c_ptr -= 1 btnl.set_focus() l_cat.text(catergories[c_ptr]) displayed_apps = update_options(options, catergories[c_ptr], pinned) index_prev = -1 if buttons.is_triggered("JOY_RIGHT"): if c_ptr < len(catergories) - 1: c_ptr += 1 btnr.set_focus() l_cat.text(catergories[c_ptr]) displayed_apps = update_options(options, catergories[c_ptr], pinned) index_prev = -1 if buttons.is_triggered("BTN_MENU"): app = displayed_apps[options.selected_index()] if app.folder_name in pinned: pinned.remove(app.folder_name) else: pinned.append(app.folder_name) update_options(options, catergories[c_ptr], pinned) database_set("pinned_apps", pinned) if buttons.is_triggered("BTN_B"): return None if buttons.is_triggered("BTN_A"): return displayed_apps[options.selected_index()] finally: for component in components: component.destroy()
def populate_options(): global options options = ugfx.List(0,0,int(ugfx.width()),ugfx.height()) global currentListTitles for title in currentListTitles: options.add_item(title)
# Generic actions def btn_unhandled(pressed): ugfx.flush() def btn_exit(pressed): if pressed: system.launcher() def btn_update(pressed): if pressed: repo.update() system.start("installer", True) # Categories list categories_list = ugfx.List(0,0,ugfx.width(),ugfx.height()-48) def show_categories(pressed=True): if not pressed: return ugfx.clear(ugfx.WHITE) #Hide category list category_list.visible(False) category_list.enabled(False) #Show categories list categories_list.visible(True) categories_list.enabled(True) #Input handling ugfx.input_attach(ugfx.BTN_START, btn_exit) ugfx.input_attach(ugfx.BTN_SELECT, btn_update) ugfx.input_attach(ugfx.BTN_A, show_category)
term.header(True, "Installer") print(msg) if error: easydraw.messageCentered("ERROR\n\n" + msg, True, "/media/alert.png") elif icon_wifi: easydraw.messageCentered("PLEASE WAIT\n\n" + msg, True, "/media/wifi.png") elif icon_ok: easydraw.messageCentered(msg, True, "/media/ok.png") else: easydraw.messageCentered("PLEASE WAIT\n\n" + msg, True, "/media/busy.png") # Listbox element myList = ugfx.List(0, 0, ugfx.width(), ugfx.height() - 48) # Generic actions def btn_unhandled(pressed): display.flush(display.FLAG_LUT_FASTEST) def btn_exit(pressed): if pressed: system.launcher() def btn_update(pressed): if pressed: repo.update()
ugfx.input_attach(ugfx.JOY_DOWN, btn_unhandled) ugfx.input_attach(ugfx.JOY_LEFT, btn_unhandled) ugfx.input_attach(ugfx.JOY_RIGHT, btn_unhandled) category = [] selectBox.clear() easydraw.messageCentered("Installing...\n" + slug, True, "/media/busy.png") with open("/cache/installList", "w") as f: f.write(slug) system.start("dashboard._installer_exec") # ---- orientation.default() selectBox = ugfx.List(0, 0, ugfx.width(), ugfx.height()) repo = woezel_repo lastCategory = 0 easydraw.messageCentered("Loading...", True, "/media/busy.png") display.drawFill() if not repo.load(): if not repo.update(): if repo.lastUpdate == 0: easydraw.messageCentered("Repository not available!", False, "/media/alert.png") display.drawFill() system.launcher() show_categories()
def prompt_option(options, index=0, text = None, title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ ugfx.set_default_font(FONT_SMALL) window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, TILDA_COLOR) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) list_y = 30 if text: list_y += 20 window.text(5, 30, text, ugfx.BLACK) else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 260 - list_y, parent = window) options_list.disable_draw() for option in options: if isinstance(option, dict) and option["title"]: options_list.add_item(option["title"]) else: options_list.add_item(str(option)) options_list.enable_draw() options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 105 if none_text else 200, 30 , select_text, parent=window) button_none = ugfx.Button(117, ugfx.height() - 50, 105, 30 , none_text, parent=window) if none_text else None try: while True: sleep.wfi() ugfx.poll() # todo: temporary hack #if (buttons.is_triggered(buttons.Buttons.JOY_Up)): # index = max(index - 1, 0) # options_list.selected_index(index) #if (buttons.is_triggered(buttons.Buttons.JOY_Down)): # index = min(index + 1, len(options) - 1) # options_list.selected_index(index) if buttons.is_triggered(buttons.Buttons.BTN_A) or buttons.is_triggered(buttons.Buttons.JOY_Center): return options[options_list.selected_index()] if button_none and buttons.is_triggered(buttons.Buttons.BTN_B): return None if button_none and buttons.is_triggered(buttons.Buttons.BTN_Menu): return None finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
import ugfx import os #options.destroy() #btn_ok.destroy() #btn_menu.destroy() ugfx.init() ugfx.set_default_font("D*") ugfx.text(40, 0, "EMF BADGE 2016", ugfx.PURPLE) ugfx.set_default_font("C*") options = ugfx.List(0, 0, 160, 150) btn_ok = ugfx.Button(200, 50, 70, 30, "A: Run") btn_menu = ugfx.Button(200, 90, 70, 30, "M: Menu") files = os.listdir() for f in files: options.add_item(f) btn_menu.attach_input(ugfx.BTN_MENU) btn_ok.attach_input(ugfx.BTN_A)
# THANKYOU # author of the original irc_pager. beautifully simple IRC solution! # - Pepijn de Vos (https://badge.sha2017.org/projects/irc_pager) import badge, ugfx, appglue, wifi, time, woezel import usocket as socket name = badge.nvs_get_str('owner', 'name', 'n00b') log_messages = [] fonts = ['Roboto_Regular18', 'PermanentMarker36', 'pixelade13'] logo_width = 77 logo_path = '/lib/hackeriet/hackeriet-77.png' is_updating = False # Add 20 width to hide list scrollbars log_ui_list = ugfx.List(logo_width, 0, ugfx.width() - logo_width + 30, ugfx.height() - 13) log_ui_list.enabled(False) def log(text): global log_messages log_messages.insert(0, text) # Keep log short if len(log_messages) > 10: log_messages.pop() # Write all log lines, then flush buffer while (log_ui_list.count() > 0): log_ui_list.remove_item(0)
sta_if = network.WLAN(network.STA_IF) sta_if.active(True) scanResults = sta_if.scan() ssidList = [] for AP in scanResults: ssidList.append(AP[0]) ssidSet = set(ssidList) clearGhosting() ugfx.clear(ugfx.WHITE) ugfx.string(25, 20, 'Found', 'Roboto_Regular18', ugfx.BLACK) ugfx.string(40, 40, str(len(ssidSet)), 'Roboto_Regular18', ugfx.BLACK) ugfx.string(10, 60, 'Networks', 'Roboto_Regular18', ugfx.BLACK) options = ugfx.List(ugfx.width() - int(ugfx.width() / 1.5), 0, int(ugfx.width() / 1.5), ugfx.height()) for ssid in ssidSet: options.add_item(ssid) def connectClick(pushed): if pushed: selected = options.selected_text().encode() print('selected') options.destroy() ssidType = scanResults[ssidList.index(selected)][4] print(ssidType) print(ssidList.index(selected)) ugfx.clear(ugfx.WHITE)
def prompt_option(options, index=0, text=None, title=None, select_text="OK", none_text=None): """Shows a dialog prompting for one of multiple options If none_text is specified the user can use the B or Menu button to skip the selection if title is specified a blue title will be displayed about the text """ ugfx.set_default_font(FONT_SMALL) window = ugfx.Container(5, 5, ugfx.width() - 10, ugfx.height() - 10) window.show() list_y = 30 if title: window.text(5, 10, title, TILDA_COLOR) window.line(0, 25, ugfx.width() - 10, 25, ugfx.BLACK) list_y = 30 if text: list_y += 20 window.text(5, 30, text, ugfx.BLACK) else: window.text(5, 10, text, ugfx.BLACK) options_list = ugfx.List(5, list_y, ugfx.width() - 25, 260 - list_y, parent=window) options_list.disable_draw() optnum = 1 for option in options: if isinstance(option, dict) and option["title"]: title = option["title"] else: title = str(option) if optnum < 11: # mod 10 to make 10th item numbered 0 options_list.add_item("{}: {}".format((optnum % 10), title)) else: options_list.add_item(" {}".format(title)) optnum = optnum + 1 options_list.enable_draw() options_list.selected_index(index) select_text = "A: " + select_text if none_text: none_text = "B: " + none_text button_select = ugfx.Button(5, ugfx.height() - 50, 105 if none_text else 200, 30, select_text, parent=window) button_none = ugfx.Button( 117, ugfx.height() - 50, 105, 30, none_text, parent=window) if none_text else None try: while True: sleep.wfi() ugfx.poll() # todo: temporary hack #if (buttons.is_triggered(buttons.Buttons.JOY_Up)): # index = max(index - 1, 0) # options_list.selected_index(index) #if (buttons.is_triggered(buttons.Buttons.JOY_Down)): # index = min(index + 1, len(options) - 1) # options_list.selected_index(index) if buttons.is_triggered( buttons.Buttons.BTN_A) or buttons.is_triggered( buttons.Buttons.JOY_Center): return options[options_list.selected_index()] if button_none and buttons.is_triggered(buttons.Buttons.BTN_B): return None if button_none and buttons.is_triggered(buttons.Buttons.BTN_Menu): return None # These are indexes for selected_index, 1 means "First item", ie index 0. 0 is treated as if it were 10 button_nums = { Buttons.BTN_1: 0, Buttons.BTN_2: 1, Buttons.BTN_3: 2, Buttons.BTN_4: 3, Buttons.BTN_5: 4, Buttons.BTN_6: 5, Buttons.BTN_7: 6, Buttons.BTN_8: 7, Buttons.BTN_9: 8, Buttons.BTN_0: 9, } for key, num in button_nums.items(): if buttons.is_triggered(key): # No need to check for too large an index; gwinListSetSelected validates this. options_list.selected_index(num) break if buttons.is_triggered(Buttons.BTN_Hash): # Page down idx = options_list.selected_index() + 10 cnt = options_list.count() if idx >= cnt: idx = cnt - 1 options_list.selected_index(idx) continue if buttons.is_triggered(Buttons.BTN_Star): # Page up idx = options_list.selected_index() - 10 if idx < 0: idx = 0 options_list.selected_index(idx) continue finally: window.hide() window.destroy() options_list.destroy() button_select.destroy() if button_none: button_none.destroy() ugfx.poll()
def home_main(): global orientation, next_tick, tick ugfx.area(0, 0, 320, 240, sty_tb.background()) ugfx.set_default_font(ugfx.FONT_MEDIUM) win_bv = ugfx.Container(0, 0, 80, 25, style=sty_tb) win_wifi = ugfx.Container(82, 0, 60, 25, style=sty_tb) win_name = ugfx.Container(0, 25, 320, 240 - 25 - 60, style=dialogs.default_style_badge) win_text = ugfx.Container(0, 240 - 60, 320, 60, style=sty_tb) windows = [win_bv, win_wifi, win_text] obj_name = apps.home.draw_name.draw(0, 25, win_name) buttons.init() gc.collect() ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) hook_feeback = ugfx.List(0, 0, win_text.width(), win_text.height(), parent=win_text) win_bv.show() win_text.show() win_wifi.show() min_ctr = 28 # Create external hooks so other apps can run code in the context of # the home screen. # To do so an app needs to have an external.py with a tick() function. # The tick period will default to 60 sec, unless you define something # else via a "period" variable in the module context (use milliseconds) # If you set a variable "needs_wifi" in the module context tick() will # only be called if wifi is available # If you set a variable "needs_wifi" in the module context tick() will # be called with a reference to a 25x25 pixel ugfx container that you # can modify external_hooks = [] icon_x = 150 for path in get_external_hook_paths(): try: module = __import__(path) if not hasattr(module, "tick"): raise Exception("%s must have a tick function" % path) hook = { "name": path[5:-9], "tick": module.tick, "needs_wifi": hasattr(module, "needs_wifi"), "period": module.period if hasattr(module, "period") else 60 * 1000, "next_tick_at": 0 } if hasattr(module, "needs_icon"): hook["icon"] = ugfx.Container(icon_x, 0, 25, 25) icon_x += 27 external_hooks.append(hook) except Exception as e: # Since we dont know what exception we're looking for, we cant do much print( "%s while parsing background task %s. This task will not run! " % (type(e).__name__, path[5:-9])) sys.print_exception(e) continue # If the module fails to load or dies during the setup, skip it backlight_adjust() inactivity = 0 last_rssi = 0 ## start connecting to wifi in the background wifi_timeout = 30 #seconds wifi_reconnect_timeout = 0 try: wifi.connect(wait=False) except OSError: print("Creating default wifi settings file") wifi.create_default_config() while True: pyb.wfi() ugfx.poll() if (next_tick <= pyb.millis()): tick = True next_tick = pyb.millis() + 1000 #if wifi still needs poking if (wifi_timeout > 0): if wifi.nic().is_connected(): wifi_timeout = -1 #wifi is connected, but if becomes disconnected, reconnect after 5 sec wifi_reconnect_timeout = 5 else: wifi.nic().update() if tick: tick = False ledg.on() if (wifi_timeout > 0): wifi_timeout -= 1 # change screen orientation ival = imu.get_acceleration() if ival['y'] < -0.5: if orientation != 0: ugfx.orientation(0) elif ival['y'] > 0.5: if orientation != 180: ugfx.orientation(180) if orientation != ugfx.orientation(): inactivity = 0 ugfx.area(0, 0, 320, 240, sty_tb.background()) orientation = ugfx.orientation() for w in windows: w.hide() w.show() apps.home.draw_name.draw(0, 25, win_name) #if wifi timeout has occured and wifi isnt connected in time if (wifi_timeout == 0) and not (wifi.nic().is_connected()): print("Giving up on Wifi connect") wifi_timeout = -1 wifi.nic().disconnect() #give up wifi_reconnect_timeout = 30 #try again in 30sec wifi_connect = wifi.nic().is_connected() #if not connected, see if we should try again if not wifi_connect: if wifi_reconnect_timeout > 0: wifi_reconnect_timeout -= 1 if wifi_reconnect_timeout == 0: wifi_timeout = 60 #seconds wifi.connect(wait=False) ledg.on() # display the wifi logo rssi = wifi.nic().get_rssi() if rssi == 0: rssi = last_rssi else: last_rssi = rssi draw_wifi(sty_tb.background(), rssi, wifi_connect, wifi_timeout > 0, win_wifi) battery_percent = onboard.get_battery_percentage() draw_battery(sty_tb.background(), battery_percent, win_bv) inactivity += 1 # turn off after some period # takes longer to turn off in the 'used' position if ugfx.orientation() == 180: inactivity_limit = 120 else: inactivity_limit = 30 if battery_percent > 120: #if charger plugged in if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(100) elif inactivity > inactivity_limit: low_power() else: backlight_adjust() ledg.off() for hook in external_hooks: try: if hook["needs_wifi"] and not wifi.nic().is_connected(): continue if hook["next_tick_at"] < pyb.millis(): text = None if "icon" in hook: text = hook["tick"](hook["icon"]) else: text = hook["tick"]() hook["next_tick_at"] = pyb.millis() + hook["period"] if text: if hook_feeback.count() > 10: hook_feeback.remove_item(0) hook_feeback.add_item(text) if hook_feeback.selected_index() >= ( hook_feeback.count() - 2): hook_feeback.selected_index(hook_feeback.count() - 1) except Exception as e: # if anything in the hook fails to work, we need to skip it print( "%s in %s background task. Not running again until next reboot! " % (type(e).__name__, hook['name'])) sys.print_exception(e) external_hooks.remove(hook) continue if buttons.is_pressed("BTN_MENU"): pyb.delay(20) break if buttons.is_pressed("BTN_A"): inactivity = 0 tick = True if buttons.is_pressed("BTN_B"): inactivity = 0 tick = True for hook in external_hooks: if "icon" in hook: hook["icon"].destroy() for w in windows: w.destroy() apps.home.draw_name.draw_destroy(obj_name) win_name.destroy() hook_feeback.destroy() if ugfx.backlight() == 0: ugfx.power_mode(ugfx.POWER_ON) ugfx.backlight(100) ugfx.orientation(180) #if we havnt connected yet then give up since the periodic function wont be poked if wifi_timeout >= 0: # not (wifi.nic().is_connected()): wifi.nic().disconnect()
ugfx.input_attach(ugfx.BTN_START, start_app) ugfx.input_attach(ugfx.JOY_LEFT, lambda pushed: ugfx.flush() if pushed else 0) ugfx.input_attach(ugfx.JOY_RIGHT, lambda pushed: ugfx.flush() if pushed else 0) text = ugfx.Textbox(int(ugfx.width() / 2), 0, int(ugfx.width() / 2), ugfx.height()) ugfx.set_lut(ugfx.LUT_FULL) ugfx.flush() badge.eink_busy_wait() ugfx.set_lut(ugfx.LUT_FASTER) gc.collect() f = requests.get("https://badge.sha2017.org/eggs/list/json") try: packages = f.json() finally: f.close() gc.collect() options = ugfx.List(0, 0, int(ugfx.width() / 2), ugfx.height()) for package in packages: options.add_item("%s rev. %s" % (package["name"], package["revision"])) show_description(True)