Repository navigation
Expand file tree
/
Copy pathmacro.c
More file actions
89 lines (65 loc) · 1.73 KB
/
Copy pathmacro.c
File metadata and controls
89 lines (65 loc) · 1.73 KB
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
#include "macro.h"
#include "logger.h"
#include "hid_report.h"
#include "pico/time.h"
#include <stddef.h>
#define MACRO_KEY_DELAY_MS 20
typedef enum {
MACRO_IDLE,
MACRO_PRESS,
MACRO_RELEASE,
} MacroState;
static const Macro *current_macro = NULL;
static int current_stroke = 0;
static MacroState state = MACRO_IDLE;
static absolute_time_t next_action;
bool macro_running(void) { return state != MACRO_IDLE; }
// sets macro to current macro to begin macro process
bool macro_start(const Macro *macro) {
if (macro == NULL || macro->length == 0)
return false;
if (macro_running())
return false;
current_macro = macro;
current_stroke = 0;
state = MACRO_PRESS;
next_action = get_absolute_time();
return true;
}
// Executes macro if current macro is set
void macro_task(void) {
if (state == MACRO_IDLE) {
return;
}
// Wait until the previous press/release has reached the host, so strokes
// are never merged away
if (hid_report_pending()) {
return;
}
if (absolute_time_diff_us(get_absolute_time(), next_action) > 0) {
return;
}
KeyStroke stroke = current_macro->strokes[current_stroke];
switch (state) {
case MACRO_PRESS: {
log_info("Pressed Key %d", stroke.key);
hid_report_set_macro(stroke.key, stroke.modifier);
state = MACRO_RELEASE;
next_action = delayed_by_ms(get_absolute_time(), MACRO_KEY_DELAY_MS);
break;
}
case MACRO_RELEASE:
hid_report_clear_macro();
current_stroke++;
if (current_stroke >= current_macro->length) {
current_macro = NULL;
state = MACRO_IDLE;
} else {
state = MACRO_PRESS;
next_action = delayed_by_ms(get_absolute_time(), MACRO_KEY_DELAY_MS);
}
break;
case MACRO_IDLE:
break;
}
}