#!/home/pi/protovac/env/bin/python import os import logging import curses import time import tui import utils DEBUG = os.environ.get('DEBUG') logging.basicConfig( filename='protovax.log', format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s', level=logging.DEBUG if DEBUG else logging.INFO) logging.info('') logging.info('Boot up') os.system('stty -ixon') def main_loop(stdscr): state = tui.AppState() screens = { 'home': tui.HomeScreen(state, stdscr), 'debug': tui.DebugScreen(state, stdscr), 'stats': tui.StatsScreen(state, stdscr), 'classes': tui.ClassesScreen(state, stdscr), 'asimov': tui.TextScreen(state, stdscr, "The Last Question", utils.LAST_QUESTION), 'info': tui.TextScreen(state, stdscr, "Info", utils.PROTO_INFO), 'protocoin': tui.ProtocoinScreen(state, stdscr), 'sign': tui.SignScreen(state, stdscr), 'protovac_sign': tui.ProtovacSignScreen(state, stdscr), 'train': tui.TrainScreen(state, stdscr), 'nametag': tui.NametagScreen(state, stdscr), 'label': tui.LabelScreen(state, stdscr), 'games': tui.GamesScreen(state, stdscr), 'message': tui.MessageScreen(state, stdscr), 'think': tui.ThinkScreen(state, stdscr), 'about': tui.AboutScreen(state, stdscr), 'help': tui.HelpScreen(state, stdscr), } logging.info('Starting main loop...') while True: c = stdscr.getch() if c != curses.ERR: # Rate limit key presses unless in a text input field if not screens[state.current_screen].is_typing and time.time() < state.last_key_time + 1: pass # Key press ignored due to rate limit else: state.c = c state.last_key_time = time.time() screens[state.current_screen].handle_input(c) if state.current_screen != state.prev_screen: logging.info('Switching to screen: %s', state.current_screen) stdscr.erase() if hasattr(screens[state.current_screen], 'on_enter'): screens[state.current_screen].on_enter() if state.current_screen != 'help': state.highlight_count = 0 state.prev_screen = state.current_screen screens[state.current_screen].draw() stdscr.move(23, 79) stdscr.refresh() if state.highlight_keys: time.sleep(0.5) state.highlight_keys = False time.sleep(0.01) def main(): stdscr = curses.initscr() curses.noecho() curses.cbreak() stdscr.keypad(True) curses.curs_set(0) stdscr.nodelay(True) try: main_loop(stdscr) except KeyboardInterrupt: pass except Exception as e: logging.exception("Caught exception in main loop") finally: curses.nocbreak() stdscr.keypad(False) curses.echo() curses.endwin() logging.info('Exiting.') if __name__ == '__main__': main()