upython.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <malloc.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <dialogs/dialogs.h>
  5. #include <storage/storage.h>
  6. #include <mp_flipper_runtime.h>
  7. #include <mp_flipper_compiler.h>
  8. #include "upython_icons.h"
  9. #define TAG "uPython"
  10. static void execute_file(FuriString* file) {
  11. size_t stack;
  12. const char* path = furi_string_get_cstr(file);
  13. FuriString* file_path = furi_string_alloc_printf("%s", path);
  14. do {
  15. FURI_LOG_I(TAG, "executing script %s", path);
  16. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  17. const size_t stack_size = 2 * 1024;
  18. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  19. FURI_LOG_D(TAG, "initial heap size is %zu bytes", heap_size);
  20. FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
  21. size_t index = furi_string_search_rchar(file_path, '/');
  22. furi_check(index != FURI_STRING_FAILURE);
  23. bool is_py_file = furi_string_end_with_str(file_path, ".py");
  24. furi_string_left(file_path, index);
  25. mp_flipper_set_root_module_path(furi_string_get_cstr(file_path));
  26. mp_flipper_init(heap, heap_size, stack_size, &stack);
  27. if(is_py_file) {
  28. mp_flipper_exec_py_file(path);
  29. } else {
  30. mp_flipper_exec_mpy_file(path);
  31. }
  32. mp_flipper_deinit();
  33. free(heap);
  34. } while(false);
  35. furi_string_free(file_path);
  36. }
  37. static bool select_python_file(FuriString* file_path) {
  38. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  39. DialogsFileBrowserOptions browser_options;
  40. dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
  41. browser_options.hide_ext = false;
  42. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  43. bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  44. furi_record_close(RECORD_DIALOGS);
  45. return result;
  46. }
  47. static void show_splash_screen() {
  48. Gui* gui = furi_record_open(RECORD_GUI);
  49. ViewPort* view_port = view_port_alloc();
  50. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  51. Canvas* canvas = gui_direct_draw_acquire(gui);
  52. canvas_draw_icon(canvas, 0, 0, &I_splash);
  53. canvas_commit(canvas);
  54. furi_delay_ms(5000);
  55. gui_direct_draw_release(gui);
  56. gui_remove_view_port(gui, view_port);
  57. view_port_free(view_port);
  58. furi_record_close(RECORD_GUI);
  59. }
  60. int32_t upython(void* p) {
  61. UNUSED(p);
  62. show_splash_screen();
  63. FuriString* file_path = furi_string_alloc();
  64. if(select_python_file(file_path)) {
  65. execute_file(file_path);
  66. }
  67. furi_string_free(file_path);
  68. return 0;
  69. }