quantum.c 27 KB

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