Example #1
0
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
Example #2
0
 def create_status_box(self, y=40):
     self.status_box = ugfx.Textbox(10,
                                    y,
                                    self.window.width() - 20,
                                    self.window.height() - y - 20,
                                    parent=self.window)
     self.status_box.enabled(False)
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 create_status_box(self, y=40):
     self.status_box = ugfx.Textbox(10,
                                    y,
                                    self.container.width() - 20,
                                    self.container.height() - y - 20,
                                    parent=self.container)
     self.status_box.enabled(False)
     self.status_box.visible(0)  # hide
Example #5
0
 def get_input(self, label, callback):
     ugfx.input_attach(ugfx.BTN_B, util.reboot)
     w = self.create_window()
     edit_size = 40
     ugfx.Textbox(5, 0, w.width() - 12, edit_size, text=label,
                  parent=w).enabled(False)
     self.kb_input = ugfx.Textbox(5,
                                  7 + edit_size,
                                  w.width() - 12,
                                  edit_size,
                                  parent=w)
     self.kb = ugfx.Keyboard(5,
                             edit_size * 2 + 12,
                             w.width() - 12,
                             w.height() - edit_size * 2 - 20,
                             parent=w)
     self.kb_callback = callback
     self.kb.set_keyboard_callback(self.kb_handler)
Example #6
0
 def create_textbox(self):
     y = 40
     if self.message_box != None:
         self.message_box.destroy()
     self.message_box = ugfx.Textbox(10,
                                     y,
                                     self.container.width() - 20,
                                     self.container.height() - y - 20,
                                     parent=self.container,
                                     style=self.popup_st)
     self.message_box.enabled(False)
     self.message_box.text(self.text)
Example #7
0
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
Example #9
0
    def main(self):
        self.create_window()
        w = self.window
        self.title()
        self.list_apps()

        self.desc = ugfx.Textbox(160, 105, 155, w.height() - 115, parent=w)
        self.widgets.append(self.desc)
        self.version = ugfx.Label(160, 50, 155, 25, text='', parent=w)
        self.widgets.append(self.version)
        self.btngroup = ButtonGroup(self.window, 160, 75, 75, 25, 5, False)
        self.btngroup.add('Install', self.install_app)
        self.btngroup.add('Delete', self.delete_app)
        self.btngroup.end()
        self.widgets.append(self.btngroup)
        self.update_desc()

        self.input_attach()
