def menu(title, items, selected=0, text="", width=32):
    clear()
    lastSelected = selected
    needFullDraw = True
    while True:
        if needFullDraw:
            draw_menu(title, items, selected, text, width)
        else:
            draw_menu_partial(title, items, selected, text, width,
                              lastSelected)
        lastSelected = selected
        key = None
        while not key:
            key = machine.stdin_get(1, 1)  #sys.stdin.read(1)
        feedPm()
        try:
            if key == "\x03" or key == "\x04":  # CTRL+C or CTRL+D
                import shell
            if (ord(key) == 0x1b):
                key = sys.stdin.read(1)
                if (key == "["):
                    key = sys.stdin.read(1)
                    if (key == "A"):
                        if (selected > 0):
                            selected -= 1
                            needFullDraw = False
                    if (key == "B"):
                        if (selected < len(items) - 1):
                            selected += 1
                            needFullDraw = False
            elif (ord(key) == 0x01):
                import tasks.powermanagement as pm  #, badge
                pm.disable()
                draw_menu(title, items, selected, text)
                pm.resume()

            elif (key == "\n" or key == "\r"):
                junk = machine.stdin_get(
                    10000,
                    10)  #Read all remaining characters and throw them away
                return selected
            else:
                clear()
                needFullDraw = True  #Refresh on any other key
        except BaseException as e:  # Input parser crashed for some reason
            clear()
            needFullDraw = True
            sys.print_exception(e)
            time.sleep(2)
            junk = machine.stdin_get(
                10000, 10)  #Read all remaining characters and throw them away
led_app = None
if cfg_led_animation != None:
	try:
		led_app = __import__('/lib/'+cfg_led_animation+'/ledsrv')
		try:
			led_app.init()
		except:
			pass
	except:
		pass
else:
	_thread.start_new_thread("LED", ledThread, ())

# Terminal menu

if cfg_term_menu:
	umenu = term_menu.UartMenu(cbSleep, pm, False)
	umenu.main()
else:
	print("Welcome!")
	print("The homescreen and it's services are currently being run.")
	print("Press CTRL+C to reboot directly to a Python prompt.")
	wait = True
	while wait:
		c = machine.stdin_get(1,1)
		if c == "\x03" or c == "\x04": # CTRL+C or CTRL+D
			wait = False
	stopThreads = True
	import shell