sam_app.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <furi.h>
  2. #include <furi_hal_speaker.h>
  3. #include <stdlib.h>
  4. #include <input/input.h>
  5. #include <dialogs/dialogs.h>
  6. #include <flipper_format/flipper_format.h>
  7. #include <gui/gui.h>
  8. #include <gui/view.h>
  9. #include <gui/view_dispatcher.h>
  10. #include <gui/modules/text_input.h>
  11. #include "stm32_sam.h"
  12. #define TAG "SAM"
  13. #define SAM_SAVE_PATH APP_DATA_PATH("message.txt")
  14. #define TEXT_BUFFER_SIZE 256
  15. STM32SAM voice;
  16. typedef enum {
  17. EventTypeTick,
  18. EventTypeKey,
  19. } EventType;
  20. typedef struct {
  21. EventType type;
  22. InputEvent input;
  23. } PluginEvent;
  24. typedef struct {
  25. ViewDispatcher* view_dispatcher;
  26. TextInput* text_input;
  27. char input[TEXT_BUFFER_SIZE];
  28. } AppState;
  29. AppState* app_state;
  30. static void say_something(char* something) {
  31. if(furi_hal_speaker_is_mine() || furi_hal_speaker_acquire(1000)) {
  32. voice.begin();
  33. voice.say(something);
  34. furi_hal_speaker_release();
  35. }
  36. }
  37. static void text_input_callback(void* ctx) {
  38. AppState* app_state = (AppState*)ctx;
  39. //FURI_LOG_D(TAG, "Input text: %s", app_state->input);
  40. // underscore_to_space(app_state->input);
  41. for(int i = 0; app_state->input[i] != '\0'; i++) {
  42. if(app_state->input[i] == '_') {
  43. app_state->input[i] = ' ';
  44. }
  45. }
  46. say_something(app_state->input);
  47. }
  48. static bool back_event_callback(void* ctx) {
  49. const AppState* app_state = (AppState*)ctx;
  50. view_dispatcher_stop(app_state->view_dispatcher);
  51. return true;
  52. }
  53. static void sam_state_init(AppState* const app_state) {
  54. app_state->view_dispatcher = view_dispatcher_alloc();
  55. app_state->text_input = text_input_alloc();
  56. }
  57. static void sam_state_free(AppState* const app_state) {
  58. text_input_free(app_state->text_input);
  59. view_dispatcher_remove_view(app_state->view_dispatcher, 0);
  60. view_dispatcher_free(app_state->view_dispatcher);
  61. free(app_state);
  62. }
  63. static void save_message(FuriString* save_string) {
  64. Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
  65. File* file = storage_file_alloc(storage);
  66. if(storage_file_open(file, SAM_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  67. storage_file_write(file, save_string, TEXT_BUFFER_SIZE);
  68. }
  69. storage_file_close(file);
  70. storage_file_free(file);
  71. furi_record_close(RECORD_STORAGE);
  72. }
  73. static bool load_messages() {
  74. Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
  75. storage_common_migrate(storage, EXT_PATH("sam.txt"), SAM_SAVE_PATH);
  76. File* file = storage_file_alloc(storage);
  77. uint16_t bytes_read = 0;
  78. if(storage_file_open(file, SAM_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  79. bytes_read = storage_file_read(file, app_state->input, TEXT_BUFFER_SIZE);
  80. }
  81. storage_file_close(file);
  82. storage_file_free(file);
  83. furi_record_close(RECORD_STORAGE);
  84. return bytes_read == TEXT_BUFFER_SIZE;
  85. }
  86. extern "C" int32_t sam_app(void* p) {
  87. UNUSED(p);
  88. app_state = (AppState*)malloc(sizeof(AppState));
  89. FURI_LOG_D(TAG, "Running sam_state_init");
  90. sam_state_init(app_state);
  91. FURI_LOG_D(TAG, "Assigning text input callback");
  92. load_messages();
  93. text_input_set_result_callback(
  94. app_state->text_input,
  95. text_input_callback,
  96. app_state,
  97. app_state->input,
  98. TEXT_BUFFER_SIZE,
  99. false); //clear default text
  100. text_input_set_header_text(app_state->text_input, "Input");
  101. Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
  102. view_dispatcher_enable_queue(app_state->view_dispatcher);
  103. FURI_LOG_D(TAG, "Adding text input view to dispatcher");
  104. view_dispatcher_add_view(
  105. app_state->view_dispatcher, 0, text_input_get_view(app_state->text_input));
  106. FURI_LOG_D(TAG, "Attaching view dispatcher to GUI");
  107. view_dispatcher_attach_to_gui(app_state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  108. FURI_LOG_D(TAG, "starting view dispatcher");
  109. view_dispatcher_set_navigation_event_callback(app_state->view_dispatcher, back_event_callback);
  110. view_dispatcher_set_event_callback_context(app_state->view_dispatcher, app_state);
  111. view_dispatcher_switch_to_view(app_state->view_dispatcher, 0);
  112. view_dispatcher_run(app_state->view_dispatcher);
  113. save_message((FuriString*)app_state->input);
  114. furi_record_close(RECORD_GUI);
  115. sam_state_free(app_state);
  116. return 0;
  117. }