1 /* Author: Wojciech Siewierski < wojciech dot siewierski at onet dot pl > */
2 #ifndef DYNAMIC_MACROS_H
3 #define DYNAMIC_MACROS_H
5 #include "action_layer.h"
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
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.
17 #define DYNAMIC_MACRO_SIZE 128
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
24 enum dynamic_macro_keycodes {
25 DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
31 /* Blink the LEDs to notify the user about some event. */
32 void dynamic_macro_led_blink(void)
40 * Start recording of the dynamic macro.
42 * @param[out] macro_pointer The new macro buffer iterator.
43 * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
45 void dynamic_macro_record_start(
46 keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
48 dynamic_macro_led_blink();
52 *macro_pointer = macro_buffer;
56 * Play the dynamic macro.
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.
62 void dynamic_macro_play(
63 keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
65 uint32_t saved_layer_state = layer_state;
70 while (macro_buffer != macro_end) {
71 process_record(macro_buffer);
72 macro_buffer += direction;
77 layer_state = saved_layer_state;
81 * Record a single key in a dynamic macro.
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.
88 void dynamic_macro_record_key(
89 keyrecord_t **macro_pointer,
90 keyrecord_t *macro_end2,
94 if (*macro_pointer + direction != macro_end2) {
95 **macro_pointer = *record;
96 *macro_pointer += direction;
98 /* Notify about the end of buffer. The blinks are paired
99 * because they should happen on both down and up events. */
105 * End recording of the dynamic macro. Essentially just update the
106 * pointer to the end of the macro.
108 void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
110 dynamic_macro_led_blink();
112 *macro_end = macro_pointer;
115 /* Handle the key events related to the dynamic macros. Should be
116 * called from process_record_user() like this:
118 * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
119 * if (!process_record_dynamic_macro(keycode, record)) {
122 * <...THE REST OF THE FUNCTION...>
125 bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
127 /* Both macros use the same buffer but read/write on different
130 * Macro1 is written left-to-right starting from the beginning of
133 * Macro2 is written right-to-left starting from the end of the
136 * ¯o_buffer macro_end
138 * +------------------------------------------------------------+
139 * |>>>>>> MACRO1 >>>>>>| |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
140 * +------------------------------------------------------------+
142 * r_macro_end r_macro_buffer
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.
151 static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
153 /* Pointer to the first buffer element after the first macro.
154 * Initially points to the very beginning of the buffer since the
156 static keyrecord_t *macro_end = macro_buffer;
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;
162 /* Like macro_end but for the second macro. */
163 static keyrecord_t *r_macro_end = r_macro_buffer;
165 /* A persistent pointer to the current macro position (iterator)
166 * used during the recording. */
167 static keyrecord_t *macro_pointer = NULL;
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;
174 /* No macro recording in progress. */
175 if (!record->event.pressed) {
178 dynamic_macro_record_start(¯o_pointer, macro_buffer);
182 dynamic_macro_record_start(¯o_pointer, r_macro_buffer);
185 case DYN_MACRO_PLAY1:
186 dynamic_macro_play(macro_buffer, macro_end, +1);
188 case DYN_MACRO_PLAY2:
189 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
194 /* A macro is being recorded right now. */
197 /* Use the layer key used to access the macro recording as
199 if (record->event.pressed) { /* Ignore the initial release
200 * just after the recoding
204 dynamic_macro_record_end(macro_pointer, ¯o_end);
207 dynamic_macro_record_end(macro_pointer, &r_macro_end);
214 /* Store the key in the macro buffer and process it normally. */
217 dynamic_macro_record_key(¯o_pointer, r_macro_end, +1, record);
220 dynamic_macro_record_key(¯o_pointer, macro_end, -1, record);