sam_app.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <furi.h>
  2. #include <furi_hal_speaker.h>
  3. // #include <furi_hal.h>
  4. #include <stdlib.h>
  5. #include <input/input.h>
  6. #include <dialogs/dialogs.h>
  7. #include <flipper_format/flipper_format.h>
  8. #include <gui/gui.h>
  9. #include <gui/view.h>
  10. #include <gui/view_dispatcher.h>
  11. #include <gui/modules/text_input.h>
  12. #include <gui/modules/text_box.h>
  13. #include "stm32_sam.h"
  14. #define TAG "SAM"
  15. #define SAM_SAVE_PATH EXT_PATH("sam.txt")
  16. #define TEXT_BUFFER_SIZE 256
  17. // #define MESSAGES_BUFFER_SIZE 256
  18. STM32SAM voice;
  19. // FuriMutex* g_state_mutex;
  20. // FuriString* message;
  21. typedef enum {
  22. EventTypeTick,
  23. EventTypeKey,
  24. } EventType;
  25. typedef struct {
  26. EventType type;
  27. InputEvent input;
  28. } PluginEvent;
  29. typedef struct {
  30. ViewDispatcher* view_dispatcher;
  31. TextInput* text_input;
  32. TextBox* text_box;
  33. char input[TEXT_BUFFER_SIZE];
  34. } AppState;
  35. AppState* app_state;
  36. static void say_something(char* something) {
  37. if(furi_hal_speaker_is_mine() || furi_hal_speaker_acquire(1000)) {
  38. voice.begin();
  39. voice.say(something);
  40. furi_hal_speaker_release();
  41. }
  42. }
  43. static void text_input_callback(void* ctx) {
  44. AppState* app_state = (AppState*)acquire_mutex((ValueMutex*)ctx, 25);
  45. FURI_LOG_D(TAG, "Input text: %s", app_state->input);
  46. // underscore_to_space(app_state->input);
  47. for(int i = 0; app_state->input[i] != '\0'; i++) {
  48. if(app_state->input[i] == '_') {
  49. app_state->input[i] = ' ';
  50. } else {
  51. app_state->input[i] = app_state->input[i];
  52. }
  53. }
  54. text_box_set_text(app_state->text_box, app_state->input);
  55. view_dispatcher_switch_to_view(app_state->view_dispatcher, 1);
  56. release_mutex((ValueMutex*)ctx, app_state);
  57. }
  58. static bool back_event_callback(void* ctx) {
  59. const AppState* app_state = (AppState*)acquire_mutex((ValueMutex*)ctx, 25);
  60. view_dispatcher_stop(app_state->view_dispatcher);
  61. release_mutex((ValueMutex*)ctx, app_state);
  62. return true;
  63. }
  64. static void sam_state_init(AppState* const app_state) {
  65. app_state->view_dispatcher = view_dispatcher_alloc();
  66. app_state->text_input = text_input_alloc();
  67. app_state->text_box = text_box_alloc();
  68. text_box_set_font(app_state->text_box, TextBoxFontText);
  69. }
  70. static void sam_state_free(AppState* const app_state) {
  71. text_input_free(app_state->text_input);
  72. text_box_free(app_state->text_box);
  73. view_dispatcher_remove_view(app_state->view_dispatcher, 0);
  74. view_dispatcher_remove_view(app_state->view_dispatcher, 1);
  75. view_dispatcher_free(app_state->view_dispatcher);
  76. free(app_state);
  77. }
  78. // static void app_draw_callback(Canvas* canvas, void* ctx) {}
  79. static void app_input_callback(InputEvent* input_event, void* ctx) {
  80. furi_assert(ctx);
  81. FuriMessageQueue* event_queue = ctx;
  82. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  83. }
  84. static void save_message(FuriString* save_string) {
  85. Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
  86. File* file = storage_file_alloc(storage);
  87. // uint16_t bytes_read = 0;
  88. if(storage_file_open(file, SAM_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  89. // bytes_read =
  90. // storage_file_write(file, save_string, sizeof(save_string));
  91. storage_file_write(file, save_string, TEXT_BUFFER_SIZE);
  92. }
  93. storage_file_close(file);
  94. storage_file_free(file);
  95. furi_record_close(RECORD_STORAGE);
  96. }
  97. static bool load_messages() {
  98. Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
  99. File* file = storage_file_alloc(storage);
  100. uint16_t bytes_read = 0;
  101. if(storage_file_open(file, SAM_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  102. bytes_read = storage_file_read(file, app_state->input, TEXT_BUFFER_SIZE);
  103. }
  104. storage_file_close(file);
  105. storage_file_free(file);
  106. furi_record_close(RECORD_STORAGE);
  107. return bytes_read == TEXT_BUFFER_SIZE;
  108. // FlipperFormat* ff = flipper_format_file_alloc(storage);
  109. // DialogsApp* dialogs = (DialogsApp*)furi_record_open(RECORD_DIALOGS);
  110. // DialogsFileBrowserOptions browser_options;
  111. // dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL);
  112. // FuriString* map_file = (FuriString*)"/ext/sam"; //furi_string_alloc();
  113. // // furi_string_set(map_file, "/ext/sam");
  114. // if(!storage_file_exists(storage, ANY_PATH("sam"))) {
  115. // storage_common_mkdir(storage, ANY_PATH("sam")); //Make Folder If dir not exist
  116. // }
  117. // bool res = dialog_file_browser_show(dialogs, map_file, map_file, &browser_options);
  118. // furi_record_close(RECORD_DIALOGS);
  119. // // if user didn't choose anything, free everything and exit
  120. // if(!res) {
  121. // FURI_LOG_I(TAG, "exit");
  122. // flipper_format_free(ff);
  123. // furi_record_close(RECORD_STORAGE);
  124. // furi_string_free(message);
  125. // view_port_enabled_set(view_port, false);
  126. // gui_remove_view_port(gui, view_port);
  127. // view_port_free(view_port);
  128. // free(g_state_mutex);
  129. // furi_message_queue_free(event_queue);
  130. // furi_record_close(RECORD_GUI);
  131. // return;
  132. // }
  133. }
  134. extern "C" int32_t sam_app(void* p) {
  135. UNUSED(p);
  136. app_state = (AppState*)malloc(sizeof(AppState));
  137. // g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
  138. FURI_LOG_D(TAG, "Running sam_state_init");
  139. sam_state_init(app_state);
  140. ValueMutex state_mutex;
  141. if(!init_mutex(&state_mutex, app_state, sizeof(AppState))) {
  142. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  143. free(app_state);
  144. return 255;
  145. }
  146. // message = furi_string_alloc();
  147. // Configure view port
  148. ViewPort* view_port = view_port_alloc();
  149. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  150. // view_port_draw_callback_set(view_port, app_draw_callback, g_state_mutex);
  151. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  152. // // Register view port in GUI
  153. // Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
  154. // gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  155. // InputEvent event;
  156. //TODO: get message from file
  157. FURI_LOG_D(TAG, "Assigning text input callback");
  158. load_messages();
  159. text_input_set_result_callback(
  160. app_state->text_input,
  161. text_input_callback,
  162. &state_mutex,
  163. app_state->input,
  164. TEXT_BUFFER_SIZE,
  165. //clear default text
  166. false);
  167. text_input_set_header_text(app_state->text_input, "Input");
  168. // Open GUI and register view_port
  169. Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
  170. //gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  171. FURI_LOG_D(TAG, "Enabling view dispatcher queue");
  172. view_dispatcher_enable_queue(app_state->view_dispatcher);
  173. FURI_LOG_D(TAG, "Adding text input view to dispatcher");
  174. view_dispatcher_add_view(
  175. app_state->view_dispatcher, 0, text_input_get_view(app_state->text_input));
  176. view_dispatcher_add_view(
  177. app_state->view_dispatcher, 1, text_box_get_view(app_state->text_box));
  178. FURI_LOG_D(TAG, "Attaching view dispatcher to GUI");
  179. view_dispatcher_attach_to_gui(app_state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  180. FURI_LOG_D(TAG, "starting view dispatcher");
  181. view_dispatcher_set_navigation_event_callback(app_state->view_dispatcher, back_event_callback);
  182. view_dispatcher_set_event_callback_context(app_state->view_dispatcher, &state_mutex);
  183. view_dispatcher_switch_to_view(app_state->view_dispatcher, 0);
  184. view_dispatcher_run(app_state->view_dispatcher);
  185. // for(bool running = true; running;) {
  186. // PluginEvent event;
  187. // FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  188. // FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  189. // if(event_status == FuriStatusOk) {
  190. // if(event.input.key == InputKeyOk) {
  191. say_something(app_state->input);
  192. save_message((FuriString*)app_state->input);
  193. FURI_LOG_D(TAG, "Spoken text: %s", app_state->input);
  194. // }
  195. // if(event.input.key == InputKeyBack) {
  196. // running = false;
  197. // }
  198. // }
  199. // }
  200. view_port_enabled_set(view_port, false);
  201. gui_remove_view_port(gui, view_port);
  202. furi_record_close(RECORD_GUI);
  203. view_port_free(view_port);
  204. delete_mutex(&state_mutex);
  205. // furi_mutex_free(g_state_mutex);
  206. sam_state_free(app_state);
  207. return 0;
  208. }