upython.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <mp_flipper_runtime.h>
  4. #include <mp_flipper_compiler.h>
  5. #include "upython.h"
  6. volatile Action action = ActionNone;
  7. FuriString* file_path = NULL;
  8. volatile FuriThreadStdoutWriteCallback stdout_callback = NULL;
  9. static void write_to_log_output(const char* data, size_t size, void* context) {
  10. UNUSED(context);
  11. furi_log_tx((const uint8_t*)data, size);
  12. }
  13. void upython_reset_file_path() {
  14. furi_string_set(file_path, APP_ASSETS_PATH("upython"));
  15. }
  16. int32_t upython(void* args) {
  17. upython_cli_register(args);
  18. do {
  19. switch(action) {
  20. case ActionNone:
  21. action = upython_splash_screen();
  22. break;
  23. case ActionOpen:
  24. if(upython_select_python_file(file_path)) {
  25. stdout_callback = write_to_log_output;
  26. action = ActionExec;
  27. } else {
  28. upython_reset_file_path();
  29. action = ActionNone;
  30. }
  31. break;
  32. case ActionRepl:
  33. break;
  34. case ActionExec:
  35. furi_thread_set_stdout_callback(stdout_callback, NULL);
  36. upython_file_execute(file_path);
  37. upython_reset_file_path();
  38. action = ActionNone;
  39. furi_thread_set_stdout_callback(stdout_callback = NULL, NULL);
  40. break;
  41. case ActionExit:
  42. action = upython_confirm_exit_action() ? ActionTerm : ActionNone;
  43. break;
  44. case ActionTerm:
  45. break;
  46. }
  47. furi_delay_ms(1);
  48. } while(action != ActionTerm);
  49. upython_cli_unregister(args);
  50. return 0;
  51. }