drashna.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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) {
  98. #ifdef OLED_DRIVER_ENABLE
  99. oled_off();
  100. #endif
  101. suspend_power_down_keymap();
  102. }
  103. __attribute__((weak)) void suspend_wakeup_init_keymap(void) {}
  104. void suspend_wakeup_init_user(void) { suspend_wakeup_init_keymap(); }
  105. __attribute__((weak)) void matrix_scan_keymap(void) {}
  106. __attribute__((weak)) void matrix_scan_secret(void) {}
  107. // No global matrix scan code, so just run keymap's matrix
  108. // scan function
  109. void matrix_scan_user(void) {
  110. static bool has_ran_yet;
  111. if (!has_ran_yet) {
  112. has_ran_yet = true;
  113. startup_user();
  114. }
  115. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  116. run_diablo_macro_check();
  117. #endif // TAP_DANCE_ENABLE
  118. #if defined(RGBLIGHT_ENABLE)
  119. matrix_scan_rgb_light();
  120. #endif // RGBLIGHT_ENABLE
  121. #if defined(RGB_MATRIX_ENABLE)
  122. matrix_scan_rgb_matrix();
  123. #endif
  124. matrix_scan_secret();
  125. matrix_scan_keymap();
  126. }
  127. #ifdef AUDIO_ENABLE
  128. float doom_song[][2] = SONG(E1M1_DOOM);
  129. #endif
  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. if (!is_keyboard_master()) {
  135. return state;
  136. }
  137. state = layer_state_set_keymap(state);
  138. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  139. #if defined(RGBLIGHT_ENABLE)
  140. state = layer_state_set_rgb_light(state);
  141. #endif // RGBLIGHT_ENABLE
  142. #if defined(AUDIO_ENABLE) && !defined(__arm__)
  143. static bool is_gamepad_on = false;
  144. if (layer_state_cmp(state, _GAMEPAD) != is_gamepad_on) {
  145. is_gamepad_on = layer_state_cmp(state, _GAMEPAD);
  146. if (is_gamepad_on) {
  147. PLAY_LOOP(doom_song);
  148. } else {
  149. stop_all_notes();
  150. }
  151. }
  152. #endif
  153. return state;
  154. }
  155. __attribute__((weak)) layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
  156. // Runs state check and changes underglow color and animation
  157. layer_state_t default_layer_state_set_user(layer_state_t state) {
  158. if (!is_keyboard_master()) {
  159. return state;
  160. }
  161. state = default_layer_state_set_keymap(state);
  162. #if 0
  163. # if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  164. state = default_layer_state_set_rgb(state);
  165. # endif // RGBLIGHT_ENABLE
  166. #endif
  167. return state;
  168. }
  169. __attribute__((weak)) void led_set_keymap(uint8_t usb_led) {}
  170. // Any custom LED code goes here.
  171. // So far, I only have keyboard specific code,
  172. // So nothing goes here.
  173. void led_set_user(uint8_t usb_led) { led_set_keymap(usb_led); }
  174. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  175. void eeconfig_init_user(void) {
  176. userspace_config.raw = 0;
  177. userspace_config.rgb_layer_change = true;
  178. eeconfig_update_user(userspace_config.raw);
  179. eeconfig_init_keymap();
  180. #ifdef VIA_ENABLE
  181. via_eeprom_reset();
  182. #endif
  183. keyboard_init();
  184. }
  185. bool hasAllBitsInMask(uint8_t value, uint8_t mask) {
  186. value &= 0xF;
  187. mask &= 0xF;
  188. return (value & mask) == mask;
  189. }