From 46901642b2b751569fe6a9ff4c42b78e61049295 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Fri, 16 Oct 2020 21:47:57 +0200
Subject: [PATCH] feat(pycardium): Return to menu when a script ends cleanly

Instead of dropping to REPL and silently requiring the user to return to
menu via button-press, directly call epic_exit(0) when a script
terminates execution normally (i.e. not via exception).

Signed-off-by: Rahix <rahix@rahix.de>
---
 pycardium/main.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/pycardium/main.c b/pycardium/main.c
index 7aa126186..f6c2933bc 100644
--- a/pycardium/main.c
+++ b/pycardium/main.c
@@ -64,10 +64,22 @@ int main(void)
 	mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_apps));
 
 	if (cnt > 0) {
-		pyexec_file_if_exists(script_name);
+		int ret = pyexec_file_if_exists(script_name);
+
+		if (ret == 0) {
+			/*
+			 * The script was aborted via uncaught exception; Sadly
+			 * we can't find out what happened so let's hope it was
+			 * a KeyboardInterrupt and drop to REPL.
+			 *
+			 * In the future it might be interesting to rework this
+			 * so any other exception will lead to epic_exit(1).
+			 */
+			epic_exec("");
+		}
 
-		/* Drop to REPL by reloading Pycardium */
-		epic_exec("");
+		/* The script ended execution normally; return to menu */
+		epic_exit(0);
 	}
 
 	for (;;) {
-- 
GitLab