Просмотр исходного кода

allow script execution with py command

Oliver Fabel 1 год назад
Родитель
Сommit
ea5bf08197
9 измененных файлов с 108 добавлено и 75 удалено
  1. 6 1
      CHANGELOG.md
  2. 1 1
      lib/micropython
  3. 3 5
      lib/micropython-port/mp_flipper_file_reader.c
  4. 26 24
      upython.c
  5. 7 3
      upython.h
  6. 59 0
      upython_cli.c
  7. 5 9
      upython_file.c
  8. 1 30
      upython_repl.c
  9. 0 2
      upython_splash.c

+ 6 - 1
CHANGELOG.md

@@ -10,9 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 ### Added
 
 
 * Allow passing the path to the script to execute as a CLI argument.
 * Allow passing the path to the script to execute as a CLI argument.
-* Open a REPL from the CLI interface by using the `py` command.
+* Open a REPL from the CLI interface by using the `py` command:
   * The `py` command is only available while the app is running.
   * The `py` command is only available while the app is running.
   * You cannot run a Python script and use the REPL at the same time.
   * You cannot run a Python script and use the REPL at the same time.
+  * You can also start a Python script with the `py` command while the app is idle.
+
+### Changed
+
+* MicroPython update to version 1.23.0.
 
 
 ## [1.3.0] - 2024-09-08
 ## [1.3.0] - 2024-09-08
 
 

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit f84fb358ce3e6bc0c868c48b4d3391b8b7a3b07c
+Subproject commit 55fca52c45c820244147bfb3a54bd9801635974e

+ 3 - 5
lib/micropython-port/mp_flipper_file_reader.c

