-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbs.c
108 lines (89 loc) · 3.35 KB
/
fbs.c
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
#include "fbs.h"
const uint16_t BT_SERIAL_BUFFER_SIZE = 128;
void draw_callback(Canvas* canvas, void* ctx) {
FBS* app = ctx;
furi_check(furi_mutex_acquire(app->app_mutex, FuriWaitForever) == FuriStatusOk);
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, (char*)app->display_text);
furi_mutex_release(app->app_mutex);
}
void input_callback(InputEvent* input, void* ctx) {
FBS* app = ctx;
furi_message_queue_put(app->event_queue, input, FuriWaitForever);
}
FBS* fbs_alloc() {
FBS* app = malloc(sizeof(FBS));
app->app_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
app->gui = furi_record_open(RECORD_GUI);
app->view_port = view_port_alloc();
view_port_draw_callback_set(app->view_port, draw_callback, app);
view_port_input_callback_set(app->view_port, input_callback, app);
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
app->bt_connected = false;
app->bt = furi_record_open(RECORD_BT);
return app;
}
void fbs_free(FBS* app) {
view_port_enabled_set(app->view_port, false);
gui_remove_view_port(app->gui, app->view_port);
furi_record_close(RECORD_GUI);
app->gui = NULL;
view_port_free(app->view_port);
free(app->display_text);
furi_mutex_free(app->app_mutex);
furi_message_queue_free(app->event_queue);
furi_record_close(RECORD_BT);
app->bt = NULL;
free(app);
}
static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
furi_assert(context);
Bt* bt = context;
UNUSED(bt);
uint16_t ret = 0;
if(event.event == SerialServiceEventTypeDataReceived) {
FURI_LOG_D(TAG, "SerialServiceEventTypeDataReceived");
FURI_LOG_D(TAG, "Size: %u", event.data.size);
FURI_LOG_D(TAG, "Data: ");
for (size_t i = 0; i < event.data.size; i++)
{
printf("%X ", event.data.buffer[i]);
}
printf("\r\n");
} else if(event.event == SerialServiceEventTypeDataSent) {
FURI_LOG_D(TAG, "SerialServiceEventTypeDataSent");
FURI_LOG_D(TAG, "Size: %u", event.data.size);
FURI_LOG_D(TAG, "Data: ");
for (size_t i = 0; i < event.data.size; i++)
{
printf("%X ", event.data.buffer[i]);
}
printf("\r\n");
}
return ret;
}
int32_t fbs_app(void* p) {
UNUSED(p);
FBS* app = fbs_alloc();
if (furi_hal_bt_is_active()) {
FURI_LOG_D(TAG, "BT is working, hijacking the serial connection...");
furi_hal_bt_serial_set_event_callback(BT_SERIAL_BUFFER_SIZE, bt_serial_event_callback, app);
furi_hal_bt_start_advertising();
} else {
FURI_LOG_D(TAG, "Please, enable the Bluetooth and restart the app");
}
InputEvent event;
for(bool processing = true; processing;) {
int status = furi_message_queue_get(app->event_queue, &event, 100);
furi_check(furi_mutex_acquire(app->app_mutex, FuriWaitForever) == FuriStatusOk);
if(status == FuriStatusOk && event.type == InputTypePress && event.key == InputKeyBack) {
processing = false;
}
furi_mutex_release(app->app_mutex);
view_port_update(app->view_port);
}
furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
fbs_free(app);
FURI_LOG_D(TAG, "Released everything");
return 0;
}