def cycle(): global state app.refresh_views() event = False while not event: event = state.do_step() app.refresh_views() time.sleep(0.01) state_code = state.on_event(event) state = load_state(state_code) GPIO.setmode(GPIO.BCM) app = Application() logging.getLogger().addHandler(app) state = load_state(Application.APP_STATE_INIT) while True: try: cycle() except KeyboardInterrupt: app.clean_views() logging.info("LEMPA stopped due to keyboard interrupt") break except Exception as e: logging.error(e) traceback.print_exc() state = load_state(Application.APP_STATE_EXCEPTION) app.error(e) GPIO.cleanup()