quantum.c 27 KB

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