Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit e688f81

Browse files
committed
Better text2sam
1 parent 1d82583 commit e688f81

File tree

1 file changed

+2
-115
lines changed

1 file changed

+2
-115
lines changed

applications/external/text2sam/sam_app.cpp

Lines changed: 2 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <furi.h>
22
#include <furi_hal_speaker.h>
3-
// #include <furi_hal.h>
43
#include <stdlib.h>
54

65
#include <input/input.h>
@@ -11,20 +10,14 @@
1110
#include <gui/view.h>
1211
#include <gui/view_dispatcher.h>
1312
#include <gui/modules/text_input.h>
14-
#include <gui/modules/text_box.h>
1513

1614
#include "stm32_sam.h"
1715

1816
#define TAG "SAM"
1917
#define SAM_SAVE_PATH EXT_PATH("sam.txt")
2018
#define TEXT_BUFFER_SIZE 256
21-
// #define MESSAGES_BUFFER_SIZE 256
2219
STM32SAM voice;
2320

24-
// FuriMutex* g_state_mutex;
25-
26-
// FuriString* message;
27-
2821
typedef enum {
2922
EventTypeTick,
3023
EventTypeKey,
@@ -36,10 +29,8 @@ typedef struct {
3629
} PluginEvent;
3730

3831
typedef struct {
39-
FuriMutex* mutex;
4032
ViewDispatcher* view_dispatcher;
4133
TextInput* text_input;
42-
TextBox* text_box;
4334
char input[TEXT_BUFFER_SIZE];
4435
} AppState;
4536

@@ -55,63 +46,40 @@ static void say_something(char* something) {
5546

5647
static void text_input_callback(void* ctx) {
5748
AppState* app_state = (AppState*)ctx;
58-
furi_mutex_acquire(app_state->mutex, FuriWaitForever);
5949
//FURI_LOG_D(TAG, "Input text: %s", app_state->input);
6050

6151
// underscore_to_space(app_state->input);
6252
for(int i = 0; app_state->input[i] != '\0'; i++) {
6353
if(app_state->input[i] == '_') {
6454
app_state->input[i] = ' ';
65-
} else {
66-
app_state->input[i] = app_state->input[i];
6755
}
6856
}
6957

70-
text_box_set_text(app_state->text_box, app_state->input);
71-
view_dispatcher_switch_to_view(app_state->view_dispatcher, 1);
72-
73-
furi_mutex_release(app_state->mutex);
58+
say_something(app_state->input);
7459
}
7560

7661
static bool back_event_callback(void* ctx) {
7762
const AppState* app_state = (AppState*)ctx;
7863
view_dispatcher_stop(app_state->view_dispatcher);
79-
furi_mutex_release(app_state->mutex);
8064
return true;
8165
}
8266

8367
static void sam_state_init(AppState* const app_state) {
8468
app_state->view_dispatcher = view_dispatcher_alloc();
8569
app_state->text_input = text_input_alloc();
86-
app_state->text_box = text_box_alloc();
87-
text_box_set_font(app_state->text_box, TextBoxFontText);
8870
}
8971

9072
static void sam_state_free(AppState* const app_state) {
9173
text_input_free(app_state->text_input);
92-
text_box_free(app_state->text_box);
9374
view_dispatcher_remove_view(app_state->view_dispatcher, 0);
94-
view_dispatcher_remove_view(app_state->view_dispatcher, 1);
9575
view_dispatcher_free(app_state->view_dispatcher);
9676
free(app_state);
9777
}
9878

99-
// static void app_draw_callback(Canvas* canvas, void* ctx) {}
100-
101-
static void app_input_callback(InputEvent* input_event, void* ctx) {
102-
furi_assert(ctx);
103-
104-
FuriMessageQueue* event_queue = ctx;
105-
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
106-
}
107-
10879
static void save_message(FuriString* save_string) {
10980
Storage* storage = (Storage*)furi_record_open(RECORD_STORAGE);
11081
File* file = storage_file_alloc(storage);
111-
// uint16_t bytes_read = 0;
11282
if(storage_file_open(file, SAM_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
113-
// bytes_read =
114-
// storage_file_write(file, save_string, sizeof(save_string));
11583
storage_file_write(file, save_string, TEXT_BUFFER_SIZE);
11684
}
11785
storage_file_close(file);
@@ -130,71 +98,15 @@ static bool load_messages() {
13098
storage_file_free(file);
13199
furi_record_close(RECORD_STORAGE);
132100
return bytes_read == TEXT_BUFFER_SIZE;
133-
// FlipperFormat* ff = flipper_format_file_alloc(storage);
134-
135-
// DialogsApp* dialogs = (DialogsApp*)furi_record_open(RECORD_DIALOGS);
136-
// DialogsFileBrowserOptions browser_options;
137-
// dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL);
138-
// FuriString* map_file = (FuriString*)"/ext/sam"; //furi_string_alloc();
139-
// // furi_string_set(map_file, "/ext/sam");
140-
// if(!storage_file_exists(storage, ANY_PATH("sam"))) {
141-
// storage_common_mkdir(storage, ANY_PATH("sam")); //Make Folder If dir not exist
142-
// }
143-
144-
// bool res = dialog_file_browser_show(dialogs, map_file, map_file, &browser_options);
145-
146-
// furi_record_close(RECORD_DIALOGS);
147-
148-
// // if user didn't choose anything, free everything and exit
149-
// if(!res) {
150-
// FURI_LOG_I(TAG, "exit");
151-
// flipper_format_free(ff);
152-
// furi_record_close(RECORD_STORAGE);
153-
154-
// furi_string_free(message);
155-
156-
// view_port_enabled_set(view_port, false);
157-
// gui_remove_view_port(gui, view_port);
158-
// view_port_free(view_port);
159-
// free(g_state_mutex);
160-
// furi_message_queue_free(event_queue);
161-
162-
// furi_record_close(RECORD_GUI);
163-
// return;
164-
// }
165101
}
166102

167103
extern "C" int32_t sam_app(void* p) {
168104
UNUSED(p);
169105
app_state = (AppState*)malloc(sizeof(AppState));
170106

171-
// g_state_mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
172-
173107
FURI_LOG_D(TAG, "Running sam_state_init");
174108
sam_state_init(app_state);
175109

176-
app_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
177-
if(!app_state->mutex) {
178-
FURI_LOG_E(TAG, "cannot create mutex\r\n");
179-
free(app_state);
180-
return 255;
181-
}
182-
183-
// message = furi_string_alloc();
184-
185-
// Configure view port
186-
ViewPort* view_port = view_port_alloc();
187-
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
188-
// view_port_draw_callback_set(view_port, app_draw_callback, g_state_mutex);
189-
view_port_input_callback_set(view_port, app_input_callback, event_queue);
190-
191-
// // Register view port in GUI
192-
// Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
193-
// gui_add_view_port(gui, view_port, GuiLayerFullscreen);
194-
195-
// InputEvent event;
196-
197-
//TODO: get message from file
198110
FURI_LOG_D(TAG, "Assigning text input callback");
199111

200112
load_messages();
@@ -204,22 +116,16 @@ extern "C" int32_t sam_app(void* p) {
204116
app_state,
205117
app_state->input,
206118
TEXT_BUFFER_SIZE,
207-
//clear default text
208-
false);
119+
false); //clear default text
209120
text_input_set_header_text(app_state->text_input, "Input");
210121

211-
// Open GUI and register view_port
212122
Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
213-
//gui_add_view_port(gui, view_port, GuiLayerFullscreen);
214123

215-
FURI_LOG_D(TAG, "Enabling view dispatcher queue");
216124
view_dispatcher_enable_queue(app_state->view_dispatcher);
217125

218126
FURI_LOG_D(TAG, "Adding text input view to dispatcher");
219127
view_dispatcher_add_view(
220128
app_state->view_dispatcher, 0, text_input_get_view(app_state->text_input));
221-
view_dispatcher_add_view(
222-
app_state->view_dispatcher, 1, text_box_get_view(app_state->text_box));
223129

224130
FURI_LOG_D(TAG, "Attaching view dispatcher to GUI");
225131
view_dispatcher_attach_to_gui(app_state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
@@ -229,28 +135,9 @@ extern "C" int32_t sam_app(void* p) {
229135
view_dispatcher_switch_to_view(app_state->view_dispatcher, 0);
230136
view_dispatcher_run(app_state->view_dispatcher);
231137

232-
// for(bool running = true; running;) {
233-
// PluginEvent event;
234-
// FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
235-
// FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
236-
// if(event_status == FuriStatusOk) {
237-
// if(event.input.key == InputKeyOk) {
238-
say_something(app_state->input);
239138
save_message((FuriString*)app_state->input);
240-
FURI_LOG_D(TAG, "Spoken text: %s", app_state->input);
241-
// }
242-
// if(event.input.key == InputKeyBack) {
243-
// running = false;
244-
// }
245-
// }
246-
// }
247139

248-
view_port_enabled_set(view_port, false);
249-
gui_remove_view_port(gui, view_port);
250140
furi_record_close(RECORD_GUI);
251-
view_port_free(view_port);
252-
furi_mutex_free(app_state->mutex);
253-
// furi_mutex_free(g_state_mutex);
254141
sam_state_free(app_state);
255142

256143
return 0;

0 commit comments

Comments
 (0)