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
11 * intended behavior. */
12 #define DYNAMIC_MACRO_SIZE 256
15 /* DYNAMIC_MACRO_RANGE must be set as the last element of user's
16 * "planck_keycodes" enum prior to including this header. This allows
19 enum dynamic_macro_keycodes {
20 DYN_REC_START1 = DYNAMIC_MACRO_RANGE,
26 /* Blink the LEDs to notify the user about some event. */
27 void dynamic_macro_led_blink(void)
35 * Start recording of the dynamic macro.
37 * @param[out] macro_pointer The new macro buffer iterator.
38 * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
40 void dynamic_macro_record_start(
41 keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
43 dynamic_macro_led_blink();
47 *macro_pointer = macro_buffer;
51 * Play the dynamic macro.
53 * @param macro_buffer[in] The beginning of the macro buffer being played.
54 * @param macro_end[in] The element after the last macro buffer element.
55 * @param direction[in] Either +1 or -1, which way to iterate the buffer.
57 void dynamic_macro_play(
58 keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
60 uint32_t saved_layer_state = layer_state;
65 while (macro_buffer != macro_end) {
66 process_record(macro_buffer);
67 macro_buffer += direction;
72 layer_state = saved_layer_state;
76 * Record a single key in a dynamic macro.
78 * @param macro_pointer[in,out] The current buffer position.
79 * @param macro_end2[in] The end of the other macro which shouldn't be overwritten.
80 * @param direction[in] Either +1 or -1, which way to iterate the buffer.
81 * @param record[in] The current keypress.
83 void dynamic_macro_record_key(
84 keyrecord_t **macro_pointer,
85 keyrecord_t *macro_end2,
89 if (*macro_pointer + direction != macro_end2) {
90 **macro_pointer = *record;
91 *macro_pointer += direction;
93 /* Notify about the end of buffer. The blinks are paired
94 * because they should happen on both down and up events. */
100 * End recording of the dynamic macro. Essentially just update the
101 * pointer to the end of the macro.
103 void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
105 dynamic_macro_led_blink();
107 *macro_end = macro_pointer;
110 /* Handle the key events related to the dynamic macros. Should be
111 * called from process_record_user() like this:
113 * bool process_record_user(uint16_t keycode, keyrecord_t *record) {
114 * if (!process_record_dynamic_macro(keycode, record)) {
117 * <...THE REST OF THE FUNCTION...>
120 bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
122 /* Both macros use the same buffer but read/write on different
125 * Macro1 is written left-to-right starting from the beginning of
128 * Macro2 is written right-to-left starting from the end of the
131 * ¯o_buffer macro_end
133 * +------------------------------------------------------------+
134 * |>>>>>> MACRO1 >>>>>>| |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
135 * +------------------------------------------------------------+
137 * r_macro_end r_macro_buffer
139 * During the recording when one macro encounters the end of the
140 * other macro, the recording is stopped. Apart from this, there
141 * are no arbitrary limits for the macros' length in relation to
142 * each other: for example one can either have two medium sized
143 * macros or one long macro and one short macro. Or even one empty
144 * and one using the whole buffer.
146 static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
148 /* Pointer to the first buffer element after the first macro.
149 * Initially points to the very beginning of the buffer since the
151 static keyrecord_t *macro_end = macro_buffer;
153 /* The other end of the macro buffer. Serves as the beginning of
154 * the second macro. */
155 static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
157 /* Like macro_end but for the second macro. */
158 static keyrecord_t *r_macro_end = r_macro_buffer;
160 /* A persistent pointer to the current macro position (iterator)
161 * used during the recording. */
162 static keyrecord_t *macro_pointer = NULL;
164 /* 0 - no macro is being recorded right now
165 * 1,2 - either macro 1 or 2 is being recorded */
166 static uint8_t macro_id = 0;
169 /* No macro recording in progress. */
170 if (!record->event.pressed) {
173 dynamic_macro_record_start(¯o_pointer, macro_buffer);
177 dynamic_macro_record_start(¯o_pointer, r_macro_buffer);
180 case DYN_MACRO_PLAY1:
181 dynamic_macro_play(macro_buffer, macro_end, +1);
183 case DYN_MACRO_PLAY2:
184 dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
189 /* A macro is being recorded right now. */
192 /* Use the layer key used to access the macro recording as
194 if (record->event.pressed) { /* Ignore the initial release
195 * just after the recoding
199 dynamic_macro_record_end(macro_pointer, ¯o_end);
202 dynamic_macro_record_end(macro_pointer, &r_macro_end);
209 /* Store the key in the macro buffer and process it normally. */
212 dynamic_macro_record_key(¯o_pointer, r_macro_end, +1, record);
215 dynamic_macro_record_key(¯o_pointer, macro_end, -1, record);