From 9ab42858d751cf984eff1c050903f343a068ad08 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=A1clav=20Jel=C3=ADnek?= <jelinva4@fel.cvut.cz>
Date: Wed, 19 Mar 2025 14:28:29 +0100
Subject: [PATCH] Reset BT callback and send ko message to user when progarm
 downloading fails

---
 lib/menu.py | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/lib/menu.py b/lib/menu.py
index 354cd65..9706a14 100644
--- a/lib/menu.py
+++ b/lib/menu.py
@@ -190,7 +190,6 @@ def main():
                     program_name = cmd[4:]
                     if program_name in program_list:
                         program_num = program_list.index(program_name)
-                    print("BT user command: ", cmd, program_name)
                 elif cmd[:4] == "prg ":
                     program_name = cmd[4:]
                     if program_name.endswith(".py"):
@@ -208,8 +207,12 @@ def main():
                     robot.esp.bt_write("oc prg ok")
                     downloading = 2
                     return
-                with open(OC_TMP_FILE, "a") as f:
-                    f.write(next_line + "\n")
+                try:
+                    with open(OC_TMP_FILE, "a") as f:
+                        f.write(next_line + "\n")
+                except:
+                    downloading = 0
+                    robot.esp.bt_write("oc prg ko")
 
     # Initialize variables
     robot_state = 0
@@ -301,16 +304,19 @@ def main():
                         menu_debounce = 1
                 else:
                     if menu_move_v != 0:
-                        if current_menu >= Menu.COUNT:
+                        if current_menu >= Menu.COUNT: # == Menu.DOWNLOADING
                             pass
                         else:
                             menu_list[current_menu] += menu_move_v
                             menu_list[current_menu] %= menu_list_size[current_menu] 
                             menu_debounce = 1
                     elif menu_move_h != 0:
-                        if current_menu >= Menu.COUNT:
+                        if current_menu >= Menu.COUNT: # == Menu.DOWNLOADING
                             current_menu = Menu.PROGRAM
                             menu_list[current_menu] = 0
+                            downloading = 0
+                            robot.esp.bt_reset()
+                            robot.esp.bt_write("oc prg ko")
                         else:
                             menu_list[current_menu] = 0
                             current_menu += menu_move_h
@@ -446,7 +452,12 @@ def main():
 
         elif current_menu == Menu.DOWNLOAD:
             display_fill_downloading(robot, program_name)
-            if downloading == 2:
+            if downloading == 0:
+                robot.esp.bt_reset()
+                current_menu = Menu.PROGRAM
+                menu_list[current_menu] = 0
+                menu_debounce = 1
+            elif downloading == 2:
                 robot.esp.bt_reset()
                 downloading = 0
                 try:
-- 
GitLab