quantum.c 25 KB

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