logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

charybdis.c (14565B)


  1. /*
  2. * Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  3. * Copyright 2021 Quentin LEBASTARD <qlebastard@gmail.com>
  4. * Copyright 2021 Charly Delay <charly@codesink.dev> (@0xcharly)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Publicw License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "charybdis.h"
  20. #include "transactions.h"
  21. #include <string.h>
  22. #ifdef CONSOLE_ENABLE
  23. # include "print.h"
  24. #endif // CONSOLE_ENABLE
  25. #ifdef POINTING_DEVICE_ENABLE
  26. # ifndef CHARYBDIS_MINIMUM_DEFAULT_DPI
  27. # define CHARYBDIS_MINIMUM_DEFAULT_DPI 400
  28. # endif // CHARYBDIS_MINIMUM_DEFAULT_DPI
  29. # ifndef CHARYBDIS_DEFAULT_DPI_CONFIG_STEP
  30. # define CHARYBDIS_DEFAULT_DPI_CONFIG_STEP 200
  31. # endif // CHARYBDIS_DEFAULT_DPI_CONFIG_STEP
  32. # ifndef CHARYBDIS_MINIMUM_SNIPING_DPI
  33. # define CHARYBDIS_MINIMUM_SNIPING_DPI 200
  34. # endif // CHARYBDIS_MINIMUM_SNIPER_MODE_DPI
  35. # ifndef CHARYBDIS_SNIPING_DPI_CONFIG_STEP
  36. # define CHARYBDIS_SNIPING_DPI_CONFIG_STEP 100
  37. # endif // CHARYBDIS_SNIPING_DPI_CONFIG_STEP
  38. // Fixed DPI for drag-scroll.
  39. # ifndef CHARYBDIS_DRAGSCROLL_DPI
  40. # define CHARYBDIS_DRAGSCROLL_DPI 100
  41. # endif // CHARYBDIS_DRAGSCROLL_DPI
  42. # ifndef CHARYBDIS_DRAGSCROLL_BUFFER_SIZE
  43. # define CHARYBDIS_DRAGSCROLL_BUFFER_SIZE 6
  44. # endif // !CHARYBDIS_DRAGSCROLL_BUFFER_SIZE
  45. typedef union {
  46. uint8_t raw;
  47. struct {
  48. uint8_t pointer_default_dpi : 4; // 16 steps available.
  49. uint8_t pointer_sniping_dpi : 2; // 4 steps available.
  50. bool is_dragscroll_enabled : 1;
  51. bool is_sniping_enabled : 1;
  52. } __attribute__((packed));
  53. } charybdis_config_t;
  54. static charybdis_config_t g_charybdis_config = {0};
  55. /**
  56. * \brief Set the value of `config` from EEPROM.
  57. *
  58. * Note that `is_dragscroll_enabled` and `is_sniping_enabled` are purposefully
  59. * ignored since we do not want to persist this state to memory. In practice,
  60. * this state is always written to maximize write-performances. Therefore, we
  61. * explicitly set them to `false` in this function.
  62. */
  63. static void read_charybdis_config_from_eeprom(charybdis_config_t* config) {
  64. config->raw = eeconfig_read_kb() & 0xff;
  65. config->is_dragscroll_enabled = false;
  66. config->is_sniping_enabled = false;
  67. }
  68. /**
  69. * \brief Save the value of `config` to eeprom.
  70. *
  71. * Note that all values are written verbatim, including whether drag-scroll
  72. * and/or sniper mode are enabled. `read_charybdis_config_from_eeprom(…)`
  73. * resets these 2 values to `false` since it does not make sense to persist
  74. * these across reboots of the board.
  75. */
  76. static void write_charybdis_config_to_eeprom(charybdis_config_t* config) {
  77. eeconfig_update_kb(config->raw);
  78. }
  79. /** \brief Return the current value of the pointer's default DPI. */
  80. static uint16_t get_pointer_default_dpi(charybdis_config_t* config) {
  81. return (uint16_t)config->pointer_default_dpi * CHARYBDIS_DEFAULT_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_DEFAULT_DPI;
  82. }
  83. /** \brief Return the current value of the pointer's sniper-mode DPI. */
  84. static uint16_t get_pointer_sniping_dpi(charybdis_config_t* config) {
  85. return (uint16_t)config->pointer_sniping_dpi * CHARYBDIS_SNIPING_DPI_CONFIG_STEP + CHARYBDIS_MINIMUM_SNIPING_DPI;
  86. }
  87. /** \brief Set the appropriate DPI for the input config. */
  88. static void maybe_update_pointing_device_cpi(charybdis_config_t* config) {
  89. if (config->is_dragscroll_enabled) {
  90. pointing_device_set_cpi(CHARYBDIS_DRAGSCROLL_DPI);
  91. } else if (config->is_sniping_enabled) {
  92. pointing_device_set_cpi(get_pointer_sniping_dpi(config));
  93. } else {
  94. pointing_device_set_cpi(get_pointer_default_dpi(config));
  95. }
  96. }
  97. /**
  98. * \brief Update the pointer's default DPI to the next or previous step.
  99. *
  100. * Increases the DPI value if `forward` is `true`, decreases it otherwise.
  101. * The increment/decrement steps are equal to CHARYBDIS_DEFAULT_DPI_CONFIG_STEP.
  102. */
  103. static void step_pointer_default_dpi(charybdis_config_t* config, bool forward) {
  104. config->pointer_default_dpi += forward ? 1 : -1;
  105. maybe_update_pointing_device_cpi(config);
  106. }
  107. /**
  108. * \brief Update the pointer's sniper-mode DPI to the next or previous step.
  109. *
  110. * Increases the DPI value if `forward` is `true`, decreases it otherwise.
  111. * The increment/decrement steps are equal to CHARYBDIS_SNIPING_DPI_CONFIG_STEP.
  112. */
  113. static void step_pointer_sniping_dpi(charybdis_config_t* config, bool forward) {
  114. config->pointer_sniping_dpi += forward ? 1 : -1;
  115. maybe_update_pointing_device_cpi(config);
  116. }
  117. uint16_t charybdis_get_pointer_default_dpi(void) {
  118. return get_pointer_default_dpi(&g_charybdis_config);
  119. }
  120. uint16_t charybdis_get_pointer_sniping_dpi(void) {
  121. return get_pointer_sniping_dpi(&g_charybdis_config);
  122. }
  123. void charybdis_cycle_pointer_default_dpi_noeeprom(bool forward) {
  124. step_pointer_default_dpi(&g_charybdis_config, forward);
  125. }
  126. void charybdis_cycle_pointer_default_dpi(bool forward) {
  127. step_pointer_default_dpi(&g_charybdis_config, forward);
  128. write_charybdis_config_to_eeprom(&g_charybdis_config);
  129. }
  130. void charybdis_cycle_pointer_sniping_dpi_noeeprom(bool forward) {
  131. step_pointer_sniping_dpi(&g_charybdis_config, forward);
  132. }
  133. void charybdis_cycle_pointer_sniping_dpi(bool forward) {
  134. step_pointer_sniping_dpi(&g_charybdis_config, forward);
  135. write_charybdis_config_to_eeprom(&g_charybdis_config);
  136. }
  137. bool charybdis_get_pointer_sniping_enabled(void) {
  138. return g_charybdis_config.is_sniping_enabled;
  139. }
  140. void charybdis_set_pointer_sniping_enabled(bool enable) {
  141. g_charybdis_config.is_sniping_enabled = enable;
  142. maybe_update_pointing_device_cpi(&g_charybdis_config);
  143. }
  144. bool charybdis_get_pointer_dragscroll_enabled(void) {
  145. return g_charybdis_config.is_dragscroll_enabled;
  146. }
  147. void charybdis_set_pointer_dragscroll_enabled(bool enable) {
  148. g_charybdis_config.is_dragscroll_enabled = enable;
  149. maybe_update_pointing_device_cpi(&g_charybdis_config);
  150. }
  151. /**
  152. * \brief Augment the pointing device behavior.
  153. *
  154. * Implement drag-scroll.
  155. */
  156. static void pointing_device_task_charybdis(report_mouse_t* mouse_report) {
  157. static int16_t scroll_buffer_x = 0;
  158. static int16_t scroll_buffer_y = 0;
  159. if (g_charybdis_config.is_dragscroll_enabled) {
  160. # ifdef CHARYBDIS_DRAGSCROLL_REVERSE_X
  161. scroll_buffer_x -= mouse_report->x;
  162. # else
  163. scroll_buffer_x += mouse_report->x;
  164. # endif // CHARYBDIS_DRAGSCROLL_REVERSE_X
  165. # ifdef CHARYBDIS_DRAGSCROLL_REVERSE_Y
  166. scroll_buffer_y -= mouse_report->y;
  167. # else
  168. scroll_buffer_y += mouse_report->y;
  169. # endif // CHARYBDIS_DRAGSCROLL_REVERSE_Y
  170. mouse_report->x = 0;
  171. mouse_report->y = 0;
  172. if (abs(scroll_buffer_x) > CHARYBDIS_DRAGSCROLL_BUFFER_SIZE) {
  173. mouse_report->h = scroll_buffer_x > 0 ? 1 : -1;
  174. scroll_buffer_x = 0;
  175. }
  176. if (abs(scroll_buffer_y) > CHARYBDIS_DRAGSCROLL_BUFFER_SIZE) {
  177. mouse_report->v = scroll_buffer_y > 0 ? 1 : -1;
  178. scroll_buffer_y = 0;
  179. }
  180. }
  181. }
  182. report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  183. if (is_keyboard_master()) {
  184. pointing_device_task_charybdis(&mouse_report);
  185. mouse_report = pointing_device_task_user(mouse_report);
  186. }
  187. return mouse_report;
  188. }
  189. # if defined(POINTING_DEVICE_ENABLE) && !defined(NO_CHARYBDIS_KEYCODES)
  190. /** \brief Whether SHIFT mod is enabled. */
  191. static bool has_shift_mod(void) {
  192. # ifdef NO_ACTION_ONESHOT
  193. return mod_config(get_mods()) & MOD_MASK_SHIFT;
  194. # else
  195. return mod_config(get_mods() | get_oneshot_mods()) & MOD_MASK_SHIFT;
  196. # endif // NO_ACTION_ONESHOT
  197. }
  198. # endif // POINTING_DEVICE_ENABLE && !NO_CHARYBDIS_KEYCODES
  199. /**
  200. * \brief Outputs the Charybdis configuration to console.
  201. *
  202. * Prints the in-memory configuration structure to console, for debugging.
  203. * Includes:
  204. * - raw value
  205. * - drag-scroll: on/off
  206. * - sniping: on/off
  207. * - default DPI: internal table index/actual DPI
  208. * - sniping DPI: internal table index/actual DPI
  209. */
  210. static void debug_charybdis_config_to_console(charybdis_config_t* config) {
  211. # ifdef CONSOLE_ENABLE
  212. dprintf("(charybdis) process_record_kb: config = {\n"
  213. "\traw = 0x%X,\n"
  214. "\t{\n"
  215. "\t\tis_dragscroll_enabled=%u\n"
  216. "\t\tis_sniping_enabled=%u\n"
  217. "\t\tdefault_dpi=0x%X (%u)\n"
  218. "\t\tsniping_dpi=0x%X (%u)\n"
  219. "\t}\n"
  220. "}\n",
  221. 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));
  222. # endif // CONSOLE_ENABLE
  223. }
  224. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  225. if (!process_record_user(keycode, record)) {
  226. debug_charybdis_config_to_console(&g_charybdis_config);
  227. return false;
  228. }
  229. # ifdef POINTING_DEVICE_ENABLE
  230. # ifndef NO_CHARYBDIS_KEYCODES
  231. switch (keycode) {
  232. case POINTER_DEFAULT_DPI_FORWARD:
  233. if (record->event.pressed) {
  234. // Step backward if shifted, forward otherwise.
  235. charybdis_cycle_pointer_default_dpi(/* forward= */ !has_shift_mod());
  236. }
  237. break;
  238. case POINTER_DEFAULT_DPI_REVERSE:
  239. if (record->event.pressed) {
  240. // Step forward if shifted, backward otherwise.
  241. charybdis_cycle_pointer_default_dpi(/* forward= */ has_shift_mod());
  242. }
  243. break;
  244. case POINTER_SNIPING_DPI_FORWARD:
  245. if (record->event.pressed) {
  246. // Step backward if shifted, forward otherwise.
  247. charybdis_cycle_pointer_sniping_dpi(/* forward= */ !has_shift_mod());
  248. }
  249. break;
  250. case POINTER_SNIPING_DPI_REVERSE:
  251. if (record->event.pressed) {
  252. // Step forward if shifted, backward otherwise.
  253. charybdis_cycle_pointer_sniping_dpi(/* forward= */ has_shift_mod());
  254. }
  255. break;
  256. case SNIPING_MODE:
  257. charybdis_set_pointer_sniping_enabled(record->event.pressed);
  258. break;
  259. case SNIPING_MODE_TOGGLE:
  260. if (record->event.pressed) {
  261. charybdis_set_pointer_sniping_enabled(!charybdis_get_pointer_sniping_enabled());
  262. }
  263. break;
  264. case DRAGSCROLL_MODE:
  265. charybdis_set_pointer_dragscroll_enabled(record->event.pressed);
  266. break;
  267. case DRAGSCROLL_MODE_TOGGLE:
  268. if (record->event.pressed) {
  269. charybdis_set_pointer_dragscroll_enabled(!charybdis_get_pointer_dragscroll_enabled());
  270. }
  271. break;
  272. }
  273. # endif // !NO_CHARYBDIS_KEYCODES
  274. # endif // POINTING_DEVICE_ENABLE
  275. if (IS_QK_KB(keycode) || IS_MOUSEKEY(keycode)) {
  276. debug_charybdis_config_to_console(&g_charybdis_config);
  277. }
  278. return true;
  279. }
  280. void eeconfig_init_kb(void) {
  281. g_charybdis_config.raw = 0;
  282. write_charybdis_config_to_eeprom(&g_charybdis_config);
  283. maybe_update_pointing_device_cpi(&g_charybdis_config);
  284. eeconfig_init_user();
  285. }
  286. void matrix_init_kb(void) {
  287. read_charybdis_config_from_eeprom(&g_charybdis_config);
  288. matrix_init_user();
  289. }
  290. # ifdef CHARYBDIS_CONFIG_SYNC
  291. void charybdis_config_sync_handler(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  292. if (initiator2target_buffer_size == sizeof(g_charybdis_config)) {
  293. memcpy(&g_charybdis_config, initiator2target_buffer, sizeof(g_charybdis_config));
  294. }
  295. }
  296. # endif
  297. void keyboard_post_init_kb(void) {
  298. maybe_update_pointing_device_cpi(&g_charybdis_config);
  299. # ifdef CHARYBDIS_CONFIG_SYNC
  300. transaction_register_rpc(RPC_ID_KB_CONFIG_SYNC, charybdis_config_sync_handler);
  301. # endif
  302. keyboard_post_init_user();
  303. }
  304. # ifdef CHARYBDIS_CONFIG_SYNC
  305. void housekeeping_task_kb(void) {
  306. if (is_keyboard_master()) {
  307. // Keep track of the last state, so that we can tell if we need to propagate to slave.
  308. static charybdis_config_t last_charybdis_config = {0};
  309. static uint32_t last_sync = 0;
  310. bool needs_sync = false;
  311. // Check if the state values are different.
  312. if (memcmp(&g_charybdis_config, &last_charybdis_config, sizeof(g_charybdis_config))) {
  313. needs_sync = true;
  314. memcpy(&last_charybdis_config, &g_charybdis_config, sizeof(g_charybdis_config));
  315. }
  316. // Send to slave every 500ms regardless of state change.
  317. if (timer_elapsed32(last_sync) > 500) {
  318. needs_sync = true;
  319. }
  320. // Perform the sync if requested.
  321. if (needs_sync) {
  322. if (transaction_rpc_send(RPC_ID_KB_CONFIG_SYNC, sizeof(g_charybdis_config), &g_charybdis_config)) {
  323. last_sync = timer_read32();
  324. }
  325. }
  326. }
  327. // No need to invoke the user-specific callback, as it's been called
  328. // already.
  329. }
  330. # endif // CHARYBDIS_CONFIG_SYNC
  331. #endif // POINTING_DEVICE_ENABLE
  332. #if defined(KEYBOARD_bastardkb_charybdis_3x5_blackpill) || defined(KEYBOARD_bastardkb_charybdis_4x6_blackpill)
  333. void keyboard_pre_init_kb(void) {
  334. gpio_set_pin_input_high(A0);
  335. keyboard_pre_init_user();
  336. }
  337. void matrix_scan_kb(void) {
  338. if (!gpio_read_pin(A0)) {
  339. reset_keyboard();
  340. }
  341. matrix_scan_user();
  342. }
  343. #endif // KEYBOARD_bastardkb_charybdis_3x5_blackpill || KEYBOARD_bastardkb_charybdis_4x6_blackpill
  344. bool shutdown_kb(bool jump_to_bootloader) {
  345. if (!shutdown_user(jump_to_bootloader)) {
  346. return false;
  347. }
  348. #ifdef RGBLIGHT_ENABLE
  349. rgblight_enable_noeeprom();
  350. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  351. rgblight_setrgb(RGB_RED);
  352. #endif // RGBLIGHT_ENABLE
  353. #ifdef RGB_MATRIX_ENABLE
  354. rgb_matrix_set_color_all(RGB_RED);
  355. rgb_matrix_update_pwm_buffers();
  356. #endif // RGB_MATRIX_ENABLE
  357. return true;
  358. }