drashna.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. void bootmagic_lite(void) {
  48. matrix_scan();
  49. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  50. wait_ms(DEBOUNCING_DELAY * 2);
  51. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  52. wait_ms(DEBOUNCE * 2);
  53. #else
  54. wait_ms(30);
  55. #endif
  56. matrix_scan();
  57. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  58. bootloader_jump();
  59. }
  60. }
  61. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  62. void keyboard_pre_init_user(void) {
  63. userspace_config.raw = eeconfig_read_user();
  64. keyboard_pre_init_keymap();
  65. }
  66. // Add reconfigurable functions here, for keymap customization
  67. // This allows for a global, userspace functions, and continued
  68. // customization of the keymap. Use _keymap instead of _user
  69. // functions in the keymaps
  70. __attribute__((weak)) void matrix_init_keymap(void) {}
  71. // Call user matrix init, set default RGB colors and then
  72. // call the keymap's init function
  73. void matrix_init_user(void) {
  74. #if defined(BOOTLOADER_CATERINA) && defined(__AVR__)
  75. DDRD &= ~(1 << 5);
  76. PORTD &= ~(1 << 5);
  77. DDRB &= ~(1 << 0);
  78. PORTB &= ~(1 << 0);
  79. #endif
  80. matrix_init_keymap();
  81. }
  82. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  83. void keyboard_post_init_user(void) {
  84. #if defined(RGBLIGHT_ENABLE)
  85. keyboard_post_init_rgb_light();
  86. #endif
  87. #if defined(RGB_MATRIX_ENABLE)
  88. keyboard_post_init_rgb_matrix();
  89. #endif
  90. keyboard_post_init_keymap();
  91. }
  92. __attribute__((weak)) void shutdown_keymap(void) {}
  93. void rgb_matrix_update_pwm_buffers(void);
  94. void shutdown_user(void) {
  95. #ifdef RGBLIGHT_ENABLE
  96. rgblight_enable_noeeprom();
  97. rgblight_mode_noeeprom(1);
  98. rgblight_setrgb_red();
  99. #endif // RGBLIGHT_ENABLE
  100. #ifdef RGB_MATRIX_ENABLE
  101. rgb_matrix_set_color_all(0xFF, 0x00, 0x00);
  102. rgb_matrix_update_pwm_buffers();
  103. #endif // RGB_MATRIX_ENABLE
  104. shutdown_keymap();
  105. }
  106. __attribute__((weak)) void suspend_power_down_keymap(void) {}
  107. void suspend_power_down_user(void) { suspend_power_down_keymap(); }
  108. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  109. void suspend_wakeup_init_user(void) { suspend_wakeup_init_keymap(); }
  110. __attribute__((weak)) void matrix_scan_keymap(void) {}
  111. // No global matrix scan code, so just run keymap's matrix
  112. // scan function
  113. void matrix_scan_user(void) {
  114. static bool has_ran_yet;
  115. if (!has_ran_yet) {
  116. has_ran_yet = true;
  117. startup_user();
  118. }
  119. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  120. run_diablo_macro_check();
  121. #endif // TAP_DANCE_ENABLE
  122. #if defined(RGBLIGHT_ENABLE)
  123. matrix_scan_rgb_light();
  124. #endif // RGBLIGHT_ENABLE
  125. #if defined(RGB_MATRIX_ENABLE)
  126. matrix_scan_rgb_matrix();
  127. #endif
  128. matrix_scan_keymap();
  129. }
  130. __attribute__((weak)) layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
  131. // on layer change, no matter where the change was initiated
  132. // Then runs keymap's layer change check
  133. layer_state_t layer_state_set_user(layer_state_t state) {
  134. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  135. #if defined(RGBLIGHT_ENABLE)
  136. state = layer_state_set_rgb_light(state);
  137. #endif // RGBLIGHT_ENABLE
  138. return layer_state_set_keymap(state);
  139. }
  140. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  141. // Runs state check and changes underglow color and animation
  142. layer_state_t default_layer_state_set_user(layer_state_t state) {
  143. state = default_layer_state_set_keymap(state);
  144. #if 0
  145. # if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  146. state = default_layer_state_set_rgb(state);
  147. # endif // RGBLIGHT_ENABLE
  148. #endif
  149. return state;
  150. }
  151. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  152. // Any custom LED code goes here.
  153. // So far, I only have keyboard specific code,
  154. // So nothing goes here.
  155. void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
  156. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  157. void eeconfig_init_user(void) {
  158. userspace_config.raw = 0;
  159. userspace_config.rgb_layer_change = true;
  160. eeconfig_update_user(userspace_config.raw);
  161. eeconfig_init_keymap();
  162. keyboard_init();
  163. }
  164. bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
  165. value &= 0xF;
  166. mask &= 0xF;
  167. return (value & mask) == mask;
  168. }