-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchess_clock.c
181 lines (159 loc) · 6.93 KB
/
chess_clock.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#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 {
ChessClockStateSetup,
ChessClockStatePlayer1,
ChessClockStatePlayer2,
ChessClockStateGameOver,
} ChessClockState;
typedef struct {
ChessClockState state;
uint32_t total_time;
uint32_t time_left_p1;
uint32_t time_left_p2;
uint32_t last_tick;
} PluginState;
static void render_callback(Canvas* const canvas, void* ctx) {
furi_assert(ctx);
PluginState* plugin_state = ctx;
char text[50];
canvas_set_font(canvas, FontPrimary);
switch(plugin_state->state) {
case ChessClockStateSetup:
case ChessClockStatePlayer1:
case ChessClockStatePlayer2:
snprintf(
text,
sizeof(text),
"Player 1: %02lu:%02lu:%02lu\nPlayer 2: %02lu:%02lu:%02lu",
plugin_state->time_left_p1 / 60000,
(plugin_state->time_left_p1 / 1000) % 60,
(plugin_state->time_left_p1 % 1000) / 10,
plugin_state->time_left_p2 / 60000,
(plugin_state->time_left_p2 / 1000) % 60,
(plugin_state->time_left_p2 % 1000) / 10);
break;
case ChessClockStateGameOver:
snprintf(text, sizeof(text), "Player %d wins!", plugin_state->time_left_p1 > 0 ? 1 : 2);
break;
}
elements_multiline_text_aligned(canvas, 64, 32, AlignCenter, AlignCenter, text);
}
static void input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* event_queue = ctx;
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
int32_t chess_clock_app() {
NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
PluginState* plugin_state = malloc(sizeof(PluginState));
plugin_state->state = ChessClockStateSetup;
plugin_state->total_time = 300000;
plugin_state->time_left_p1 = plugin_state->total_time;
plugin_state->time_left_p2 = plugin_state->total_time;
plugin_state->last_tick = 0;
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);
PluginEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 10);
uint32_t now = furi_get_tick();
if(event_status == FuriStatusOk && event.type == EventTypeKey &&
event.input.key == InputKeyBack) {
if(event.input.type == InputTypeShort) {
plugin_state->state = ChessClockStateSetup;
plugin_state->time_left_p1 = plugin_state->total_time;
plugin_state->time_left_p2 = plugin_state->total_time;
notification_message(notification, &sequence_reset_rgb);
} else if(event.input.type == InputTypeLong) {
processing = false;
}
}
switch(plugin_state->state) {
case ChessClockStateSetup:
if(event_status == FuriStatusOk && event.type == EventTypeKey) {
if(event.input.key == InputKeyUp && event.input.type == InputTypePress) {
plugin_state->total_time += 15000;
plugin_state->time_left_p1 = plugin_state->total_time;
plugin_state->time_left_p2 = plugin_state->total_time;
} else if(event.input.key == InputKeyDown && event.input.type == InputTypePress) {
if(plugin_state->total_time > 15000) {
plugin_state->total_time -= 15000;
plugin_state->time_left_p1 = plugin_state->total_time;
plugin_state->time_left_p2 = plugin_state->total_time;
}
} else if(event.input.key == InputKeyOk && event.input.type == InputTypePress) {
plugin_state->state = ChessClockStatePlayer1;
plugin_state->last_tick = now;
}
}
break;
case ChessClockStatePlayer1:
if(event_status == FuriStatusOk && event.type == EventTypeKey &&
event.input.key == InputKeyOk && event.input.type == InputTypePress) {
plugin_state->state = ChessClockStatePlayer2;
plugin_state->last_tick = now;
} else {
plugin_state->time_left_p1 -= now - plugin_state->last_tick;
if(plugin_state->time_left_p1 <= 0 || plugin_state->time_left_p1 >= UINT32_MAX - 1000) {
plugin_state->time_left_p1 = 0;
plugin_state->state = ChessClockStateGameOver;
notification_message(notification, &sequence_set_only_blue_255);
notification_message(notification, &sequence_single_vibro);
}
plugin_state->last_tick = now;
}
break;
case ChessClockStatePlayer2:
if(event_status == FuriStatusOk && event.type == EventTypeKey &&
event.input.key == InputKeyOk && event.input.type == InputTypePress) {
plugin_state->state = ChessClockStatePlayer1;
plugin_state->last_tick = now;
} else {
plugin_state->time_left_p2 -= now - plugin_state->last_tick;
if(plugin_state->time_left_p2 <= 0 || plugin_state->time_left_p2 >= UINT32_MAX - 1000) {
plugin_state->time_left_p2 = 0;
plugin_state->state = ChessClockStateGameOver;
notification_message(notification, &sequence_set_only_green_255);
notification_message(notification, &sequence_single_vibro);
}
plugin_state->last_tick = now;
}
break;
case ChessClockStateGameOver:
if(event_status == FuriStatusOk && event.type == EventTypeKey &&
event.input.key == InputKeyBack && event.input.type == InputTypeShort) {
plugin_state->state = ChessClockStateSetup;
plugin_state->time_left_p1 = plugin_state->total_time;
plugin_state->time_left_p2 = plugin_state->total_time;
}
break;
}
view_port_update(view_port);
}
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;
}