]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/dynamic_macro.h
dynamic_macro.h: Ignore all the initial key releases
[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 /**
57  * Start recording of the dynamic macro.
58  *
59  * @param[out] macro_pointer The new macro buffer iterator.
60  * @param[in]  macro_buffer  The macro buffer used to initialize macro_pointer.
61  */
62 void dynamic_macro_record_start(
63     keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
64 {
65     dynamic_macro_led_blink();
66
67     clear_keyboard();
68     layer_clear();
69     *macro_pointer = macro_buffer;
70 }
71
72 /**
73  * Play the dynamic macro.
74  *
75  * @param macro_buffer[in] The beginning of the macro buffer being played.
76  * @param macro_end[in]    The element after the last macro buffer element.
77  * @param direction[in]    Either +1 or -1, which way to iterate the buffer.
78  */
79 void dynamic_macro_play(
80     keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
81 {
82     uint32_t saved_layer_state = layer_state;
83
84     clear_keyboard();
85     layer_clear();
86
87     while (macro_buffer != macro_end) {
88         process_record(macro_buffer);
89         macro_buffer += direction;
90     }
91
92     clear_keyboard();
93
94     layer_state = saved_layer_state;
95 }
96
97 /**
98  * Record a single key in a dynamic macro.
99  *
100  * @param macro_buffer[in] The start of the used macro buffer.
101  * @param macro_pointer[in,out] The current buffer position.
102  * @param macro_end2[in] The end of the other macro which shouldn't be overwritten.
103  * @param direction[in]  Either +1 or -1, which way to iterate the buffer.
104  * @param record[in]     The current keypress.
105  */
106 void dynamic_macro_record_key(
107     keyrecord_t *macro_buffer,
108     keyrecord_t **macro_pointer,
109     keyrecord_t *macro_end2,
110     int8_t direction,
111     keyrecord_t *record)
112 {
113     /* If we've just started recording, ignore all the key releases. */
114     if (!record->event.pressed && *macro_pointer == macro_buffer) {
115         return;
116     }
117
118     if (*macro_pointer + direction != macro_end2) {
119         **macro_pointer = *record;
120         *macro_pointer += direction;
121     } else {
122         /* Notify about the end of buffer. The blinks are paired
123          * because they should happen on both down and up events. */
124         backlight_toggle();
125     }
126 }
127
128 /**
129  * End recording of the dynamic macro. Essentially just update the
130  * pointer to the end of the macro.
131  */
132 void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
133 {
134     dynamic_macro_led_blink();
135
136     *macro_end = macro_pointer;
137 }
138
139 /* Handle the key events related to the dynamic macros. Should be
140  * called from process_record_user() like this:
141  *
142  *   bool process_record_user(uint16_t keycode, keyrecord_t *record) {
143  *       if (!process_record_dynamic_macro(keycode, record)) {
144  *           return false;
145  *       }
146  *       <...THE REST OF THE FUNCTION...>
147  *   }
148  */
149 bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
150 {
151     /* Both macros use the same buffer but read/write on different
152      * ends of it.
153      *
154      * Macro1 is written left-to-right starting from the beginning of
155      * the buffer.
156      *
157      * Macro2 is written right-to-left starting from the end of the
158      * buffer.
159      *
160      * &macro_buffer   macro_end
161      *  v                   v
162      * +------------------------------------------------------------+
163      * |>>>>>> MACRO1 >>>>>>|    |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
164      * +------------------------------------------------------------+
165      *                           ^                                 ^
166      *                         r_macro_end                  r_macro_buffer
167      *
168      * During the recording when one macro encounters the end of the
169      * other macro, the recording is stopped. Apart from this, there
170      * are no arbitrary limits for the macros' length in relation to
171      * each other: for example one can either have two medium sized
172      * macros or one long macro and one short macro. Or even one empty
173      * and one using the whole buffer.
174      */
175     static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
176
177     /* Pointer to the first buffer element after the first macro.
178      * Initially points to the very beginning of the buffer since the
179      * macro is empty. */
180     static keyrecord_t *macro_end = macro_buffer;
181
182     /* The other end of the macro buffer. Serves as the beginning of
183      * the second macro. */
184     static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
185
186     /* Like macro_end but for the second macro. */
187     static keyrecord_t *r_macro_end = r_macro_buffer;
188
189     /* A persistent pointer to the current macro position (iterator)
190      * used during the recording. */
191     static keyrecord_t *macro_pointer = NULL;
192
193     /* 0   - no macro is being recorded right now
194      * 1,2 - either macro 1 or 2 is being recorded */
195     static uint8_t macro_id = 0;
196
197     if (macro_id == 0) {
198         /* No macro recording in progress. */
199         if (!record->event.pressed) {
200             switch (keycode) {
201             case DYN_REC_START1:
202                 dynamic_macro_record_start(&macro_pointer, macro_buffer);
203                 macro_id = 1;
204                 return false;
205             case DYN_REC_START2:
206                 dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
207                 macro_id = 2;
208                 return false;
209             case DYN_MACRO_PLAY1:
210                 dynamic_macro_play(macro_buffer, macro_end, +1);
211                 return false;
212             case DYN_MACRO_PLAY2:
213                 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
214                 return false;
215             }
216         }
217     } else {
218         /* A macro is being recorded right now. */
219         switch (keycode) {
220         case DYN_REC_STOP:
221             /* Stop the macro recording. */
222             if (record->event.pressed) { /* Ignore the initial release
223                                           * just after the recoding
224                                           * starts. */
225                 switch (macro_id) {
226                 case 1:
227                     dynamic_macro_record_end(macro_pointer, &macro_end);
228                     break;
229                 case 2:
230                     dynamic_macro_record_end(macro_pointer, &r_macro_end);
231                     break;
232                 }
233                 macro_id = 0;
234             }
235             return false;
236         default:
237             /* Store the key in the macro buffer and process it normally. */
238             switch (macro_id) {
239             case 1:
240                 dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
241                 break;
242             case 2:
243                 dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
244                 break;
245             }
246             return true;
247             break;
248         }
249     }
250
251     return true;
252 }
253
254 #endif