EMF_Camp_Badge/trains/main.py

93 lines
2.2 KiB
Python
Raw Permalink Normal View History

2018-09-09 08:14:30 -04:00
"""Mini train departure board for your badge
Configurable with which station you want to monitor
"""
___title___ = "trains"
___license___ = "MIT"
___dependencies___ = ["app", "sleep", "wifi", "http", "ugfx_helper"]
___categories___ = ["Homescreens", "Other"]
2018-09-15 17:20:20 -04:00
___bootstrapped___ = False
2018-09-09 08:14:30 -04:00
2018-09-15 17:20:20 -04:00
import database
2018-09-09 08:14:30 -04:00
import wifi
import ugfx
import app
import sleep
2018-09-15 17:20:20 -04:00
import ntp
2018-09-09 08:14:30 -04:00
from tilda import Buttons, LED
2018-09-15 17:20:20 -04:00
from trains import api
from trains import screen
from trains.departure_screen import DepartureScreen
from trains.settings_screen import SettingsScreen
2018-09-09 08:14:30 -04:00
2018-09-15 17:20:20 -04:00
def init_screen(orientation):
2018-09-12 17:26:48 -04:00
# initialize screen
ugfx.clear()
2018-09-15 17:20:20 -04:00
ugfx.orientation(orientation)
2018-09-12 17:26:48 -04:00
ugfx.backlight(50)
2018-09-15 17:20:20 -04:00
# show initial screen
# photo credit: https://www.flickr.com/photos/remedy451/8061918891
ugfx.display_image(0, 0, 'trains/splash.gif', 90)
def init():
print('trains/main: Init')
ugfx.init()
ntp.set_NTP_time()
2018-09-12 17:26:48 -04:00
# ensure wifi connection
if not wifi.is_connected():
2018-09-15 18:01:29 -04:00
wifi.connect(show_wait_message=True)
2018-09-09 08:14:30 -04:00
2018-09-15 17:20:20 -04:00
def exit():
print('trains/main: Exit')
ugfx.clear()
app.restart_to_default()
2018-09-12 17:26:48 -04:00
2018-09-15 17:20:20 -04:00
app_screens = {
screen.SETTINGS: SettingsScreen(),
screen.DEPARTURES: DepartureScreen()
}
2018-09-12 17:26:48 -04:00
2018-09-15 17:20:20 -04:00
def get_initial_screen():
station_code = database.get('trains.station_code', None)
if station_code == None:
return app_screens[screen.SETTINGS]
return app_screens[screen.DEPARTURES]
2018-09-12 17:26:48 -04:00
2018-09-15 17:20:20 -04:00
def run_screen(instance):
print('trains/main: Starting screen {}'.format(instance))
instance.enter()
2018-09-12 17:26:48 -04:00
2018-09-15 17:20:20 -04:00
is_running = True
next_screen_name = None
while is_running:
status, value = instance.tick()
2018-09-09 08:14:30 -04:00
2018-09-15 17:20:20 -04:00
if status == screen.SWITCH_SCREEN:
is_running = False
next_screen_name = value
elif status == screen.EXIT_APP:
is_running = False
2018-09-12 17:26:48 -04:00
2018-09-15 17:20:20 -04:00
print('trains/main: Stopping screen {} (next = {})'.format(instance, next_screen_name))
instance.exit()
return next_screen_name
2018-09-09 08:14:30 -04:00
2018-09-15 17:20:20 -04:00
init()
current_screen = get_initial_screen()
is_app_running = True
while is_app_running:
init_screen(current_screen.orientation())
next_screen_name = run_screen(current_screen)
if next_screen_name != None:
current_screen = app_screens[next_screen_name]
else:
is_app_running = False
exit()