]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/dynamic_macro.h
dynamic_macro.h: Add debug logs
[qmk_firmware.git] / quantum / dynamic_macro.h
1 /* Copyright 2016 Jack Humbert
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
18 #ifndef DYNAMIC_MACROS_H
19 #define DYNAMIC_MACROS_H
20
21 #include "action_layer.h"
22
23 #ifndef DYNAMIC_MACRO_SIZE
24 /* May be overridden with a custom value. Be aware that the effective
25  * macro length is half of this value: each keypress is recorded twice
26  * because of the down-event and up-event. This is not a bug, it's the
27  * intended behavior.
28  *
29  * Usually it should be fine to set the macro size to at least 256 but
30  * there have been reports of it being too much in some users' cases,
31  * so 128 is considered a safe default.
32  */
33 #define DYNAMIC_MACRO_SIZE 128
34 #endif
35
36 /* DYNAMIC_MACRO_RANGE must be set as the last element of user's
37  * "planck_keycodes" enum prior to including this header. This allows
38  * us to 'extend' it.
39  */
40 enum dynamic_macro_keycodes {
41     DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
42     DYN_REC_START2,
43     DYN_REC_STOP,
44     DYN_MACRO_PLAY1,
45     DYN_MACRO_PLAY2,
46 };
47
48 /* Blink the LEDs to notify the user about some event. */
49 void dynamic_macro_led_blink(void)
50 {
51     backlight_toggle();
52     _delay_ms(100);
53     backlight_toggle();
54 }
55
56 /* Convenience macros used for retrieving the debug info. All of them
57  * need a `direction` variable accessible at the call site.
58  */
59 #define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2)
60 #define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) \
61     ((int)(direction * ((POINTER) - (BEGIN))))
62 #define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) \
63     ((int)(direction * ((END2) - (BEGIN)) + 1))
64
65 /**
66  * Start recording of the dynamic macro.
67  *
68  * @param[out] macro_pointer The new macro buffer iterator.
69  * @param[in]  macro_buffer  The macro buffer used to initialize macro_pointer.
70  */
71 void dynamic_macro_record_start(
72     keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
73 {
74     dprintln("dynamic macro recording: started");
75
76     dynamic_macro_led_blink();
77
78     clear_keyboard();
79     layer_clear();
80     *macro_pointer = macro_buffer;
81 }
82
83 /**
84  * Play the dynamic macro.
85  *
86  * @param macro_buffer[in] The beginning of the macro buffer being played.
87  * @param macro_end[in]    The element after the last macro buffer element.
88  * @param direction[in]    Either +1 or -1, which way to iterate the buffer.
89  */
90 void dynamic_macro_play(
91     keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
92 {
93     dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
94
95     uint32_t saved_layer_state = layer_state;
96
97     clear_keyboard();
98     layer_clear();
99
100     while (macro_buffer != macro_end) {
101         process_record(macro_buffer);
102         macro_buffer += direction;
103     }
104
105     clear_keyboard();
106
107     layer_state = saved_layer_state;
108 }
109
110 /**
111  * Record a single key in a dynamic macro.
112  *
113  * @param macro_buffer[in] The start of the used macro buffer.
114  * @param macro_pointer[in,out] The current buffer position.
115  * @param macro2_end[in] The end of the other macro.
116  * @param direction[in]  Either +1 or -1, which way to iterate the buffer.
117  * @param record[in]     The current keypress.
118  */
119 void dynamic_macro_record_key(
120     keyrecord_t *macro_buffer,
121     keyrecord_t **macro_pointer,
122     keyrecord_t *macro2_end,
123     int8_t direction,
124     keyrecord_t *record)
125 {
126     /* If we've just started recording, ignore all the key releases. */
127     if (!record->event.pressed && *macro_pointer == macro_buffer) {
128         dprintln("dynamic macro: ignoring a leading key-up event");
129         return;
130     }
131
132     /* The other end of the other macro is the last buffer element it
133      * is safe to use before overwriting the other macro.
134      */
135     if (*macro_pointer - direction != macro2_end) {
136         **macro_pointer = *record;
137         *macro_pointer += direction;
138     } else {
139         dynamic_macro_led_blink();
140     }
141
142     dprintf(
143         "dynamic macro: slot %d length: %d/%d\n",
144         DYNAMIC_MACRO_CURRENT_SLOT(),
145         DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer),
146         DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
147 }
148
149 /**
150  * End recording of the dynamic macro. Essentially just update the
151  * pointer to the end of the macro.
152  */
153 void dynamic_macro_record_end(
154     keyrecord_t *macro_buffer,
155     keyrecord_t *macro_pointer,
156     int8_t direction,
157     keyrecord_t **macro_end)
158 {
159     dynamic_macro_led_blink();
160
161     /* Do not save the keys being held when stopping the recording,
162      * i.e. the keys used to access the layer DYN_REC_STOP is on.
163      */
164     while (macro_pointer != macro_buffer &&
165            (macro_pointer - direction)->event.pressed) {
166         dprintln("dynamic macro: trimming a trailing key-down event");
167         macro_pointer -= direction;
168     }
169
170     dprintf(
171         "dynamic macro: slot %d saved, length: %d\n",
172         DYNAMIC_MACRO_CURRENT_SLOT(),
173         DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
174
175     *macro_end = macro_pointer;
176 }
177
178 /* Handle the key events related to the dynamic macros. Should be
179  * called from process_record_user() like this:
180  *
181  *   bool process_record_user(uint16_t keycode, keyrecord_t *record) {
182  *       if (!process_record_dynamic_macro(keycode, record)) {
183  *           return false;
184  *       }
185  *       <...THE REST OF THE FUNCTION...>
186  *   }
187  */
188 bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
189 {
190     /* Both macros use the same buffer but read/write on different
191      * ends of it.
192      *
193      * Macro1 is written left-to-right starting from the beginning of
194      * the buffer.
195      *
196      * Macro2 is written right-to-left starting from the end of the
197      * buffer.
198      *
199      * &macro_buffer   macro_end
200      *  v                   v
201      * +------------------------------------------------------------+
202      * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
203      * +------------------------------------------------------------+
204      *                           ^                                 ^
205      *                         r_macro_end                  r_macro_buffer
206      *
207      * During the recording when one macro encounters the end of the
208      * other macro, the recording is stopped. Apart from this, there
209      * are no arbitrary limits for the macros' length in relation to
210      * each other: for example one can either have two medium sized
211      * macros or one long macro and one short macro. Or even one empty
212      * and one using the whole buffer.
213      */
214     static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
215
216     /* Pointer to the first buffer element after the first macro.
217      * Initially points to the very beginning of the buffer since the
218      * macro is empty. */
219     static keyrecord_t *macro_end = macro_buffer;
220
221     /* The other end of the macro buffer. Serves as the beginning of
222      * the second macro. */
223     static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
224
225     /* Like macro_end but for the second macro. */
226     static keyrecord_t *r_macro_end = r_macro_buffer;
227
228     /* A persistent pointer to the current macro position (iterator)
229      * used during the recording. */
230     static keyrecord_t *macro_pointer = NULL;
231
232     /* 0   - no macro is being recorded right now
233      * 1,2 - either macro 1 or 2 is being recorded */
234     static uint8_t macro_id = 0;
235
236     if (macro_id == 0) {
237         /* No macro recording in progress. */
238         if (!record->event.pressed) {
239             switch (keycode) {
240             case DYN_REC_START1:
241                 dynamic_macro_record_start(&macro_pointer, macro_buffer);
242                 macro_id = 1;
243                 return false;
244             case DYN_REC_START2:
245                 dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
246                 macro_id = 2;
247                 return false;
248             case DYN_MACRO_PLAY1:
249                 dynamic_macro_play(macro_buffer, macro_end, +1);
250                 return false;
251             case DYN_MACRO_PLAY2:
252                 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
253                 return false;
254             }
255         }
256     } else {
257         /* A macro is being recorded right now. */
258         switch (keycode) {
259         case DYN_REC_STOP:
260             /* Stop the macro recording. */
261             if (record->event.pressed) { /* Ignore the initial release
262                                           * just after the recoding
263                                           * starts. */
264                 switch (macro_id) {
265                 case 1:
266                     dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
267                     break;
268                 case 2:
269                     dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
270                     break;
271                 }
272                 macro_id = 0;
273             }
274             return false;
275         default:
276             /* Store the key in the macro buffer and process it normally. */
277             switch (macro_id) {
278             case 1:
279                 dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
280                 break;
281             case 2:
282                 dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
283                 break;
284             }
285             return true;
286             break;
287         }
288     }
289
290     return true;
291 }
292
293 #undef DYNAMIC_MACRO_CURRENT_SLOT
294 #undef DYNAMIC_MACRO_CURRENT_LENGTH
295 #undef DYNAMIC_MACRO_CURRENT_CAPACITY
296
297 #endif