drashna.c 16 KB

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