From 4b50ea15a954de2e6062aa7228bd1f9f76669ce7 Mon Sep 17 00:00:00 2001
From: Wojciech Siewierski <wojciech.siewierski@onet.pl>
Date: Thu, 4 May 2017 01:37:46 +0200
Subject: dynamic_macro.h: Do not save the keys being held when stopping the
 recording

More specifically, we save them and then place the `macro_end` pointer
before them so they are essentially ignored and the other macro may
freely overwrite them.
---
 quantum/dynamic_macro.h | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

(limited to 'quantum')

diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h
index 9e7845c992..c4017aec97 100644
--- a/quantum/dynamic_macro.h
+++ b/quantum/dynamic_macro.h
@@ -127,10 +127,22 @@ void dynamic_macro_record_key(
  * End recording of the dynamic macro. Essentially just update the
  * pointer to the end of the macro.
  */
-void dynamic_macro_record_end(keyrecord_t *macro_pointer, keyrecord_t **macro_end)
+void dynamic_macro_record_end(
+    keyrecord_t *macro_buffer,
+    keyrecord_t *macro_pointer,
+    int8_t direction,
+    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) {
+        macro_pointer -= direction;
+    }
+
     *macro_end = macro_pointer;
 }
 
@@ -222,10 +234,10 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
                                           * starts. */
                 switch (macro_id) {
                 case 1:
-                    dynamic_macro_record_end(macro_pointer, &macro_end);
+                    dynamic_macro_record_end(macro_buffer, macro_pointer, +1, &macro_end);
                     break;
                 case 2:
-                    dynamic_macro_record_end(macro_pointer, &r_macro_end);
+                    dynamic_macro_record_end(r_macro_buffer, macro_pointer, -1, &r_macro_end);
                     break;
                 }
                 macro_id = 0;
-- 
cgit v1.2.3


From 8e94c9b4cba4cf3479154a11faacfa2bbad50098 Mon Sep 17 00:00:00 2001
From: Wojciech Siewierski <wojciech.siewierski@onet.pl>
Date: Thu, 4 May 2017 22:39:02 +0200
Subject: dynamic_macro.h: Make the documentation more clear

---
 quantum/dynamic_macro.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

(limited to 'quantum')

diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h
index c4017aec97..6aae7d2302 100644
--- a/quantum/dynamic_macro.h
+++ b/quantum/dynamic_macro.h
@@ -99,7 +99,7 @@ void dynamic_macro_play(
  *
  * @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 last buffer element it is safe to use before overwriting the other macro.
+ * @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.
  */
@@ -115,6 +115,9 @@ void dynamic_macro_record_key(
         return;
     }
 
+    /* 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;
@@ -170,7 +173,7 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
      * &macro_buffer   macro_end
      *  v                   v
      * +------------------------------------------------------------+
-     * |>>>>>> MACRO1 >>>>>>|    |<<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
+     * |>>>>>> MACRO1 >>>>>>      <<<<<<<<<<<<< MACRO2 <<<<<<<<<<<<<|
      * +------------------------------------------------------------+
      *                           ^                                 ^
      *                         r_macro_end                  r_macro_buffer
-- 
cgit v1.2.3


From 10a7cd7e5ae1affe226423dd94c6443f8cf64e22 Mon Sep 17 00:00:00 2001
From: Wojciech Siewierski <wojciech.siewierski@onet.pl>
Date: Thu, 4 May 2017 22:55:35 +0200
Subject: dynamic_macro.h: Add debug logs

---
 quantum/dynamic_macro.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

(limited to 'quantum')

diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h
index 6aae7d2302..7dca30f076 100644
--- a/quantum/dynamic_macro.h
+++ b/quantum/dynamic_macro.h
@@ -53,6 +53,15 @@ void dynamic_macro_led_blink(void)
     backlight_toggle();
 }
 
+/* Convenience macros used for retrieving the debug info. All of them
+ * need a `direction` variable accessible at the call site.
+ */
+#define DYNAMIC_MACRO_CURRENT_SLOT() (direction > 0 ? 1 : 2)
+#define DYNAMIC_MACRO_CURRENT_LENGTH(BEGIN, POINTER) \
+    ((int)(direction * ((POINTER) - (BEGIN))))
+#define DYNAMIC_MACRO_CURRENT_CAPACITY(BEGIN, END2) \
+    ((int)(direction * ((END2) - (BEGIN)) + 1))
+
 /**
  * Start recording of the dynamic macro.
  *
@@ -62,6 +71,8 @@ void dynamic_macro_led_blink(void)
 void dynamic_macro_record_start(
     keyrecord_t **macro_pointer, keyrecord_t *macro_buffer)
 {
+    dprintln("dynamic macro recording: started");
+
     dynamic_macro_led_blink();
 
     clear_keyboard();
@@ -79,6 +90,8 @@ void dynamic_macro_record_start(
 void dynamic_macro_play(
     keyrecord_t *macro_buffer, keyrecord_t *macro_end, int8_t direction)
 {
+    dprintf("dynamic macro: slot %d playback\n", DYNAMIC_MACRO_CURRENT_SLOT());
+
     uint32_t saved_layer_state = layer_state;
 
     clear_keyboard();
@@ -112,6 +125,7 @@ void dynamic_macro_record_key(
 {
     /* If we've just started recording, ignore all the key releases. */
     if (!record->event.pressed && *macro_pointer == macro_buffer) {
+        dprintln("dynamic macro: ignoring a leading key-up event");
         return;
     }
 
@@ -124,6 +138,12 @@ void dynamic_macro_record_key(
     } else {
         dynamic_macro_led_blink();
     }
+
+    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));
 }
 
 /**
@@ -143,9 +163,15 @@ void dynamic_macro_record_end(
      */
     while (macro_pointer != macro_buffer &&
            (macro_pointer - direction)->event.pressed) {
+        dprintln("dynamic macro: trimming a trailing key-down event");
         macro_pointer -= direction;
     }
 
+    dprintf(
+        "dynamic macro: slot %d saved, length: %d\n",
+        DYNAMIC_MACRO_CURRENT_SLOT(),
+        DYNAMIC_MACRO_CURRENT_LENGTH(macro_buffer, macro_pointer));
+
     *macro_end = macro_pointer;
 }
 
@@ -264,4 +290,8 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record)
     return true;
 }
 
+#undef DYNAMIC_MACRO_CURRENT_SLOT
+#undef DYNAMIC_MACRO_CURRENT_LENGTH
+#undef DYNAMIC_MACRO_CURRENT_CAPACITY
+
 #endif
-- 
cgit v1.2.3


From a1e156a3d20e10134ac01b4cc2eaf2c92c0d2f23 Mon Sep 17 00:00:00 2001
From: Wojciech Siewierski <wojciech.siewierski@onet.pl>
Date: Fri, 5 May 2017 00:11:24 +0200
Subject: dynamic_macro.h: Do not use backlight_toggle if backlight is disabled

Fixes #1199.
---
 quantum/dynamic_macro.h | 2 ++
 1 file changed, 2 insertions(+)

(limited to 'quantum')

diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h
index 7dca30f076..f242405def 100644
--- a/quantum/dynamic_macro.h
+++ b/quantum/dynamic_macro.h
@@ -48,9 +48,11 @@ enum dynamic_macro_keycodes {
 /* Blink the LEDs to notify the user about some event. */
 void dynamic_macro_led_blink(void)
 {
+#ifdef BACKLIGHT_ENABLE
     backlight_toggle();
     _delay_ms(100);
     backlight_toggle();
+#endif
 }
 
 /* Convenience macros used for retrieving the debug info. All of them
-- 
cgit v1.2.3