mp_flipper_app.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <malloc.h>
  2. #include <furi.h>
  3. #include <dialogs/dialogs.h>
  4. #include <storage/storage.h>
  5. #include <mp_flipper_runtime.h>
  6. #include "mp_flipper_app.h"
  7. #include "py_app.h"
  8. const char *root_module_path;
  9. ViewPort *mp_flipper_view_port;
  10. FuriMessageQueue *mp_flipper_event_queue;
  11. static void load_python_file(FuriString *file_path, FuriString *code)
  12. {
  13. Storage *storage = furi_record_open(RECORD_STORAGE);
  14. DialogsApp *dialogs = furi_record_open(RECORD_DIALOGS);
  15. File *file = storage_file_alloc(storage);
  16. DialogsFileBrowserOptions browser_options;
  17. dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
  18. browser_options.hide_ext = false;
  19. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  20. bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  21. if (result)
  22. {
  23. result = storage_file_open(file, furi_string_get_cstr(file_path), FSAM_READ, FSOM_OPEN_EXISTING);
  24. }
  25. if (result)
  26. {
  27. size_t read = 0;
  28. do
  29. {
  30. uint8_t buffer[64] = {'\0'};
  31. read = storage_file_read(file, buffer, sizeof(buffer) - 1);
  32. for (size_t i = 0; i < read; i++)
  33. {
  34. furi_string_push_back(code, buffer[i]);
  35. }
  36. } while (read > 0);
  37. furi_string_trim(code);
  38. }
  39. else
  40. {
  41. furi_string_set(code, "print('it works!')");
  42. }
  43. storage_file_free(file);
  44. furi_record_close(RECORD_DIALOGS);
  45. furi_record_close(RECORD_STORAGE);
  46. }
  47. int32_t mp_flipper_app(void *p)
  48. {
  49. UNUSED(p);
  50. FuriString *file_path = furi_string_alloc();
  51. FuriString *code = furi_string_alloc();
  52. load_python_file(file_path, code);
  53. py_app((void *)furi_string_get_cstr(file_path));
  54. furi_string_free(file_path);
  55. furi_string_free(code);
  56. return 0;
  57. }