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