Merge pull request #1003 from mneme/master
[jackhill/qmk/firmware.git] / quantum / dynamic_macro.h
CommitLineData
39e8e612
WS
1/* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
2#ifndef DYNAMIC_MACROS_H
3#define DYNAMIC_MACROS_H
4
5#include "action_layer.h"
6
7#ifndef DYNAMIC_MACRO_SIZE
8/* May be overridden with a custom value. Be aware that the effective
9 * macro length is half of this value: each keypress is recorded twice
10 * because of the down-event and up-event. This is not a bug, it's the
70f32842
WS
11 * intended behavior.
12 *
13 * Usually it should be fine to set the macro size to at least 256 but
14 * there have been reports of it being too much in some users' cases,
15 * so 128 is considered a safe default.
16 */
17#define DYNAMIC_MACRO_SIZE 128
39e8e612
WS
18#endif
19
20/* DYNAMIC_MACRO_RANGE must be set as the last element of user's
21 * "planck_keycodes" enum prior to including this header. This allows
22 * us to 'extend' it.
23 */
24enum dynamic_macro_keycodes {
25 DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
26 DYN_REC_START2,
27 DYN_MACRO_PLAY1,
28 DYN_MACRO_PLAY2,
29};
30
31/* Blink the LEDs to notify the user about some event. */
32void dynamic_macro_led_blink(void)
33{
34 backlight_toggle();
35 _delay_ms(100);
36 backlight_toggle();
37}
38
39/**
40 * Start recording of the dynamic macro.
41 *
42 * @param[out] macro_pointer The new macro buffer iterator.
43 * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
44 */
45void dynamic_macro_record_start(
46 keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
47{
48 dynamic_macro_led_blink();
49
50 clear_keyboard();
51 layer_clear();
52 *macro_pointer = macro_buffer;
53}
54
55/**
56 * Play the dynamic macro.
57 *
58 * @param macro_buffer[in] The beginning of the macro buffer being played.
59 * @param macro_end[in] The element after the last macro buffer element.
60 * @param direction[in] Either +1 or -1, which way to iterate the buffer.
61 */
62void dynamic_macro_play(
63 keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
64{
65 uint32_t saved_layer_state = layer_state;
66
67 clear_keyboard();
68 layer_clear();
69
70 while (macro_buffer != macro_end) {
71 process_record(macro_buffer);
72 macro_buffer += direction;
73 }
74
75 clear_keyboard();
76
77 layer_state = saved_layer_state;
78}
79
80/**
81 * Record a single key in a dynamic macro.
82 *
83 * @param macro_pointer[in,out] The current buffer position.
84 * @param macro_end2[in] The end of the other macro which shouldn't be overwritten.
85 * @param direction[in] Either +1 or -1, which way to iterate the buffer.
86 * @param record[in] The current keypress.
87 */
88void dynamic_macro_record_key(
89 keyrecord_t **macro_pointer,
90 keyrecord_t *macro_end2,
91 int8_t direction,
92 keyrecord_t *record)
93{
94 if (*macro_pointer + direction != macro_end2) {
95 **macro_pointer = *record;
96 *macro_pointer += direction;
97 } else {
98 /* Notify about the end of buffer. The blinks are paired
99 * because they should happen on both down and up events. */
100 backlight_toggle();
101 }
102}
103
104/**
105 * End recording of the dynamic macro. Essentially just update the
106 * pointer to the end of the macro.
107 */
108void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
109{
110 dynamic_macro_led_blink();
111
112 *macro_end = macro_pointer;
113}
114
115/* Handle the key events related to the dynamic macros. Should be
116 * called from process_record_user() like this:
117 *
118 * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
119 * if (!process_record_dynamic_macro(keycode, record)) {
120 * return false;
121 * }
122 * <...THE REST OF THE FUNCTION...>
123 * }
124 */
125bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
126{
127 /* Both macros use the same buffer but read/write on different
128 * ends of it.
129 *
130 * Macro1 is written left-to-right starting from the beginning of
131 * the buffer.
132 *
133 * Macro2 is written right-to-left starting from the end of the
134 * buffer.
135 *
136 * &macro_buffer macro_end
137 * v v
138 * +------------------------------------------------------------+
139 * |>>>>>> MACRO1 >>>>>>| |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
140 * +------------------------------------------------------------+
141 * ^ ^
142 * r_macro_end r_macro_buffer
143 *
144 * During the recording when one macro encounters the end of the
145 * other macro, the recording is stopped. Apart from this, there
146 * are no arbitrary limits for the macros' length in relation to
147 * each other: for example one can either have two medium sized
148 * macros or one long macro and one short macro. Or even one empty
149 * and one using the whole buffer.
150 */
151 static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
152
153 /* Pointer to the first buffer element after the first macro.
154 * Initially points to the very beginning of the buffer since the
155 * macro is empty. */
156 static keyrecord_t *macro_end = macro_buffer;
157
158 /* The other end of the macro buffer. Serves as the beginning of
159 * the second macro. */
160 static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
161
162 /* Like macro_end but for the second macro. */
163 static keyrecord_t *r_macro_end = r_macro_buffer;
164
165 /* A persistent pointer to the current macro position (iterator)
166 * used during the recording. */
167 static keyrecord_t *macro_pointer = NULL;
168
169 /* 0 - no macro is being recorded right now
170 * 1,2 - either macro 1 or 2 is being recorded */
171 static uint8_t macro_id = 0;
172
173 if (macro_id == 0) {
174 /* No macro recording in progress. */
175 if (!record->event.pressed) {
176 switch (keycode) {
177 case DYN_REC_START1:
178 dynamic_macro_record_start(&macro_pointer, macro_buffer);
179 macro_id = 1;
180 return false;
181 case DYN_REC_START2:
182 dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
183 macro_id = 2;
184 return false;
185 case DYN_MACRO_PLAY1:
186 dynamic_macro_play(macro_buffer, macro_end, +1);
187 return false;
188 case DYN_MACRO_PLAY2:
189 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
190 return false;
191 }
192 }
193 } else {
194 /* A macro is being recorded right now. */
195 switch (keycode) {
196 case MO(_DYN):
197 /* Use the layer key used to access the macro recording as
198 * a stop button. */
199 if (record->event.pressed) { /* Ignore the initial release
200 * just after the recoding
201 * starts. */
202 switch (macro_id) {
203 case 1:
204 dynamic_macro_record_end(macro_pointer, &macro_end);
205 break;
206 case 2:
207 dynamic_macro_record_end(macro_pointer, &r_macro_end);
208 break;
209 }
210 macro_id = 0;
211 }
212 return false;
213 default:
214 /* Store the key in the macro buffer and process it normally. */
215 switch (macro_id) {
216 case 1:
217 dynamic_macro_record_key(&macro_pointer, r_macro_end, +1, record);
218 break;
219 case 2:
220 dynamic_macro_record_key(&macro_pointer, macro_end, -1, record);
221 break;
222 }
223 return true;
224 break;
225 }
226 }
227
228 return true;
229}
230
231#endif