quantum.c 27 KB

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