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_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 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 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()
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_icon" 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()
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()
from time import sleep status_height = 20 ssid = 'emfcamp-legacy18' ugfx.init() ugfx.clear() 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()
x = 0 i=0 ugfx.set_default_font(ugfx.FONT_SMALL) for p in toplot: ugfx.Label(x+13,0,50,25,p,parent=win_legend) win_legend.thickline(x,13,x+10,13,colour[ i ],3,1,) i += 1 x += 75 plot(seeks[0],zoom[0],xscale) plot_index = 0 buttons.init() while True: pyb.wfi() ugfx.poll() inc = 0 if buttons.is_triggered("JOY_RIGHT"): inc = -1 if buttons.is_triggered("JOY_LEFT"): inc = 1 if buttons.is_triggered("BTN_B"): break; if not inc == 0: inc += plot_index if inc < 0: pass elif inc >= len(zoom): pass elif seeks[0] == 0: ## dont allow zoom out if we dont have enough data
def tick_inc(t): global ugfx ugfx.poll()
def teardown(self, pressed=True): if pressed and self.window and self.window.enabled() != 0: self.window.hide() self.window.destroy() ugfx.poll()
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()
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()