Просмотр исходного кода

Message actually saves on exit (sam.txt)

Cathy Nguyen 2 лет назад
Родитель
Сommit
12de385e16
1 измененных файлов с 116 добавлено и 24 удалено
  1. 116 24
      sam_app.cpp

+ 116 - 24
sam_app.cpp

@@ -4,6 +4,8 @@
 #include <stdlib.h>
 #include <stdlib.h>
 
 
 #include <input/input.h>
 #include <input/input.h>
+#include <dialogs/dialogs.h>
+#include <flipper_format/flipper_format.h>
 
 
 #include <gui/gui.h>
 #include <gui/gui.h>
 #include <gui/view.h>
 #include <gui/view.h>
@@ -13,9 +15,16 @@
 
 
 #include "stm32_sam.h"
 #include "stm32_sam.h"
 
 
+#define TAG "SAM"
+#define SAM_SAVE_PATH EXT_PATH("sam.txt")
 #define TEXT_BUFFER_SIZE 256
 #define TEXT_BUFFER_SIZE 256
+// #define MESSAGES_BUFFER_SIZE 256
 STM32SAM voice;
 STM32SAM voice;
 
 
+// FuriMutex* g_state_mutex;
+
+// FuriString* message;
+
 typedef enum {
 typedef enum {
     EventTypeTick,
     EventTypeTick,
     EventTypeKey,
     EventTypeKey,
@@ -33,28 +42,19 @@ typedef struct {
     char input[TEXT_BUFFER_SIZE];
     char input[TEXT_BUFFER_SIZE];
 } AppState;
 } AppState;
 
 
+AppState* app_state;
+
 static void say_something(char* something) {
 static void say_something(char* something) {
     if(furi_hal_speaker_is_mine() || furi_hal_speaker_acquire(1000)) {
     if(furi_hal_speaker_is_mine() || furi_hal_speaker_acquire(1000)) {
         voice.begin();
         voice.begin();
         voice.say(something);
         voice.say(something);
         furi_hal_speaker_release();
         furi_hal_speaker_release();
     }
     }
-    // return 0;
 }
 }
 
 
-// static void underscore_to_space(char* input) {
-//     for(int i = 0; input[i] != '\0'; i++) {
-//         if(input[i] == '_') {
-//             app_state->input[i] = ' ';
-//         } else {
-//             app_state->input[i] = input[i];
-//         }
-//     }
-// }
-
 static void text_input_callback(void* ctx) {
 static void text_input_callback(void* ctx) {
     AppState* app_state = (AppState*)acquire_mutex((ValueMutex*)ctx, 25);
     AppState* app_state = (AppState*)acquire_mutex((ValueMutex*)ctx, 25);
-    FURI_LOG_D("SAM", "Input text: %s", app_state->input);
+    FURI_LOG_D(TAG, "Input text: %s", app_state->input);
 
 
     // underscore_to_space(app_state->input);
     // underscore_to_space(app_state->input);
     for(int i = 0; app_state->input[i] != '\0'; i++) {
     for(int i = 0; app_state->input[i] != '\0'; i++) {
@@ -94,21 +94,108 @@ static void sam_state_free(AppState* const app_state) {
     free(app_state);
     free(app_state);
 }
 }
 
 
+// static void app_draw_callback(Canvas* canvas, void* ctx) {}
+
+static void app_input_callback(InputEvent* input_event, void* ctx) {
+    furi_assert(ctx);
+
+    FuriMessageQueue* event_queue = ctx;
+    furi_message_queue_put(event_queue, input_event, FuriWaitForever);
+}
+
+static void save_message(FuriString* save_string) {
+    Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
+    File* file = storage_file_alloc(storage);
+    // uint16_t bytes_read = 0;
+    if(storage_file_open(file, SAM_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
+        // bytes_read =
+        // storage_file_write(file, save_string, sizeof(save_string));
+        storage_file_write(file, save_string, TEXT_BUFFER_SIZE);
+    }
+    storage_file_close(file);
+    storage_file_free(file);
+    furi_record_close(RECORD_STORAGE);
+}
+
+static bool load_messages() {
+    Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
+    File* file = storage_file_alloc(storage);
+    uint16_t bytes_read = 0;
+    if(storage_file_open(file, SAM_SAVE_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
+        bytes_read = storage_file_read(file, app_state->input, TEXT_BUFFER_SIZE);
+    }
+    storage_file_close(file);
+    storage_file_free(file);
+    furi_record_close(RECORD_STORAGE);
+    return bytes_read == TEXT_BUFFER_SIZE;
+    // FlipperFormat* ff = flipper_format_file_alloc(storage);
+
+    // DialogsApp* dialogs = (DialogsApp*)furi_record_open(RECORD_DIALOGS);
+    // DialogsFileBrowserOptions browser_options;
+    // dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL);
+    // FuriString* map_file = (FuriString*)"/ext/sam"; //furi_string_alloc();
+    // // furi_string_set(map_file, "/ext/sam");
+    // if(!storage_file_exists(storage, ANY_PATH("sam"))) {
+    //     storage_common_mkdir(storage, ANY_PATH("sam")); //Make Folder If dir not exist
+    // }
+
+    // bool res = dialog_file_browser_show(dialogs, map_file, map_file, &browser_options);
+
+    // furi_record_close(RECORD_DIALOGS);
+
+    // // if user didn't choose anything, free everything and exit
+    // if(!res) {
+    //     FURI_LOG_I(TAG, "exit");
+    //     flipper_format_free(ff);
+    //     furi_record_close(RECORD_STORAGE);
+
+    //     furi_string_free(message);
+
+    //     view_port_enabled_set(view_port, false);
+    //     gui_remove_view_port(gui, view_port);
+    //     view_port_free(view_port);
+    //     free(g_state_mutex);
+    //     furi_message_queue_free(event_queue);
+
+    //     furi_record_close(RECORD_GUI);
+    //     return;
+    // }
+}
+
 extern "C" int32_t sam_app(void* p) {
 extern "C" int32_t sam_app(void* p) {
     UNUSED(p);
     UNUSED(p);
-    AppState* app_state = (AppState*)malloc(sizeof(AppState));
+    app_state = (AppState*)malloc(sizeof(AppState));
+
+    // g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
 
 
-    FURI_LOG_D("SAM", "Running sam_state_init");
+    FURI_LOG_D(TAG, "Running sam_state_init");
     sam_state_init(app_state);
     sam_state_init(app_state);
 
 
     ValueMutex state_mutex;
     ValueMutex state_mutex;
     if(!init_mutex(&state_mutex, app_state, sizeof(AppState))) {
     if(!init_mutex(&state_mutex, app_state, sizeof(AppState))) {
-        FURI_LOG_E("SAM", "cannot create mutex\r\n");
+        FURI_LOG_E(TAG, "cannot create mutex\r\n");
         free(app_state);
         free(app_state);
         return 255;
         return 255;
     }
     }
 
 
-    FURI_LOG_D("SAM", "Assigning text input callback");
+    // message = furi_string_alloc();
+
+    // Configure view port
+    ViewPort* view_port = view_port_alloc();
+    FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
+    // view_port_draw_callback_set(view_port, app_draw_callback, g_state_mutex);
+    view_port_input_callback_set(view_port, app_input_callback, event_queue);
+
+    // // Register view port in GUI
+    // Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
+    // gui_add_view_port(gui, view_port, GuiLayerFullscreen);
+
+    // InputEvent event;
+
+    //TODO: get message from file
+    FURI_LOG_D(TAG, "Assigning text input callback");
+
+    load_messages();
     text_input_set_result_callback(
     text_input_set_result_callback(
         app_state->text_input,
         app_state->text_input,
         text_input_callback,
         text_input_callback,
@@ -116,25 +203,25 @@ extern "C" int32_t sam_app(void* p) {
         app_state->input,
         app_state->input,
         TEXT_BUFFER_SIZE,
         TEXT_BUFFER_SIZE,
         //clear default text
         //clear default text
-        true);
+        false);
     text_input_set_header_text(app_state->text_input, "Input");
     text_input_set_header_text(app_state->text_input, "Input");
 
 
     // Open GUI and register view_port
     // Open GUI and register view_port
-    Gui* gui = (Gui*)furi_record_open("gui");
+    Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
     //gui_add_view_port(gui, view_port, GuiLayerFullscreen);
     //gui_add_view_port(gui, view_port, GuiLayerFullscreen);
 
 
-    FURI_LOG_D("SAM", "Enabling view dispatcher queue");
+    FURI_LOG_D(TAG, "Enabling view dispatcher queue");
     view_dispatcher_enable_queue(app_state->view_dispatcher);
     view_dispatcher_enable_queue(app_state->view_dispatcher);
 
 
-    FURI_LOG_D("SAM", "Adding text input view to dispatcher");
+    FURI_LOG_D(TAG, "Adding text input view to dispatcher");
     view_dispatcher_add_view(
     view_dispatcher_add_view(
         app_state->view_dispatcher, 0, text_input_get_view(app_state->text_input));
         app_state->view_dispatcher, 0, text_input_get_view(app_state->text_input));
     view_dispatcher_add_view(
     view_dispatcher_add_view(
         app_state->view_dispatcher, 1, text_box_get_view(app_state->text_box));
         app_state->view_dispatcher, 1, text_box_get_view(app_state->text_box));
 
 
-    FURI_LOG_D("SAM", "Attaching view dispatcher to GUI");
+    FURI_LOG_D(TAG, "Attaching view dispatcher to GUI");
     view_dispatcher_attach_to_gui(app_state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
     view_dispatcher_attach_to_gui(app_state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
-    FURI_LOG_D("SAM", "starting view dispatcher");
+    FURI_LOG_D(TAG, "starting view dispatcher");
     view_dispatcher_set_navigation_event_callback(app_state->view_dispatcher, back_event_callback);
     view_dispatcher_set_navigation_event_callback(app_state->view_dispatcher, back_event_callback);
     view_dispatcher_set_event_callback_context(app_state->view_dispatcher, &state_mutex);
     view_dispatcher_set_event_callback_context(app_state->view_dispatcher, &state_mutex);
     view_dispatcher_switch_to_view(app_state->view_dispatcher, 0);
     view_dispatcher_switch_to_view(app_state->view_dispatcher, 0);
@@ -147,7 +234,8 @@ extern "C" int32_t sam_app(void* p) {
     //     if(event_status == FuriStatusOk) {
     //     if(event_status == FuriStatusOk) {
     //         if(event.input.key == InputKeyOk) {
     //         if(event.input.key == InputKeyOk) {
     say_something(app_state->input);
     say_something(app_state->input);
-    FURI_LOG_D("SAM", "Spoken text: %s", app_state->input);
+    save_message((FuriString*)app_state->input);
+    FURI_LOG_D(TAG, "Spoken text: %s", app_state->input);
     //         }
     //         }
     //         if(event.input.key == InputKeyBack) {
     //         if(event.input.key == InputKeyBack) {
     //             running = false;
     //             running = false;
@@ -155,8 +243,12 @@ extern "C" int32_t sam_app(void* p) {
     //     }
     //     }
     // }
     // }
 
 
-    furi_record_close("gui");
+    view_port_enabled_set(view_port, false);
+    gui_remove_view_port(gui, view_port);
+    furi_record_close(RECORD_GUI);
+    view_port_free(view_port);
     delete_mutex(&state_mutex);
     delete_mutex(&state_mutex);
+    // furi_mutex_free(g_state_mutex);
     sam_state_free(app_state);
     sam_state_free(app_state);
 
 
     return 0;
     return 0;