drashna.c 14 KB

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