From b9dcd5ac38bc7cd3dc2fb97ac3842df03ee5f780 Mon Sep 17 00:00:00 2001
From: Drashna Jaelre <drashna@live.com>
Date: Sat, 7 Aug 2021 21:40:48 -0700
Subject: [Keymap] Drashna split transport improvement (#13905)

* Fix up split stuff

* Fix Split perf issues

* Allow LTO to be disabled

* Fixup WPM and encoders

* Fixup qmk keys per scan

* Add bootloader info

* Change encoder pins

* Fixup corne oled code

* Expand transport sync

* Improve user transport

* Cleanup mouse processing at keymap level

* Improve layer checking for mouse layering
---
 users/drashna/config.h         |  16 ++---
 users/drashna/oled_stuff.c     |  15 +++--
 users/drashna/rules.mk         |   4 +-
 users/drashna/transport_sync.c | 137 ++++++++++++++++++++++++++++++++++++-----
 4 files changed, 142 insertions(+), 30 deletions(-)

(limited to 'users/drashna')

diff --git a/users/drashna/config.h b/users/drashna/config.h
index fbba210e7a..ab0080234d 100644
--- a/users/drashna/config.h
+++ b/users/drashna/config.h
@@ -23,13 +23,15 @@
 #define USB_POLLING_INTERVAL_MS 1
 
 #if defined(SPLIT_KEYBOARD)
-#    define SPLIT_MODS_ENABLE
+// #    define SPLIT_TRANSPORT_MIRROR
 #    define SPLIT_LAYER_STATE_ENABLE
 #    define SPLIT_LED_STATE_ENABLE
-
-// #    define SPLIT_TRANSPORT_MIRROR
-#    define SERIAL_USE_MULTI_TRANSACTION
-#    define SPLIT_TRANSACTION_IDS_USER RPC_ID_USER_STATE_SYNC
+#    define SPLIT_MODS_ENABLE
+#    ifdef WPM_ENABLE
+#        define SPLIT_WPM_ENABLE
+#    endif
+#    define SELECT_SOFT_SERIAL_SPEED   1
+#    define SPLIT_TRANSACTION_IDS_USER RPC_ID_USER_STATE_SYNC, RPC_ID_USER_KEYMAP_SYNC, RPC_ID_USER_CONFIG_SYNC
 #endif
 
 #ifdef AUDIO_ENABLE
@@ -165,8 +167,8 @@
 
 #ifdef QMK_KEYS_PER_SCAN
 #    undef QMK_KEYS_PER_SCAN
-#    define QMK_KEYS_PER_SCAN 2
-#endif  // !QMK_KEYS_PER_SCAN
+#endif
+#define QMK_KEYS_PER_SCAN 4
 
 // this makes it possible to do rolling combos (zx) with keys that
 // convert to other keys on hold (z becomes ctrl when you hold it,
diff --git a/users/drashna/oled_stuff.c b/users/drashna/oled_stuff.c
index b2f49a2e7e..103b1fc7a7 100644
--- a/users/drashna/oled_stuff.c
+++ b/users/drashna/oled_stuff.c
@@ -141,10 +141,8 @@ void render_keylock_status(uint8_t led_usb_state) {
     oled_write_P(PSTR(OLED_RENDER_LOCK_CAPS), led_usb_state & (1 << USB_LED_CAPS_LOCK));
     oled_write_P(PSTR(" "), false);
     oled_write_P(PSTR(OLED_RENDER_LOCK_SCLK), led_usb_state & (1 << USB_LED_SCROLL_LOCK));
-#ifndef OLED_DISPLAY_128X64
-    oled_advance_page(true);
-#endif
 }
+
 void render_matrix_scan_rate(void) {
 #ifdef DEBUG_MATRIX_SCAN_RATE
     char     matrix_rate[5];
@@ -315,12 +313,11 @@ void render_wpm(void) {
 }
 
 #if defined(KEYBOARD_handwired_tractyl_manuform_5x6_right)
-extern keyboard_config_t keyboard_config;
-extern uint16_t          dpi_array[];
 
+extern kb_runtime_config_t kb_state;
 void render_pointing_dpi_status(void) {
     char     dpi_status[6];
-    uint16_t n    = dpi_array[keyboard_config.dpi_config];
+    uint16_t n    = kb_state.device_cpi;
     dpi_status[5] = '\0';
     dpi_status[4] = '0' + n % 10;
     dpi_status[3] = (n /= 10) % 10 ? '0' + (n) % 10 : (n / 10) % 10 ? '0' : ' ';
@@ -340,7 +337,9 @@ void render_status_secondary(void) {
     render_default_layer_state();
     render_layer_state();
     render_mod_status(get_mods() | get_oneshot_mods());
-
+#if !defined(OLED_DISPLAY_128X64) && defined(WPM_ENABLE)
+    render_wpm();
+#endif
     // render_keylock_status(host_keyboard_leds());
 }
 
@@ -349,7 +348,7 @@ void render_status_main(void) {
     oled_driver_render_logo();
 #    ifdef DEBUG_MATRIX_SCAN_RATE
     render_matrix_scan_rate();
-#    else
+#    elif defined(WPM_ENABLE)
     render_wpm();
 #    endif
 #    if defined(KEYBOARD_handwired_tractyl_manuform_5x6_right)
diff --git a/users/drashna/rules.mk b/users/drashna/rules.mk
index 1ccc51ecf1..b93ab1ee6c 100644
--- a/users/drashna/rules.mk
+++ b/users/drashna/rules.mk
@@ -2,7 +2,9 @@ SRC += drashna.c \
        process_records.c
 
 ifneq ($(PLATFORM),CHIBIOS)
-    LTO_ENABLE        = yes
+    ifneq ($(strip $(LTO_SUPPORTED)), no)
+        LTO_ENABLE        = yes
+    endif
 endif
 SPACE_CADET_ENABLE    = no
 GRAVE_ESC_ENABLE      = no
diff --git a/users/drashna/transport_sync.c b/users/drashna/transport_sync.c
index 8a3e6d1bfa..c3ef2eff5b 100644
--- a/users/drashna/transport_sync.c
+++ b/users/drashna/transport_sync.c
@@ -3,31 +3,77 @@
 #    include "transactions.h"
 #    include <string.h>
 
+#    ifdef UNICODE_ENABLE
+extern unicode_config_t unicode_config;
+#    endif
+#    ifdef AUDIO_ENABLE
+#        include "audio.h"
+#    endif
+#    if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
+extern bool tap_toggling;
+#    endif
+#    ifdef SWAP_HANDS_ENABLE
+extern bool swap_hands;
+#    endif
+extern userspace_config_t userspace_config;
+
 typedef struct {
     bool     oled_on;
-    uint16_t keymap_config;
+    bool     audio_enable;
+    bool     audio_clicky_enable;
+    bool     tap_toggling;
+    bool     unicode_mode;
+    bool     swap_hands;
+    uint8_t  reserved :2;
 } user_runtime_config_t;
 
+uint16_t transport_keymap_config    = 0;
+uint32_t transport_userspace_config = 0;
+
 user_runtime_config_t user_state;
 
-void user_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
+void user_state_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
     if (initiator2target_buffer_size == sizeof(user_state)) {
         memcpy(&user_state, initiator2target_buffer, initiator2target_buffer_size);
     }
 }
+void user_keymap_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
+    if (initiator2target_buffer_size == sizeof(transport_keymap_config)) {
+        memcpy(&transport_keymap_config, initiator2target_buffer, initiator2target_buffer_size);
+    }
+}
+void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
+    if (initiator2target_buffer_size == sizeof(transport_userspace_config)) {
+        memcpy(&transport_userspace_config, initiator2target_buffer, initiator2target_buffer_size);
+    }
+}
 
 void keyboard_post_init_transport_sync(void) {
     // Register keyboard state sync split transaction
-    transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_sync);
+    transaction_register_rpc(RPC_ID_USER_STATE_SYNC, user_state_sync);
+    transaction_register_rpc(RPC_ID_USER_KEYMAP_SYNC, user_keymap_sync);
+    transaction_register_rpc(RPC_ID_USER_CONFIG_SYNC, user_config_sync);
 }
 
