drashna.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "drashna.h"
  17. userspace_config_t userspace_config;
  18. bool mod_key_press_timer(uint16_t code, uint16_t mod_code, bool pressed) {
  19. static uint16_t this_timer;
  20. if (pressed) {
  21. this_timer = timer_read();
  22. } else {
  23. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  24. tap_code(code);
  25. } else {
  26. register_code(mod_code);
  27. tap_code(code);
  28. unregister_code(mod_code);
  29. }
  30. }
  31. return false;
  32. }
  33. bool mod_key_press(uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
  34. if (pressed) {
  35. this_timer = timer_read();
  36. } else {
  37. if (timer_elapsed(this_timer) < TAPPING_TERM) {
  38. tap_code(code);
  39. } else {
  40. register_code(mod_code);
  41. tap_code(code);
  42. unregister_code(mod_code);
  43. }
  44. }
  45. return false;
  46. }
  47. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  48. void keyboard_pre_init_user(void) {
  49. userspace_config.raw = eeconfig_read_user();
  50. keyboard_pre_init_keymap();
  51. }
  52. // Add reconfigurable functions here, for keymap customization
  53. // This allows for a global, userspace functions, and continued
  54. // customization of the keymap. Use _keymap instead of _user
  55. // functions in the keymaps
  56. __attribute__((weak)) void matrix_init_keymap(void) {}
  57. __attribute__((weak)) void matrix_init_secret(void) {}
  58. // Call user matrix init, set default RGB colors and then
  59. // call the keymap's init function
  60. void matrix_init_user(void) {
  61. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__)
  62. DDRD &= ~(1 << 5);
  63. PORTD &= ~(1 << 5);
  64. DDRB &= ~(1 << 0);
  65. PORTB &= ~(1 << 0);
  66. #endif
  67. matrix_init_secret();
  68. matrix_init_keymap();
  69. }
  70. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  71. void keyboard_post_init_user(void) {
  72. #if defined(RGBLIGHT_ENABLE)
  73. keyboard_post_init_rgb_light();
  74. #endif
  75. #if defined(RGB_MATRIX_ENABLE)
  76. keyboard_post_init_rgb_matrix();
  77. #endif
  78. keyboard_post_init_keymap();
  79. }
  80. __attribute__((weak)) void shutdown_keymap(void) {}
  81. #ifdef RGB_MATRIX_ENABLE
  82. void rgb_matrix_update_pwm_buffers(void);
  83. #endif
  84. void shutdown_user(void) {
  85. #ifdef RGBLIGHT_ENABLE
  86. rgblight_enable_noeeprom();
  87. rgblight_mode_noeeprom(1);
  88. rgblight_setrgb_red();
  89. #endif // RGBLIGHT_ENABLE
  90. #ifdef RGB_MATRIX_ENABLE
  91. rgb_matrix_set_color_all(0xFF, 0x00, 0x00);
  92. rgb_matrix_update_pwm_buffers();
  93. #endif // RGB_MATRIX_ENABLE
  94. shutdown_keymap();
  95. }
  96. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  97. void suspend_power_down_user(void) { suspend_power_down_keymap(); }
  98. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  99. void suspend_wakeup_init_user(void) { suspend_wakeup_init_keymap(); }
  100. __attribute__((weak)) void matrix_scan_keymap(void) {}
  101. __attribute__((weak)) void matrix_scan_secret(void) {}
  102. // No global matrix scan code, so just run keymap's matrix
  103. // scan function
  104. void matrix_scan_user(void) {
  105. static bool has_ran_yet;
  106. if (!has_ran_yet) {
  107. has_ran_yet = true;
  108. startup_user();
  109. }
  110. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  111. run_diablo_macro_check();
  112. #endif // TAP_DANCE_ENABLE
  113. #if defined(RGBLIGHT_ENABLE)
  114. matrix_scan_rgb_light();
  115. #endif // RGBLIGHT_ENABLE
  116. #if defined(RGB_MATRIX_ENABLE)
  117. matrix_scan_rgb_matrix();
  118. #endif
  119. matrix_scan_secret();
  120. matrix_scan_keymap();
  121. }
  122. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  123. // on layer change, no matter where the change was initiated
  124. // Then runs keymap's layer change check
  125. layer_state_t layer_state_set_user(layer_state_t state) {
  126. if (!is_keyboard_master()) { return state; }
  127. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  128. #if defined(RGBLIGHT_ENABLE)
  129. state = layer_state_set_rgb_light(state);
  130. #endif // RGBLIGHT_ENABLE
  131. return layer_state_set_keymap(state);
  132. }
  133. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  134. // Runs state check and changes underglow color and animation
  135. layer_state_t default_layer_state_set_user(layer_state_t state) {
  136. if (!is_keyboard_master()) { return state; }
  137. state = default_layer_state_set_keymap(state);
  138. #if 0
  139. # if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  140. state = default_layer_state_set_rgb(state);
  141. # endif // RGBLIGHT_ENABLE
  142. #endif
  143. return state;
  144. }
  145. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  146. // Any custom LED code goes here.
  147. // So far, I only have keyboard specific code,
  148. // So nothing goes here.
  149. void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
  150. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  151. void eeconfig_init_user(void) {
  152. userspace_config.raw = 0;
  153. userspace_config.rgb_layer_change = true;
  154. eeconfig_update_user(userspace_config.raw);
  155. eeconfig_init_keymap();
  156. keyboard_init();
  157. }
  158. bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
  159. value &= 0xF;
  160. mask &= 0xF;
  161. return (value & mask) == mask;
  162. }