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