scope_scene_save.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <float.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <furi_hal_resources.h>
  5. #include <gui/gui.h>
  6. #include <gui/view_dispatcher.h>
  7. #include <gui/scene_manager.h>
  8. #include <gui/modules/submenu.h>
  9. #include <gui/modules/variable_item_list.h>
  10. #include <gui/modules/widget.h>
  11. #include <gui/elements.h>
  12. #include <notification/notification_messages.h>
  13. #include <flipper_format/flipper_format.h>
  14. #include "../scope_app_i.h"
  15. void scope_scene_save_text_input_callback(void* context) {
  16. ScopeApp* app = context;
  17. view_dispatcher_send_custom_event(app->view_dispatcher, ScopeCustomEventTextInputDone);
  18. }
  19. void scope_scene_save_on_enter(void* context) {
  20. ScopeApp* app = context;
  21. // Setup view
  22. TextInput* text_input = app->text_input;
  23. FuriString* file_name = furi_string_alloc();
  24. FuriString* dir_name = furi_string_alloc();
  25. text_input_set_header_text(text_input, "Name signal");
  26. text_input_set_result_callback(
  27. text_input,
  28. scope_scene_save_text_input_callback,
  29. app,
  30. app->file_name_tmp,
  31. MAX_LEN_NAME,
  32. false);
  33. furi_string_free(file_name);
  34. furi_string_free(dir_name);
  35. view_dispatcher_switch_to_view(app->view_dispatcher, ScopeViewSave);
  36. }
  37. bool scope_scene_save_on_event(void* context, SceneManagerEvent event) {
  38. ScopeApp* app = context;
  39. UNUSED(app);
  40. UNUSED(event);
  41. bool consumed = false;
  42. if(event.type == SceneManagerEventTypeCustom && event.event == ScopeCustomEventTextInputDone) {
  43. if(strcmp(app->file_name_tmp, "") != 0) {
  44. FuriString* temp_str = furi_string_alloc();
  45. furi_string_printf(
  46. temp_str,
  47. "%s/%s%s",
  48. APP_DATA_PATH(""),
  49. app->file_name_tmp,
  50. FLIPPERSCOPE_APP_EXTENSION);
  51. Storage* storage = furi_record_open(RECORD_STORAGE);
  52. File* file = storage_file_alloc(storage);
  53. if(!storage_file_open(
  54. file, furi_string_get_cstr(temp_str), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  55. // Todo: Display error
  56. }
  57. if(!storage_file_write(
  58. file, app->data, sizeof(uint16_t) * ADC_CONVERTED_DATA_BUFFER_SIZE)) {
  59. // Todo: Display error
  60. }
  61. storage_file_close(file);
  62. storage_file_free(file);
  63. furi_record_close(RECORD_STORAGE);
  64. free(app->data);
  65. app->data = NULL;
  66. scene_manager_previous_scene(app->scene_manager);
  67. consumed = true;
  68. }
  69. }
  70. return consumed;
  71. }
  72. void scope_scene_save_on_exit(void* context) {
  73. ScopeApp* app = context;
  74. // Clear views
  75. text_input_reset(app->text_input);
  76. }