quantum.c 29 KB

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