quantum.c 26 KB

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