-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaction_time.c
151 lines (132 loc) · 5.41 KB
/
reaction_time.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <stdlib.h>
#include <notification/notification_messages.h>
#include <gui/elements.h>
#include <furi_hal.h>
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
typedef enum {
GameStateStart,
GameStateReady,
GameStateWait,
GameStatePress,
GameStateShowTime,
GameStateLoss,
} GameState;
typedef struct {
GameState state;
uint32_t start_tick;
uint32_t reaction_time;
} PluginState;
static void render_callback(Canvas* const canvas, void* ctx) {
furi_assert(ctx);
PluginState* plugin_state = ctx;
canvas_set_font(canvas, FontPrimary);
switch(plugin_state->state) {
case GameStateStart:
elements_multiline_text_aligned(
canvas, 64, 28, AlignCenter, AlignTop, "Press to start");
break;
case GameStateReady:
case GameStateWait:
elements_multiline_text_aligned(
canvas, 64, 28, AlignCenter, AlignTop, "Get ready...");
break;
case GameStatePress:
elements_multiline_text_aligned(
canvas, 64, 28, AlignCenter, AlignTop, "PRESS");
break;
case GameStateShowTime: {
char text[50];
snprintf(text, sizeof(text), "Reaction time: %lu ms", plugin_state->reaction_time);
elements_multiline_text_aligned(canvas, 64, 28, AlignCenter, AlignTop, text);
break;
}
case GameStateLoss:
elements_multiline_text_aligned(
canvas, 64, 28, AlignCenter, AlignTop, "Too soon!");
break;
}
}
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
int32_t reaction_time_app() {
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
PluginState* plugin_state = malloc(sizeof(PluginState));
plugin_state->state = GameStateStart;
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, render_callback, plugin_state);
view_port_input_callback_set(view_port, input_callback, event_queue);
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
uint32_t wait_time = 2000 + (rand() % 18000);
PluginEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 10);
if(event_status == FuriStatusOk && event.type == EventTypeKey && event.input.key == InputKeyBack) {
break;
}
switch(plugin_state->state) {
case GameStateStart:
if(event_status == FuriStatusOk && event.type == EventTypeKey && event.input.type == InputTypePress) {
plugin_state->start_tick = furi_get_tick();
plugin_state->state = GameStateReady;
}
break;
case GameStateReady:
plugin_state->state = GameStateWait;
notification_message(notification, &sequence_set_only_red_255);
break;
case GameStateWait:
if(event_status == FuriStatusOk && event.type == EventTypeKey && event.input.type == InputTypePress) {
plugin_state->state = GameStateLoss;
notification_message(notification, &sequence_single_vibro);
} else if((furi_get_tick() - plugin_state->start_tick) > wait_time) {
plugin_state->start_tick = furi_get_tick();
plugin_state->state = GameStatePress;
notification_message(notification, &sequence_set_only_blue_255);
notification_message(notification, &sequence_single_vibro);
}
break;
case GameStatePress:
if(event_status == FuriStatusOk && event.type == EventTypeKey && event.input.type == InputTypePress) {
plugin_state->reaction_time = (furi_get_tick() - plugin_state->start_tick);
plugin_state->state = GameStateShowTime;
}
break;
case GameStateShowTime:
if(event_status == FuriStatusOk && event.type == EventTypeKey && event.input.type == InputTypePress) {
plugin_state->state = GameStateStart;
notification_message(notification, &sequence_reset_rgb);
}
break;
case GameStateLoss:
if(event_status == FuriStatusOk && event.type == EventTypeKey && event.input.type == InputTypePress) {
plugin_state->state = GameStateStart;
notification_message(notification, &sequence_reset_rgb);
}
break;
}
view_port_update(view_port);
}
notification_message(notification, &sequence_reset_rgb);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close(RECORD_GUI);
view_port_free(view_port);
furi_message_queue_free(event_queue);
free(plugin_state);
return 0;
}