upython.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. typedef enum {
  11. ActionNone,
  12. ActionOpen,
  13. ActionExit
  14. } Action;
  15. static Action action = ActionNone;
  16. static void execute_file(FuriString* file) {
  17. size_t stack;
  18. const char* path = furi_string_get_cstr(file);
  19. FuriString* file_path = furi_string_alloc_printf("%s", path);
  20. do {
  21. FURI_LOG_I(TAG, "executing script %s", path);
  22. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  23. const size_t stack_size = 2 * 1024;
  24. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  25. FURI_LOG_D(TAG, "initial heap size is %zu bytes", heap_size);
  26. FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
  27. size_t index = furi_string_search_rchar(file_path, '/');
  28. furi_check(index != FURI_STRING_FAILURE);
  29. bool is_py_file = furi_string_end_with_str(file_path, ".py");
  30. furi_string_left(file_path, index);
  31. mp_flipper_set_root_module_path(furi_string_get_cstr(file_path));
  32. mp_flipper_init(heap, heap_size, stack_size, &stack);
  33. if(is_py_file) {
  34. mp_flipper_exec_py_file(path);
  35. } else {
  36. mp_flipper_exec_mpy_file(path);
  37. }
  38. mp_flipper_deinit();
  39. free(heap);
  40. } while(false);
  41. furi_string_free(file_path);
  42. }
  43. static bool select_python_file(FuriString* file_path) {
  44. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  45. DialogsFileBrowserOptions browser_options;
  46. dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
  47. browser_options.hide_ext = false;
  48. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  49. bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  50. furi_record_close(RECORD_DIALOGS);
  51. return result;
  52. }
  53. static void on_input(const void* event, void* ctx) {
  54. UNUSED(ctx);
  55. InputKey key = ((InputEvent*)event)->key;
  56. InputType type = ((InputEvent*)event)->type;
  57. if(type != InputTypeRelease) {
  58. return;
  59. }
  60. switch(key) {
  61. case InputKeyOk:
  62. action = ActionOpen;
  63. break;
  64. case InputKeyBack:
  65. action = ActionExit;
  66. break;
  67. default:
  68. action = ActionNone;
  69. break;
  70. }
  71. }
  72. static void show_splash_screen() {
  73. Gui* gui = furi_record_open(RECORD_GUI);
  74. FuriPubSub* input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  75. FuriPubSubSubscription* input_event = furi_pubsub_subscribe(input_event_queue, on_input, NULL);
  76. ViewPort* view_port = view_port_alloc();
  77. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  78. Canvas* canvas = gui_direct_draw_acquire(gui);
  79. canvas_draw_icon(canvas, 0, 0, &I_splash);
  80. canvas_draw_icon(canvas, 82, 17, &I_qrcode);
  81. canvas_set_color(canvas, ColorBlack);
  82. canvas_set_font(canvas, FontSecondary);
  83. canvas_draw_str_aligned(canvas, 66, 3, AlignLeft, AlignTop, "Micro");
  84. canvas_set_font(canvas, FontPrimary);
  85. canvas_draw_str_aligned(canvas, 90, 2, AlignLeft, AlignTop, "Python");
  86. canvas_set_font(canvas, FontSecondary);
  87. canvas_draw_icon(canvas, 65, 53, &I_Pin_back_arrow_10x8);
  88. canvas_draw_str_aligned(canvas, 78, 54, AlignLeft, AlignTop, "Exit");
  89. canvas_draw_icon(canvas, 98, 54, &I_ButtonCenter_7x7);
  90. canvas_draw_str_aligned(canvas, 107, 54, AlignLeft, AlignTop, "Open");
  91. canvas_commit(canvas);
  92. while(action == ActionNone) {
  93. furi_delay_ms(1);
  94. }
  95. furi_pubsub_unsubscribe(input_event_queue, input_event);
  96. gui_direct_draw_release(gui);
  97. gui_remove_view_port(gui, view_port);
  98. view_port_free(view_port);
  99. furi_record_close(RECORD_INPUT_EVENTS);
  100. furi_record_close(RECORD_GUI);
  101. }
  102. int32_t upython(void* p) {
  103. UNUSED(p);
  104. do {
  105. show_splash_screen();
  106. if(action == ActionExit) {
  107. break;
  108. }
  109. FuriString* file_path = furi_string_alloc_set_str(APP_ASSETS_PATH("upython"));
  110. if(select_python_file(file_path)) {
  111. execute_file(file_path);
  112. }
  113. furi_string_free(file_path);
  114. action = ActionNone;
  115. } while(true);
  116. return 0;
  117. }