@@ -22,14 +22,12 @@ inline void* mp_flipper_file_reader_context_alloc(const char* filename) {
 
 
     do {
     do {
         if(mp_flipper_try_resolve_filesystem_path(path) == MP_FLIPPER_IMPORT_STAT_NO_EXIST) {
         if(mp_flipper_try_resolve_filesystem_path(path) == MP_FLIPPER_IMPORT_STAT_NO_EXIST) {
-            furi_string_free(path);
-
             mp_flipper_raise_os_error_with_filename(MP_ENOENT, filename);
             mp_flipper_raise_os_error_with_filename(MP_ENOENT, filename);
+
+            break;
         }
         }
 
 
         if(!storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
         if(!storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
-            storage_file_free(file);
-
             mp_flipper_raise_os_error_with_filename(MP_ENOENT, filename);
             mp_flipper_raise_os_error_with_filename(MP_ENOENT, filename);
 
 
             break;
             break;
@@ -73,4 +71,4 @@ void mp_flipper_file_reader_close(void* data) {
     furi_string_free(ctx->content);
     furi_string_free(ctx->content);
 
 
     free(data);
     free(data);
-}
+}

+ 26 - 24
upython.c

@@ -12,42 +12,44 @@
 
 
 #include "upython.h"
 #include "upython.h"
 
 
+Action action = ActionNone;
+FuriString* file_path = NULL;
+
 int32_t upython(void* args) {
 int32_t upython(void* args) {
-    if(args == NULL) {
-        mp_flipper_repl_register();
-    } else {
-        action = ActionOpen;
-    }
+    mp_flipper_cli_register(args);
 
 
     do {
     do {
-        if(args == NULL && mp_flipper_splash_screen() == ActionExit) {
-            break;
-        }
+        switch(action) {
+        case ActionNone:
+            action = mp_flipper_splash_screen();
 
 
-        if(action == ActionOpen) {
-            FuriString* file_path = NULL;
-
-            if(args != NULL) {
-                file_path = furi_string_alloc_set_str(args);
+            break;
+        case ActionOpen:
+            if(mp_flipper_select_python_file(file_path)) {
+                action = ActionExec;
             } else {
             } else {
-                file_path = furi_string_alloc_set_str(APP_ASSETS_PATH("upython"));
+                furi_string_set(file_path, APP_ASSETS_PATH("upython"));
             }
             }
 
 
-            if(args != NULL || mp_flipper_select_python_file(file_path)) {
-                mp_flipper_file_execute(file_path);
-            }
+            break;
+        case ActionRepl:
+            break;
+        case ActionExec:
+            mp_flipper_file_execute(file_path);
 
 
-            furi_string_free(file_path);
-        }
+            furi_string_set(file_path, APP_ASSETS_PATH("upython"));
 
 
-        if(args != NULL) {
+            action = ActionNone;
+
+            break;
+        case ActionExit:
             break;
             break;
         }
         }
-    } while(true);
 
 
-    if(args == NULL) {
-        mp_flipper_repl_unregister();
-    }
+        furi_delay_ms(1);
+    } while(action != ActionExit);
+
+    mp_flipper_cli_unregister(args);
 
 
     return 0;
     return 0;
 }
 }

+ 7 - 3
upython.h

@@ -4,6 +4,7 @@
 #include <furi.h>
 #include <furi.h>
 
 
 #define TAG "uPython"
 #define TAG "uPython"
+#define CLI "py"
 
 
 typedef enum {
 typedef enum {
     ActionNone,
     ActionNone,
@@ -14,13 +15,16 @@ typedef enum {
 } Action;
 } Action;
 
 
 extern Action action;
 extern Action action;
+extern FuriString* file_path;
 
 
 Action mp_flipper_splash_screen();
 Action mp_flipper_splash_screen();
 bool mp_flipper_select_python_file(FuriString* file_path);
 bool mp_flipper_select_python_file(FuriString* file_path);
 
 
-void mp_flipper_repl_register();
-void mp_flipper_repl_unregister();
+void mp_flipper_cli_register(void* args);
+void mp_flipper_cli_unregister(void* args);
 
 
-void mp_flipper_repl_execute(Cli* cli, FuriString* args, void* ctx);
+void mp_flipper_cli(Cli* cli, FuriString* args, void* ctx);
+
+void mp_flipper_repl_execute(Cli* cli);
 
 
 void mp_flipper_file_execute(FuriString* file);
 void mp_flipper_file_execute(FuriString* file);

+ 59 - 0
upython_cli.c

@@ -0,0 +1,59 @@
+#include <storage/storage.h>
+
+#include "upython.h"
+
+void mp_flipper_cli(Cli* cli, FuriString* args, void* ctx) {
+    UNUSED(ctx);
+
+    if(action != ActionNone) {
+        printf("%s is busy!\n", TAG);
+
+        return;
+    }
+
+    if(furi_string_empty(args)) {
+        action = ActionRepl;
+
+        mp_flipper_repl_execute(cli);
+
+        action = ActionNone;
+    } else {
+        furi_string_set(file_path, args);
+
+        action = ActionExec;
+    }
+}
+
+void mp_flipper_cli_register(void* args) {
+    if(args != NULL) {
+        file_path = furi_string_alloc_set_str(args);
+
+        action = ActionExec;
+
+        return;
+    } else {
+        file_path = furi_string_alloc_set_str(APP_ASSETS_PATH("upython"));
+
+        action = ActionNone;
+    }
+
+    Cli* cli = furi_record_open(RECORD_CLI);
+
+    cli_add_command(cli, CLI, CliCommandFlagParallelSafe, mp_flipper_cli, NULL);
+
+    furi_record_close(RECORD_CLI);
+}
+
+void mp_flipper_cli_unregister(void* args) {
+    furi_string_free(file_path);
+
+    if(args != NULL) {
+        return;
+    }
+
+    Cli* cli = furi_record_open(RECORD_CLI);
+
+    cli_delete_command(cli, CLI);
+
+    furi_record_close(RECORD_CLI);
+}

+ 5 - 9
upython_file.c

@@ -9,12 +9,6 @@
 #include "upython.h"
 #include "upython.h"
 
 
 void mp_flipper_file_execute(FuriString* file) {
 void mp_flipper_file_execute(FuriString* file) {
-    if(action == ActionOpen) {
-        action = ActionExec;
-    } else {
-        return;
-    }
-
     size_t stack;
     size_t stack;
 
 
     const char* path = furi_string_get_cstr(file);
     const char* path = furi_string_get_cstr(file);
@@ -32,7 +26,11 @@ void mp_flipper_file_execute(FuriString* file) {
 
 
         size_t index = furi_string_search_rchar(file_path, '/');
         size_t index = furi_string_search_rchar(file_path, '/');
 
 
-        furi_check(index != FURI_STRING_FAILURE);
+        if(index == FURI_STRING_FAILURE) {
+            FURI_LOG_E(TAG, "invalid file path");
+
+            break;
+        }
 
 
         bool is_py_file = furi_string_end_with_str(file_path, ".py");
         bool is_py_file = furi_string_end_with_str(file_path, ".py");
 
 
@@ -52,6 +50,4 @@ void mp_flipper_file_execute(FuriString* file) {
     } while(false);
     } while(false);
 
 
     furi_string_free(file_path);
     furi_string_free(file_path);
-
-    action = ActionNone;
 }
 }

+ 1 - 30
upython_repl.c

@@ -243,20 +243,9 @@ inline static bool continue_with_input(mp_flipper_repl_context_t* context) {
     return true;
     return true;
 }
 }
 
 
-void mp_flipper_repl_execute(Cli* cli, FuriString* args, void* ctx) {
-    if(action == ActionNone) {
-        action = ActionRepl;
-    } else {
-        printf("%s is busy - cannot start REPL!\n", TAG);
-
-        return;
-    }
-
+void mp_flipper_repl_execute(Cli* cli) {
     size_t stack;
     size_t stack;
 
 
-    UNUSED(args);
-    UNUSED(ctx);
-
     const size_t heap_size = memmgr_get_free_heap() * 0.1;
     const size_t heap_size = memmgr_get_free_heap() * 0.1;
     const size_t stack_size = 2 * 1024;
     const size_t stack_size = 2 * 1024;
     uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
     uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
@@ -406,22 +395,4 @@ void mp_flipper_repl_execute(Cli* cli, FuriString* args, void* ctx) {
 
 
     free(heap);
     free(heap);
     free(buffer);
     free(buffer);
-
-    action = ActionNone;
-}
-
-void mp_flipper_repl_register() {
-    Cli* cli = furi_record_open(RECORD_CLI);
-
-    cli_add_command(cli, "py", CliCommandFlagParallelSafe, mp_flipper_repl_execute, NULL);
-
-    furi_record_close(RECORD_CLI);
-}
-
-void mp_flipper_repl_unregister() {
-    Cli* cli = furi_record_open(RECORD_CLI);
-
-    cli_delete_command(cli, "py");
-
-    furi_record_close(RECORD_CLI);
 }
 }

+ 0 - 2
upython_splash.c

@@ -13,8 +13,6 @@
 #include "upython.h"
 #include "upython.h"
 #include "upython_icons.h"
 #include "upython_icons.h"
 
 
-Action action = ActionNone;
-
 bool mp_flipper_select_python_file(FuriString* file_path) {
 bool mp_flipper_select_python_file(FuriString* file_path) {
     DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
     DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);