Ejemplo n.º 1
0
def backlight_adjust():
    if ugfx.backlight() == 0:
        ugfx.power_mode(ugfx.POWER_ON)
    l = pyb.ADC(16).read()
    if (l > 90):
        ugfx.backlight(100)
    elif (l > 20):
        ugfx.backlight(70)
    else:
        ugfx.backlight(30)
Ejemplo n.º 2
0
def backlight_adjust():
	if ugfx.backlight() == 0:
		ugfx.power_mode(ugfx.POWER_ON)
	l = pyb.ADC(16).read()
	if (l > 90):
		ugfx.backlight(100)
	elif (l > 20):
		ugfx.backlight(70)
	else:
		ugfx.backlight(30)
Ejemplo n.º 3
0
 def __init__(self):
     ugfx.init()
     if ugfx.backlight() == 0:
         ugfx.power_mode(ugfx.POWER_ON)
     ugfx.backlight(70)
     self.rm = chuckie.render.RenderManager()
     self.raster = raster.RasterTile()
     self._tick = time.ticks_ms()
     self._delta = 0
     buttons.init()
Ejemplo n.º 4
0
def low_power():
    ugfx.backlight(0)
    ugfx.power_mode(ugfx.POWER_OFF)
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
def low_power():
	ugfx.backlight(0)
	ugfx.power_mode(ugfx.POWER_OFF)
Ejemplo n.º 7
0
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()