Oliver Fabel 1 год назад
Родитель
Сommit
cbec46b8d8
5 измененных файлов с 47 добавлено и 93 удалено
  1. 3 0
      application.fam
  2. 1 1
      lib/micropython
  3. 4 0
      lib/micropython-port/mp_flipper_halport.c
  4. 12 45
      mp_flipper_app.c
  5. 27 47
      py_app.c

+ 3 - 0
application.fam

@@ -15,6 +15,7 @@ App(
     cdefines=[
     cdefines=[
         "MP_FLIPPER_RUNTIME",
         "MP_FLIPPER_RUNTIME",
         "MP_FLIPPER_COMPILER",
         "MP_FLIPPER_COMPILER",
+        "MP_FLIPPER_SPLIT_HEAP",
     ],
     ],
     sources=[
     sources=[
         "*.c*",
         "*.c*",
@@ -26,6 +27,7 @@ App(
             cdefines=[
             cdefines=[
                 "MP_FLIPPER_RUNTIME",
                 "MP_FLIPPER_RUNTIME",
                 "MP_FLIPPER_COMPILER",
                 "MP_FLIPPER_COMPILER",
+                "MP_FLIPPER_SPLIT_HEAP",
             ],
             ],
             cflags=[
             cflags=[
                 "-Wno-error",
                 "-Wno-error",
@@ -47,6 +49,7 @@ App(
             cdefines=[
             cdefines=[
                 "MP_FLIPPER_RUNTIME",
                 "MP_FLIPPER_RUNTIME",
                 "MP_FLIPPER_COMPILER",
                 "MP_FLIPPER_COMPILER",
+                "MP_FLIPPER_SPLIT_HEAP",
             ],
             ],
             cflags=[
             cflags=[
                 "-Wno-error",
                 "-Wno-error",

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit 5cf27042ab76cd5aed51fe85457bf401c9130656
+Subproject commit 89a4d70de2a67851d41473ebff08ad4d9504e72c

+ 4 - 0
lib/micropython-port/mp_flipper_halport.c

@@ -27,3 +27,7 @@ inline mp_flipper_import_stat_t mp_flipper_import_stat(const char* path) {
 
 
     return stat;
     return stat;
 }
 }
+
+size_t gc_get_max_new_split(void) {
+    return memmgr_heap_get_max_free_block();
+}

+ 12 - 45
mp_flipper_app.c

@@ -9,17 +9,14 @@
 #include "mp_flipper_app.h"
 #include "mp_flipper_app.h"
 #include "py_app.h"
 #include "py_app.h"
 
 
-const char *root_module_path;
+const char* root_module_path;
 
 
-ViewPort *mp_flipper_view_port;
-FuriMessageQueue *mp_flipper_event_queue;
+ViewPort* mp_flipper_view_port;
+FuriMessageQueue* mp_flipper_event_queue;
 
 
-static void load_python_file(FuriString *file_path, FuriString *code)
-{
-    Storage *storage = furi_record_open(RECORD_STORAGE);
-    DialogsApp *dialogs = furi_record_open(RECORD_DIALOGS);
+static bool select_python_file(FuriString* file_path) {
+    DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
 
 
-    File *file = storage_file_alloc(storage);
     DialogsFileBrowserOptions browser_options;
     DialogsFileBrowserOptions browser_options;
 
 
     dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
     dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
@@ -29,51 +26,21 @@ static void load_python_file(FuriString *file_path, FuriString *code)
 
 
     bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
     bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
 
 
-    if (result)
-    {
-        result = storage_file_open(file, furi_string_get_cstr(file_path), FSAM_READ, FSOM_OPEN_EXISTING);
-    }
-
-    if (result)
-    {
-        size_t read = 0;
-        do
-        {
-            uint8_t buffer[64] = {'\0'};
-
-            read = storage_file_read(file, buffer, sizeof(buffer) - 1);
-
-            for (size_t i = 0; i < read; i++)
-            {
-                furi_string_push_back(code, buffer[i]);
-            }
-        } while (read > 0);
-
-        furi_string_trim(code);
-    }
-    else
-    {
-        furi_string_set(code, "print('it works!')");
-    }
-
-    storage_file_free(file);
     furi_record_close(RECORD_DIALOGS);
     furi_record_close(RECORD_DIALOGS);
-    furi_record_close(RECORD_STORAGE);
+
+    return result;
 }
 }
 
 
-int32_t mp_flipper_app(void *p)
-{
+int32_t mp_flipper_app(void* p) {
     UNUSED(p);
     UNUSED(p);
 
 
-    FuriString *file_path = furi_string_alloc();
-    FuriString *code = furi_string_alloc();
+    FuriString* file_path = furi_string_alloc();
 
 
-    load_python_file(file_path, code);
-
-    py_app((void *)furi_string_get_cstr(file_path));
+    if(select_python_file(file_path)) {
+        py_app_file_execute(file_path);
+    }
 
 
     furi_string_free(file_path);
     furi_string_free(file_path);
-    furi_string_free(code);
 
 
     return 0;
     return 0;
 }
 }

+ 27 - 47
py_app.c

@@ -9,14 +9,12 @@
 #define PYTHON_SETTINGS_VERSION 0x01
 #define PYTHON_SETTINGS_VERSION 0x01
 #define PYTHON_SETTINGS_PATH INT_PATH(".python.settings")
 #define PYTHON_SETTINGS_PATH INT_PATH(".python.settings")
 
 
-int32_t py_app(void *arg)
-{
-    if (arg == NULL || strlen(arg) == 0)
-    {
+int32_t py_app(void* arg) {
+    if(arg == NULL || strlen(arg) == 0) {
         return 0;
         return 0;
     }
     }
 
 
-    FuriString *file_path = furi_string_alloc_printf("%s", (const char *)arg);
+    FuriString* file_path = furi_string_alloc_printf("%s", (const char*)arg);
 
 
     py_app_file_execute(file_path);
     py_app_file_execute(file_path);
 
 
@@ -25,21 +23,19 @@ int32_t py_app(void *arg)
     return 0;
     return 0;
 }
 }
 
 
-void py_app_file_execute(FuriString *file)
-{
+void py_app_file_execute(FuriString* file) {
     size_t stack;
     size_t stack;
 
 
-    PyApp *app = py_app_alloc();
-    const char *path = furi_string_get_cstr(file);
-    FuriString *file_path = furi_string_alloc_printf("%s", path);
+    PyApp* app = py_app_alloc();
+    const char* path = furi_string_get_cstr(file);
+    FuriString* file_path = furi_string_alloc_printf("%s", path);
 
 
-    do
-    {
+    do {
         FURI_LOG_I(TAG, "executing script %s", path);
         FURI_LOG_I(TAG, "executing script %s", path);
 
 
         const size_t heap_size = memmgr_get_free_heap() * app->heap_size;
         const size_t heap_size = memmgr_get_free_heap() * app->heap_size;
         const size_t stack_size = app->stack_size;
         const size_t stack_size = app->stack_size;
-        uint8_t *heap = malloc(heap_size * sizeof(uint8_t));
+        uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
 
 
         FURI_LOG_D(TAG, "allocated memory is %zu bytes", heap_size);
         FURI_LOG_D(TAG, "allocated memory is %zu bytes", heap_size);
         FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
         FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
@@ -56,58 +52,49 @@ void py_app_file_execute(FuriString *file)
 
 
         mp_flipper_init(heap, heap_size, stack_size, &stack);
         mp_flipper_init(heap, heap_size, stack_size, &stack);
 
 
-        if (is_py_file)
-        {
+        if(is_py_file) {
             mp_flipper_exec_py_file(path);
             mp_flipper_exec_py_file(path);
-        }
-        else
-        {
+        } else {
             mp_flipper_exec_mpy_file(path);
             mp_flipper_exec_mpy_file(path);
         }
         }
 
 
         mp_flipper_deinit();
         mp_flipper_deinit();
 
 
         free(heap);
         free(heap);
-    } while (false);
+    } while(false);
 
 
     furi_string_free(file_path);
     furi_string_free(file_path);
 
 
     py_app_free(app);
     py_app_free(app);
 }
 }
 
 
-PyApp *py_app_load_settings()
-{
-    File *file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
+PyApp* py_app_load_settings() {
+    File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
 
 
-    PyApp *settings = malloc(sizeof(PyApp));
+    PyApp* settings = malloc(sizeof(PyApp));
 
 
     const size_t settings_size = sizeof(PyApp);
     const size_t settings_size = sizeof(PyApp);
 
 
     FURI_LOG_I(TAG, "loading settings from \"%s\"", PYTHON_SETTINGS_PATH);
     FURI_LOG_I(TAG, "loading settings from \"%s\"", PYTHON_SETTINGS_PATH);
     bool fs_result = storage_file_open(file, PYTHON_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
     bool fs_result = storage_file_open(file, PYTHON_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
 
 
-    if (fs_result)
-    {
+    if(fs_result) {
         size_t bytes_count = storage_file_read(file, settings, settings_size);
         size_t bytes_count = storage_file_read(file, settings, settings_size);
 
 
         fs_result = bytes_count == settings_size;
         fs_result = bytes_count == settings_size;
     }
     }
 
 
-    if (fs_result)
-    {
+    if(fs_result) {
         FURI_LOG_I(TAG, "load success");
         FURI_LOG_I(TAG, "load success");
-    }
-    else
-    {
+    } else {
         FURI_LOG_E(TAG, "load failed, %s", storage_file_get_error_desc(file));
         FURI_LOG_E(TAG, "load failed, %s", storage_file_get_error_desc(file));
 
 
-        settings->heap_size = 0.3;
+        settings->heap_size = 0.1;
         settings->stack_size = 1024;
         settings->stack_size = 1024;
         settings->version = PYTHON_SETTINGS_VERSION;
         settings->version = PYTHON_SETTINGS_VERSION;
     }
     }
 
 
-    if (fs_result && settings->version != PYTHON_SETTINGS_VERSION)
-    {
+    if(fs_result && settings->version != PYTHON_SETTINGS_VERSION) {
         FURI_LOG_E(TAG, "version(%d != %d) mismatch", settings->version, PYTHON_SETTINGS_VERSION);
         FURI_LOG_E(TAG, "version(%d != %d) mismatch", settings->version, PYTHON_SETTINGS_VERSION);
     }
     }
 
 
@@ -119,9 +106,8 @@ PyApp *py_app_load_settings()
     return settings;
     return settings;
 };
 };
 
 
-void py_app_save_settings(PyApp *settings)
-{
-    File *file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
+void py_app_save_settings(PyApp* settings) {
+    File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
 
 
     const size_t settings_size = sizeof(PyApp);
     const size_t settings_size = sizeof(PyApp);
 
 
@@ -129,19 +115,15 @@ void py_app_save_settings(PyApp *settings)
 
 
     bool fs_result = storage_file_open(file, PYTHON_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);
     bool fs_result = storage_file_open(file, PYTHON_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);
 
 
-    if (fs_result)
-    {
+    if(fs_result) {
         size_t bytes_count = storage_file_write(file, settings, settings_size);
         size_t bytes_count = storage_file_write(file, settings, settings_size);
 
 
         fs_result = bytes_count == settings_size;
         fs_result = bytes_count == settings_size;
     }
     }
 
 
-    if (fs_result)
-    {
+    if(fs_result) {
         FURI_LOG_I(TAG, "save success");
         FURI_LOG_I(TAG, "save success");
-    }
-    else
-    {
+    } else {
         FURI_LOG_E(TAG, "save failed, %s", storage_file_get_error_desc(file));
         FURI_LOG_E(TAG, "save failed, %s", storage_file_get_error_desc(file));
     }
     }
 
 
@@ -151,12 +133,10 @@ void py_app_save_settings(PyApp *settings)
     furi_record_close(RECORD_STORAGE);
     furi_record_close(RECORD_STORAGE);
 };
 };
 
 
-PyApp *py_app_alloc()
-{
+PyApp* py_app_alloc() {
     return py_app_load_settings();
     return py_app_load_settings();
 }
 }
 
 
-void py_app_free(PyApp *app)
-{
+void py_app_free(PyApp* app) {
     free(app);
     free(app);
 }
 }