upython_file.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <cli/cli.h>
  2. #include <furi.h>
  3. #include <genhdr/mpversion.h>
  4. #include <mp_flipper_compiler.h>
  5. #include <mp_flipper_repl.h>
  6. #include "upython.h"
  7. void mp_flipper_file_execute(FuriString* file) {
  8. if(action == ActionOpen) {
  9. action = ActionExec;
  10. } else {
  11. return;
  12. }
  13. size_t stack;
  14. const char* path = furi_string_get_cstr(file);
  15. FuriString* file_path = furi_string_alloc_printf("%s", path);
  16. do {
  17. FURI_LOG_I(TAG, "executing script %s", path);
  18. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  19. const size_t stack_size = 2 * 1024;
  20. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  21. FURI_LOG_D(TAG, "initial heap size is %zu bytes", heap_size);
  22. FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
  23. size_t index = furi_string_search_rchar(file_path, '/');
  24. furi_check(index != FURI_STRING_FAILURE);
  25. bool is_py_file = furi_string_end_with_str(file_path, ".py");
  26. furi_string_left(file_path, index);
  27. mp_flipper_set_root_module_path(furi_string_get_cstr(file_path));
  28. mp_flipper_init(heap, heap_size, stack_size, &stack);
  29. if(is_py_file) {
  30. mp_flipper_exec_py_file(path);
  31. }
  32. mp_flipper_deinit();
  33. free(heap);
  34. } while(false);
  35. furi_string_free(file_path);
  36. action = ActionNone;
  37. }