drashna.c 15 KB

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