Example #10
0
def prompt_text(description,
                init_text="",
                true_text="OK",
                false_text="Back",
                width=ugfx.width(),
                height=ugfx.height(),
                font="Roboto_BlackItalic24",
                cb=None):
    """Shows a dialog and keyboard that allows the user to input/change a string

	Calls the 'cb' callback or return None if user aborts with button B. Using a callback is highly recommended as it's not
	possible to process events inside an event callback.

	The caller is responsible for flushing the display after processing the response.
	"""
    global wait_for_interrupt, button_pushed

    window = ugfx.Container(int((ugfx.width() - width) / 2),
                            int((ugfx.height() - height) / 2), width, height)
    window.show()

    ugfx.set_default_font("Roboto_Regular12")
    kb_height = int(height / 2) + 30
    kb = ugfx.Keyboard(0, height - kb_height, width, kb_height, parent=window)

    ugfx.set_default_font("Roboto_Regular18")
    edit_height = 25
    edit = ugfx.Textbox(5,
                        height - kb_height - 5 - edit_height,
                        int(width * 4 / 5) - 10,
                        edit_height,
                        text=init_text,
                        parent=window)
    ugfx.set_default_font("Roboto_Regular12")
    button_height = 25

    def done(result):
        window.destroy()
        if cb:
            cb(result)
        return result

    def syncSuccess(evt):
        if evt:
            # We'd like promises here, but for now this should do
            global wait_for_interrupt, button_pushed
            button_pushed = "A"
            wait_for_interrupt = False

    def syncCancel(evt):
        if evt:
            # We'd like promises here, but for now this should do
            global wait_for_interrupt, button_pushed
            button_pushed = "B"
            wait_for_interrupt = False

    def asyncSuccess(evt):
        if evt:
            done(edit.text())

    def asyncCancel(evt):
        if evt:
            done(None)

    button_yes = ugfx.Button(int(width * 4 / 5),
                             height - kb_height - button_height,
                             int(width * 1 / 5) - 3,
                             button_height,
                             true_text,
                             parent=window,
                             cb=asyncSuccess if cb else syncSuccess)
    button_no = ugfx.Button(int(width * 4 / 5),
                            height - kb_height - button_height - button_height,
                            int(width / 5) - 3,
                            button_height,
                            false_text,
                            parent=window) if false_text else None
    ugfx.set_default_font(font)
    label = ugfx.Label(5,
                       1,
                       int(width * 4 / 5),
                       height - kb_height - 5 - edit_height - 5,
                       description,
                       parent=window)

    def vkey_backspace():
        edit.backspace()
        ugfx.flush()

    focus = 0

    def toggle_focus(pressed):
        if pressed:
            if focus == 0:
                edit.set_focus()
                kb.enabled(1)
                ugfx.input_attach(
                    version.btn_cancel, lambda pressed: vkey_backspace()
                    if pressed else 0)
                ugfx.input_attach(
                    version.btn_ok, lambda pressed: 0
                    if pressed else ugfx.flush())
                focus = 1
            elif focus == 1 or not button_no:
                button_yes.set_focus()
                kb.enabled(0)
                ugfx.input_attach(version.btn_ok,
                                  asyncSuccess if cb else syncSuccess)
                ugfx.input_attach(version.btn_cancel,
                                  asyncCancel if cb else syncCancel)
                focus = (2 if button_no else 0)
            else:
                button_no.set_focus()
                kb.enabled(0)
                ugfx.input_attach(version.btn_ok,
                                  asyncCancel if cb else syncCancel)
                ugfx.input_attach(version.btn_cancel,
                                  asyncCancel if cb else syncCancel)
                focus = 0
            ugfx.flush()

    ugfx.input_init()
    ugfx.input_attach(ugfx.BTN_SELECT, toggle_focus)
    ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: ugfx.flush()
                      if pressed else 0)
    ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: ugfx.flush()
                      if pressed else 0)
    ugfx.input_attach(ugfx.JOY_UP, lambda pressed: ugfx.flush()
                      if pressed else 0)
    ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: ugfx.flush()
                      if pressed else 0)

    toggle_focus(True)

    ugfx.set_lut(ugfx.LUT_NORMAL)
    ugfx.flush()

    wait_for_interrupt = True
    if cb:
        return
    else:
        while wait_for_interrupt:
            time.sleep(0.2)

        if (focus == 0 and no_button) or button_pushed == "B":
            return done(False)
        return done(edit.text())
