diff options
Diffstat (limited to 'keyboards/bastardkb/charybdis/charybdis.c')
-rw-r--r-- | keyboards/bastardkb/charybdis/charybdis.c | 173 |
1 files changed, 111 insertions, 62 deletions
diff --git a/keyboards/bastardkb/charybdis/charybdis.c b/keyboards/bastardkb/charybdis/charybdis.c index f94682ba41..d0619ef172 100644 --- a/keyboards/bastardkb/charybdis/charybdis.c +++ b/keyboards/bastardkb/charybdis/charybdis.c @@ -18,46 +18,44 @@ */ #include "charybdis.h" +#include "transactions.h" +#include <string.h> #ifdef CONSOLE_ENABLE # include "print.h" -#endif // CONSOLE_ENABLE +#endif // CONSOLE_ENABLE #ifdef POINTING_DEVICE_ENABLE # ifndef CHARYBDIS_MINIMUM_DEFAULT_DPI # define CHARYBDIS_MINIMUM_DEFAULT_DPI 400 -# endif // CHARYBDIS_MINIMUM_DEFAULT_DPI +# endif // CHARYBDIS_MINIMUM_DEFAULT_DPI # ifndef CHARYBDIS_DEFAULT_DPI_CONFIG_STEP # define CHARYBDIS_DEFAULT_DPI_CONFIG_STEP 200 -# endif // CHARYBDIS_DEFAULT_DPI_CONFIG_STEP +# endif // CHARYBDIS_DEFAULT_DPI_CONFIG_STEP # ifndef CHARYBDIS_MINIMUM_SNIPING_DPI # define CHARYBDIS_MINIMUM_SNIPING_DPI 200 -# endif // CHARYBDIS_MINIMUM_SNIPER_MODE_DPI +# endif // CHARYBDIS_MINIMUM_SNIPER_MODE_DPI # ifndef CHARYBDIS_SNIPING_DPI_CONFIG_STEP # define CHARYBDIS_SNIPING_DPI_CONFIG_STEP 100 -# endif // CHARYBDIS_SNIPING_DPI_CONFIG_STEP +# endif // CHARYBDIS_SNIPING_DPI_CONFIG_STEP // Fixed DPI for drag-scroll. # ifndef CHARYBDIS_DRAGSCROLL_DPI # define CHARYBDIS_DRAGSCROLL_DPI 100 -# endif // CHARYBDIS_DRAGSCROLL_DPI +# endif // CHARYBDIS_DRAGSCROLL_DPI # ifndef CHARYBDIS_DRAGSCROLL_BUFFER_SIZE # define CHARYBDIS_DRAGSCROLL_BUFFER_SIZE 6 -# endif // !CHARYBDIS_DRAGSCROLL_BUFFER_SIZE - -# ifndef CHARYBDIS_POINTER_ACCELERATION_FACTOR -# define CHARYBDIS_POINTER_ACCELERATION_FACTOR 24 -# endif // !CHARYBDIS_POINTER_ACCELERATION_FACTOR +# endif // !CHARYBDIS_DRAGSCROLL_BUFFER_SIZE typedef union { uint8_t raw; struct { - uint8_t pointer_default_dpi : 4; // 16 steps available. - uint8_t pointer_sniping_dpi : 2; // 4 steps available. + uint8_t pointer_default_dpi : 4; // 16 steps available. + uint8_t pointer_sniping_dpi : 2; // 4 steps available. bool is_dragscroll_enabled : 1; bool is_sniping_enabled : 1; } __attribute__((packed)); @@ -87,13 +85,19 @@ static void read_charybdis_config_from_eeprom(charybdis_config_t* config) { * resets these 2 values to `false` since it does not make sense to persist * these across reboots of the board. */ -static void write_charybdis_config_to_eeprom(charybdis_config_t* config) { eeconfig_update_kb(config->raw); } +static void write_charybdis_config_to_eeprom(charybdis_config_t* config) { + eeconfig_update_kb(config->raw); +} /** \brief Return the current value of the pointer's default DPI. */ -static uint16_t get_pointer_default_dpi(charybdis_config_t* config) { return (uint16_t)config->pointer_default_dpi * CHARYBDIS_DEFAULT_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_DEFAULT_DPI; } +static uint16_t get_pointer_default_dpi(charybdis_config_t* config) { + return (uint16_t)config->pointer_default_dpi * CHARYBDIS_DEFAULT_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_DEFAULT_DPI; +} /** \brief Return the current value of the pointer's sniper-mode DPI. */ -static uint16_t get_pointer_sniping_dpi(charybdis_config_t* config) { return (uint16_t)config->pointer_sniping_dpi * CHARYBDIS_SNIPING_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_SNIPING_DPI; } +static uint16_t get_pointer_sniping_dpi(charybdis_config_t* config) { + return (uint16_t)config->pointer_sniping_dpi * CHARYBDIS_SNIPING_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_SNIPING_DPI; +} /** \brief Set the appropriate DPI for the input config. */ static void maybe_update_pointing_device_cpi(charybdis_config_t* config) { @@ -128,66 +132,54 @@ static void step_pointer_sniping_dpi(charybdis_config_t* config, bool forward) { maybe_update_pointing_device_cpi(config); } -uint16_t charybdis_get_pointer_default_dpi(void) { return get_pointer_default_dpi(&g_charybdis_config); } +uint16_t charybdis_get_pointer_default_dpi(void) { + return get_pointer_default_dpi(&g_charybdis_config); +} -uint16_t charybdis_get_pointer_sniping_dpi(void) { return get_pointer_sniping_dpi(&g_charybdis_config); } +uint16_t charybdis_get_pointer_sniping_dpi(void) { + return get_pointer_sniping_dpi(&g_charybdis_config); +} -void charybdis_cycle_pointer_default_dpi_noeeprom(bool forward) { step_pointer_default_dpi(&g_charybdis_config, forward); } +void charybdis_cycle_pointer_default_dpi_noeeprom(bool forward) { + step_pointer_default_dpi(&g_charybdis_config, forward); +} void charybdis_cycle_pointer_default_dpi(bool forward) { step_pointer_default_dpi(&g_charybdis_config, forward); write_charybdis_config_to_eeprom(&g_charybdis_config); } -void charybdis_cycle_pointer_sniping_dpi_noeeprom(bool forward) { step_pointer_sniping_dpi(&g_charybdis_config, forward); } +void charybdis_cycle_pointer_sniping_dpi_noeeprom(bool forward) { + step_pointer_sniping_dpi(&g_charybdis_config, forward); +} void charybdis_cycle_pointer_sniping_dpi(bool forward) { step_pointer_sniping_dpi(&g_charybdis_config, forward); write_charybdis_config_to_eeprom(&g_charybdis_config); } -bool charybdis_get_pointer_sniping_enabled(void) { return g_charybdis_config.is_sniping_enabled; } +bool charybdis_get_pointer_sniping_enabled(void) { + return g_charybdis_config.is_sniping_enabled; +} void charybdis_set_pointer_sniping_enabled(bool enable) { g_charybdis_config.is_sniping_enabled = enable; maybe_update_pointing_device_cpi(&g_charybdis_config); } -bool charybdis_get_pointer_dragscroll_enabled(void) { return g_charybdis_config.is_dragscroll_enabled; } +bool charybdis_get_pointer_dragscroll_enabled(void) { + return g_charybdis_config.is_dragscroll_enabled; +} void charybdis_set_pointer_dragscroll_enabled(bool enable) { g_charybdis_config.is_dragscroll_enabled = enable; maybe_update_pointing_device_cpi(&g_charybdis_config); } -void pointing_device_init_kb(void) { maybe_update_pointing_device_cpi(&g_charybdis_config); } - -# ifndef CONSTRAIN_HID -# define CONSTRAIN_HID(value) ((value) < -127 ? -127 : ((value) > 127 ? 127 : (value))) -# endif // !CONSTRAIN_HID - -/** - * \brief Add optional acceleration effect. - * - * If `CHARYBDIS_ENABLE_POINTER_ACCELERATION` is defined, add a simple and naive - * acceleration effect to the provided value. Return the value unchanged - * otherwise. - */ -# ifndef DISPLACEMENT_WITH_ACCELERATION -# ifdef CHARYBDIS_POINTER_ACCELERATION_ENABLE -# define DISPLACEMENT_WITH_ACCELERATION(d) (CONSTRAIN_HID(d > 0 ? d * d / CHARYBDIS_POINTER_ACCELERATION_FACTOR + d : -d * d / CHARYBDIS_POINTER_ACCELERATION_FACTOR + d)) -# else // !CHARYBDIS_POINTER_ACCELERATION_ENABLE -# define DISPLACEMENT_WITH_ACCELERATION(d) (d) -# endif // CHARYBDIS_POINTER_ACCELERATION_ENABLE -# endif // !DISPLACEMENT_WITH_ACCELERATION - /** * \brief Augment the pointing device behavior. * - * Implement the Charybdis-specific features for pointing devices: - * - Drag-scroll - * - Sniping - * - Acceleration + * Implement drag-scroll. */ static void pointing_device_task_charybdis(report_mouse_t* mouse_report) { static int16_t scroll_buffer_x = 0; @@ -197,12 +189,12 @@ static void pointing_device_task_charybdis(report_mouse_t* mouse_report) { scroll_buffer_x -= mouse_report->x; # else scroll_buffer_x += mouse_report->x; -# endif // CHARYBDIS_DRAGSCROLL_REVERSE_X +# endif // CHARYBDIS_DRAGSCROLL_REVERSE_X # ifdef CHARYBDIS_DRAGSCROLL_REVERSE_Y scroll_buffer_y -= mouse_report->y; # else scroll_buffer_y += mouse_report->y; -# endif // CHARYBDIS_DRAGSCROLL_REVERSE_Y +# endif // CHARYBDIS_DRAGSCROLL_REVERSE_Y mouse_report->x = 0; mouse_report->y = 0; if (abs(scroll_buffer_x) > CHARYBDIS_DRAGSCROLL_BUFFER_SIZE) { @@ -213,9 +205,6 @@ static void pointing_device_task_charybdis(report_mouse_t* mouse_report) { mouse_report->v = scroll_buffer_y > 0 ? 1 : -1; scroll_buffer_y = 0; } - } else if (!g_charybdis_config.is_sniping_enabled) { - mouse_report->x = DISPLACEMENT_WITH_ACCELERATION(mouse_report->x); - mouse_report->y = DISPLACEMENT_WITH_ACCELERATION(mouse_report->y); } } @@ -234,9 +223,9 @@ static bool has_shift_mod(void) { return mod_config(get_mods()) & MOD_MASK_SHIFT; # else return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT; -# endif // NO_ACTION_ONESHOT +# endif // NO_ACTION_ONESHOT } -# endif // POINTING_DEVICE_ENABLE && !NO_CHARYBDIS_KEYCODES +# endif // POINTING_DEVICE_ENABLE && !NO_CHARYBDIS_KEYCODES /** * \brief Outputs the Charybdis configuration to console. @@ -252,16 +241,16 @@ static bool has_shift_mod(void) { static void debug_charybdis_config_to_console(charybdis_config_t* config) { # ifdef CONSOLE_ENABLE dprintf("(charybdis) process_record_kb: config = {\n" - "\traw = 0x%04X,\n" + "\traw = 0x%X,\n" "\t{\n" "\t\tis_dragscroll_enabled=%b\n" "\t\tis_sniping_enabled=%b\n" - "\t\tdefault_dpi=0x%02X (%ld)\n" - "\t\tsniping_dpi=0x%01X (%ld)\n" + "\t\tdefault_dpi=0x%X (%ld)\n" + "\t\tsniping_dpi=0x%X (%ld)\n" "\t}\n" "}\n", config->raw, config->is_dragscroll_enabled, config->is_sniping_enabled, config->pointer_default_dpi, get_pointer_default_dpi(config), config->pointer_sniping_dpi, get_pointer_sniping_dpi(config)); -# endif // CONSOLE_ENABLE +# endif // CONSOLE_ENABLE } bool process_record_kb(uint16_t keycode, keyrecord_t* record) { @@ -313,7 +302,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) { } break; } -# endif // !NO_CHARYBDIS_KEYCODES +# endif // !NO_CHARYBDIS_KEYCODES # ifndef MOUSEKEY_ENABLE // Simulate mouse keys if full support is not enabled (reduces firmware size // while maintaining support for mouse keys). @@ -323,8 +312,8 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) { pointing_device_set_report(mouse_report); pointing_device_send(); } -# endif // !MOUSEKEY_ENABLE -# endif // POINTING_DEVICE_ENABLE +# endif // !MOUSEKEY_ENABLE +# endif // POINTING_DEVICE_ENABLE debug_charybdis_config_to_console(&g_charybdis_config); return true; } @@ -340,4 +329,64 @@ void matrix_init_kb(void) { read_charybdis_config_from_eeprom(&g_charybdis_config); matrix_init_user(); } -#endif // POINTING_DEVICE_ENABLE + +# ifdef CHARYBDIS_CONFIG_SYNC +void charybdis_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) { + if (initiator2target_buffer_size == sizeof(g_charybdis_config)) { + memcpy(&g_charybdis_config, initiator2target_buffer, sizeof(g_charybdis_config)); + } +} +# endif + +void keyboard_post_init_kb(void) { + maybe_update_pointing_device_cpi(&g_charybdis_config); +# ifdef CHARYBDIS_CONFIG_SYNC + transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, charybdis_config_sync_handler); +# endif + keyboard_post_init_user(); +} + +# ifdef CHARYBDIS_CONFIG_SYNC +void housekeeping_task_kb(void) { + if (is_keyboard_master()) { + // Keep track of the last state, so that we can tell if we need to propagate to slave. + static charybdis_config_t last_charybdis_config = {0}; + static uint32_t last_sync = 0; + bool needs_sync = false; + + // Check if the state values are different. + if (memcmp(&g_charybdis_config, &last_charybdis_config, sizeof(g_charybdis_config))) { + needs_sync = true; + memcpy(&last_charybdis_config, &g_charybdis_config, sizeof(g_charybdis_config)); + } + // Send to slave every 500ms regardless of state change. + if (timer_elapsed32(last_sync) > 500) { + needs_sync = true; + } + + // Perform the sync if requested. + if (needs_sync) { + if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(g_charybdis_config), &g_charybdis_config)) { + last_sync = timer_read32(); + } + } + } + // No need to invoke the user-specific callback, as it's been called + // already. +} +# endif // CHARYBDIS_CONFIG_SYNC +#endif // POINTING_DEVICE_ENABLE + +#if defined(KEYBOARD_bastardkb_charybdis_3x5_blackpill) || defined(KEYBOARD_bastardkb_charybdis_4x6_blackpill) +void keyboard_pre_init_kb(void) { + setPinInputHigh(A0); + keyboard_pre_init_user(); +} + +void matrix_scan_kb(void) { + if (!readPin(A0)) { + reset_keyboard(); + } + matrix_scan_user(); +} +#endif // KEYBOARD_bastardkb_charybdis_3x5_blackpill || KEYBOARD_bastardkb_charybdis_4x6_blackpill |