quantum.c 25 KB

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