Example #11
0
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()

    wifi.nic().active(False)
    wifi.nic().active(True)

    # networks = [{ "ssid": ap[0], "mac": ap[1], "channel": ap[2], "signal": ap[3] } for ap in wifi.nic().scan()]
    networks = sorted([net for net in wifi.nic().scan() if net[0] == ssid],
                      key=lambda n: n[3],
def prompt_text(description,
                init_text="",
                true_text="OK",
                false_text="Back",
                width=300,
                height=200,
                font="Roboto_BlackItalic24"):
    """Shows a dialog and keyboard that allows the user to input/change a string

	Returns None if user aborts with button B

	The caller is responsible for flushing the display after processing the response.
	"""
    global wait_for_interrupt, button_pushed

    window = ugfx.Container(int((ugfx.width() - width) / 2),
                            int((ugfx.height() - height) / 2), width, height)
    window.show()

    ugfx.set_default_font("Roboto_Regular12")
    kb_height = int(height / 2) + 30
    kb = ugfx.Keyboard(0, height - kb_height, width, kb_height, parent=window)

    ugfx.set_default_font("Roboto_Regular18")
    edit_height = 25
    edit = ugfx.Textbox(5,
                        height - kb_height - 5 - edit_height,
                        int(width * 4 / 5) - 10,
                        edit_height,
                        text=init_text,
                        parent=window)
    ugfx.set_default_font("Roboto_Regular12")
    button_height = 25

    def okay(evt):
        # We'd like promises here, but for now this should do
        global wait_for_interrupt
        button_pushed = "A"
        wait_for_interrupt = False

    button_yes = ugfx.Button(int(width * 4 / 5),
                             height - kb_height - button_height,
                             int(width * 1 / 5) - 3,
                             button_height,
                             true_text,
                             parent=window,
                             cb=okay)
    button_no = ugfx.Button(int(width * 4 / 5),
                            height - kb_height - button_height - button_height,
                            int(width / 5) - 3,
                            button_height,
                            false_text,
                            parent=window) if false_text else None
    ugfx.set_default_font(font)
    label = ugfx.Label(5,
                       1,
                       int(width * 4 / 5),
                       height - kb_height - 5 - edit_height - 5,
                       description,
                       parent=window)

    def vkey_backspace():
        edit.backspace()
        ugfx.flush()

    focus = 0

    def toggle_focus(pressed):
        if pressed:
            if focus == 0:
                edit.set_focus()
                kb.enabled(1)
                ugfx.input_attach(
                    ugfx.BTN_B, lambda pressed: vkey_backspace()
                    if pressed else 0)
                ugfx.input_attach(
                    ugfx.BTN_A, lambda pressed: 0 if pressed else ugfx.flush())
                focus = 1
            elif focus == 1 or not button_no:
                button_yes.set_focus()
                kb.enabled(0)
                ugfx.input_attach(ugfx.BTN_A, pressed_a)
                ugfx.input_attach(ugfx.BTN_B, pressed_b)
                focus = (2 if button_no else 0)
            else:
                button_no.set_focus()
                kb.enabled(0)
                ugfx.input_attach(ugfx.BTN_A, pressed_a)
                ugfx.input_attach(ugfx.BTN_B, pressed_b)
                focus = 0
            ugfx.flush()

    ugfx.input_init()
    ugfx.input_attach(ugfx.BTN_SELECT, toggle_focus)
    ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: ugfx.flush()
                      if pressed else 0)
    ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: ugfx.flush()
                      if pressed else 0)
    ugfx.input_attach(ugfx.JOY_UP, lambda pressed: ugfx.flush()
                      if pressed else 0)
    ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: ugfx.flush()
                      if pressed else 0)

    toggle_focus(True)

    ugfx.set_lut(ugfx.LUT_NORMAL)
    ugfx.flush()

    wait_for_interrupt = True
    while wait_for_interrupt:
        time.sleep(0.2)

    def done(value):
        window.hide()
        window.destroy()
        button_yes.destroy()
        if button_no: button_no.destroy()
        label.destroy()
        kb.destroy()
        edit.destroy()
        return value

    if (focus == 0 and no_button) or button_pushed == "B": return done(False)
    return done(edit.text())
Example #13
0
ugfx.input_init()

window = ugfx.Container(0, 0, ugfx.width(), ugfx.height())

ugfx.input_attach(ugfx.JOY_UP, show_description)
ugfx.input_attach(ugfx.JOY_DOWN, show_description)
ugfx.input_attach(ugfx.BTN_A, woezel_it)
ugfx.input_attach(ugfx.BTN_B, woezel_it)

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()
# Smooth
Vos = 0.8
idx = 0

# init ugfx
ugfx.init()
ugfx.clear(ugfx.BLACK)

# Container
width = ugfx.width()
height = ugfx.height()
container = ugfx.Container(0, 0, width, height, style=styles.ibm_st)

y = 40
status_box = ugfx.Textbox(10, y, container.width() - 20, container.height() - y - 20, parent=container)
status_box.enabled(False)
status_box.visible(1) # hide

def readValue():
    global Voc, Vos, idx

    VoRaw = dust_sensor.readRawVo(220, 40) # adjusted 280 -> 220

    # Compute the output voltage in Volts.
    Vo = VoRaw / 4095.0 * 5.0

    # Exponential Moving Average
    # https://www.investopedia.com/terms/e/ema.asp
    # EMA(t) = Value(t)*k + EMA(y) * (1-k)
    # k = 2 / (N+1)