drashna.c 15 KB

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