def drawLogo(offset=0, max_height=ugfx.height(), center=True): global cfg_logo, default_logo try: info = badge.png_info(cfg_logo) except: try: cfg_logo = default_logo info = badge.png_info(cfg_logo) except: return 0 width = info[0] height = info[1] if width > ugfx.width(): print("Image too large (x)") return if height > ugfx.height(): print("Image too large (y)") x = int((ugfx.width() - width) / 2) if center: if max_height - height < 0: print("Not enough space for logo", max_height, height) return 0 y = int((max_height - height) / 2) + offset else: y = offset try: #badge.png(x,y,cfg_logo) ugfx.display_image(x, y, cfg_logo) return height except BaseException as e: sys.print_exception(e) return 0
def __init__(self, text="Please Wait...", title=version.dialog_title): self.window = ugfx.Container(30, 30, ugfx.width() - 60, ugfx.height() - 60) self.window.show() self.window.text(5, 10, title, ugfx.BLACK) self.window.line(0, 30, ugfx.width() - 60, 30, ugfx.BLACK) self.label = ugfx.Label(5, 40, self.window.width() - 10, ugfx.height() - 40, text=text, parent=self.window) # Indicator to show something is going on self.indicator = ugfx.Label(ugfx.width() - 100, 0, 20, 20, text="...", parent=self.window) self.timer = machine.Timer(-1) self.timer.init(period=2000, mode=self.timer.PERIODIC, callback=lambda t: self.indicator.visible( not self.indicator.visible()))
def __init__(self, text="Please Wait...", title="SHA2017Badge"): self.window = ugfx.Container(30, 30, ugfx.width() - 60, ugfx.height() - 60) self.window.show() self.window.text(5, 10, title, ugfx.BLACK) self.window.line(0, 30, ugfx.width() - 60, 30, ugfx.BLACK) self.label = ugfx.Label(5, 40, self.window.width() - 10, ugfx.height() - 40, text=text, parent=self.window) # Indicator to show something is going on self.indicator = ugfx.Label(ugfx.width() - 100, 0, 20, 20, text="...", parent=self.window) self.timer = pyb.Timer(3) self.timer.init(freq=3) self.timer.callback( lambda t: self.indicator.visible(not self.indicator.visible()))
def run_app(path): import buttons buttons.init() if not buttons.has_interrupt("BTN_MENU"): buttons.enable_menu_reset() try: mod = __import__(path) if "main" in dir(mod): mod.main() except Exception as e: import sys import uio import ugfx s = uio.StringIO() sys.print_exception(e, s) ugfx.clear() ugfx.set_default_font(ugfx.FONT_SMALL) w=ugfx.Container(0,0,ugfx.width(),ugfx.height()) ugfx.Label(0,0,ugfx.width(),ugfx.height(),s.getvalue(),parent=w) w.show() raise(e) import stm stm.mem8[0x40002850] = 0x9C import pyb pyb.hard_reset()
def draw_badge(): style.set_enabled([ugfx.WHITE, ugfx.html_color(0x800080), ugfx.html_color(0x800080), ugfx.html_color(0x800080)]) style.set_background(ugfx.html_color(0x800080)) ugfx.clear(ugfx.html_color(0x800080)) ugfx.set_default_style(style) # Logo stuff ugfx.display_image( int((ugfx.width() - logo_width) / 2), int((ugfx.height() - logo_height) / 2), logo_path ) # Draw for people to see ugfx.orientation(90) # Draw introduction ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(0, ugfx.height() - name_height - intro_height, ugfx.width(), intro_height, intro_text, justification=ugfx.Label.CENTER) # Process name name_setting = name("Set your name in the settings app") if len(name_setting) <= max_name: ugfx.set_default_font(ugfx.FONT_NAME) else: ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) # Draw name ugfx.Label(0, ugfx.height() - name_height, ugfx.width(), name_height, name_setting, justification=ugfx.Label.CENTER) # Draw for wearer to see ugfx.orientation(270) ugfx.set_default_font(ugfx.FONT_SMALL) status = ugfx.Label(0, ugfx.height() - status_height, ugfx.width(), status_height, "", justification=ugfx.Label.LEFT)
def show_names(): global names c = False for n in range(0, len(names)): color = ugfx.BLACK if (c): c = False else: c = True color = ugfx.WHITE fill_screen_with_crap(color) x = int.from_bytes(uos.urandom(1), 1) y = round(int.from_bytes(uos.urandom(1), 1) / 2) w = ugfx.get_string_width(names[n], "PermanentMarker22") if (x + w > ugfx.width()): x = x + w if (y > ugfx.height() - 22): y = ugfx.height() - 22 if (x == 0): x = 1 if (y == 0): y = 1 print("NAME AT " + str(x) + ", " + str(y)) draw_name(x, y, names[n]) time.sleep(1)
def draw_name(): intro_text = "Hi! I'm" intro_height = 30 name_height = 60 max_name = 8 ugfx.orientation(90) ugfx.set_default_font(ugfx.FONT_TITLE) # Process name name_setting = name("Set your name in the settings app") if len(name_setting) <= max_name: ugfx.set_default_font(ugfx.FONT_NAME) else: ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) ugfx.Label(0, ugfx.height() - name_height - intro_height, ugfx.width(), intro_height, intro_text, justification=ugfx.Label.CENTER) ugfx.Label(0, ugfx.height() - name_height, ugfx.width(), name_height, name_setting, justification=ugfx.Label.CENTER) ugfx.orientation(270)
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 reset(): global i global j i = int(ugfx.width() / 2) j = int(ugfx.height() / 2) ugfx.area(0, 0, ugfx.width(), ugfx.height(), ugfx.BLACK) ugfx.area((i - 1) if i > 0 else 0, (j - 1) if j > 0 else 0, 3 if (i > 0 and i < (ugfx.width() - 1)) else 2, 3 if (j > 0 and j < (ugfx.height() - 1)) else 2, ugfx.GREY)
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 program_main(): ugfx.init() ugfx.clear(ugfx.WHITE) badge.leds_init() try: badge.eink_png(0, 0, logo_path) except: log('+ Failed to load graphics') # Name tag ugfx.string(ugfx.width() - ugfx.get_string_width(name, fonts[1]), ugfx.height() - 36, name, fonts[1], ugfx.BLACK) # Button info ugfx.string(0, ugfx.height() - 13, '[FLASH to update] [B to exit]', fonts[2], ugfx.BLACK) ugfx.flush() ugfx.input_init() ugfx.input_attach(ugfx.BTN_B, lambda pressed: exit_app()) ugfx.input_attach(ugfx.BTN_FLASH, lambda pressed: start_self_update()) HOST = "chat.freenode.net" PORT = 6667 NICK = name + "[luv]" REALNAME = name + ' @ SHA2017' log('+ waiting for network...') wifi.init() while not wifi.sta_if.isconnected(): time.sleep(0.1) s = socket.socket() s.connect((HOST, PORT)) s.send(bytes("NICK %s\r\n" % NICK, "UTF-8")) s.send(bytes("USER %s 0 * :%s\r\n" % (NICK, REALNAME), "UTF-8")) s.send(bytes("JOIN #sha2017\r\n", "UTF-8")) # IRC Client while True: line = s.readline().rstrip() parts = line.split() if parts: if (parts[0] == b"PING"): s.send(bytes("PONG %s\r\n" % line[1], "UTF-8")) blink_led(green) if (parts[1] == b"PRIVMSG"): blink_led(red) msg = b' '.join(parts[3:]) rnick = line.split(b'!')[0] log(b'%s: %s' % (rnick, msg))
def drawPageIndicator(amount, position): x = 5 size = 4 offset = 6 for i in range(amount): if i == position: ugfx.fill_circle(x, ugfx.height() - 8, size, ugfx.BLACK) else: ugfx.circle(x, ugfx.height() - 8, size, ugfx.BLACK) x += size + offset
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 __init__(self, text = "Please Wait...", title="TiLDA"): self.window = ugfx.Container(30, 30, ugfx.width() - 60, ugfx.height() - 60) self.window.show() self.window.text(5, 10, title, TILDA_COLOR) self.window.line(0, 30, ugfx.width() - 60, 30, ugfx.BLACK) self.label = ugfx.Label(5, 40, self.window.width() - 10, ugfx.height() - 40, text = text, parent=self.window) # Indicator to show something is going on self.indicator = ugfx.Label(ugfx.width() - 100, 0, 20, 20, text = "...", parent=self.window) self.timer = pyb.Timer(3) self.timer.init(freq=3) self.timer.callback(lambda t: self.indicator.visible(not self.indicator.visible()))
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, text="Please Wait...", title="TiLDA"): self.window = ugfx.Container(30, 30, ugfx.width() - 60, ugfx.height() - 60) self.window.show() self.window.text(5, 10, title, TILDA_COLOR) self.window.line(0, 30, ugfx.width() - 60, 30, ugfx.BLACK) self.label = ugfx.Label(5, 40, self.window.width() - 10, ugfx.height() - 40, text=text, parent=self.window)
def start(): ugfx.input_init() ugfx.set_lut(ugfx.LUT_FASTER) ugfx.clear(ugfx.WHITE) # Instructions if orientation.isLandscape(): ugfx.line(148, 78, 296, 78, ugfx.BLACK) ugfx.string_box(148, 78, 148, 18, " A: Run", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.string_box(148, 78, 148, 18, " B: Return to home", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyRight) ugfx.string_box(148, 92, 148, 18, " SELECT: Uninstall", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.line(148, 110, 296, 110, ugfx.BLACK) ugfx.string_box(148, 110, 148, 18, " " + version.name, "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) else: ugfx.line(0, ugfx.height() - 18 * 4, ugfx.width(), ugfx.height() - 18 * 4, ugfx.BLACK) ugfx.string_box(0, ugfx.height() - 18 * 4, ugfx.width(), 18, " A: Run", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.string_box(0, ugfx.height() - 18 * 3, ugfx.width(), 18, " B: Return to home", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.string_box(0, ugfx.height() - 18 * 2, ugfx.width(), 18, " SELECT: Uninstall", "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) ugfx.line(0, ugfx.height() - 18 * 1, ugfx.width(), ugfx.height() - 18 * 1, ugfx.BLACK) ugfx.string_box(0, ugfx.height() - 18 * 1, ugfx.width(), 18, " " + version.name, "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) global options global install_path options = None install_path = None ugfx.input_attach(ugfx.BTN_A, input_a) ugfx.input_attach(ugfx.BTN_B, input_b) ugfx.input_attach(ugfx.BTN_SELECT, input_select) ugfx.input_attach(ugfx.JOY_UP, input_other) ugfx.input_attach(ugfx.JOY_DOWN, input_other) ugfx.input_attach(ugfx.JOY_LEFT, input_other) ugfx.input_attach(ugfx.JOY_RIGHT, input_other) ugfx.input_attach(ugfx.BTN_START, input_other) populate_apps() populate_category() populate_options() # do a greyscale flush on start ugfx.flush(ugfx.GREYSCALE)
def viewmsg(): msgid = db.get('msgseq') msg = inbox.get(msgid) if msg == None: ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.area(0,0,ugfx.width(),ugfx.height(),0x0000) ugfx.text(40,100,"NO MESSAGES",ugfx.BLUE) pyb.delay(1000) return else: data = json.loads(msg) printmsg(data['sender'], data['payload'], data['ts']) while True: if buttons.is_triggered("JOY_UP"): print(msgid) msgid -= 1 msg = inbox.get(msgid) if msg != None: data = json.loads(msg) printmsg(data['sender'], data['payload'], data['ts']) else: msgid += 1 if buttons.is_triggered("JOY_DOWN"): print(msgid) msgid += 1 msg = inbox.get(msgid) if msg != None: data = json.loads(msg) printmsg(data['sender'], data['payload'], data['ts']) else: msgid -= 1 if buttons.is_triggered("BTN_B"): display() return
def msg_nosplit(message, title='Loading...', reset=False): global NUM_LINES """Show a terminal style loading screen with title title can be optionaly set when resetting or first call """ global messageHistory try: messageHistory if reset: raise exception except: ugfx.clear(ugfx.WHITE) ugfx.string(0, 0, title, version.font_header, ugfx.BLACK) messageHistory = [] if len(messageHistory) < NUM_LINES: ugfx.string(0, 19 + (len(messageHistory) * 13), message, version.font_default, ugfx.BLACK) messageHistory.append(message) else: messageHistory.pop(0) messageHistory.append(message) ugfx.area(0, 15, ugfx.width(), ugfx.height() - 15, ugfx.WHITE) for i, message in enumerate(messageHistory): ugfx.string(0, 19 + (i * 13), message, version.font_default, ugfx.BLACK) ugfx.flush(ugfx.LUT_FASTER)
def __init__(self): # initialize ugfx ugfx.init() ugfx.clear(ugfx.WHITE) # Buttons ugfx.input_init() self.init_buttons() # Container width = ugfx.width() height = ugfx.height() ind_height = 46 container_height = height - ind_height self.indicator = ugfx.Container(0, 0, width, ind_height, style=styles.ibm_st) self.container = ugfx.Container(0, ind_height, width, container_height, style=styles.ibm_st) self.graph_basepos = container_height - 5 # Sensor SCL = Pin(26) # SCL SDA = Pin(25) # SDA self.sensor = MPU6050(I2C(scl=SCL, sda=SDA)) # Buzzer self.buzzer = Buzzer()
def loading_screen(): logo = 'lobstervision/lobsterpictures.gif' ugfx.area(0, 0, ugfx.width(), ugfx.height(), 0xFFFF) ugfx.display_image(0, 50, logo) ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.text(15, 305, "lobstervision.tv/emf2018", ugfx.GREY) display_loading()
def loading_screen(): logo = 'praise_horse_worship_melon/loading.gif' ugfx.area(0,0,ugfx.width(),ugfx.height(),0xFFFF) ugfx.display_image(2,2,logo) ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.text(60, 145, "Praise Horse (A)", ugfx.GREY) ugfx.text(55, 305, "Worship Melon (B)", ugfx.GREY)
def setup_screen(self): """ Set up the screen and the labels that display values on it. """ ugfx.init() width=ugfx.width() height=ugfx.height() ugfx.clear(ugfx.html_color(0x800080)) style = ugfx.Style() style.set_enabled([ugfx.WHITE, ugfx.html_color(0x800080), ugfx.html_color(0x800080), ugfx.html_color(0x800080)]) style.set_background(ugfx.html_color(0x800080)) ugfx.set_default_style(style) ugfx.orientation(90) ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.Label(0, 0, width, 60,"Air Quality", justification=ugfx.Label.CENTER) label_height=45 self.ppm10_label = ugfx.Label(0, label_height, width, label_height,"PPM 10: starting", justification=ugfx.Label.CENTER) self.ppm25_label = ugfx.Label(0, label_height*2, width, label_height,"PPM 2.5: starting", justification=ugfx.Label.CENTER) self.temp_label = ugfx.Label(0, label_height*3, width, label_height,"Temp: starting", justification=ugfx.Label.CENTER) self.humid_label = ugfx.Label(0, label_height*4, width, label_height,"Humid: starting", justification=ugfx.Label.CENTER) self.error_label = ugfx.Label(0, label_height*5, width, label_height,"", justification=ugfx.Label.CENTER) self.message_label = ugfx.Label(0, label_height*6, width, label_height,"", justification=ugfx.Label.CENTER) self.error_count = 0 self.error_message = "" self.neopix = Neopix() self.p10_decode = ((100,0x00ff00),(250,0xffff00),(350,0xff8000),(430,0xff0000),(-1,0xcc6600)) self.p25_decode = ((60, 0x00ff00),(91, 0xffff00), (121,0xff8000),(251,0xff0000),(-1,0xcc6600))
def show_shift_detail(): global shifts global shift_list shift_list.visible(False) ugfx.clear(ugfx.WHITE) ugfx.flush() i = shift_list.selected_index() title = shifts[i]["name"] title_height = 20 if ugfx.get_string_width( title, "PermanentMarker22") <= ugfx.width() else 60 title = ugfx.string_box(0, 0, ugfx.width(), title_height, title, "PermanentMarker22", ugfx.BLACK, ugfx.justifyCenter) location = ugfx.string(0, title_height + 5, "Location: " + shifts[i]["Name"], "Roboto_Regular18", ugfx.BLACK) description = ugfx.string_box(0, title_height + 25, ugfx.width(), 40, "Description: " + shifts[i]["title"], "Roboto_Regular12", ugfx.BLACK, ugfx.justifyLeft) time = ugfx.string( 0, ugfx.height() - 20, "Time: " + generate_timedelta_text( int(shifts[i]["start"]), int(shifts[i]["end"])), "Roboto_Regular18", ugfx.BLACK) ugfx.flush() ugfx.input_attach(ugfx.BTN_B, lambda pressed: show_shift_list() if pressed else None)
def draw_everything(): # We draw the bird at X, and pipes every X*10 locations. ugfx.area(0,0,ugfx.width(), ugfx.height(), back_colour) draw_bird(10,y) for i in range(0,len(pipe_heights)): draw_pipe(10+i*10-x,pipe_heights[i]) #ugfx.text(30,10, "Bird %d,%d Pipe: %d,%d" % (x,y,pipe_heights[x//10],pipe_heights[x//10]+gap), ugfx.WHITE) ugfx.text(30,10, "Score %d" % (score), ugfx.WHITE)
def draw_background(self, colour): logo = 'apps/emf_hub_mon/emf_hub_mon.gif' ugfx.area(0, 0, ugfx.width(), ugfx.height(), colour) ugfx.area(0, 0, ugfx.width(), 25, ugfx.GREY) ugfx.display_image(10, 30, logo)
def display(): logo = 'apps/nexmo~messages/nexmo_logo.gif' ugfx.area(0,0,ugfx.width(),ugfx.height(),0xFFFF) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) ugfx.text(20,20,"My Number is...",ugfx.BLACK) ugfx.text(20,130,"Powered By, ",ugfx.GREY) ugfx.display_image(15,150,logo) ugfx.set_default_font(ugfx.FONT_TITLE) ugfx.text(40,75,mynumber+" ",ugfx.BLUE)
def drawTutorial(): ugfx.orientation(270) # Draw for user blankScreen() ugfx.text(5, 5, "Buttons: A = music, B = lights", ugfx.WHITE) ugfx.text(5, 25, "JoyStick Click = scrolling", ugfx.WHITE) ugfx.text(5, ugfx.height() - 20, "By Pez (@Pezmc)", ugfx.WHITE)
def draw_task(): global drawCallback #The function that allows us to hook into our host if drawCallback == None: return 100 #Don't draw if we can't hook global drawCallbacks #The functions of the services requestedInterval = 99999999 y = ugfx.height() drawCallback(False) # Prepare draw deleted = [] for i in range(0, len(drawCallbacks)): rqi = 0 try: cb = drawCallbacks[i].draw try: [rqi, space_used] = cb(y, False) except: [rqi, space_used] = cb(y) y = y - space_used except BaseException as e: print("[SERVICES] Exception in service draw:") sys.print_exception(e) deleted.append(cb) continue if rqi > 0 and rqi < requestedInterval: # Service wants to loop again in rqi ms requestedInterval = rqi elif rqi <= 0: # Service doesn't want to draw again until next wakeup deleted.append(cb) for i in range(0, len(deleted)): dcb = deleted[i] print("[DEBUG] Deleted draw callback: ", dcb) drawCallbacks = list(dcb for dcb in drawCallbacks if dcb != deleted[i]) if requestedInterval < 1000: #Draw at most once a second print("[SERVICES] Can't draw more than once a second!") requestedInterval = 1000 if requestedInterval >= 99999999: print("[SERVICES] No draw interval returned.") requestedInterval = -1 retVal = 0 if len(drawCallbacks) > 0 and requestedInterval >= 0: #print("[SERVICES] New draw requested in "+str(requestedInterval)) retVal = requestedInterval drawCallback(True) # Complete draw return retVal
def getdata(): server = 'badge.emf.camp' #url = 'http://'+server+':9002/schedule' #url = 'http://hackspace-leaderboard-scollins.c9users.io/schedule' url = 'http://api.ipify.org/' resp = get(url).text ugfx.area(0,0,ugfx.width(),ugfx.height(),0x0000) while True: ugfx.text(30,30,resp,ugfx.WHITE) return json.loads(resp)
def create_window(self, show=True): ugfx.clear(ugfx.BLACK) self.window = ugfx.Container(0, 0, ugfx.width(), ugfx.height(), style=styles.ibm_st) if show: self.window.show() return self.window
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 draw_flag(colours): # Orientation for other people to see ugfx.orientation(90) # Draw each "band" of colour in the flag colour_width = ugfx.width() / len(colours) for num, colour in enumerate(colours): width_loc = int(num * colour_width) flag_height = ugfx.height() - (name_height + info_height) ugfx.area(width_loc, info_height, int(colour_width), flag_height, ugfx.html_color(colour))
def draw_user_info(): # Draw for the user to see ugfx.orientation(270) # Calc width center of screen center_width = int(ugfx.width() / 2) ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.area(0, ugfx.height() - info_height, ugfx.width(), info_height, ugfx.WHITE) wifi_strength_value = homescreen.wifi_strength() if wifi_strength_value: wifi_message = 'WiFi: %s%%' % int(wifi_strength_value) ugfx.text(center_width, ugfx.height() - info_height, wifi_message, ugfx.BLACK) battery_value = homescreen.battery() if battery_value: battery_message = 'Battery: %s%%' % int(battery_value) ugfx.text(0, ugfx.height() - info_height, battery_message, ugfx.BLACK)
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 do_circle_of_life(): ugfx.init() buttons.init() buttons.disable_menu_reset() colour = get_colour() grid = Grid(ugfx.width(), ugfx.height(), colour_fore = colour, colour_back = COLOUR_BACK) grid.randomise() ugfx.clear(COLOUR_BACK) hash_count = 0 hash_last = None hash_last_last = None while True: # display hash_val = grid.display_badge() # randomise, if needed if hash_val == hash_last_last: hash_count += 1 if hash_count == HASH_COUNT_LIMIT: colour = get_colour(colour) grid.set_colour(colour) grid.randomise() hash_count = 0 else: hash_count = 0 hash_last_last = hash_last hash_last = hash_val # process next generation grid.next_generation() grid.swap_cell_buffers() # buttons pyb.wfi() if buttons.is_triggered("BTN_A") or buttons.is_triggered("BTN_B"): colour = get_colour(colour) grid.set_colour(colour) grid.randomise() if buttons.is_triggered("BTN_MENU"): break
def printmsg(sender, text, ts): ugfx.set_default_font(ugfx.FONT_SMALL) ugfx.area(0,0,ugfx.width(),ugfx.height(),0x0000) ugfx.text(10,10,"From: "+sender,ugfx.BLUE) timestamp = timestring(ts) linelen = 40 lines = int(math.ceil(len(text)/linelen)) for l in range(0, lines): pixels = l*25+35 start = l*linelen end = l*linelen+linelen if end>len(text): end = len(text) linetext = text[start:end] ugfx.text(10,pixels,linetext,0xFFFF) ugfx.text(10,200,timestamp,ugfx.GREEN) return
def prompt_boolean(text, title="TiLDA", true_text="Yes", false_text="No", width = 260, height = 180, font=ugfx.FONT_SMALL, style=None): """A simple one and two-options dialog if 'false_text' is set to None only one button is displayed. If both 'true_text' and 'false_text' are given a boolean is returned """ global default_style_dialog if style == None: style = default_style_dialog ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) window = ugfx.Container((ugfx.width() - width) // 2, (ugfx.height() - height) // 2, width, height, style=style) window.show() ugfx.set_default_font(font) window.text(5, 10, title, TILDA_COLOR) window.line(0, 30, width, 30, ugfx.BLACK) if false_text: true_text = "A: " + true_text false_text = "B: " + false_text ugfx.set_default_font(font) label = ugfx.Label(5, 30, width - 10, height - 80, text = text, parent=window) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) button_yes = ugfx.Button(5, height - 40, width // 2 - 15 if false_text else width - 15, 30 , true_text, parent=window) button_no = ugfx.Button(width // 2 + 5, height - 40, width // 2 - 15, 30 , false_text, parent=window) if false_text else None try: buttons.init() button_yes.attach_input(ugfx.BTN_A,0) if button_no: button_no.attach_input(ugfx.BTN_B,0) window.show() while True: pyb.wfi() if buttons.is_triggered("BTN_A"): return True if buttons.is_triggered("BTN_B"): return False finally: window.hide() window.destroy() button_yes.destroy() if button_no: button_no.destroy() label.destroy()
def showevent(stage, event): start = event['start_date'] end = event['end_date'] speaker = event['speaker'] text = event['title'] ugfx.set_default_font(ugfx.FONT_MEDIUM) ugfx.area(0,0,ugfx.width(),ugfx.height(),0x0000) ugfx.text(10,10,stage,ugfx.GREY) ugfx.text(10,35,"Start: "+start,ugfx.GREEN) ugfx.text(10,60,"End: "+end,ugfx.RED) ugfx.text(10,85,"Speaker: "+speaker,ugfx.BLUE) linelen = 25 lines = int(math.ceil(len(text)/linelen)) for l in range(0, lines): pixels = l*25+110 start = l*linelen end = l*linelen+linelen if end>len(text): end = len(text) linetext = text[start:end] ugfx.text(10,pixels,linetext,0xFFFF) return
import pyb import ugfx import buttons ugfx.init() buttons.init() BALLCOLOR1 = ugfx.RED BALLCOLOR2 = ugfx.YELLOW WALLCOLOR = ugfx.GREEN BACKCOLOR = ugfx.BLUE FLOORCOLOR = ugfx.PURPLE SHADOWALPHA = (255-255*0.2) width = ugfx.width() height = ugfx.height() radius=height/5+height%2+1 # The ball radius ii = 1.0/radius # radius as easy math floor=height/5-1 # floor position spin=0.0 # current spin angle on the ball spinspeed=0.1 # current spin speed of the ball ballx=width/2 # ball x position (relative to the ball center) bally=height/4 # ball y position (relative to the ball center) dx=.01*width # motion in the x axis dy=0.0 # motion in the y axis ballcx = 12*radius/5 # ball x diameter including the shadow ballcy = 21*radius/10 # ball y diameter including the shadow # The clipping window for this frame. minx = miny = 0
def mainscreen(): ugfx.area(0,0,ugfx.width(),ugfx.height(),0x0000) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) ugfx.text(30,30,"Whats My IP",ugfx.YELLOW) ugfx.text(40,75,"Press [A] to continue",ugfx.YELLOW) return
def draw_pipe(x,height): ugfx.area((x+1)*grid_size, 0, grid_size, (height+1)*grid_size, pipe_colour) ugfx.area((x+1)*grid_size, ((height+gap)*grid_size), grid_size, ugfx.height()-(height+gap)*grid_size, pipe_colour)
sha256.update(buf) buf = file.read(128) return str(binascii.hexlify(sha256.digest()), "utf8") except: return "ERR" def download(url, target, expected_hash): while True: get(url).raise_for_status().download_to(target) if calculate_hash(target) == expected_hash: break ugfx.init() ugfx.clear(ugfx.WHITE) ugfx.text(5, 5, "Downloading TiLDA software", ugfx.BLACK) label = ugfx.Label(5, 30, ugfx.width() - 10, ugfx.height() - 60, "Please wait") ugfx.Label(5, ugfx.height() - 30, ugfx.width() - 10, 30, "If nothing happens for 2 minutes please press the reset button on the back") w = {} try: if "wifi.json" in os.listdir(): with open("wifi.json") as f: w = json.loads(f.read()) except ValueError as e: print(e) if type(w) is dict: # convert original format json (dict, one network) to new (list, many networks) w = [w] wifi_info = "\nMore information:\nbadge.emfcamp.org/TiLDA_MK3/wifi"
import ugfx import pyb keepgoing = 1 tgl_menu = pyb.Pin("BTN_MENU", pyb.Pin.IN) tgl_menu.init(pyb.Pin.IN, pyb.Pin.PULL_UP) ugfx.enable_tear() tear = pyb.Pin("TEAR", pyb.Pin.IN) wi = ugfx.width() hi = ugfx.height() while(keepgoing): while(tear.value() == 0): 2+2 while(tear.value()): 2+2 ugfx.area(0,0,wi,hi,ugfx.RED) pyb.delay(60) while(tear.value() == 0): 2+2 while(tear.value()): 2+2 ugfx.area(0,0,wi,hi,ugfx.GREEN) pyb.delay(60) while(tear.value() == 0): 2+2 while(tear.value()): 2+2
import usocket, ujson from imu import IMU def cls(): ugfx.area(0, 0, 320, 240, ugfx.BLACK) c.hide() c.show() print("Stats app starting") # Initialise graphics print("Init uGFX") ugfx.init() ugfx.area(0, 0, ugfx.width(), ugfx.height(), ugfx.BLACK) ugfx.set_default_font(ugfx.FONT_TITLE) # Set up all the styles print("Init Styles") sty = ugfx.Style() sty.set_enabled([ugfx.PURPLE, ugfx.BLACK, ugfx.GREEN, ugfx.GREY]) sty.background(ugfx.BLACK) styUse = ugfx.Style() styUse.set_enabled([ugfx.PURPLE, ugfx.BLACK, ugfx.GREEN, ugfx.GREY]) styUse.background(ugfx.BLACK) styLbl = ugfx.Style()
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()
gc.collect() pyb.info() print("Loading: %s" % app_to_load) mod = __import__(app_to_load.main_path[:-3]) if "main" in dir(mod): mod.main() except Exception as e: s = uio.StringIO() sys.print_exception(e, s) u=pyb.USB_VCP() if u.isconnected(): raise(e) else: ugfx.clear() ugfx.set_default_font(ugfx.FONT_SMALL) w=ugfx.Container(0,0,ugfx.width(),ugfx.height()) l=ugfx.Label(0,0,ugfx.width(),ugfx.height(),s.getvalue(),parent=w) w.show() while True: pyb.wfi() if (buttons.is_triggered("BTN_B")) or (buttons.is_triggered("BTN_B")) or (buttons.is_triggered("BTN_MENU")): break; #str=s.getvalue().split("\n") #if len(str)>=4: #out = "\n".join(str[4:]) #dialogs.notice(out, width=wi-10, height=hi-10) onboard.semihard_reset() #deinit ugfx here #could hard reset here too
def mainscreen(): ugfx.area(0,0,ugfx.width(),ugfx.height(),0x0000) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) ugfx.text(30,30,"EMF Schedule Now & Next ",ugfx.GREY) ugfx.text(40,75,"Press [A] to get events ",ugfx.BLUE) return
def one_round(): grid_size = 8; body_colour = ugfx.RED back_colour = 0; food_colour = ugfx.YELLOW wall_colour = ugfx.BLUE score = 0; edge_x = math.floor(ugfx.width()/grid_size)-2; edge_y = math.floor(ugfx.height()/grid_size)-2; def disp_square(x,y,colour): ugfx.area((x+1)*grid_size, (y+1)*grid_size, grid_size, grid_size, colour) def disp_body_straight(x,y,rotation,colour): if (rotation == 0): ugfx.area((x+1)*grid_size+1, (y+1)*grid_size+1, grid_size-2, grid_size, colour) elif (rotation == 90): ugfx.area((x+1)*grid_size+1, (y+1)*grid_size+1, grid_size, grid_size-2, colour) elif (rotation == 180): ugfx.area((x+1)*grid_size+1, (y+1)*grid_size-1, grid_size-2, grid_size, colour) else: ugfx.area((x+1)*grid_size-1, (y+1)*grid_size+1, grid_size, grid_size-2, colour) def disp_eaten_food(x,y,colour): ugfx.area((x+1)*grid_size, (y+1)*grid_size, grid_size, grid_size, colour) def randn_square(): return [pyb.rng()%edge_x, pyb.rng()%edge_y] body_x = [12,13,14,15,16] body_y = [2,2,2,2,2] ugfx.area(0,0,ugfx.width(),ugfx.height(),0) ugfx.area(0,0,grid_size*(edge_x+1),grid_size,wall_colour) ugfx.area(0,0,grid_size,grid_size*(edge_y+1),wall_colour) ugfx.area(grid_size*(edge_x+1),0,grid_size,grid_size*(edge_y+1),wall_colour) ugfx.area(0,grid_size*(edge_y+1),grid_size*(edge_x+2),grid_size,wall_colour) keepgoing = 1; food = [20,20] dir_x = 1 dir_y = 0 orient = 270 #for i in range(0,len(body_x)): # disp_body_straight(body_x[i],body_y[i],orient,body_colour) while keepgoing: disp_square(food[0],food[1],food_colour) if buttons.is_pressed("JOY_RIGHT"): if orient != 90: dir_x = 1; dir_y = 0; orient = 270 elif buttons.is_pressed("JOY_LEFT"): if orient != 270: dir_x = -1; dir_y = 0; orient = 90 elif buttons.is_pressed("JOY_DOWN"): if orient != 0: dir_y = 1; dir_x = 0; orient = 180 elif buttons.is_pressed("JOY_UP"): if orient != 180: dir_y = -1; dir_x = 0; orient = 0 body_x.append(body_x[-1]+dir_x) body_y.append(body_y[-1]+dir_y) for i in range(0,len(body_x)-1): if (body_x[i] == body_x[-1]) and (body_y[i] == body_y[-1]): keepgoing = 0 if not((body_x[-1] == food[0]) and (body_y[-1] == food[1])): x_del = body_x.pop(0) y_del = body_y.pop(0) disp_eaten_food(x_del,y_del,back_colour) else: disp_eaten_food(food[0],food[1],body_colour) food = randn_square() disp_square(food[0],food[1],food_colour) score = score + 1 disp_body_straight(body_x[-1],body_y[-1],orient,body_colour) if ((body_x[-1] >= edge_x) or (body_x[-1] < 0) or (body_y[-1] >= edge_y) or (body_y[-1] < 0)): break pyb.delay(100) return score
def display_name(): ugfx.area(0,0,ugfx.width(),ugfx.height(),0xFFFF) ugfx.set_default_font("D*") ugfx.text(40,120,"MATT",ugfx.YELLOW) ugfx.circle(160,200,40,ugfx.GREEN)
import pyb import math import ugfx # Example of how a simple animation can be done # ToDo: This is quite flickery. It would work a lot better with # Pixmaps, but I couldn't get them to work :( ugfx.init() ugfx.enable_tear() tear = pyb.Pin("TEAR", pyb.Pin.IN) ugfx.set_tear_line((int(320/2)+0)) ugfx.area(0,0,ugfx.width(), ugfx.height(), 0) sec = 0; def draw_hand(cx, cy, angle, length, thickness, color): x = int(math.cos(angle) * length + cx); y = int(math.sin(angle) * length + cy); ugfx.thickline(cx, cy, x, y, color, thickness, 1) while True: # Center cx = int(ugfx.width() / 2); cy = int(ugfx.height() / 2); # Hand: hours angel_hour = math.pi * 2 * sec / 60 / 60 / 12 - math.pi / 2
def quick_launch_screen(): wi = ugfx.width() hi = ugfx.height() win_header = ugfx.Container(0,0,wi,30) win_quick = ugfx.Container(0,33,wi,hi-33-33) win_help = ugfx.Container(0,hi-30,wi,30) DEFAULT_APPS = ["app_library", "sponsors", "changename"] with Database() as db: pinned = [App(a) for a in db.get("pinned_apps", DEFAULT_APPS)] pinned = [app for app in pinned if app.loadable] # Filter out deleted apps pinned = pinned[:7] # Limit to 7 db.set("pinned_apps", [app.folder_name for app in pinned]) ugfx.set_default_font(ugfx.FONT_TITLE) title = ugfx.Label(3,3,wi-10,45,"EMF Camp 2016",parent=win_header) ugfx.set_default_font(ugfx.FONT_MEDIUM_BOLD) pinned_buttons = [] for i in range(0, 8): x = i % 2 y = i // 2 button_title = "View all" if i == 7 else "" if i < len(pinned): button_title = pinned[i].title pinned_buttons.append(ugfx.Button(35 + 155 * x, 5 + 40 * y, 120, 35, button_title, parent=win_quick)) btn_ok = ugfx.Button(10,5,20,20,"A",parent=win_help,shape=ugfx.Button.ELLIPSE) l_ok = ugfx.Label(40,5,100,20,"Run",parent=win_help) btn_back = ugfx.Button(100,5,20,20,"B",parent=win_help,shape=ugfx.Button.ELLIPSE) l_back = ugfx.Label(130,5,100,20,"Back",parent=win_help) btn_menu = ugfx.Button(200,5,20,20,"M",parent=win_help,shape=ugfx.Button.ROUNDED) l_back = ugfx.Label(230,5,100,20,"Menu",parent=win_help) sty = dialogs.default_style_badge win_header.show() win_quick.show() win_help.show() buttons.init() cursor = {"x": 0, "y": 0} last_cursor = cursor.copy() _draw_cursor(0, 0, ugfx.RED, win_quick) app_to_load = "sponsors" if not database_get("quicklaunch_firstrun"): dialogs.notice("""This screen displays the most commonly used apps. Apps pinned here can also interact with the name screen. To view all apps, pin and un-pin, select 'View All' """, title="TiLDA - Quick Launch", close_text="Close") database_set("quicklaunch_firstrun", True) try: while True: pyb.wfi() if buttons.is_triggered("JOY_UP"): cursor["y"] = max(0, cursor["y"] - 1) if buttons.is_triggered("JOY_DOWN"): cursor["y"] = min(3, cursor["y"] + 1) if buttons.is_triggered("JOY_RIGHT"): cursor["x"] = 1 if buttons.is_triggered("JOY_LEFT"): cursor["x"] = 0 if cursor["x"] != last_cursor["x"] or cursor["y"] != last_cursor["y"]: # Has the cursor moved? _draw_cursor(last_cursor["x"], last_cursor["y"], dialogs.default_style_badge.background(), win_quick) _draw_cursor(cursor["x"], cursor["y"], ugfx.RED, win_quick) last_cursor = cursor.copy() if buttons.is_triggered("BTN_B"): return None #if buttons.is_triggered("BTN_MENU"): # open unpin dialog # break; if buttons.is_triggered("BTN_A"): index = cursor["x"] + cursor["y"] * 2 if index == 7: return "file_loader" if index < len(pinned): return pinned[index] finally: buttons.disable_all_interrupt() win_header.hide() win_quick.hide() win_help.hide() for b in pinned_buttons: b.destroy() btn_ok.destroy() l_ok.destroy() btn_back.destroy() l_back.destroy() btn_menu.destroy() l_back.destroy() win_header.destroy() win_quick.destroy() win_help.destroy() title.destroy()
### Author: EMF Badge team ### Description: Test app for the hook into the home screen ### Category: Example ### License: MIT ### Appname : Home Callback Test import ugfx, buttons, pyb ugfx.init() buttons.init() ugfx.clear() ugfx.Label(5, 5, ugfx.width(), ugfx.height(), "Nothing to see here") while True: pyb.wfi()
break pyb.delay(100) return score playing = 1 while playing: score = one_round() new_high_score = False with Database() as db: high_score = db.get("improved_snake-high-score", 0) if score > high_score: db.set("improved_snake-high-score", score) new_high_score = True ugfx.area(0,0,ugfx.width(),ugfx.height(),0) ugfx.text(30, 30, "GAME OVER Score: %d" % (score), 0xFFFF) if new_high_score: ugfx.text(30, 60, "NEW HIGH SCORE!", 0xFFFF) else: ugfx.text(30, 60, "High score: %d" % (high_score), 0xFFFF) ugfx.text(30, 90, "Press A to play again", 0xFFFF) ugfx.text(30, 120, "Press MENU to quit" , 0xFFFF) while True: pyb.wfi() if buttons.is_triggered("BTN_A"): break if buttons.is_triggered("BTN_MENU"): playing = 0 #pyb.hard_reset() break