drashna.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. #include "version.h"
  16. #if (__has_include("secrets.h") && !defined(NO_SECRETS))
  17. #include "secrets.h"
  18. #else
  19. // `PROGMEM const char secret[][x]` may work better, but it takes up more space in the firmware
  20. // And I'm not familiar enough to know which is better or why...
  21. PROGMEM const char secret[][64] = {
  22. "test1",
  23. "test2",
  24. "test3",
  25. "test4",
  26. "test5"
  27. };
  28. #endif
  29. float tone_copy[][2] = SONG(SCROLL_LOCK_ON_SOUND);
  30. float tone_paste[][2] = SONG(SCROLL_LOCK_OFF_SOUND);
  31. static uint16_t copy_paste_timer;
  32. userspace_config_t userspace_config;
  33. // Helper Functions
  34. void tap(uint16_t keycode){ register_code(keycode); unregister_code(keycode); };
  35. #ifdef RGBLIGHT_ENABLE
  36. void rgblight_sethsv_default_helper(uint8_t index) {
  37. uint8_t default_layer = eeconfig_read_default_layer();
  38. if (default_layer & (1UL << _COLEMAK)) {
  39. rgblight_sethsv_at(300, 255, 255, index);
  40. rgblight_sethsv_at(300, 255, 255, index);
  41. }
  42. else if (default_layer & (1UL << _DVORAK)) {
  43. rgblight_sethsv_at(120, 255, 255, index);
  44. rgblight_sethsv_at(120, 255, 255, index);
  45. }
  46. else if (default_layer & (1UL << _WORKMAN)) {
  47. rgblight_sethsv_at(43, 255, 255, index);
  48. rgblight_sethsv_at(43, 255, 255, index);
  49. }
  50. else {
  51. rgblight_sethsv_at(180, 255, 255, index);
  52. rgblight_sethsv_at(180, 255, 255, index);
  53. }
  54. }
  55. #endif // RGBLIGHT_ENABLE
  56. // ========================================= TAP DANCE =========================================
  57. #ifdef TAP_DANCE_ENABLE
  58. //define diablo macro timer variables
  59. static uint16_t diablo_timer[4];
  60. static uint8_t diablo_times[] = { 0, 1, 3, 5, 10, 30 };
  61. static uint8_t diablo_key_time[4];
  62. // has the correct number of seconds elapsed (as defined by diablo_times)
  63. bool check_dtimer(uint8_t dtimer) { return (timer_elapsed(diablo_timer[dtimer]) < (diablo_key_time[dtimer] * 1000)) ? false : true; };
  64. // Cycle through the times for the macro, starting at 0, for disabled.
  65. // Max of six values, so don't exceed
  66. void diablo_tapdance_master(qk_tap_dance_state_t *state, void *user_data, uint8_t diablo_key) {
  67. if (state->count >= 7) {
  68. diablo_key_time[diablo_key] = diablo_times[0];
  69. reset_tap_dance(state);
  70. } else {
  71. diablo_key_time[diablo_key] = diablo_times[state->count - 1];
  72. }
  73. }
  74. // Would rather have one function for all of this, but no idea how to do that...
  75. void diablo_tapdance1(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 0); }
  76. void diablo_tapdance2(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 1); }
  77. void diablo_tapdance3(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 2); }
  78. void diablo_tapdance4(qk_tap_dance_state_t *state, void *user_data) { diablo_tapdance_master(state, user_data, 3); }
  79. //Tap Dance Definitions
  80. qk_tap_dance_action_t tap_dance_actions[] = {
  81. // tap once to disable, and more to enable timed micros
  82. [TD_D3_1] = ACTION_TAP_DANCE_FN(diablo_tapdance1),
  83. [TD_D3_2] = ACTION_TAP_DANCE_FN(diablo_tapdance2),
  84. [TD_D3_3] = ACTION_TAP_DANCE_FN(diablo_tapdance3),
  85. [TD_D3_4] = ACTION_TAP_DANCE_FN(diablo_tapdance4),
  86. };
  87. // Sends the key press to system, but only if on the Diablo layer
  88. void send_diablo_keystroke(uint8_t diablo_key) {
  89. if (biton32(layer_state) == _DIABLO) {
  90. switch (diablo_key) {
  91. case 0:
  92. tap(KC_1); break;
  93. case 1:
  94. tap(KC_2); break;
  95. case 2:
  96. tap(KC_3); break;
  97. case 3:
  98. tap(KC_4); break;
  99. }
  100. }
  101. }
  102. // Checks each of the 4 timers/keys to see if enough time has elapsed
  103. // Runs the "send string" command if enough time has passed, and resets the timer.
  104. void run_diablo_macro_check(void) {
  105. uint8_t dtime;
  106. for (dtime = 0; dtime < 4; dtime++) {
  107. if (check_dtimer(dtime) && diablo_key_time[dtime]) {
  108. diablo_timer[dtime] = timer_read();
  109. send_diablo_keystroke(dtime);
  110. }
  111. }
  112. }
  113. #endif // TAP_DANCE_ENABLE
  114. // Add reconfigurable functions here, for keymap customization
  115. // This allows for a global, userspace functions, and continued
  116. // customization of the keymap. Use _keymap instead of _user
  117. // functions in the keymaps
  118. __attribute__ ((weak))
  119. void matrix_init_keymap(void) {}
  120. __attribute__ ((weak))
  121. void matrix_scan_keymap(void) {}
  122. __attribute__ ((weak))
  123. bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
  124. return true;
  125. }
  126. __attribute__ ((weak))
  127. uint32_t layer_state_set_keymap (uint32_t state) {
  128. return state;
  129. }
  130. __attribute__ ((weak))
  131. void led_set_keymap(uint8_t usb_led) {}
  132. // Call user matrix init, set default RGB colors and then
  133. // call the keymap's init function
  134. void matrix_init_user(void) {
  135. uint8_t default_layer = eeconfig_read_default_layer();
  136. userspace_config.raw = eeprom_read_byte(EECONFIG_USERSPACE);
  137. #ifdef BOOTLOADER_CATERINA
  138. DDRD &= ~(1<<5);
  139. PORTD &= ~(1<<5);
  140. DDRB &= ~(1<<0);
  141. PORTB &= ~(1<<0);
  142. #endif
  143. if (userspace_config.rgb_layer_change) {
  144. #ifdef RGBLIGHT_ENABLE
  145. rgblight_enable();
  146. #endif // RGBLIGHT_ENABLE
  147. if (default_layer & (1UL << _COLEMAK)) {
  148. #ifdef RGBLIGHT_ENABLE
  149. rgblight_sethsv_magenta();
  150. #endif // RGBLIGHT_ENABLE
  151. } else if (default_layer & (1UL << _DVORAK)) {
  152. #ifdef RGBLIGHT_ENABLE
  153. rgblight_sethsv_green();
  154. #endif // RGBLIGHT_ENABLE
  155. } else if (default_layer & (1UL << _WORKMAN)) {
  156. #ifdef RGBLIGHT_ENABLE
  157. rgblight_sethsv_goldenrod();
  158. #endif // RGBLIGHT_ENABLE
  159. } else {
  160. #ifdef RGBLIGHT_ENABLE
  161. rgblight_sethsv_teal();
  162. #endif // RGBLIGHT_ENABLE
  163. }
  164. }
  165. #ifdef AUDIO_CLICKY
  166. clicky_enable = userspace_config.clicky_enable;
  167. #endif
  168. #if ( defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE) )
  169. set_unicode_input_mode(UC_WINC);
  170. #endif //UNICODE_ENABLE
  171. matrix_init_keymap();
  172. }
  173. // No global matrix scan code, so just run keymap's matrix
  174. // scan function
  175. void matrix_scan_user(void) {
  176. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  177. run_diablo_macro_check();
  178. #endif // TAP_DANCE_ENABLE
  179. matrix_scan_keymap();
  180. }
  181. // This block is for all of the gaming macros, as they were all doing
  182. // the same thing, but with differring text sent.
  183. bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
  184. if (!record->event.pressed || override) {
  185. clear_keyboard();
  186. tap(userspace_config.is_overwatch ? KC_BSPC : KC_ENTER);
  187. wait_ms(50);
  188. send_string(str);
  189. wait_ms(50);
  190. tap(KC_ENTER);
  191. }
  192. if (override) wait_ms(3000);
  193. return false;
  194. }
  195. // Defines actions tor my global custom keycodes. Defined in drashna.h file
  196. // Then runs the _keymap's record handier if not processed here
  197. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  198. // If console is enabled, it will print the matrix position and status of each key pressed
  199. #ifdef CONSOLE_ENABLE
  200. xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  201. #endif //CONSOLE_ENABLE
  202. switch (keycode) {
  203. case KC_QWERTY:
  204. if (record->event.pressed) {
  205. set_single_persistent_default_layer(_QWERTY);
  206. }
  207. return false;
  208. break;
  209. case KC_COLEMAK:
  210. if (record->event.pressed) {
  211. set_single_persistent_default_layer(_COLEMAK);
  212. }
  213. return false;
  214. break;
  215. case KC_DVORAK:
  216. if (record->event.pressed) {
  217. set_single_persistent_default_layer(_DVORAK);
  218. }
  219. return false;
  220. break;
  221. case KC_WORKMAN:
  222. if (record->event.pressed) {
  223. set_single_persistent_default_layer(_WORKMAN);
  224. }
  225. return false;
  226. break;
  227. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  228. if (!record->event.pressed) {
  229. SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
  230. #if (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
  231. ":dfu"
  232. #elif defined(BOOTLOADER_HALFKAY)
  233. ":teensy"
  234. #elif defined(BOOTLOADER_CATERINA)
  235. ":avrdude"
  236. #endif // bootloader options
  237. SS_TAP(X_ENTER));
  238. }
  239. return false;
  240. break;
  241. case KC_RESET: // Custom RESET code that sets RGBLights to RED
  242. if (!record->event.pressed) {
  243. #ifdef RGBLIGHT_ENABLE
  244. rgblight_enable();
  245. rgblight_mode(1);
  246. rgblight_setrgb_red();
  247. #endif // RGBLIGHT_ENABLE
  248. reset_keyboard();
  249. }
  250. return false;
  251. break;
  252. case EPRM: // Resets EEPROM
  253. if (record->event.pressed) {
  254. eeconfig_init();
  255. }
  256. return false;
  257. break;
  258. case VRSN: // Prints firmware version
  259. if (record->event.pressed) {
  260. SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE);
  261. }
  262. return false;
  263. break;
  264. case KC_SECRET_1 ... KC_SECRET_5: // Secrets! Externally defined strings, not stored in repo
  265. if (!record->event.pressed) {
  266. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  267. send_string_P(secret[keycode - KC_SECRET_1]);
  268. }
  269. return false;
  270. break;
  271. // These are a serious of gaming macros.
  272. // Only enables for the viterbi, basically,
  273. // to save on firmware space, since it's limited.
  274. #if !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_iris_rev2))
  275. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  276. if (record->event.pressed) { userspace_config.is_overwatch ^= 1; eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw); }
  277. #ifdef RGBLIGHT_ENABLE
  278. userspace_config.is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
  279. #endif //RGBLIGHT_ENABLE
  280. return false; break;
  281. case KC_SALT:
  282. return send_game_macro("Salt, salt, salt...", record, false);
  283. case KC_MORESALT:
  284. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  285. case KC_SALTHARD:
  286. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  287. case KC_GOODGAME:
  288. return send_game_macro("Good game, everyone!", record, false);
  289. case KC_GLHF:
  290. return send_game_macro("Good luck, have fun!!!", record, false);
  291. case KC_SYMM:
  292. return send_game_macro("Left click to win!", record, false);
  293. case KC_JUSTGAME:
  294. return send_game_macro("It may be a game, but if you don't want to actually try, please go play AI, so that people that actually want to take the game seriously and \"get good\" have a place to do so without trolls like you throwing games.", record, false);
  295. case KC_TORB:
  296. return send_game_macro("That was positively riveting!", record, false);
  297. case KC_AIM:
  298. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  299. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  300. case KC_C9:
  301. return send_game_macro("OMG!!! C9!!!", record, false);
  302. case KC_GGEZ:
  303. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  304. #endif // !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
  305. #ifdef TAP_DANCE_ENABLE
  306. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  307. if (record->event.pressed) {
  308. uint8_t dtime;
  309. for (dtime = 0; dtime < 4; dtime++) {
  310. diablo_key_time[dtime] = diablo_times[0];
  311. }
  312. }
  313. return false; break;
  314. #endif // TAP_DANCE_ENABLE
  315. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  316. #ifdef RGBLIGHT_ENABLE
  317. if (record->event.pressed) {
  318. userspace_config.rgb_layer_change ^= 1;
  319. eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
  320. if (userspace_config.rgb_layer_change) {
  321. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  322. }
  323. }
  324. #endif // RGBLIGHT_ENABLE
  325. return false; break;
  326. #ifdef RGBLIGHT_ENABLE
  327. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  328. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  329. userspace_config.rgb_layer_change = false;
  330. eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
  331. }
  332. return true; break;
  333. #endif // RGBLIGHT_ENABLE
  334. case KC_CCCV: // One key copy/paste
  335. if(record->event.pressed){
  336. copy_paste_timer = timer_read();
  337. } else {
  338. if (timer_elapsed(copy_paste_timer) > TAPPING_TERM) { // Hold, copy
  339. register_code(KC_LCTL);
  340. tap(KC_C);
  341. unregister_code(KC_LCTL);
  342. #ifdef AUDIO_ENABLE
  343. PLAY_SONG(tone_copy);
  344. #endif
  345. } else { // Tap, paste
  346. register_code(KC_LCTL);
  347. tap(KC_V);
  348. unregister_code(KC_LCTL);
  349. #ifdef AUDIO_ENABLE
  350. PLAY_SONG(tone_paste);
  351. #endif
  352. }
  353. }
  354. return false;
  355. break;
  356. case CLICKY_TOGGLE:
  357. #ifdef AUDIO_CLICKY
  358. userspace_config.clicky_enable = clicky_enable;
  359. eeprom_update_byte(EECONFIG_USERSPACE, userspace_config.raw);
  360. #endif
  361. break;
  362. #ifdef UNICODE_ENABLE
  363. case UC_FLIP: // (╯°□°)╯ ︵ ┻━┻
  364. if (record->event.pressed) {
  365. register_code(KC_RSFT);
  366. tap(KC_9);
  367. unregister_code(KC_RSFT);
  368. process_unicode((0x256F | QK_UNICODE), record); // Arm
  369. process_unicode((0x00B0 | QK_UNICODE), record); // Eye
  370. process_unicode((0x25A1 | QK_UNICODE), record); // Mouth
  371. process_unicode((0x00B0 | QK_UNICODE), record); // Eye
  372. register_code(KC_RSFT);
  373. tap(KC_0);
  374. unregister_code(KC_RSFT);
  375. process_unicode((0x256F | QK_UNICODE), record); // Arm
  376. tap(KC_SPC);
  377. process_unicode((0x0361 | QK_UNICODE), record); // Flippy
  378. tap(KC_SPC);
  379. process_unicode((0x253B | QK_UNICODE), record); // Table
  380. process_unicode((0x2501 | QK_UNICODE), record); // Table
  381. process_unicode((0x253B | QK_UNICODE), record); // Table
  382. }
  383. return false;
  384. break;
  385. #endif // UNICODE_ENABLE
  386. }
  387. return process_record_keymap(keycode, record);
  388. }
  389. // Runs state check and changes underglow color and animation
  390. // on layer change, no matter where the change was initiated
  391. // Then runs keymap's layer change check
  392. uint32_t layer_state_set_user(uint32_t state) {
  393. uint8_t default_layer = eeconfig_read_default_layer();
  394. state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
  395. switch (biton32(state)) {
  396. case _MACROS:
  397. #ifdef RGBLIGHT_ENABLE
  398. if (userspace_config.rgb_layer_change) {
  399. rgblight_sethsv_orange();
  400. userspace_config.is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
  401. }
  402. #endif // RGBLIGHT_ENABLE
  403. break;
  404. case _MEDIA:
  405. #ifdef RGBLIGHT_ENABLE
  406. if (userspace_config.rgb_layer_change) {
  407. rgblight_sethsv_chartreuse();
  408. rgblight_mode(22);
  409. }
  410. #endif // RGBLIGHT_ENABLE
  411. break;
  412. case _GAMEPAD:
  413. #ifdef RGBLIGHT_ENABLE
  414. if (userspace_config.rgb_layer_change) {
  415. rgblight_sethsv_orange();
  416. rgblight_mode(17);
  417. }
  418. #endif // RGBLIGHT_ENABLE
  419. break;
  420. case _DIABLO:
  421. #ifdef RGBLIGHT_ENABLE
  422. if (userspace_config.rgb_layer_change) {
  423. rgblight_sethsv_red();
  424. rgblight_mode(5);
  425. }
  426. #endif // RGBLIGHT_ENABLE
  427. break;
  428. case _RAISE:
  429. #ifdef RGBLIGHT_ENABLE
  430. if (userspace_config.rgb_layer_change) {
  431. rgblight_sethsv_yellow();
  432. rgblight_mode(5);
  433. }
  434. #endif // RGBLIGHT_ENABLE
  435. break;
  436. case _LOWER:
  437. #ifdef RGBLIGHT_ENABLE
  438. if (userspace_config.rgb_layer_change) {
  439. rgblight_sethsv_orange();
  440. rgblight_mode(5);
  441. }
  442. #endif // RGBLIGHT_ENABLE
  443. break;
  444. case _ADJUST:
  445. #ifdef RGBLIGHT_ENABLE
  446. if (userspace_config.rgb_layer_change) {
  447. rgblight_sethsv_red();
  448. rgblight_mode(23);
  449. }
  450. #endif // RGBLIGHT_ENABLE
  451. break;
  452. default: // for any other layers, or the default layer
  453. if (default_layer & (1UL << _COLEMAK)) {
  454. #ifdef RGBLIGHT_ENABLE
  455. if (userspace_config.rgb_layer_change) { rgblight_sethsv_magenta(); }
  456. #endif // RGBLIGHT_ENABLE
  457. }
  458. else if (default_layer & (1UL << _DVORAK)) {
  459. #ifdef RGBLIGHT_ENABLE
  460. if (userspace_config.rgb_layer_change) { rgblight_sethsv_green(); }
  461. #endif // RGBLIGHT_ENABLE
  462. }
  463. else if (default_layer & (1UL << _WORKMAN)) {
  464. #ifdef RGBLIGHT_ENABLE
  465. if (userspace_config.rgb_layer_change) { rgblight_sethsv_goldenrod(); }
  466. #endif // RGBLIGHT_ENABLE
  467. }
  468. else {
  469. #ifdef RGBLIGHT_ENABLE
  470. if (userspace_config.rgb_layer_change) { rgblight_sethsv_teal(); }
  471. #endif // RGBLIGHT_ENABLE
  472. }
  473. if (biton32(state) == _MODS) { // If the non-OSM layer is enabled, then breathe
  474. #ifdef RGBLIGHT_ENABLE
  475. if (userspace_config.rgb_layer_change) { rgblight_mode(2); }
  476. #endif // RGBLIGHT_ENABLE
  477. } else { // otherwise, stay solid
  478. #ifdef RGBLIGHT_ENABLE
  479. if (userspace_config.rgb_layer_change) { rgblight_mode(1); }
  480. #endif // RGBLIGHT_ENABLE
  481. }
  482. break;
  483. }
  484. return layer_state_set_keymap (state);
  485. }
  486. // Any custom LED code goes here.
  487. // So far, I only have keyboard specific code,
  488. // So nothing goes here.
  489. void led_set_user(uint8_t usb_led) {
  490. led_set_keymap(usb_led);
  491. }