EMF_Camp_Badge/lib/homescreen.py

70 lines
2.0 KiB
Python
Raw Normal View History

2018-07-15 06:53:48 -04:00
"""Shared functionality for home screen apps
Apps in the "homescreen" should behave in a similar manner to not confuse users.
In particular, they *should*:
* Call "homescreen.init()" at the beginning. This will initiate ugfx, clear the screen and
initiate button handline.
* Use "sleep.wfi()" as much as possible to avoid draining the battery.
2018-07-15 06:53:48 -04:00
* Not use
They also *may*:
* Display a name, returned by "homescreen.name()"
* Display network strength "homescreen.wifi_strength()" (0-1, might return "None" if not connected)
* Display remaining battery "homescreen.battery()" (0-1)
"""
2018-08-05 17:14:31 -04:00
___license___ = "MIT"
2018-08-29 07:38:59 -04:00
___dependencies___ = ["database", "buttons", "random", "app", "sleep", "ugfx_helper", "wifi"]
2018-07-15 06:53:48 -04:00
import database, ugfx, random, buttons, tilda, sleep, ugfx_helper, wifi, time
2018-08-05 17:14:31 -04:00
from app import App
2018-07-15 06:53:48 -04:00
2018-08-06 16:45:35 -04:00
_state = None
def init(enable_menu_button = True):
global _state
_state = {"menu": False}
2018-08-26 15:27:23 -04:00
ugfx_helper.init()
2018-08-06 16:45:35 -04:00
if enable_menu_button:
2018-08-24 18:00:09 -04:00
pass
#buttons.enable_interrupt("BTN_MENU", lambda t: set_state("menu"), on_release = True)
2018-08-06 16:45:35 -04:00
def set_state(key, value = True):
2018-08-07 17:41:33 -04:00
# we can't allocate memory in interrupts, so make sure all keys are set beforehand and
# you're only using numbers and booleans
2018-08-06 16:45:35 -04:00
global _state
_state[key] = value
def clean_up():
pass
2018-08-06 16:45:35 -04:00
def time_as_string(seconds=False):
t = time.localtime()
if seconds:
return "%d:%02d:%02d" % (t[3], t[4], t[5])
return "%d:%02d" % (t[3], t[4]) #todo: add a setting for AM/PM mode
2018-08-24 18:00:09 -04:00
def sleep_or_exit(interval = 0.5):
# todo: do this better - check button multiple times and sleep for only a short while
if buttons.is_triggered(tilda.Buttons.BTN_Menu):
2018-08-06 16:45:35 -04:00
clean_up()
App("launcher").boot()
2018-08-24 18:00:09 -04:00
sleep.sleep(interval)
2018-08-06 16:45:35 -04:00
2018-07-15 06:53:48 -04:00
2018-08-05 17:14:31 -04:00
def name(default = None):
return database.get("homescreen.name", default)
2018-07-15 06:53:48 -04:00
# Strength in %, None if unavailable
2018-07-15 06:53:48 -04:00
def wifi_strength():
return wifi.get_strength()
2018-07-15 06:53:48 -04:00
# Charge in %, None if unavailable
2018-07-15 06:53:48 -04:00
def battery():
return None # todo: fix me, we can get this from the sim800
2018-07-15 06:53:48 -04:00