quantum.c 31 KB

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