2
0

drashna.c 15 KB

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