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") && !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. 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 // TAP_DANCE_ENABLE
  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_setrgb_red();
  154. rgblight_mode(5);
  155. }
  156. #endif // RGBLIGHT_ENABLE
  157. #if ( defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE) )
  158. set_unicode_input_mode(UC_WINC);
  159. #endif //UNICODE_ENABLE
  160. matrix_init_keymap();
  161. }
  162. // No global matrix scan code, so just run keymap's matrix
  163. // scan function
  164. void matrix_scan_user(void) {
  165. #ifdef TAP_DANCE_ENABLE // Run Diablo 3 macro checking code.
  166. run_diablo_macro_check();
  167. #endif // TAP_DANCE_ENABLE
  168. matrix_scan_keymap();
  169. }
  170. // This block is for all of the gaming macros, as they were all doing
  171. // the same thing, but with differring text sent.
  172. bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
  173. if (!record->event.pressed || override) {
  174. clear_keyboard();
  175. register_code(is_overwatch ? KC_BSPC : KC_ENTER);
  176. unregister_code(is_overwatch ? KC_BSPC : KC_ENTER);
  177. wait_ms(50);
  178. send_string(str);
  179. register_code(KC_ENTER);
  180. unregister_code(KC_ENTER);
  181. }
  182. if (override) wait_ms(3000);
  183. return false;
  184. }
  185. // Defines actions tor my global custom keycodes. Defined in drashna.h file
  186. // Then runs the _keymap's record handier if not processed here
  187. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  188. // If console is enabled, it will print the matrix position and status of each key pressed
  189. #ifdef CONSOLE_ENABLE
  190. xprintf("KL: row: %u, column: %u, pressed: %u\n", record->event.key.col, record->event.key.row, record->event.pressed);
  191. #endif //CONSOLE_ENABLE
  192. // Run custom faux click code, but only if faux clicky is enabled
  193. #ifdef AUDIO_ENABLE
  194. if ( (faux_click_enabled && keycode != KC_FXCL) || (!faux_click_enabled && keycode == KC_FXCL) ) {
  195. if (record->event.pressed) {
  196. stop_all_notes();
  197. PLAY_SONG(fauxclicky_pressed);
  198. } else {
  199. stop_all_notes();
  200. PLAY_SONG(fauxclicky_released);
  201. }
  202. }
  203. #endif //AUDIO_ENABLE
  204. switch (keycode) {
  205. case KC_QWERTY:
  206. if (record->event.pressed) {
  207. set_single_persistent_default_layer(_QWERTY);
  208. }
  209. return false;
  210. break;
  211. case KC_COLEMAK:
  212. if (record->event.pressed) {
  213. set_single_persistent_default_layer(_COLEMAK);
  214. }
  215. return false;
  216. break;
  217. case KC_DVORAK:
  218. if (record->event.pressed) {
  219. set_single_persistent_default_layer(_DVORAK);
  220. }
  221. return false;
  222. break;
  223. case KC_WORKMAN:
  224. if (record->event.pressed) {
  225. set_single_persistent_default_layer(_WORKMAN);
  226. }
  227. return false;
  228. break;
  229. case LOWER:
  230. if (record->event.pressed) {
  231. layer_on(_LOWER);
  232. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  233. }
  234. else {
  235. layer_off(_LOWER);
  236. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  237. }
  238. return false;
  239. break;
  240. case RAISE:
  241. if (record->event.pressed) {
  242. layer_on(_RAISE);
  243. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  244. }
  245. else {
  246. layer_off(_RAISE);
  247. update_tri_layer(_LOWER, _RAISE, _ADJUST);
  248. }
  249. return false;
  250. break;
  251. case ADJUST:
  252. if (record->event.pressed) {
  253. layer_on(_ADJUST);
  254. }
  255. else {
  256. layer_off(_ADJUST);
  257. }
  258. return false;
  259. break;
  260. case KC_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  261. if (!record->event.pressed) {
  262. SEND_STRING("make " QMK_KEYBOARD ":" QMK_KEYMAP
  263. #if (defined(BOOTLOADER_DFU) || defined(BOOTLOADER_LUFA_DFU) || defined(BOOTLOADER_QMK_DFU))
  264. ":dfu"
  265. #elif defined(BOOTLOADER_HALFKAY)
  266. ":teensy"
  267. #elif defined(BOOTLOADER_CATERINA)
  268. ":avrdude"
  269. #endif // bootloader options
  270. SS_TAP(X_ENTER));
  271. }
  272. return false;
  273. break;
  274. case KC_RESET: // Custom RESET code that sets RGBLights to RED
  275. if (!record->event.pressed) {
  276. #ifdef RGBLIGHT_ENABLE
  277. rgblight_enable();
  278. rgblight_mode(1);
  279. rgblight_setrgb_red();
  280. #endif // RGBLIGHT_ENABLE
  281. reset_keyboard();
  282. }
  283. return false;
  284. break;
  285. case EPRM: // Resets EEPROM
  286. if (record->event.pressed) {
  287. eeconfig_init();
  288. }
  289. return false;
  290. break;
  291. case VRSN: // Prints firmware version
  292. if (record->event.pressed) {
  293. SEND_STRING(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE);
  294. }
  295. return false;
  296. break;
  297. case KC_SECRET_1 ... KC_SECRET_5: // Secrets! Externally defined strings, not stored in repo
  298. if (!record->event.pressed) {
  299. clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
  300. send_string_P(secret[keycode - KC_SECRET_1]);
  301. }
  302. return false;
  303. break;
  304. // These are a serious of gaming macros.
  305. // Only enables for the viterbi, basically,
  306. // to save on firmware space, since it's limited.
  307. #if !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
  308. case KC_OVERWATCH: // Toggle's if we hit "ENTER" or "BACKSPACE" to input macros
  309. if (record->event.pressed) { is_overwatch = !is_overwatch; }
  310. #ifdef RGBLIGHT_ENABLE
  311. is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
  312. #endif //RGBLIGHT_ENABLE
  313. return false; break;
  314. case KC_SALT:
  315. return send_game_macro("Salt, salt, salt...", record, false);
  316. case KC_MORESALT:
  317. return send_game_macro("Please sir, can I have some more salt?!", record, false);
  318. case KC_SALTHARD:
  319. return send_game_macro("Your salt only makes me harder, and even more aggressive!", record, false);
  320. case KC_GOODGAME:
  321. return send_game_macro("Good game, everyone!", record, false);
  322. case KC_GLHF:
  323. return send_game_macro("Good luck, have fun!!!", record, false);
  324. case KC_SYMM:
  325. return send_game_macro("Left click to win!", record, false);
  326. case KC_JUSTGAME:
  327. 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);
  328. case KC_TORB:
  329. return send_game_macro("That was positively riveting!", record, false);
  330. case KC_AIM:
  331. send_game_macro("That aim is absolutely amazing. It's almost like you're a machine!", record, true);
  332. return send_game_macro("Wait! That aim is TOO good! You're clearly using an aim hack! CHEATER!", record, false);
  333. case KC_C9:
  334. return send_game_macro("OMG!!! C9!!!", record, false);
  335. case KC_GGEZ:
  336. return send_game_macro("That was a fantastic game, though it was a bit easy. Try harder next time!", record, false);
  337. #endif // !(defined(KEYBOARD_orthodox_rev1) || defined(KEYBOARD_orthodox_rev3) || defined(KEYBOARD_ergodox_ez))
  338. #ifdef TAP_DANCE_ENABLE
  339. case KC_DIABLO_CLEAR: // reset all Diablo timers, disabling them
  340. if (record->event.pressed) {
  341. uint8_t dtime;
  342. for (dtime = 0; dtime < 4; dtime++) {
  343. diablo_key_time[dtime] = diablo_times[0];
  344. }
  345. }
  346. return false; break;
  347. #endif // TAP_DANCE_ENABLE
  348. case KC_FXCL:
  349. if (!record->event.pressed) { // Toggles the custom faux click code
  350. faux_click_enabled = !faux_click_enabled;
  351. }
  352. return false; break;
  353. case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
  354. #ifdef RGBLIGHT_ENABLE
  355. if (record->event.pressed) {
  356. rgb_layer_change = !rgb_layer_change;
  357. if (rgb_layer_change) {
  358. layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
  359. }
  360. }
  361. #endif // RGBLIGHT_ENABLE
  362. return false; break;
  363. #ifdef RGBLIGHT_ENABLE
  364. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
  365. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  366. rgb_layer_change = false;
  367. }
  368. return true; break;
  369. #endif // RGBLIGHT_ENABLE
  370. }
  371. return process_record_keymap(keycode, record);
  372. }
  373. // Runs state check and changes underglow color and animation
  374. // on layer change, no matter where the change was initiated
  375. // Then runs keymap's layer change check
  376. uint32_t layer_state_set_user(uint32_t state) {
  377. #ifdef RGBLIGHT_ENABLE
  378. uint8_t default_layer = eeconfig_read_default_layer();
  379. if (rgb_layer_change) {
  380. switch (biton32(state)) {
  381. case _NAV:
  382. rgblight_sethsv_blue();
  383. rgblight_mode(1);
  384. break;
  385. case _SYMB:
  386. rgblight_sethsv_blue();
  387. rgblight_mode(2);
  388. break;
  389. case _MOUS:
  390. rgblight_sethsv_yellow();
  391. rgblight_mode(1);
  392. break;
  393. case _MACROS:
  394. rgblight_sethsv_orange();
  395. is_overwatch ? rgblight_mode(17) : rgblight_mode(18);
  396. break;
  397. case _MEDIA:
  398. rgblight_sethsv_chartreuse();
  399. rgblight_mode(22);
  400. break;
  401. case _GAMEPAD:
  402. rgblight_sethsv_orange();
  403. rgblight_mode(17);
  404. break;
  405. case _DIABLO:
  406. rgblight_sethsv_red();
  407. rgblight_mode(5);
  408. break;
  409. case _RAISE:
  410. rgblight_sethsv_yellow();
  411. rgblight_mode(5);
  412. break;
  413. case _LOWER:
  414. rgblight_sethsv_orange();
  415. rgblight_mode(5);
  416. break;
  417. case _ADJUST:
  418. rgblight_sethsv_red();
  419. rgblight_mode(23);
  420. break;
  421. case _COVECUBE:
  422. rgblight_sethsv_green();
  423. rgblight_mode(2);
  424. break;
  425. default: // for any other layers, or the default layer
  426. if (default_layer & (1UL << _COLEMAK)) {
  427. rgblight_sethsv_magenta();
  428. }
  429. else if (default_layer & (1UL << _DVORAK)) {
  430. rgblight_sethsv_green();
  431. }
  432. else if (default_layer & (1UL << _WORKMAN)) {
  433. rgblight_sethsv_goldenrod();
  434. }
  435. else {
  436. rgblight_sethsv_teal();
  437. }
  438. if (biton32(state) == _MODS) { // If the non-OSM layer is enabled, then breathe
  439. rgblight_mode(2);
  440. } else { // otherwise, stay solid
  441. rgblight_mode(1);
  442. }
  443. break;
  444. }
  445. }
  446. #endif // RGBLIGHT_ENABLE
  447. return layer_state_set_keymap (state);
  448. }
  449. // Any custom LED code goes here.
  450. // So far, I only have keyboard specific code,
  451. // So nothing goes here.
  452. void led_set_user(uint8_t usb_led) {
  453. led_set_keymap(usb_led);
  454. }