quantum.c 26 KB

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