Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h
index 07cba19f6..0f4ac3e55 100644
--- a/quantum/dynamic_macro.h
+++ b/quantum/dynamic_macro.h
@@ -64,14 +64,60 @@ void dynamic_macro_led_blink(void)
#define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) \
((int)(direction * ((END2) - (BEGIN)) + 1))
+/* Both macros use the same buffer but read/write on different
+ * ends of it.
+ *
+ * Macro1 is written left-to-right starting from the beginning of
+ * the buffer.
+ *
+ * Macro2 is written right-to-left starting from the end of the
+ * buffer.
+ *
+ * &MACRO_BUFFER MACRO_END
+ * v v
+ * +------------------------------------------------------------+
+ * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
+ * +------------------------------------------------------------+
+ * ^ ^
+ * R_MACRO_END R_MACRO_BUFFER
+ *
+ * During the recording when one macro encounters the end of the
+ * other macro, the recording is stopped. Apart from this, there
+ * are no arbitrary limits for the macros' length in relation to
+ * each other: for example one can either have two medium sized
+ * macros or one long macro and one short macro. Or even one empty
+ * and one using the whole buffer.
+ */
+keyrecord_t MACRO_BUFFER[DYNAMIC_MACRO_SIZE];
+
+/* Pointer to the first buffer element after the first macro.
+ * Initially points to the very beginning of the buffer since the
+ * macro is empty. */
+keyrecord_t *MACRO_END = MACRO_BUFFER;
+
+/* The other end of the macro buffer. Serves as the beginning of
+ * the second macro. */
+keyrecord_t *const R_MACRO_BUFFER = MACRO_BUFFER + DYNAMIC_MACRO_SIZE - 1;
+
+/* Like MACRO_END but for the second macro. */
+keyrecord_t *R_MACRO_END = R_MACRO_BUFFER;
+
+/* A persistent pointer to the current macro position (iterator)
+ * used during the recording. */
+keyrecord_t *MACRO_POINTER = NULL;
+
+/* 0 - no macro is being recorded right now
+ * 1,2 - either macro 1 or 2 is being recorded */
+uint8_t MACRO_ID = 0;
+
/**
* Start recording of the dynamic macro.
*
- * @param[out] macro_pointer The new macro buffer iterator.
- * @param[in] macro_buffer The macro buffer used to initialize macro_pointer.
+ * @param[out] MACRO_POINTER The new macro buffer iterator.
+ * @param[in] MACRO_BUFFER The macro buffer used to initialize MACRO_POINTER.
*/
void dynamic_macro_record_start(
- keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
+ keyrecord_t **MACRO_POINTER, keyrecord_t *MACRO_BUFFER)
{
dprintln("dynamic macro recording: started");
@@ -79,18 +125,28 @@ void dynamic_macro_record_start(
clear_keyboard();
layer_clear();
- *macro_pointer = macro_buffer;
+ *MACRO_POINTER = MACRO_BUFFER;
+}
+
+void dynamic_macro_record_start_1(void) {
+ dynamic_macro_record_start(&MACRO_POINTER, MACRO_BUFFER);
+ MACRO_ID = 1;
+}
+
+void dynamic_macro_record_start_2(void) {
+ dynamic_macro_record_start(&MACRO_POINTER, MACRO_BUFFER);
+ MACRO_ID = 2;
}
/**
* Play the dynamic macro.
*
- * @param macro_buffer[in] The beginning of the macro buffer being played.
- * @param macro_end[in] The element after the last macro buffer element.
+ * @param MACRO_BUFFER[in] The beginning of the macro buffer being played.
+ * @param MACRO_END[in] The element after the last macro buffer element.
* @param direction[in] Either +1 or -1, which way to iterate the buffer.
*/
void dynamic_macro_play(
- keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
+ keyrecord_t *MACRO_BUFFER, keyrecord_t *MACRO_END, int8_t direction)
{
dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
@@ -99,9 +155,9 @@ void dynamic_macro_play(
clear_keyboard();
layer_clear();
- while (macro_buffer != macro_end) {
- process_record(macro_buffer);
- macro_buffer += direction;
+ while (MACRO_BUFFER != MACRO_END) {
+ process_record(MACRO_BUFFER);
+ MACRO_BUFFER += direction;
}
clear_keyboard();
@@ -109,24 +165,32 @@ void dynamic_macro_play(
layer_state = saved_layer_state;
}
+void dynamic_macro_play_1(void) {
+ dynamic_macro_play(MACRO_BUFFER, MACRO_END, +1);
+}
+
+void dynamic_macro_play_2(void) {
+ dynamic_macro_play(R_MACRO_BUFFER, R_MACRO_END, -1);
+}
+
/**
* Record a single key in a dynamic macro.
*
- * @param macro_buffer[in] The start of the used macro buffer.
- * @param macro_pointer[in,out] The current buffer position.
+ * @param MACRO_BUFFER[in] The start of the used macro buffer.
+ * @param MACRO_POINTER[in,out] The current buffer position.
* @param macro2_end[in] The end of the other macro.
* @param direction[in] Either +1 or -1, which way to iterate the buffer.
* @param record[in] The current keypress.
*/
void dynamic_macro_record_key(
- keyrecord_t *macro_buffer,
- keyrecord_t **macro_pointer,
+ keyrecord_t *MACRO_BUFFER,
+ keyrecord_t **MACRO_POINTER,
keyrecord_t *macro2_end,
int8_t direction,
keyrecord_t *record)
{
/* If we've just started recording, ignore all the key releases. */
- if (!record->event.pressed && *macro_pointer == macro_buffer) {
+ if (!record->event.pressed && *MACRO_POINTER == MACRO_BUFFER) {
dprintln("dynamic macro: ignoring a leading key-up event");
return;
}
@@ -134,9 +198,9 @@ void dynamic_macro_record_key(
/* The other end of the other macro is the last buffer element it
* is safe to use before overwriting the other macro.
*/
- if (*macro_pointer - direction != macro2_end) {
- **macro_pointer = *record;
- *macro_pointer += direction;
+ if (*MACRO_POINTER - direction != macro2_end) {
+ **MACRO_POINTER = *record;
+ *MACRO_POINTER += direction;
} else {
dynamic_macro_led_blink();
}
@@ -144,8 +208,8 @@ void dynamic_macro_record_key(
dprintf(
"dynamic macro: slot %d length: %d/%d\n",
DYNAMIC_MACRO_CURRENT_SLOT(),
- DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, *macro_pointer),
- DYNAMIC_MACRO_CURRENT_CAPACITY(macro_buffer, macro2_end));
+ DYNAMIC_MACRO_CURRENT_LENGTH(MACRO_BUFFER, *MACRO_POINTER),
+ DYNAMIC_MACRO_CURRENT_CAPACITY(MACRO_BUFFER, macro2_end));
}
/**
@@ -153,28 +217,36 @@ void dynamic_macro_record_key(
* pointer to the end of the macro.
*/
void dynamic_macro_record_end(
- keyrecord_t *macro_buffer,
- keyrecord_t *macro_pointer,
+ keyrecord_t *MACRO_BUFFER,
+ keyrecord_t *MACRO_POINTER,
int8_t direction,
- keyrecord_t **macro_end)
+ keyrecord_t **MACRO_END)
{
dynamic_macro_led_blink();
/* Do not save the keys being held when stopping the recording,
* i.e. the keys used to access the layer DYN_REC_STOP is on.
*/
- while (macro_pointer != macro_buffer &&
- (macro_pointer - direction)->event.pressed) {
+ while (MACRO_POINTER != MACRO_BUFFER &&
+ (MACRO_POINTER - direction)->event.pressed) {
dprintln("dynamic macro: trimming a trailing key-down event");
- macro_pointer -= direction;
+ MACRO_POINTER -= direction;
}
dprintf(
"dynamic macro: slot %d saved, length: %d\n",
DYNAMIC_MACRO_CURRENT_SLOT(),
- DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
+ DYNAMIC_MACRO_CURRENT_LENGTH(MACRO_BUFFER, MACRO_POINTER));
+
+ *MACRO_END = MACRO_POINTER;
+}
+
+void dynamic_macro_record_end_1(void) {
+ dynamic_macro_record_end(MACRO_BUFFER, MACRO_POINTER, +1, &MACRO_END);
+}
- *macro_end = macro_pointer;
+void dynamic_macro_record_end_2(void) {
+ dynamic_macro_record_end(R_MACRO_BUFFER, MACRO_POINTER, -1, &R_MACRO_END);
}
/* Handle the key events related to the dynamic macros. Should be
@@ -187,71 +259,22 @@ void dynamic_macro_record_end(
* <...THE REST OF THE FUNCTION...>
* }
*/
-bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
-{
- /* Both macros use the same buffer but read/write on different
- * ends of it.
- *
- * Macro1 is written left-to-right starting from the beginning of
- * the buffer.
- *
- * Macro2 is written right-to-left starting from the end of the
- * buffer.
- *
- * &macro_buffer macro_end
- * v v
- * +------------------------------------------------------------+
- * |>>>>>> MACRO1 >>>>>> <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
- * +------------------------------------------------------------+
- * ^ ^
- * r_macro_end r_macro_buffer
- *
- * During the recording when one macro encounters the end of the
- * other macro, the recording is stopped. Apart from this, there
- * are no arbitrary limits for the macros' length in relation to
- * each other: for example one can either have two medium sized
- * macros or one long macro and one short macro. Or even one empty
- * and one using the whole buffer.
- */
- static keyrecord_t macro_buffer[DYNAMIC_MACRO_SIZE];
-
- /* Pointer to the first buffer element after the first macro.
- * Initially points to the very beginning of the buffer since the
- * macro is empty. */
- static keyrecord_t *macro_end = macro_buffer;
-
- /* The other end of the macro buffer. Serves as the beginning of
- * the second macro. */
- static keyrecord_t *const r_macro_buffer = macro_buffer + DYNAMIC_MACRO_SIZE - 1;
-
- /* Like macro_end but for the second macro. */
- static keyrecord_t *r_macro_end = r_macro_buffer;
-
- /* A persistent pointer to the current macro position (iterator)
- * used during the recording. */
- static keyrecord_t *macro_pointer = NULL;
-
- /* 0 - no macro is being recorded right now
- * 1,2 - either macro 1 or 2 is being recorded */
- static uint8_t macro_id = 0;
-
- if (macro_id == 0) {
+bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record) {
+ if (MACRO_ID == 0) {
/* No macro recording in progress. */
if (!record->event.pressed) {
switch (keycode) {
case DYN_REC_START1:
- dynamic_macro_record_start(&macro_pointer, macro_buffer);
- macro_id = 1;
+ dynamic_macro_record_start_1();
return false;
case DYN_REC_START2:
- dynamic_macro_record_start(&macro_pointer, r_macro_buffer);
- macro_id = 2;
+ dynamic_macro_record_start_2();
return false;
case DYN_MACRO_PLAY1:
- dynamic_macro_play(macro_buffer, macro_end, +1);
+ dynamic_macro_play_1();
return false;
case DYN_MACRO_PLAY2:
- dynamic_macro_play(r_macro_buffer, r_macro_end, -1);
+ dynamic_macro_play_2();
return false;
}
}
@@ -263,15 +286,15 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
if (record->event.pressed) { /* Ignore the initial release
* just after the recoding
* starts. */
- switch (macro_id) {
+ switch (MACRO_ID) {
case 1:
- dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
+ dynamic_macro_record_end_1();
break;
case 2:
- dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
+ dynamic_macro_record_end_2();
break;
}
- macro_id = 0;
+ MACRO_ID = 0;
}
return false;
case DYN_MACRO_PLAY1:
@@ -280,12 +303,12 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
return false;
default:
/* Store the key in the macro buffer and process it normally. */
- switch (macro_id) {
+ switch (MACRO_ID) {
case 1:
- dynamic_macro_record_key(macro_buffer, &macro_pointer, r_macro_end, +1, record);
+ dynamic_macro_record_key(MACRO_BUFFER, &MACRO_POINTER, R_MACRO_END, +1, record);
break;
case 2:
- dynamic_macro_record_key(r_macro_buffer, &macro_pointer, macro_end, -1, record);
+ dynamic_macro_record_key(R_MACRO_BUFFER, &MACRO_POINTER, MACRO_END, -1, record);
break;
}
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.