2
0

quantum.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /* Copyright 2016-2017 Jack Humbert
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef PROTOCOL_LUFA
  18. #include "outputselect.h"
  19. #endif
  20. #ifndef TAPPING_TERM
  21. #define TAPPING_TERM 200
  22. #endif
  23. #include "backlight.h"
  24. extern backlight_config_t backlight_config;
  25. #ifdef FAUXCLICKY_ENABLE
  26. #include "fauxclicky.h"
  27. #endif
  28. #ifdef AUDIO_ENABLE
  29. #ifndef GOODBYE_SONG
  30. #define GOODBYE_SONG SONG(GOODBYE_SOUND)
  31. #endif
  32. #ifndef AG_NORM_SONG
  33. #define AG_NORM_SONG SONG(AG_NORM_SOUND)
  34. #endif
  35. #ifndef AG_SWAP_SONG
  36. #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
  37. #endif
  38. float goodbye_song[][2] = GOODBYE_SONG;
  39. float ag_norm_song[][2] = AG_NORM_SONG;
  40. float ag_swap_song[][2] = AG_SWAP_SONG;
  41. #ifdef DEFAULT_LAYER_SONGS
  42. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  43. #endif
  44. #endif
  45. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  46. switch (code) {
  47. case QK_MODS ... QK_MODS_MAX:
  48. break;
  49. default:
  50. return;
  51. }
  52. if (code & QK_LCTL)
  53. f(KC_LCTL);
  54. if (code & QK_LSFT)
  55. f(KC_LSFT);
  56. if (code & QK_LALT)
  57. f(KC_LALT);
  58. if (code & QK_LGUI)
  59. f(KC_LGUI);
  60. if (code < QK_RMODS_MIN) return;
  61. if (code & QK_RCTL)
  62. f(KC_RCTL);
  63. if (code & QK_RSFT)
  64. f(KC_RSFT);
  65. if (code & QK_RALT)
  66. f(KC_RALT);
  67. if (code & QK_RGUI)
  68. f(KC_RGUI);
  69. }
  70. static inline void qk_register_weak_mods(uint8_t kc) {
  71. add_weak_mods(MOD_BIT(kc));
  72. send_keyboard_report();
  73. }
  74. static inline void qk_unregister_weak_mods(uint8_t kc) {
  75. del_weak_mods(MOD_BIT(kc));
  76. send_keyboard_report();
  77. }
  78. static inline void qk_register_mods(uint8_t kc) {
  79. add_weak_mods(MOD_BIT(kc));
  80. send_keyboard_report();
  81. }
  82. static inline void qk_unregister_mods(uint8_t kc) {
  83. del_weak_mods(MOD_BIT(kc));
  84. send_keyboard_report();
  85. }
  86. void register_code16 (uint16_t code) {
  87. if (IS_MOD(code) || code == KC_NO) {
  88. do_code16 (code, qk_register_mods);
  89. } else {
  90. do_code16 (code, qk_register_weak_mods);
  91. }
  92. register_code (code);
  93. }
  94. void unregister_code16 (uint16_t code) {
  95. unregister_code (code);
  96. if (IS_MOD(code) || code == KC_NO) {
  97. do_code16 (code, qk_unregister_mods);
  98. } else {
  99. do_code16 (code, qk_unregister_weak_mods);
  100. }
  101. }
  102. __attribute__ ((weak))
  103. bool process_action_kb(keyrecord_t *record) {
  104. return true;
  105. }
  106. __attribute__ ((weak))
  107. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  108. return process_record_user(keycode, record);
  109. }
  110. __attribute__ ((weak))
  111. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  112. return true;
  113. }
  114. void reset_keyboard(void) {
  115. clear_keyboard();
  116. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
  117. music_all_notes_off();
  118. uint16_t timer_start = timer_read();
  119. PLAY_SONG(goodbye_song);
  120. shutdown_user();
  121. while(timer_elapsed(timer_start) < 250)
  122. wait_ms(1);
  123. stop_all_notes();
  124. #else
  125. wait_ms(250);
  126. #endif
  127. #ifdef CATERINA_BOOTLOADER
  128. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  129. #endif
  130. bootloader_jump();
  131. }
  132. // Shift / paren setup
  133. #ifndef LSPO_KEY
  134. #define LSPO_KEY KC_9
  135. #endif
  136. #ifndef RSPC_KEY
  137. #define RSPC_KEY KC_0
  138. #endif
  139. static bool shift_interrupted[2] = {0, 0};
  140. static uint16_t scs_timer[2] = {0, 0};
  141. /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
  142. * Used to ensure that the correct keycode is released if the key is released.
  143. */
  144. static bool grave_esc_was_shifted = false;
  145. bool process_record_quantum(keyrecord_t *record) {
  146. /* This gets the keycode from the key pressed */
  147. keypos_t key = record->event.key;
  148. uint16_t keycode;
  149. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  150. /* TODO: Use store_or_get_action() or a similar function. */
  151. if (!disable_action_cache) {
  152. uint8_t layer;
  153. if (record->event.pressed) {
  154. layer = layer_switch_get_layer(key);
  155. update_source_layers_cache(key, layer);
  156. } else {
  157. layer = read_source_layers_cache(key);
  158. }
  159. keycode = keymap_key_to_keycode(layer, key);
  160. } else
  161. #endif
  162. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  163. // This is how you use actions here
  164. // if (keycode == KC_LEAD) {
  165. // action_t action;
  166. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  167. // process_action(record, action);
  168. // return false;
  169. // }
  170. if (!(
  171. #if defined(KEY_LOCK_ENABLE)
  172. // Must run first to be able to mask key_up events.
  173. process_key_lock(&keycode, record) &&
  174. #endif
  175. process_record_kb(keycode, record) &&
  176. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYPRESSES)
  177. process_rgb_matrix(keycode, record) &&
  178. #endif
  179. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  180. process_midi(keycode, record) &&
  181. #endif
  182. #ifdef AUDIO_ENABLE
  183. process_audio(keycode, record) &&
  184. #endif
  185. #ifdef STENO_ENABLE
  186. process_steno(keycode, record) &&
  187. #endif
  188. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  189. process_music(keycode, record) &&
  190. #endif
  191. #ifdef TAP_DANCE_ENABLE
  192. process_tap_dance(keycode, record) &&
  193. #endif
  194. #ifndef DISABLE_LEADER
  195. process_leader(keycode, record) &&
  196. #endif
  197. #ifndef DISABLE_CHORDING
  198. process_chording(keycode, record) &&
  199. #endif
  200. #ifdef COMBO_ENABLE
  201. process_combo(keycode, record) &&
  202. #endif
  203. #ifdef UNICODE_ENABLE
  204. process_unicode(keycode, record) &&
  205. #endif
  206. #ifdef UCIS_ENABLE
  207. process_ucis(keycode, record) &&
  208. #endif
  209. #ifdef PRINTING_ENABLE
  210. process_printer(keycode, record) &&
  211. #endif
  212. #ifdef AUTO_SHIFT_ENABLE
  213. process_auto_shift(keycode, record) &&
  214. #endif
  215. #ifdef UNICODEMAP_ENABLE
  216. process_unicode_map(keycode, record) &&
  217. #endif
  218. #ifdef TERMINAL_ENABLE
  219. process_terminal(keycode, record) &&
  220. #endif
  221. true)) {
  222. return false;
  223. }
  224. // Shift / paren setup
  225. switch(keycode) {
  226. case RESET:
  227. if (record->event.pressed) {
  228. reset_keyboard();
  229. }
  230. return false;
  231. case DEBUG:
  232. if (record->event.pressed) {
  233. debug_enable = true;
  234. print("DEBUG: enabled.\n");
  235. }
  236. return false;
  237. #ifdef FAUXCLICKY_ENABLE
  238. case FC_TOG:
  239. if (record->event.pressed) {
  240. FAUXCLICKY_TOGGLE;
  241. }
  242. return false;
  243. case FC_ON:
  244. if (record->event.pressed) {
  245. FAUXCLICKY_ON;
  246. }
  247. return false;
  248. case FC_OFF:
  249. if (record->event.pressed) {
  250. FAUXCLICKY_OFF;
  251. }
  252. return false;
  253. #endif
  254. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  255. case RGB_TOG:
  256. if (record->event.pressed) {
  257. rgblight_toggle();
  258. }
  259. return false;
  260. case RGB_MOD:
  261. if (record->event.pressed) {
  262. rgblight_step();
  263. }
  264. return false;
  265. case RGB_HUI:
  266. if (record->event.pressed) {
  267. rgblight_increase_hue();
  268. }
  269. return false;
  270. case RGB_HUD:
  271. if (record->event.pressed) {
  272. rgblight_decrease_hue();
  273. }
  274. return false;
  275. case RGB_SAI:
  276. if (record->event.pressed) {
  277. rgblight_increase_sat();
  278. }
  279. return false;
  280. case RGB_SAD:
  281. if (record->event.pressed) {
  282. rgblight_decrease_sat();
  283. }
  284. return false;
  285. case RGB_VAI:
  286. if (record->event.pressed) {
  287. rgblight_increase_val();
  288. }
  289. return false;
  290. case RGB_VAD:
  291. if (record->event.pressed) {
  292. rgblight_decrease_val();
  293. }
  294. return false;
  295. case RGB_MODE_PLAIN:
  296. if (record->event.pressed) {
  297. rgblight_mode(1);
  298. }
  299. return false;
  300. case RGB_MODE_BREATHE:
  301. if (record->event.pressed) {
  302. if ((2 <= rgblight_get_mode()) && (rgblight_get_mode() < 5)) {
  303. rgblight_step();
  304. } else {
  305. rgblight_mode(2);
  306. }
  307. }
  308. return false;
  309. case RGB_MODE_RAINBOW:
  310. if (record->event.pressed) {
  311. if ((6 <= rgblight_get_mode()) && (rgblight_get_mode() < 8)) {
  312. rgblight_step();
  313. } else {
  314. rgblight_mode(6);
  315. }
  316. }
  317. return false;
  318. case RGB_MODE_SWIRL:
  319. if (record->event.pressed) {
  320. if ((9 <= rgblight_get_mode()) && (rgblight_get_mode() < 14)) {
  321. rgblight_step();
  322. } else {
  323. rgblight_mode(9);
  324. }
  325. }
  326. return false;
  327. case RGB_MODE_SNAKE:
  328. if (record->event.pressed) {
  329. if ((15 <= rgblight_get_mode()) && (rgblight_get_mode() < 20)) {
  330. rgblight_step();
  331. } else {
  332. rgblight_mode(15);
  333. }
  334. }
  335. return false;
  336. case RGB_MODE_KNIGHT:
  337. if (record->event.pressed) {
  338. if ((21 <= rgblight_get_mode()) && (rgblight_get_mode() < 23)) {
  339. rgblight_step();
  340. } else {
  341. rgblight_mode(21);
  342. }
  343. }
  344. return false;
  345. case RGB_MODE_XMAS:
  346. if (record->event.pressed) {
  347. rgblight_mode(24);
  348. }
  349. return false;
  350. case RGB_MODE_GRADIENT:
  351. if (record->event.pressed) {
  352. if ((25 <= rgblight_get_mode()) && (rgblight_get_mode() < 34)) {
  353. rgblight_step();
  354. } else {
  355. rgblight_mode(25);
  356. }
  357. }
  358. return false;
  359. #endif
  360. #ifdef PROTOCOL_LUFA
  361. case OUT_AUTO:
  362. if (record->event.pressed) {
  363. set_output(OUTPUT_AUTO);
  364. }
  365. return false;
  366. case OUT_USB:
  367. if (record->event.pressed) {
  368. set_output(OUTPUT_USB);
  369. }
  370. return false;
  371. #ifdef BLUETOOTH_ENABLE
  372. case OUT_BT:
  373. if (record->event.pressed) {
  374. set_output(OUTPUT_BLUETOOTH);
  375. }
  376. return false;
  377. #endif
  378. #endif
  379. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  380. if (record->event.pressed) {
  381. // MAGIC actions (BOOTMAGIC without the boot)
  382. if (!eeconfig_is_enabled()) {
  383. eeconfig_init();
  384. }
  385. /* keymap config */
  386. keymap_config.raw = eeconfig_read_keymap();
  387. switch (keycode)
  388. {
  389. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  390. keymap_config.swap_control_capslock = true;
  391. break;
  392. case MAGIC_CAPSLOCK_TO_CONTROL:
  393. keymap_config.capslock_to_control = true;
  394. break;
  395. case MAGIC_SWAP_LALT_LGUI:
  396. keymap_config.swap_lalt_lgui = true;
  397. break;
  398. case MAGIC_SWAP_RALT_RGUI:
  399. keymap_config.swap_ralt_rgui = true;
  400. break;
  401. case MAGIC_NO_GUI:
  402. keymap_config.no_gui = true;
  403. break;
  404. case MAGIC_SWAP_GRAVE_ESC:
  405. keymap_config.swap_grave_esc = true;
  406. break;
  407. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  408. keymap_config.swap_backslash_backspace = true;
  409. break;
  410. case MAGIC_HOST_NKRO:
  411. keymap_config.nkro = true;
  412. break;
  413. case MAGIC_SWAP_ALT_GUI:
  414. keymap_config.swap_lalt_lgui = true;
  415. keymap_config.swap_ralt_rgui = true;
  416. #ifdef AUDIO_ENABLE
  417. PLAY_SONG(ag_swap_song);
  418. #endif
  419. break;
  420. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  421. keymap_config.swap_control_capslock = false;
  422. break;
  423. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  424. keymap_config.capslock_to_control = false;
  425. break;
  426. case MAGIC_UNSWAP_LALT_LGUI:
  427. keymap_config.swap_lalt_lgui = false;
  428. break;
  429. case MAGIC_UNSWAP_RALT_RGUI:
  430. keymap_config.swap_ralt_rgui = false;
  431. break;
  432. case MAGIC_UNNO_GUI:
  433. keymap_config.no_gui = false;
  434. break;
  435. case MAGIC_UNSWAP_GRAVE_ESC:
  436. keymap_config.swap_grave_esc = false;
  437. break;
  438. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  439. keymap_config.swap_backslash_backspace = false;
  440. break;
  441. case MAGIC_UNHOST_NKRO:
  442. keymap_config.nkro = false;
  443. break;
  444. case MAGIC_UNSWAP_ALT_GUI:
  445. keymap_config.swap_lalt_lgui = false;
  446. keymap_config.swap_ralt_rgui = false;
  447. #ifdef AUDIO_ENABLE
  448. PLAY_SONG(ag_norm_song);
  449. #endif
  450. break;
  451. case MAGIC_TOGGLE_NKRO:
  452. keymap_config.nkro = !keymap_config.nkro;
  453. break;
  454. default:
  455. break;
  456. }
  457. eeconfig_update_keymap(keymap_config.raw);
  458. clear_keyboard(); // clear to prevent stuck keys
  459. return false;
  460. }
  461. break;
  462. case KC_LSPO: {
  463. if (record->event.pressed) {
  464. shift_interrupted[0] = false;
  465. scs_timer[0] = timer_read ();
  466. register_mods(MOD_BIT(KC_LSFT));
  467. }
  468. else {
  469. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  470. if (get_mods() & MOD_BIT(KC_RSFT)) {
  471. shift_interrupted[0] = true;
  472. shift_interrupted[1] = true;
  473. }
  474. #endif
  475. if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
  476. register_code(LSPO_KEY);
  477. unregister_code(LSPO_KEY);
  478. }
  479. unregister_mods(MOD_BIT(KC_LSFT));
  480. }
  481. return false;
  482. }
  483. case KC_RSPC: {
  484. if (record->event.pressed) {
  485. shift_interrupted[1] = false;
  486. scs_timer[1] = timer_read ();
  487. register_mods(MOD_BIT(KC_RSFT));
  488. }
  489. else {
  490. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  491. if (get_mods() & MOD_BIT(KC_LSFT)) {
  492. shift_interrupted[0] = true;
  493. shift_interrupted[1] = true;
  494. }
  495. #endif
  496. if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  497. register_code(RSPC_KEY);
  498. unregister_code(RSPC_KEY);
  499. }
  500. unregister_mods(MOD_BIT(KC_RSFT));
  501. }
  502. return false;
  503. }
  504. case GRAVE_ESC: {
  505. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  506. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  507. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  508. // if CTRL is pressed, ESC is always read as ESC, even if SHIFT or GUI is pressed.
  509. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  510. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL)))
  511. shifted = 0;
  512. #endif
  513. if (record->event.pressed) {
  514. grave_esc_was_shifted = shifted;
  515. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  516. }
  517. else {
  518. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  519. }
  520. send_keyboard_report();
  521. }
  522. default: {
  523. shift_interrupted[0] = true;
  524. shift_interrupted[1] = true;
  525. break;
  526. }
  527. }
  528. return process_action_kb(record);
  529. }
  530. __attribute__ ((weak))
  531. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  532. 0, 0, 0, 0, 0, 0, 0, 0,
  533. 0, 0, 0, 0, 0, 0, 0, 0,
  534. 0, 0, 0, 0, 0, 0, 0, 0,
  535. 0, 0, 0, 0, 0, 0, 0, 0,
  536. 0, 1, 1, 1, 1, 1, 1, 0,
  537. 1, 1, 1, 1, 0, 0, 0, 0,
  538. 0, 0, 0, 0, 0, 0, 0, 0,
  539. 0, 0, 1, 0, 1, 0, 1, 1,
  540. 1, 1, 1, 1, 1, 1, 1, 1,
  541. 1, 1, 1, 1, 1, 1, 1, 1,
  542. 1, 1, 1, 1, 1, 1, 1, 1,
  543. 1, 1, 1, 0, 0, 0, 1, 1,
  544. 0, 0, 0, 0, 0, 0, 0, 0,
  545. 0, 0, 0, 0, 0, 0, 0, 0,
  546. 0, 0, 0, 0, 0, 0, 0, 0,
  547. 0, 0, 0, 1, 1, 1, 1, 0
  548. };
  549. __attribute__ ((weak))
  550. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  551. 0, 0, 0, 0, 0, 0, 0, 0,
  552. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  553. 0, 0, 0, 0, 0, 0, 0, 0,
  554. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  555. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  556. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  557. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  558. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  559. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  560. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  561. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  562. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  563. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  564. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  565. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  566. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  567. };
  568. void send_string(const char *str) {
  569. send_string_with_delay(str, 0);
  570. }
  571. void send_string_P(const char *str) {
  572. send_string_with_delay_P(str, 0);
  573. }
  574. void send_string_with_delay(const char *str, uint8_t interval) {
  575. while (1) {
  576. char ascii_code = *str;
  577. if (!ascii_code) break;
  578. if (ascii_code == 1) {
  579. // tap
  580. uint8_t keycode = *(++str);
  581. register_code(keycode);
  582. unregister_code(keycode);
  583. } else if (ascii_code == 2) {
  584. // down
  585. uint8_t keycode = *(++str);
  586. register_code(keycode);
  587. } else if (ascii_code == 3) {
  588. // up
  589. uint8_t keycode = *(++str);
  590. unregister_code(keycode);
  591. } else {
  592. send_char(ascii_code);
  593. }
  594. ++str;
  595. // interval
  596. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  597. }
  598. }
  599. void send_string_with_delay_P(const char *str, uint8_t interval) {
  600. while (1) {
  601. char ascii_code = pgm_read_byte(str);
  602. if (!ascii_code) break;
  603. if (ascii_code == 1) {
  604. // tap
  605. uint8_t keycode = pgm_read_byte(++str);
  606. register_code(keycode);
  607. unregister_code(keycode);
  608. } else if (ascii_code == 2) {
  609. // down
  610. uint8_t keycode = pgm_read_byte(++str);
  611. register_code(keycode);
  612. } else if (ascii_code == 3) {
  613. // up
  614. uint8_t keycode = pgm_read_byte(++str);
  615. unregister_code(keycode);
  616. } else {
  617. send_char(ascii_code);
  618. }
  619. ++str;
  620. // interval
  621. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  622. }
  623. }
  624. void send_char(char ascii_code) {
  625. uint8_t keycode;
  626. keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  627. if (pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code])) {
  628. register_code(KC_LSFT);
  629. register_code(keycode);
  630. unregister_code(keycode);
  631. unregister_code(KC_LSFT);
  632. } else {
  633. register_code(keycode);
  634. unregister_code(keycode);
  635. }
  636. }
  637. void set_single_persistent_default_layer(uint8_t default_layer) {
  638. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  639. PLAY_SONG(default_layer_songs[default_layer]);
  640. #endif
  641. eeconfig_update_default_layer(1U<<default_layer);
  642. default_layer_set(1U<<default_layer);
  643. }
  644. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  645. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  646. layer_on(layer3);
  647. } else {
  648. layer_off(layer3);
  649. }
  650. }
  651. void tap_random_base64(void) {
  652. #if defined(__AVR_ATmega32U4__)
  653. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  654. #else
  655. uint8_t key = rand() % 64;
  656. #endif
  657. switch (key) {
  658. case 0 ... 25:
  659. register_code(KC_LSFT);
  660. register_code(key + KC_A);
  661. unregister_code(key + KC_A);
  662. unregister_code(KC_LSFT);
  663. break;
  664. case 26 ... 51:
  665. register_code(key - 26 + KC_A);
  666. unregister_code(key - 26 + KC_A);
  667. break;
  668. case 52:
  669. register_code(KC_0);
  670. unregister_code(KC_0);
  671. break;
  672. case 53 ... 61:
  673. register_code(key - 53 + KC_1);
  674. unregister_code(key - 53 + KC_1);
  675. break;
  676. case 62:
  677. register_code(KC_LSFT);
  678. register_code(KC_EQL);
  679. unregister_code(KC_EQL);
  680. unregister_code(KC_LSFT);
  681. break;
  682. case 63:
  683. register_code(KC_SLSH);
  684. unregister_code(KC_SLSH);
  685. break;
  686. }
  687. }
  688. void matrix_init_quantum() {
  689. #ifdef BACKLIGHT_ENABLE
  690. backlight_init_ports();
  691. #endif
  692. #ifdef AUDIO_ENABLE
  693. audio_init();
  694. #endif
  695. #ifdef RGB_MATRIX_ENABLE
  696. rgb_matrix_init_drivers();
  697. #endif
  698. matrix_init_kb();
  699. }
  700. // uint16_t rgb_matrix_task_counter = 0;
  701. void matrix_scan_quantum() {
  702. #ifdef AUDIO_ENABLE
  703. matrix_scan_music();
  704. #endif
  705. #ifdef TAP_DANCE_ENABLE
  706. matrix_scan_tap_dance();
  707. #endif
  708. #ifdef COMBO_ENABLE
  709. matrix_scan_combo();
  710. #endif
  711. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  712. backlight_task();
  713. #endif
  714. #ifdef RGB_MATRIX_ENABLE
  715. // if (rgb_matrix_task_counter == 0)
  716. rgb_matrix_task();
  717. // rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % 5);
  718. #endif
  719. matrix_scan_kb();
  720. }
  721. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  722. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  723. #if BACKLIGHT_PIN == B7
  724. # define COM1x1 COM1C1
  725. # define OCR1x OCR1C
  726. #elif BACKLIGHT_PIN == B6
  727. # define COM1x1 COM1B1
  728. # define OCR1x OCR1B
  729. #elif BACKLIGHT_PIN == B5
  730. # define COM1x1 COM1A1
  731. # define OCR1x OCR1A
  732. #else
  733. # define NO_BACKLIGHT_CLOCK
  734. #endif
  735. #ifndef BACKLIGHT_ON_STATE
  736. #define BACKLIGHT_ON_STATE 0
  737. #endif
  738. __attribute__ ((weak))
  739. void backlight_init_ports(void)
  740. {
  741. // Setup backlight pin as output and output to on state.
  742. // DDRx |= n
  743. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  744. #if BACKLIGHT_ON_STATE == 0
  745. // PORTx &= ~n
  746. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  747. #else
  748. // PORTx |= n
  749. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  750. #endif
  751. #ifndef NO_BACKLIGHT_CLOCK
  752. // Use full 16-bit resolution.
  753. ICR1 = 0xFFFF;
  754. // I could write a wall of text here to explain... but TL;DW
  755. // Go read the ATmega32u4 datasheet.
  756. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  757. // Pin PB7 = OCR1C (Timer 1, Channel C)
  758. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  759. // (i.e. start high, go low when counter matches.)
  760. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  761. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  762. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  763. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  764. #endif
  765. backlight_init();
  766. #ifdef BACKLIGHT_BREATHING
  767. breathing_defaults();
  768. #endif
  769. }
  770. __attribute__ ((weak))
  771. void backlight_set(uint8_t level)
  772. {
  773. // Prevent backlight blink on lowest level
  774. // #if BACKLIGHT_ON_STATE == 0
  775. // // PORTx &= ~n
  776. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  777. // #else
  778. // // PORTx |= n
  779. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  780. // #endif
  781. if ( level == 0 ) {
  782. #ifndef NO_BACKLIGHT_CLOCK
  783. // Turn off PWM control on backlight pin, revert to output low.
  784. TCCR1A &= ~(_BV(COM1x1));
  785. OCR1x = 0x0;
  786. #else
  787. // #if BACKLIGHT_ON_STATE == 0
  788. // // PORTx |= n
  789. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  790. // #else
  791. // // PORTx &= ~n
  792. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  793. // #endif
  794. #endif
  795. }
  796. #ifndef NO_BACKLIGHT_CLOCK
  797. else if ( level == BACKLIGHT_LEVELS ) {
  798. // Turn on PWM control of backlight pin
  799. TCCR1A |= _BV(COM1x1);
  800. // Set the brightness
  801. OCR1x = 0xFFFF;
  802. }
  803. else {
  804. // Turn on PWM control of backlight pin
  805. TCCR1A |= _BV(COM1x1);
  806. // Set the brightness
  807. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  808. }
  809. #endif
  810. #if defined(BACKLIGHT_BREATHING)
  811. breathing_intensity_default();
  812. #endif
  813. }
  814. uint8_t backlight_tick = 0;
  815. __attribute__ ((weak))
  816. void backlight_task(void) {
  817. #ifdef NO_BACKLIGHT_CLOCK
  818. if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  819. #if BACKLIGHT_ON_STATE == 0
  820. // PORTx &= ~n
  821. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  822. #else
  823. // PORTx |= n
  824. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  825. #endif
  826. } else {
  827. #if BACKLIGHT_ON_STATE == 0
  828. // PORTx |= n
  829. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  830. #else
  831. // PORTx &= ~n
  832. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  833. #endif
  834. }
  835. backlight_tick = (backlight_tick + 1) % 16;
  836. #endif
  837. }
  838. #ifdef BACKLIGHT_BREATHING
  839. #ifdef NO_BACKLIGHT_CLOCK
  840. void breathing_enable(void) {}
  841. void breathing_pulse(void) {}
  842. void breathing_disable(void) {}
  843. void breathing_self_disable(void) {}
  844. void breathing_toggle(void) {}
  845. bool is_breathing(void) { return false; }
  846. void breathing_intensity_default(void) {}
  847. void breathing_intensity_set(uint8_t value) {}
  848. void breathing_speed_default(void) {}
  849. void breathing_speed_set(uint8_t value) {}
  850. void breathing_speed_inc(uint8_t value) {}
  851. void breathing_speed_dec(uint8_t value) {}
  852. void breathing_defaults(void) {}
  853. #else
  854. #define BREATHING_NO_HALT 0
  855. #define BREATHING_HALT_OFF 1
  856. #define BREATHING_HALT_ON 2
  857. static uint8_t breath_intensity;
  858. static uint8_t breath_speed;
  859. static uint16_t breathing_index;
  860. static uint8_t breathing_halt;
  861. void breathing_enable(void)
  862. {
  863. if (get_backlight_level() == 0)
  864. {
  865. breathing_index = 0;
  866. }
  867. else
  868. {
  869. // Set breathing_index to be at the midpoint (brightest point)
  870. breathing_index = 0x20 << breath_speed;
  871. }
  872. breathing_halt = BREATHING_NO_HALT;
  873. // Enable breathing interrupt
  874. TIMSK1 |= _BV(OCIE1A);
  875. }
  876. void breathing_pulse(void)
  877. {
  878. if (get_backlight_level() == 0)
  879. {
  880. breathing_index = 0;
  881. }
  882. else
  883. {
  884. // Set breathing_index to be at the midpoint + 1 (brightest point)
  885. breathing_index = 0x21 << breath_speed;
  886. }
  887. breathing_halt = BREATHING_HALT_ON;
  888. // Enable breathing interrupt
  889. TIMSK1 |= _BV(OCIE1A);
  890. }
  891. void breathing_disable(void)
  892. {
  893. // Disable breathing interrupt
  894. TIMSK1 &= ~_BV(OCIE1A);
  895. backlight_set(get_backlight_level());
  896. }
  897. void breathing_self_disable(void)
  898. {
  899. if (get_backlight_level() == 0)
  900. {
  901. breathing_halt = BREATHING_HALT_OFF;
  902. }
  903. else
  904. {
  905. breathing_halt = BREATHING_HALT_ON;
  906. }
  907. //backlight_set(get_backlight_level());
  908. }
  909. void breathing_toggle(void)
  910. {
  911. if (!is_breathing())
  912. {
  913. if (get_backlight_level() == 0)
  914. {
  915. breathing_index = 0;
  916. }
  917. else
  918. {
  919. // Set breathing_index to be at the midpoint + 1 (brightest point)
  920. breathing_index = 0x21 << breath_speed;
  921. }
  922. breathing_halt = BREATHING_NO_HALT;
  923. }
  924. // Toggle breathing interrupt
  925. TIMSK1 ^= _BV(OCIE1A);
  926. // Restore backlight level
  927. if (!is_breathing())
  928. {
  929. backlight_set(get_backlight_level());
  930. }
  931. }
  932. bool is_breathing(void)
  933. {
  934. return (TIMSK1 && _BV(OCIE1A));
  935. }
  936. void breathing_intensity_default(void)
  937. {
  938. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  939. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  940. }
  941. void breathing_intensity_set(uint8_t value)
  942. {
  943. breath_intensity = value;
  944. }
  945. void breathing_speed_default(void)
  946. {
  947. breath_speed = 4;
  948. }
  949. void breathing_speed_set(uint8_t value)
  950. {
  951. bool is_breathing_now = is_breathing();
  952. uint8_t old_breath_speed = breath_speed;
  953. if (is_breathing_now)
  954. {
  955. // Disable breathing interrupt
  956. TIMSK1 &= ~_BV(OCIE1A);
  957. }
  958. breath_speed = value;
  959. if (is_breathing_now)
  960. {
  961. // Adjust index to account for new speed
  962. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  963. // Enable breathing interrupt
  964. TIMSK1 |= _BV(OCIE1A);
  965. }
  966. }
  967. void breathing_speed_inc(uint8_t value)
  968. {
  969. if ((uint16_t)(breath_speed - value) > 10 )
  970. {
  971. breathing_speed_set(0);
  972. }
  973. else
  974. {
  975. breathing_speed_set(breath_speed - value);
  976. }
  977. }
  978. void breathing_speed_dec(uint8_t value)
  979. {
  980. if ((uint16_t)(breath_speed + value) > 10 )
  981. {
  982. breathing_speed_set(10);
  983. }
  984. else
  985. {
  986. breathing_speed_set(breath_speed + value);
  987. }
  988. }
  989. void breathing_defaults(void)
  990. {
  991. breathing_intensity_default();
  992. breathing_speed_default();
  993. breathing_halt = BREATHING_NO_HALT;
  994. }
  995. /* Breathing Sleep LED brighness(PWM On period) table
  996. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  997. *
  998. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  999. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  1000. */
  1001. static const uint8_t breathing_table[64] PROGMEM = {
  1002. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  1003. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  1004. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  1005. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1006. };
  1007. ISR(TIMER1_COMPA_vect)
  1008. {
  1009. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  1010. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  1011. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  1012. {
  1013. // Disable breathing interrupt
  1014. TIMSK1 &= ~_BV(OCIE1A);
  1015. }
  1016. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  1017. }
  1018. #endif // no clock
  1019. #endif // breathing
  1020. #else // backlight
  1021. __attribute__ ((weak))
  1022. void backlight_init_ports(void)
  1023. {
  1024. }
  1025. __attribute__ ((weak))
  1026. void backlight_set(uint8_t level)
  1027. {
  1028. }
  1029. #endif // backlight
  1030. // Functions for spitting out values
  1031. //
  1032. void send_dword(uint32_t number) { // this might not actually work
  1033. uint16_t word = (number >> 16);
  1034. send_word(word);
  1035. send_word(number & 0xFFFFUL);
  1036. }
  1037. void send_word(uint16_t number) {
  1038. uint8_t byte = number >> 8;
  1039. send_byte(byte);
  1040. send_byte(number & 0xFF);
  1041. }
  1042. void send_byte(uint8_t number) {
  1043. uint8_t nibble = number >> 4;
  1044. send_nibble(nibble);
  1045. send_nibble(number & 0xF);
  1046. }
  1047. void send_nibble(uint8_t number) {
  1048. switch (number) {
  1049. case 0:
  1050. register_code(KC_0);
  1051. unregister_code(KC_0);
  1052. break;
  1053. case 1 ... 9:
  1054. register_code(KC_1 + (number - 1));
  1055. unregister_code(KC_1 + (number - 1));
  1056. break;
  1057. case 0xA ... 0xF:
  1058. register_code(KC_A + (number - 0xA));
  1059. unregister_code(KC_A + (number - 0xA));
  1060. break;
  1061. }
  1062. }
  1063. __attribute__((weak))
  1064. uint16_t hex_to_keycode(uint8_t hex)
  1065. {
  1066. hex = hex & 0xF;
  1067. if (hex == 0x0) {
  1068. return KC_0;
  1069. } else if (hex < 0xA) {
  1070. return KC_1 + (hex - 0x1);
  1071. } else {
  1072. return KC_A + (hex - 0xA);
  1073. }
  1074. }
  1075. void api_send_unicode(uint32_t unicode) {
  1076. #ifdef API_ENABLE
  1077. uint8_t chunk[4];
  1078. dword_to_bytes(unicode, chunk);
  1079. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1080. #endif
  1081. }
  1082. __attribute__ ((weak))
  1083. void led_set_user(uint8_t usb_led) {
  1084. }
  1085. __attribute__ ((weak))
  1086. void led_set_kb(uint8_t usb_led) {
  1087. led_set_user(usb_led);
  1088. }
  1089. __attribute__ ((weak))
  1090. void led_init_ports(void)
  1091. {
  1092. }
  1093. __attribute__ ((weak))
  1094. void led_set(uint8_t usb_led)
  1095. {
  1096. // Example LED Code
  1097. //
  1098. // // Using PE6 Caps Lock LED
  1099. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  1100. // {
  1101. // // Output high.
  1102. // DDRE |= (1<<6);
  1103. // PORTE |= (1<<6);
  1104. // }
  1105. // else
  1106. // {
  1107. // // Output low.
  1108. // DDRE &= ~(1<<6);
  1109. // PORTE &= ~(1<<6);
  1110. // }
  1111. led_set_kb(usb_led);
  1112. }
  1113. //------------------------------------------------------------------------------
  1114. // Override these functions in your keymap file to play different tunes on
  1115. // different events such as startup and bootloader jump
  1116. __attribute__ ((weak))
  1117. void startup_user() {}
  1118. __attribute__ ((weak))
  1119. void shutdown_user() {}
  1120. //------------------------------------------------------------------------------