From 4ff40a551a310e9b29a5838f87a9db58c0e5767e Mon Sep 17 00:00:00 2001 From: Weiyi Lou Date: Sat, 29 Apr 2017 22:02:01 +1000 Subject: Add `DYN_REC_STOP` to dynamic macros Dynamic macro functionality is modified to check for `DYN_REC_STOP`, so that macro recording can be stopped with a designated key combination (e.g. `qs` or anything) instead of mandating the use of a `_DYN` layer. `_DYN` layer stopping can still be done by passing `DYN_REC_STOP` within `process_record_user()`: bool process_record_user(uint16_t keycode, keyrecord_t *record) { uint16_t macro_kc = (keycode == MO(_DYN) ? DYN_REC_STOP : keycode); if (!process_record_dynamic_macro(macro_kc, record)) { return false; } return true; } --- quantum/dynamic_macro.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'quantum/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index 64093f293..939816a59 100644 --- a/quantum/dynamic_macro.h +++ b/quantum/dynamic_macro.h @@ -40,6 +40,7 @@ enum dynamic_macro_keycodes { DYN_REC_START1 = DYNAMIC_MACRO_RANGE, DYN_REC_START2, + DYN_REC_STOP, DYN_MACRO_PLAY1, DYN_MACRO_PLAY2, }; @@ -209,9 +210,8 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record) } else { /* A macro is being recorded right now. */ switch (keycode) { - case MO(_DYN): - /* Use the layer key used to access the macro recording as - * a stop button. */ + case DYN_REC_STOP: + /* Stop the macro recording. */ if (record->event.pressed) { /* Ignore the initial release * just after the recoding * starts. */ -- cgit v1.2.3 From 40fe30e4d6b521284fa3cb7ae217ebb6d013bcdf Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski Date: Wed, 3 May 2017 23:47:52 +0200 Subject: dynamic_macro.h: Ignore all the initial key releases Right after the user initiates the macro recording, they usually need to release some keys used to access the DYN_REC_START layers. It makes sense to ignore them. Note: The keys used to access the DYN_REC_STOP key are *not* ignored. --- quantum/dynamic_macro.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'quantum/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index 939816a59..1a8ec4032 100644 --- a/quantum/dynamic_macro.h +++ b/quantum/dynamic_macro.h @@ -97,17 +97,24 @@ void dynamic_macro_play( /** * 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_end2[in] The end of the other macro which shouldn't be overwritten. * @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_end2, 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) { + return; + } + if (*macro_pointer + direction != macro_end2) { **macro_pointer = *record; *macro_pointer += direction; @@ -230,10 +237,10 @@ bool process_record_dynamic_macro(uint16_t keycode, keyrecord_t *record) /* Store the key in the macro buffer and process it normally. */ switch (macro_id) { case 1: - dynamic_macro_record_key(¯o_pointer, r_macro_end, +1, record); + dynamic_macro_record_key(macro_buffer, ¯o_pointer, r_macro_end, +1, record); break; case 2: - dynamic_macro_record_key(¯o_pointer, macro_end, -1, record); + dynamic_macro_record_key(r_macro_buffer, ¯o_pointer, macro_end, -1, record); break; } return true; -- cgit v1.2.3 From 5e2a9992783e584f66dfeef16abf9d31c976311a Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski Date: Thu, 4 May 2017 00:58:01 +0200 Subject: dynamic_macro.h: Always toggle the backlight twice as a notification Apparently sometimes the backlight was toggled only once and it was left on. --- quantum/dynamic_macro.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'quantum/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index 1a8ec4032..c9120897f 100644 --- a/quantum/dynamic_macro.h +++ b/quantum/dynamic_macro.h @@ -119,9 +119,7 @@ void dynamic_macro_record_key( **macro_pointer = *record; *macro_pointer += direction; } else { - /* Notify about the end of buffer. The blinks are paired - * because they should happen on both down and up events. */ - backlight_toggle(); + dynamic_macro_led_blink(); } } -- cgit v1.2.3 From 436d661775178fb62b46afdc3d755fdb413dcb35 Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski Date: Thu, 4 May 2017 01:19:05 +0200 Subject: dynamic_macro.h: Fix an off-by-two error We need to check whether we just passed the after-the-end point of the other macro. Instead we were checking whether we are going to reach it now. --- quantum/dynamic_macro.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'quantum/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index c9120897f..9e7845c99 100644 --- a/quantum/dynamic_macro.h +++ b/quantum/dynamic_macro.h @@ -99,14 +99,14 @@ 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 macro_end2[in] The end of the other macro which shouldn't be overwritten. + * @param macro2_end[in] The last buffer element it is safe to use before overwriting 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_end2, + keyrecord_t *macro2_end, int8_t direction, keyrecord_t *record) { @@ -115,7 +115,7 @@ void dynamic_macro_record_key( return; } - if (*macro_pointer + direction != macro_end2) { + if (*macro_pointer - direction != macro2_end) { **macro_pointer = *record; *macro_pointer += direction; } else { -- cgit v1.2.3 From 4b50ea15a954de2e6062aa7228bd1f9f76669ce7 Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski 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/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index 9e7845c99..c4017aec9 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, ¯o_end); + dynamic_macro_record_end(macro_buffer, macro_pointer, +1, ¯o_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 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/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index c4017aec9..6aae7d230 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) * ¯o_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 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/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index 6aae7d230..7dca30f07 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 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/dynamic_macro.h') diff --git a/quantum/dynamic_macro.h b/quantum/dynamic_macro.h index 7dca30f07..f242405de 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