drashna.c 5.8 KB

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