Explorar o código

add file loader and examples

Oliver Fabel hai 1 ano
pai
achega
1d377cb1c5
Modificáronse 5 ficheiros con 66 adicións e 3 borrados
  1. 1 0
      application.fam
  2. 13 0
      examples/greeter.py
  3. 1 0
      examples/hello.py
  4. 50 1
      mp_flipper_app.c
  5. 1 2
      port/mphalport.c

+ 1 - 0
application.fam

@@ -9,6 +9,7 @@ App(
     fap_category="Examples",
     fap_icon="icon.png",  # 10x10 1-bit PNG
     fap_author="Oliver Fabel",
+    fap_file_assets="examples",
     # fap_weburl="https://github.com/user/funky_flipper_app",
     # fap_icon_assets="images",  # Image assets to compile for this application
     sources=[

+ 13 - 0
examples/greeter.py

@@ -0,0 +1,13 @@
+class Greeter:
+  def __init__(self, name: str):
+    self._name = name
+
+  def __str__(self):
+    return 'hello {name}!'.format(name=self._name)
+
+  def __repr__(self):
+    return self.__str__()
+
+world_greeter = Greeter('world')
+
+print(world_greeter)

+ 1 - 0
examples/hello.py

@@ -0,0 +1 @@
+print("hello world!")

+ 50 - 1
mp_flipper_app.c

@@ -1,7 +1,52 @@
 #include <furi.h>
+#include <dialogs/dialogs.h>
+#include <storage/storage.h>
 
 #include <port/micropython_embed.h>
 
+static void load_python_file(FuriString* code) {
+    Storage* storage = furi_record_open(RECORD_STORAGE);
+    DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
+
+    File* file = storage_file_alloc(storage);
+    FuriString* file_path = furi_string_alloc();
+    DialogsFileBrowserOptions browser_options;
+
+    dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
+
+    browser_options.hide_ext = false;
+    browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
+
+    bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
+
+    furi_record_close(RECORD_DIALOGS);
+
+    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_STORAGE);
+    furi_string_free(file_path);
+}
+
 int32_t mp_flipper_app(void* p) {
     UNUSED(p);
 
@@ -9,10 +54,14 @@ int32_t mp_flipper_app(void* p) {
     const size_t stack_size = 2 * 1024;
     uint8_t* memory = malloc(memory_size * sizeof(uint8_t));
 
+    FuriString* code = furi_string_alloc();
+    load_python_file(code);
+
     mp_embed_init(memory + stack_size, memory_size - stack_size, memory);
-    mp_embed_exec_str("print('hello world!');");
+    mp_embed_exec_str(furi_string_get_cstr(code));
     mp_embed_deinit();
 
+    furi_string_free(code);
     free(memory);
 
     return 0;

+ 1 - 2
port/mphalport.c

@@ -1,7 +1,6 @@
-#include <furi.h>
 #include <stdio.h>
 
-#include "py/mphal.h"
+#include <py/mphal.h>
 
 // Send string of given length to stdout, converting \n to \r\n.
 void mp_hal_stdout_tx_strn_cooked(const char* str, size_t len) {