keymap.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright 2021 Jonathan Law, Jay Greco
  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. * Original: j-inc's kyria keymap
  17. */
  18. #include QMK_KEYBOARD_H
  19. #include "animation_frames.h"
  20. enum layer_names {
  21. _BASE,
  22. _VIA1,
  23. _VIA2,
  24. _VIA3
  25. };
  26. #define KC_DISC_MUTE KC_F23
  27. #define KC_DISC_DEAF KC_F24
  28. enum custom_keycodes {
  29. PROG = QK_KB_0,
  30. DISC_MUTE,
  31. DISC_DEAF,
  32. SUPER_ALT_TAB,
  33. _NUM_CUST_KCS,
  34. };
  35. // Macro variables
  36. bool is_alt_tab_active = false;
  37. uint16_t alt_tab_timer = 0;
  38. bool muted = false;
  39. bool deafened = false;
  40. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  41. [_BASE] = LAYOUT_all(
  42. QK_GESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_HOME,
  43. KC_F13, KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL,
  44. KC_F14, KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_PGUP,
  45. KC_F15, KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_PGDN,
  46. KC_F16, KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, MO(_VIA1), KC_RALT, KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT
  47. ),
  48. [_VIA1] = LAYOUT_all(
  49. QK_BOOT, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, KC_END,
  50. RGB_TOG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  51. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  52. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  53. _______, _______, _______, _______, _______, _______, _______, _______, KC_MPRV, KC_MPLY, KC_MNXT
  54. ),
  55. [_VIA2] = LAYOUT_all(
  56. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  57. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  58. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  59. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  60. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  61. ),
  62. [_VIA3] = LAYOUT_all(
  63. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  64. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  65. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  66. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  67. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  68. ),
  69. };
  70. bool encoder_update_user(uint8_t index, bool clockwise) {
  71. if (clockwise) {
  72. tap_code(KC_VOLU);
  73. } else {
  74. tap_code(KC_VOLD);
  75. }
  76. return true;
  77. }
  78. #ifdef OLED_ENABLE
  79. #define IDLE_FRAME_DURATION 200 // Idle animation iteration rate in ms
  80. oled_rotation_t oled_init_user(oled_rotation_t rotation) { return OLED_ROTATION_270; }
  81. uint32_t anim_timer = 0;
  82. uint32_t anim_sleep = 0;
  83. uint8_t current_idle_frame = 0;
  84. bool tap_anim = false;
  85. bool tap_anim_toggle = false;
  86. // Decompress and write a precompressed bitmap frame to the OLED.
  87. // Documentation and python compression script available at:
  88. // https://github.com/nullbitsco/squeez-o
  89. #ifdef USE_OLED_BITMAP_COMPRESSION
  90. static void oled_write_compressed_P(const char* input_block_map, const char* input_block_list) {
  91. uint16_t block_index = 0;
  92. for (uint16_t i=0; i<NUM_OLED_BYTES; i++) {
  93. uint8_t bit = i%8;
  94. uint8_t map_index = i/8;
  95. uint8_t _block_map = (uint8_t)pgm_read_byte_near(input_block_map + map_index);
  96. uint8_t nonzero_byte = (_block_map & (1 << bit));
  97. if (nonzero_byte) {
  98. const char data = (const char)pgm_read_byte_near(input_block_list + block_index++);
  99. oled_write_raw_byte(data, i);
  100. } else {
  101. const char data = (const char)0x00;
  102. oled_write_raw_byte(data, i);
  103. }
  104. }
  105. }
  106. #endif
  107. static void render_anim(void) {
  108. // Idle animation
  109. void animation_phase(void) {
  110. if (!tap_anim) {
  111. current_idle_frame = (current_idle_frame + 1) % NUM_IDLE_FRAMES;
  112. uint8_t idx = abs((NUM_IDLE_FRAMES - 1) - current_idle_frame);
  113. #ifdef USE_OLED_BITMAP_COMPRESSION
  114. oled_write_compressed_P(idle_block_map[idx], idle_frames[idx]);
  115. #else
  116. oled_write_raw_P(idle_frames[idx], NUM_OLED_BYTES);
  117. #endif
  118. }
  119. }
  120. // Idle behaviour
  121. if (get_current_wpm() != 000) { // prevent sleep
  122. oled_on();
  123. if (timer_elapsed32(anim_timer) > IDLE_FRAME_DURATION) {
  124. anim_timer = timer_read32();
  125. animation_phase();
  126. }
  127. anim_sleep = timer_read32();
  128. } else { // Turn off screen when timer threshold elapsed or reset time since last input
  129. if (timer_elapsed32(anim_sleep) > OLED_TIMEOUT) {
  130. oled_off();
  131. } else {
  132. if (timer_elapsed32(anim_timer) > IDLE_FRAME_DURATION) {
  133. anim_timer = timer_read32();
  134. animation_phase();
  135. }
  136. }
  137. }
  138. }
  139. bool oled_task_user(void) {
  140. render_anim();
  141. oled_set_cursor(0, 14);
  142. uint8_t n = get_current_wpm();
  143. char wpm_counter[6];
  144. wpm_counter[5] = '\0';
  145. wpm_counter[4] = '0' + n % 10;
  146. wpm_counter[3] = '0' + (n /= 10) % 10;
  147. wpm_counter[2] = '0' + n / 10 ;
  148. wpm_counter[1] = '0';
  149. wpm_counter[0] = '>';
  150. oled_write_ln(wpm_counter, false);
  151. return false;
  152. }
  153. #endif
  154. // Animate tap
  155. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  156. #ifdef OLED_ENABLE
  157. // Check if non-mod
  158. if ((keycode >= KC_A && keycode <= KC_0) || (keycode >= KC_TAB && keycode <= KC_SLASH)) {
  159. if (record->event.pressed) {
  160. // Display tap frames
  161. tap_anim_toggle = !tap_anim_toggle;
  162. #ifdef USE_OLED_BITMAP_COMPRESSION
  163. oled_write_compressed_P(tap_block_map[tap_anim_toggle], tap_frames[tap_anim_toggle]);
  164. #else
  165. oled_write_raw_P(tap_frames[tap_anim_toggle], NUM_OLED_BYTES);
  166. #endif
  167. }
  168. }
  169. #endif
  170. switch(keycode) {
  171. case PROG:
  172. if (record->event.pressed) {
  173. rgblight_disable_noeeprom();
  174. #ifdef OLED_ENABLE
  175. oled_off();
  176. #endif
  177. bootloader_jump();
  178. }
  179. break;
  180. case DISC_MUTE:
  181. if (record->event.pressed) {
  182. tap_code(KC_DISC_MUTE);
  183. if (!rgblight_is_enabled()) break;
  184. if (muted) {
  185. rgblight_enable_noeeprom();
  186. } else {
  187. rgblight_timer_disable();
  188. uint8_t val = rgblight_get_val();
  189. rgblight_sethsv_range(255, 255, val, 0, 1);
  190. }
  191. muted = !muted;
  192. }
  193. break;
  194. case DISC_DEAF:
  195. if (record->event.pressed) {
  196. tap_code(KC_DISC_DEAF);
  197. if (!rgblight_is_enabled()) break;
  198. if (deafened) {
  199. rgblight_enable_noeeprom();
  200. } else {
  201. rgblight_timer_disable();
  202. uint8_t val = rgblight_get_val();
  203. rgblight_sethsv_range(255, 255, val, 0, RGBLED_NUM-1);
  204. }
  205. deafened = !deafened;
  206. }
  207. break;
  208. case SUPER_ALT_TAB:
  209. if (record->event.pressed) {
  210. if (!is_alt_tab_active) {
  211. is_alt_tab_active = true;
  212. register_code(KC_LALT);
  213. }
  214. alt_tab_timer = timer_read();
  215. register_code(KC_TAB);
  216. } else {
  217. unregister_code(KC_TAB);
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. return true;
  224. }
  225. void matrix_scan_user(void) {
  226. if (is_alt_tab_active) {
  227. if (timer_elapsed(alt_tab_timer) > 1000) {
  228. unregister_code(KC_LALT);
  229. is_alt_tab_active = false;
  230. }
  231. }
  232. }