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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* Copyright 2023 Jay Greco
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "typehud.h"
static bool is_initialized;
static uint16_t timer;
static int8_t bar_height;
static uint8_t wpm_arr[_GRAPH_WIDTH];
static uint8_t point_arr[_GRAPH_WIDTH];
static void
render_graph(uint8_t wpm),
render_caret(void),
render_axis(void),
render_bar(void),
render_init(void);
/*
* Renders the wpm counter.
*/
static void render_wpm(uint8_t wpm) {
oled_set_cursor(0, 0);
oled_write("WPM", false);
oled_set_cursor(0, 1);
oled_write(get_u8_str(wpm, '0'), false);
}
/*
* Renders the keyboard matrix.
*/
static void render_matrix(keyrecord_t *record) {
uint8_t x = _MATRIX_X;
uint8_t y = _MATRIX_Y;
uint8_t width = _MATRIX_WIDTH;
uint8_t height = _MATRIX_HEIGHT;
#ifdef SPLIT_KEYBOARD
uint8_t rows = _NML_MATRIX_ROWS;
uint8_t cols = _NML_MATRIX_COLS;
#endif
// On initial render draw the matrix outline
if (!is_initialized) {
for (uint8_t i = 1; i <= width - 2; i++) {
oled_write_pixel(x + i, y, true);
oled_write_pixel(x + i, y + height - 1, true);
}
for (uint8_t j = 1; j <= height - 2; j++) {
oled_write_pixel(x, y + j, true);
oled_write_pixel(x + width - 1, y + j, true);
}
return;
}
// Determine position based on matrix rotation
// For split keyboards the keys on the right half get appended as additional rows and
// have their columns reset at 0
#ifdef SPLIT_KEYBOARD
uint8_t row = (record->event.key.row % rows);
uint8_t col = record->event.key.col;
if (record->event.key.row >= rows) {
col += (cols / 2);
}
#else
uint8_t row = record->event.key.row;
uint8_t col = record->event.key.col;
#endif
#ifdef TYPEHUD_MATRIX_ROW_SHIFT
row += TYPEHUD_MATRIX_ROW_SHIFT;
#endif
#ifdef TYPEHUD_MATRIX_COL_SHIFT
col += TYPEHUD_MATRIX_COL_SHIFT;
#endif
// Scale position to key size
uint8_t size = _MATRIX_SIZE;
row *= size;
col *= size;
// Render key in matrix
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
#if defined(TYPEHUD_MATRIX_ROTATE_90)
uint8_t key_x = x + width - 1 - size - row;
uint8_t key_y = y + 1 + col;
#elif defined(TYPEHUD_MATRIX_ROTATE_180)
uint8_t key_x = x + width - 1 - size - col;
uint8_t key_y = y + height - 1 - size - row;
#elif defined(TYPEHUD_MATRIX_ROTATE_270)
uint8_t key_x = x + 1 + row;
uint8_t key_y = y + height - 1 - size - col;
#else
uint8_t key_x = x + 1 + col;
uint8_t key_y = y + 1 + row;
#endif
oled_write_pixel(key_x + i, key_y + j, record->event.pressed);
}
}
}
/*
* Renders the graph.
*/
static void render_graph(uint8_t wpm) {
uint8_t x = _GRAPH_X;
uint8_t y = _GRAPH_Y + _GRAPH_HEIGHT;
uint8_t width = _GRAPH_WIDTH;
uint8_t height = _GRAPH_HEIGHT;
// Handle intial graph render
if (!is_initialized) {
for (uint8_t i = 0; i < width; i++) {
oled_write_pixel(x + i, y, true);
}
return;
}
uint8_t i = 0;
// Shift all graph points except last to the left and re-render
for (; i < width - 1; i++) {
int8_t point_delta = point_arr[i + 1] - point_arr[i];
#ifdef TYPEHUD_FILLGRAPH
if (point_delta < 0) {
#else
if (point_delta != 0) {
#endif
oled_write_pixel(x + i, y - point_arr[i], false);
}
wpm_arr[i] = wpm_arr[i + 1];
point_arr[i] = point_arr[i + 1];
if (point_delta != 0) {
oled_write_pixel(x + i, y - point_arr[i], true);
}
}
// Clear last graph point
if (wpm > wpm_arr[i] && point_arr[i] + 1 <= height) {
#ifndef TYPEHUD_FILLGRAPH
oled_write_pixel(x + i, y - point_arr[i], false);
#endif
point_arr[i] = point_arr[i] + 1;
} else if ((wpm < wpm_arr[i] && point_arr[i] - 1 >= 0) || (wpm <= 0 && point_arr[i] > 0)) {
oled_write_pixel(x + i, y - point_arr[i], false);
point_arr[i] = point_arr[i] - 1;
}
// Render last graph point
wpm_arr[i] = wpm;
if (point_arr[i] != point_arr[i - 1]) {
oled_write_pixel(x + i, y - point_arr[i], true);
}
}
/*
* Renders the caret.
*/
static void render_caret(void) {
uint8_t x = _GRAPH_X + _GRAPH_WIDTH + _GRAPH_RPAD + _CARET_WIDTH;
uint8_t y = 0;
uint8_t width = _CARET_WIDTH;
uint8_t height = _CARET_HEIGHT;
uint8_t g_width = _GRAPH_WIDTH;
uint8_t g_height = _GRAPH_HEIGHT;
// Handle initial caret render
if (!is_initialized) {
y = g_height - point_arr[g_width - 1];
for (uint8_t i = 0; i < width; i++) {
for (uint8_t j = i; j < height - i; j++) {
oled_write_pixel(x - i, y - j, true);
}
}
return;
}
// Handle caret updates and re-render
int8_t point_delta = point_arr[g_width - 1] - point_arr[g_width - 2];
if (point_delta > 0) {
y = g_height - point_arr[g_width - 2];
if (y - height + 1 > 0) {
for (uint8_t i = 0; i < width; i++) {
oled_write_pixel(x - i, y - i, false);
oled_write_pixel(x - i, y - height + i, true);
}
}
} else if (point_delta < 0) {
y = g_height - point_arr[g_width - 1];
if (y - height + 1 > 0) {
for (uint8_t i = 0; i < width; i++) {
oled_write_pixel(x - i, y - height + i, false);
oled_write_pixel(x - i, y - i, true);
}
}
}
}
/*
* Renders the axis.
*/
static void render_axis(void) {
uint8_t x = _AXIS_X;
uint8_t y = _AXIS_HEIGHT;
uint8_t width = _AXIS_WIDTH;
uint8_t height = _AXIS_HEIGHT;
uint8_t tick_width = _AXIS_TICK_WIDTH;
uint8_t subtick_width = _AXIS_SUBTICK_WIDTH;
uint8_t interval = _AXIS_INTERVAL;
uint8_t tick_interval = _AXIS_TICK_INTERVAL;
for (uint8_t j = 0; j <= height; j += interval) {
uint8_t curr_tick_width = 0;
// Determine tick width and draw extra point if at interval
if (j % tick_interval == 0) {
curr_tick_width = tick_width;
oled_write_pixel(x, y - j, true);
} else {
curr_tick_width = subtick_width;
}
// Draw tick
for (uint8_t i = 0; i < curr_tick_width; i++) {
oled_write_pixel(x + width - i, y - j, true);
}
}
}
/*
* Renders the input bar.
*/
static void render_bar(void) {
uint8_t x = _BAR_X;
uint8_t width = _BAR_WIDTH;
uint8_t height = _BAR_HEIGHT;
// Increment bar height
bar_height = (bar_height + 1) % height;
// When bar resets back to 0, clear bar pixels
if (bar_height % height == 0) {
for (uint8_t i = 0; i < width; i++) {
for (uint8_t j = 0; j < height; j++) {
oled_write_pixel(x + i, j, false);
}
}
}
// Draw new bar pixels
for (uint8_t i = 0; i < width; i++) {
oled_write_pixel(x + i, height - bar_height, true);
}
}
/*
* Renders the initial frame for all components.
*/
static void render_init(void) {
render_graph(0);
render_caret();
render_matrix(NULL);
render_axis();
}
/*
* Initializes and resets the typehud.
*/
void typehud_init(void) {
// Reset variables
is_initialized = false;
timer = 0;
bar_height = -1;
for (uint8_t i = 0; i < _GRAPH_WIDTH; i++) {
wpm_arr[i] = 0;
point_arr[i] = 0;
}
// Draw the initial graph
for (uint8_t i = 0; i < _GRAPH_WIDTH; i++) {
oled_write_pixel(_GRAPH_X + i, _GRAPH_HEIGHT, true);
}
}
/*
* Renders the typehud.
*/
void typehud_render(void) {
uint8_t wpm = get_current_wpm();
// Run initial rendering once
if (!is_initialized) {
render_init();
is_initialized = true;
}
// Render wpm
render_wpm(wpm);
// Render next graph and caret frame when timer reaches refresh rate
if (timer_elapsed(timer) > _GRAPH_REFRESH) {
render_graph(wpm);
render_caret();
timer = timer_read();
}
}
/*
* Handles keypresses for the typehud.
*/
void typehud_process_record(keyrecord_t *record) {
// For split keyboards, only draw on correct side
#ifdef SPLIT_KEYBOARD
# ifdef TYPEHUD_MASTER
if (!is_keyboard_master()) {
# else
if (is_keyboard_master()) {
# endif
return;
}
#endif
// Render/update matrix
render_matrix(record);
// Render/update input bar on keypress
if (record->event.pressed) {
render_bar();
}
}
|