-void user_state_update(void) {
+void user_transport_update(void) {
     if (is_keyboard_master()) {
 #    ifdef OLED_DRIVER_ENABLE
         user_state.oled_on = is_oled_on();
 #    endif
 
-        user_state.keymap_config = keymap_config.raw;
+        transport_keymap_config    = keymap_config.raw;
+        transport_userspace_config = userspace_config.raw;
+#    ifdef AUDIO_ENABLE
+        user_state.audio_enable        = is_audio_on();
+        user_state.audio_clicky_enable = is_clicky_on();
+#    endif
+#    if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
+        user_state.tap_toggling = tap_toggling;
+#    endif
+#    ifdef SWAP_HANDS_ENABLE
+        user_state.swap_hands = swap_hands;
+#    endif
+
     } else {
 #    ifdef OLED_DRIVER_ENABLE
         if (user_state.oled_on) {
@@ -36,17 +82,43 @@ void user_state_update(void) {
             oled_off();
         }
 #    endif
-        if (keymap_config.raw != user_state.keymap_config) {
-            keymap_config.raw = user_state.keymap_config;
+        keymap_config.raw    = transport_keymap_config;
+        userspace_config.raw = transport_userspace_config;
+#    ifdef UNICODE_ENABLE
+        unicode_config.input_mode = user_state.unicode_mode;
+#    endif
+#    ifdef AUDIO_ENABLE
+        if (user_state.audio_enable != is_audio_on()) {
+            if (user_state.audio_enable) {
+                audio_on();
+            } else {
+                audio_off();
+            }
+        }
+        if (user_state.audio_clicky_enable != is_clicky_on()) {
+            if (user_state.audio_clicky_enable) {
+                clicky_on();
+            } else {
+                clicky_off();
+            }
         }
+#    endif
+#    if defined(POINTING_DEVICE_ENABLE) && defined(KEYBOARD_handwired_tractyl_manuform)
+        tap_toggling = user_state.tap_toggling;
+#    endif
+#    ifdef SWAP_HANDS_ENABLE
+        swap_hands = user_state.swap_hands;
+#    endif
     }
 }
 
-void user_state_sync(void) {
+void user_transport_sync(void) {
     if (is_keyboard_master()) {
         // Keep track of the last state, so that we can tell if we need to propagate to slave
         static user_runtime_config_t last_user_state;
-        static uint32_t              last_sync;
+        static uint16_t              last_keymap = 0;
+        static uint32_t              last_config = 0;
+        static uint32_t              last_sync[3];
         bool                         needs_sync = false;
 
         // Check if the state values are different
@@ -54,16 +126,53 @@ void user_state_sync(void) {
             needs_sync = true;
             memcpy(&last_user_state, &user_state, sizeof(user_state));
         }
-
         // Send to slave every 500ms regardless of state change
-        if (timer_elapsed32(last_sync) > 250) {
+        if (timer_elapsed32(last_sync[0]) > 250) {
             needs_sync = true;
         }
 
         // Perform the sync if requested
         if (needs_sync) {
             if (transaction_rpc_send(RPC_ID_USER_STATE_SYNC, sizeof(user_state), &user_state)) {
-                last_sync = timer_read32();
+                last_sync[0] = timer_read32();
+            }
+            needs_sync = false;
+        }
+
+        // Check if the state values are different
+        if (memcmp(&transport_keymap_config, &last_keymap, sizeof(transport_keymap_config))) {
+            needs_sync = true;
+            memcpy(&last_keymap, &transport_keymap_config, sizeof(transport_keymap_config));
+        }
+
+        // Send to slave every 500ms regardless of state change
+        if (timer_elapsed32(last_sync[1]) > 250) {
+            needs_sync = true;
+        }
+
+        // Perform the sync if requested
+        if (needs_sync) {
+            if (transaction_rpc_send(RPC_ID_USER_KEYMAP_SYNC, sizeof(transport_keymap_config), &transport_keymap_config)) {
+                last_sync[1] = timer_read32();
+            }
+            needs_sync = false;
+        }
+
+        // Check if the state values are different
+        if (memcmp(&user_state, &last_config, sizeof(transport_userspace_config))) {
+            needs_sync = true;
+            memcpy(&last_config, &user_state, sizeof(transport_userspace_config));
+        }
+
+        // Send to slave every 500ms regardless of state change
+        if (timer_elapsed32(last_sync[2]) > 250) {
+            needs_sync = true;
+        }
+
+        // Perform the sync if requested
+        if (needs_sync) {
+            if (transaction_rpc_send(RPC_ID_USER_CONFIG_SYNC, sizeof(transport_userspace_config), &transport_userspace_config)) {
+                last_sync[2] = timer_read32();
             }
         }
     }
@@ -71,9 +180,9 @@ void user_state_sync(void) {
 
 void housekeeping_task_user(void) {
     // Update kb_state so we can send to slave
-    user_state_update();
+    user_transport_update();
 
     // Data sync from master to slave
-    user_state_sync();
+    user_transport_sync();
 }
 #endif
-- 
cgit v1.2.3