-
Notifications
You must be signed in to change notification settings - Fork 5
/
xbox_controller.c
122 lines (98 loc) · 3.95 KB
/
xbox_controller.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <infrared_worker.h>
#include <infrared_transmit.h>
#include "xbox_controller.h"
#define TAG "XboxControllerApp"
enum XboxControllerSubmenuIndex {
XboxControllerSubmenuIndexXboxOne,
XboxControllerSubmenuIndexPower
};
uint32_t usb_hid_exit_confirm_view(void* context) {
UNUSED(context);
return UsbHidViewSubmenu;
}
uint32_t usb_hid_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
}
void usb_hid_submenu_callback(void* context, uint32_t index) {
furi_assert(context);
XboxController* app = context;
if(index == XboxControllerSubmenuIndexXboxOne) {
app->view_id = UsbHidViewXboxController;
view_dispatcher_switch_to_view(app->view_dispatcher, UsbHidViewXboxController);
} else if(index == XboxControllerSubmenuIndexPower) {
InfraredMessage* message = malloc(sizeof(InfraredMessage));
message->protocol = InfraredProtocolNECext;
message->address = 0xD880;
message->command = 0xD02F;
message->repeat = false;
infrared_send(message, 2);
free(message);
}
}
void usb_hid_dialog_callback(DialogExResult result, void* context) {
furi_assert(context);
XboxController* app = context;
if(result == DialogExResultLeft) {
view_dispatcher_stop(app->view_dispatcher);
} else if(result == DialogExResultRight) {
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id); // Show last view
} else if(result == DialogExResultCenter) {
view_dispatcher_switch_to_view(app->view_dispatcher, UsbHidViewSubmenu);
}
}
XboxController* xbox_controller_app_alloc() {
XboxController* app = malloc(sizeof(XboxController));
app->gui = furi_record_open(RECORD_GUI);
app->notifications = furi_record_open(RECORD_NOTIFICATION);
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
// Submenu view
app->submenu = submenu_alloc();
view_set_orientation(submenu_get_view(app->submenu), ViewOrientationVertical);
submenu_add_item(
app->submenu, "Xbox One", XboxControllerSubmenuIndexXboxOne, usb_hid_submenu_callback, app);
submenu_add_item(
app->submenu, "Power", XboxControllerSubmenuIndexPower, usb_hid_submenu_callback, app);
view_set_previous_callback(submenu_get_view(app->submenu), usb_hid_exit);
view_dispatcher_add_view(
app->view_dispatcher, UsbHidViewSubmenu, submenu_get_view(app->submenu));
// Xbox Controller View
app->xbox_controller_view = xbox_controller_view_alloc(app->notifications);
view_set_previous_callback(
xbox_controller_view_get_view(app->xbox_controller_view), usb_hid_exit_confirm_view);
view_dispatcher_add_view(
app->view_dispatcher,
UsbHidViewXboxController,
xbox_controller_view_get_view(app->xbox_controller_view));
// Switch to Xbox Controller by default
app->view_id = UsbHidViewSubmenu;
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id);
return app;
}
void xbox_controller_app_free(XboxController* app) {
furi_assert(app);
// Reset notification
notification_internal_message(app->notifications, &sequence_reset_blue);
// Free views
view_dispatcher_remove_view(app->view_dispatcher, UsbHidViewSubmenu);
submenu_free(app->submenu);
view_dispatcher_remove_view(app->view_dispatcher, UsbHidViewXboxController);
xbox_controller_view_free(app->xbox_controller_view);
view_dispatcher_free(app->view_dispatcher);
// Close records
furi_record_close(RECORD_GUI);
app->gui = NULL;
furi_record_close(RECORD_NOTIFICATION);
app->notifications = NULL;
// Free rest
free(app);
}
int32_t xbox_controller_app(void* p) {
UNUSED(p);
XboxController* app = xbox_controller_app_alloc();
view_dispatcher_run(app->view_dispatcher);
xbox_controller_app_free(app);
return 0;
}