mass_storage_scene_file_name.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "../mass_storage_app_i.h"
  2. #define WRITE_BUF_LEN 4096
  3. static void mass_storage_file_name_text_callback(void* context) {
  4. furi_assert(context);
  5. MassStorageApp* app = context;
  6. view_dispatcher_send_custom_event(app->view_dispatcher, MassStorageCustomEventNameInput);
  7. }
  8. static void mass_storage_create_image(Storage* storage, const char* file_path, uint32_t size) {
  9. FURI_LOG_I("TAG", "Creating image %s, len:%lu", file_path, size);
  10. File* file = storage_file_alloc(storage);
  11. do {
  12. if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) break;
  13. uint32_t size_left = size;
  14. uint8_t* buf = malloc(WRITE_BUF_LEN);
  15. memset(buf, 0, WRITE_BUF_LEN);
  16. while(size_left > 0) {
  17. uint32_t wr_len = size_left;
  18. if(wr_len > WRITE_BUF_LEN) {
  19. wr_len = WRITE_BUF_LEN;
  20. }
  21. if(storage_file_write(file, buf, wr_len) != wr_len) break;
  22. size_left -= wr_len;
  23. }
  24. free(buf);
  25. } while(false);
  26. storage_file_close(file);
  27. storage_file_free(file);
  28. }
  29. void mass_storage_scene_file_name_on_enter(void* context) {
  30. MassStorageApp* app = context;
  31. text_input_set_header_text(app->text_input, "Enter image name");
  32. ValidatorIsFile* validator_is_file =
  33. validator_is_file_alloc_init(MASS_STORAGE_APP_PATH_FOLDER, MASS_STORAGE_APP_EXTENSION, "");
  34. text_input_set_validator(app->text_input, validator_is_file_callback, validator_is_file);
  35. text_input_set_result_callback(
  36. app->text_input,
  37. mass_storage_file_name_text_callback,
  38. app,
  39. app->new_file_name,
  40. MASS_STORAGE_FILE_NAME_LEN,
  41. true);
  42. view_dispatcher_switch_to_view(app->view_dispatcher, MassStorageAppViewTextInput);
  43. }
  44. bool mass_storage_scene_file_name_on_event(void* context, SceneManagerEvent event) {
  45. UNUSED(event);
  46. MassStorageApp* app = context;
  47. bool consumed = false;
  48. if(event.type == SceneManagerEventTypeCustom) {
  49. if(event.event == MassStorageCustomEventNameInput) {
  50. mass_storage_app_show_loading_popup(app, true);
  51. furi_string_printf(
  52. app->file_path,
  53. "%s/%s%s",
  54. MASS_STORAGE_APP_PATH_FOLDER,
  55. app->new_file_name,
  56. MASS_STORAGE_APP_EXTENSION);
  57. mass_storage_create_image(
  58. app->fs_api, furi_string_get_cstr(app->file_path), app->new_file_size);
  59. scene_manager_next_scene(app->scene_manager, MassStorageSceneWork);
  60. }
  61. }
  62. return consumed;
  63. }
  64. void mass_storage_scene_file_name_on_exit(void* context) {
  65. UNUSED(context);
  66. MassStorageApp* app = context;
  67. void* validator_context = text_input_get_validator_callback_context(app->text_input);
  68. text_input_set_validator(app->text_input, NULL, NULL);
  69. validator_is_file_free(validator_context);
  70. text_input_reset(app->text_input);
  71. }