quantum.c 25 KB

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