From 14c7ba62b01a9e17090a79b904867fe0c3b54815 Mon Sep 17 00:00:00 2001
From: Ilya Zhuravlev <whatever@xyz.is>
Date: Sun, 11 Jul 2021 23:41:53 -0400
Subject: [PATCH] qmk_settings: fix variable delays on avr

---
 quantum/process_keycode/process_auto_shift.c |  2 +-
 quantum/qmk_settings.h                       |  9 +++++++++
 quantum/quantum.c                            |  2 +-
 quantum/vial.c                               |  4 ++--
 tmk_core/common/action.c                     | 10 +++++-----
 5 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/quantum/process_keycode/process_auto_shift.c b/quantum/process_keycode/process_auto_shift.c
index 9ef953c4b9..e5e3778b65 100644
--- a/quantum/process_keycode/process_auto_shift.c
+++ b/quantum/process_keycode/process_auto_shift.c
@@ -110,7 +110,7 @@ if (QS_auto_shift_repeat && !QS_auto_shift_no_auto_repeat) {
 }
         }
 
-        wait_ms(QS_tap_code_delay);
+        qs_wait_ms(QS_tap_code_delay);
         unregister_code(autoshift_lastkey);
         del_weak_mods(MOD_BIT(KC_LSFT));
     } else {
diff --git a/quantum/qmk_settings.h b/quantum/qmk_settings.h
index f1086b637f..4ce67103c3 100644
--- a/quantum/qmk_settings.h
+++ b/quantum/qmk_settings.h
@@ -203,3 +203,12 @@ extern qmk_settings_t QS;
 #define QS_tap_hold_caps_delay TAP_HOLD_CAPS_DELAY
 
 #endif
+
+#if defined(__AVR__) && defined(QMK_SETTINGS)
+#include <util/delay.h>
+static inline void qs_wait_ms(uint16_t timer) {
+    while (timer--) _delay_ms(1);
+}
+#else
+    #define qs_wait_ms wait_ms
+#endif
diff --git a/quantum/quantum.c b/quantum/quantum.c
index b71faa7f84..47477c5c5c 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -108,7 +108,7 @@ void unregister_code16(uint16_t code) {
 
 void tap_code16(uint16_t code) {
     register_code16(code);
-    wait_ms(QS_tap_code_delay);
+    qs_wait_ms(QS_tap_code_delay);
     unregister_code16(code);
 }
 
diff --git a/quantum/vial.c b/quantum/vial.c
index 3193ead7a8..8a43c01423 100644
--- a/quantum/vial.c
+++ b/quantum/vial.c
@@ -286,7 +286,7 @@ static void vial_keycode_tap(uint16_t keycode) __attribute__((unused));
 
 static void vial_keycode_tap(uint16_t keycode) {
     vial_keycode_down(keycode);
-    wait_ms(QS_tap_code_delay);
+    qs_wait_ms(QS_tap_code_delay);
     vial_keycode_up(keycode);
 }
 
@@ -425,7 +425,7 @@ static void on_dance_reset(qk_tap_dance_state_t *state, void *user_data) {
     uint8_t index = (uintptr_t)user_data;
     if (dynamic_keymap_get_tap_dance(index, &td_entry) != 0)
         return;
-    wait_ms(QS_tap_code_delay);
+    qs_wait_ms(QS_tap_code_delay);
     uint8_t st = dance_state[index];
     state->count = 0;
     dance_state[index] = 0;
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c
index 09e21e769e..d7c29c6d55 100644
--- a/tmk_core/common/action.c
+++ b/tmk_core/common/action.c
@@ -364,9 +364,9 @@ if (QS_oneshot_tap_toggle > 1) {
                         if (tap_count > 0) {
                             dprint("MODS_TAP: Tap: unregister_code\n");
                             if (action.layer_tap.code == KC_CAPS) {
-                                wait_ms(QS_tap_hold_caps_delay);
+                                qs_wait_ms(QS_tap_hold_caps_delay);
                             } else {
-                                wait_ms(QS_tap_code_delay);
+                                qs_wait_ms(QS_tap_code_delay);
                             }
                             unregister_code(action.key.code);
                         } else {
@@ -589,9 +589,9 @@ if (QS_oneshot_tap_toggle > 1) {
                         if (tap_count > 0) {
                             dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
                             if (action.layer_tap.code == KC_CAPS) {
-                                wait_ms(QS_tap_hold_caps_delay);
+                                qs_wait_ms(QS_tap_hold_caps_delay);
                             } else {
-                                wait_ms(QS_tap_code_delay);
+                                qs_wait_ms(QS_tap_code_delay);
                             }
                             unregister_code(action.layer_tap.code);
                         } else {
@@ -670,7 +670,7 @@ if (QS_oneshot_tap_toggle > 1) {
                         if (event.pressed) {
                             register_code(action.swap.code);
                         } else {
-                            wait_ms(QS_tap_code_delay);
+                            qs_wait_ms(QS_tap_code_delay);
                             unregister_code(action.swap.code);
                             *record = (keyrecord_t){};  // hack: reset tap mode
                         }