quantum.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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[2] = {0, 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[0] = 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[0]) < 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[1] = 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[1]) < 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. case GRAVE_ESC: {
  410. void (*method)(uint8_t) = (record->event.pressed) ? &add_key : &del_key;
  411. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  412. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  413. method(shifted ? KC_GRAVE : KC_ESCAPE);
  414. send_keyboard_report();
  415. }
  416. default: {
  417. shift_interrupted[0] = true;
  418. shift_interrupted[1] = true;
  419. break;
  420. }
  421. }
  422. return process_action_kb(record);
  423. }
  424. __attribute__ ((weak))
  425. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  426. 0, 0, 0, 0, 0, 0, 0, 0,
  427. 0, 0, 0, 0, 0, 0, 0, 0,
  428. 0, 0, 0, 0, 0, 0, 0, 0,
  429. 0, 0, 0, 0, 0, 0, 0, 0,
  430. 0, 1, 1, 1, 1, 1, 1, 0,
  431. 1, 1, 1, 1, 0, 0, 0, 0,
  432. 0, 0, 0, 0, 0, 0, 0, 0,
  433. 0, 0, 1, 0, 1, 0, 1, 1,
  434. 1, 1, 1, 1, 1, 1, 1, 1,
  435. 1, 1, 1, 1, 1, 1, 1, 1,
  436. 1, 1, 1, 1, 1, 1, 1, 1,
  437. 1, 1, 1, 0, 0, 0, 1, 1,
  438. 0, 0, 0, 0, 0, 0, 0, 0,
  439. 0, 0, 0, 0, 0, 0, 0, 0,
  440. 0, 0, 0, 0, 0, 0, 0, 0,
  441. 0, 0, 0, 1, 1, 1, 1, 0
  442. };
  443. __attribute__ ((weak))
  444. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  445. 0, 0, 0, 0, 0, 0, 0, 0,
  446. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  447. 0, 0, 0, 0, 0, 0, 0, 0,
  448. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  449. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  450. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  451. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  452. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  453. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  454. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  455. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  456. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  457. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  458. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  459. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  460. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  461. };
  462. void send_string(const char *str) {
  463. send_string_with_delay(str, 0);
  464. }
  465. void send_string_with_delay(const char *str, uint8_t interval) {
  466. while (1) {
  467. uint8_t keycode;
  468. uint8_t ascii_code = pgm_read_byte(str);
  469. if (!ascii_code) break;
  470. keycode = pgm_read_byte(&ascii_to_keycode_lut[ascii_code]);
  471. if (pgm_read_byte(&ascii_to_shift_lut[ascii_code])) {
  472. register_code(KC_LSFT);
  473. register_code(keycode);
  474. unregister_code(keycode);
  475. unregister_code(KC_LSFT);
  476. }
  477. else {
  478. register_code(keycode);
  479. unregister_code(keycode);
  480. }
  481. ++str;
  482. // interval
  483. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  484. }
  485. }
  486. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  487. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  488. layer_on(layer3);
  489. } else {
  490. layer_off(layer3);
  491. }
  492. }
  493. void tap_random_base64(void) {
  494. #if defined(__AVR_ATmega32U4__)
  495. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  496. #else
  497. uint8_t key = rand() % 64;
  498. #endif
  499. switch (key) {
  500. case 0 ... 25:
  501. register_code(KC_LSFT);
  502. register_code(key + KC_A);
  503. unregister_code(key + KC_A);
  504. unregister_code(KC_LSFT);
  505. break;
  506. case 26 ... 51:
  507. register_code(key - 26 + KC_A);
  508. unregister_code(key - 26 + KC_A);
  509. break;
  510. case 52:
  511. register_code(KC_0);
  512. unregister_code(KC_0);
  513. break;
  514. case 53 ... 61:
  515. register_code(key - 53 + KC_1);
  516. unregister_code(key - 53 + KC_1);
  517. break;
  518. case 62:
  519. register_code(KC_LSFT);
  520. register_code(KC_EQL);
  521. unregister_code(KC_EQL);
  522. unregister_code(KC_LSFT);
  523. break;
  524. case 63:
  525. register_code(KC_SLSH);
  526. unregister_code(KC_SLSH);
  527. break;
  528. }
  529. }
  530. void matrix_init_quantum() {
  531. #ifdef BACKLIGHT_ENABLE
  532. backlight_init_ports();
  533. #endif
  534. matrix_init_kb();
  535. }
  536. void matrix_scan_quantum() {
  537. #ifdef AUDIO_ENABLE
  538. matrix_scan_music();
  539. #endif
  540. #ifdef TAP_DANCE_ENABLE
  541. matrix_scan_tap_dance();
  542. #endif
  543. #ifdef COMBO_ENABLE
  544. matrix_scan_combo();
  545. #endif
  546. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  547. backlight_task();
  548. #endif
  549. matrix_scan_kb();
  550. }
  551. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  552. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  553. #if BACKLIGHT_PIN == B7
  554. # define COM1x1 COM1C1
  555. # define OCR1x OCR1C
  556. #elif BACKLIGHT_PIN == B6
  557. # define COM1x1 COM1B1
  558. # define OCR1x OCR1B
  559. #elif BACKLIGHT_PIN == B5
  560. # define COM1x1 COM1A1
  561. # define OCR1x OCR1A
  562. #else
  563. # define NO_BACKLIGHT_CLOCK
  564. #endif
  565. #ifndef BACKLIGHT_ON_STATE
  566. #define BACKLIGHT_ON_STATE 0
  567. #endif
  568. __attribute__ ((weak))
  569. void backlight_init_ports(void)
  570. {
  571. // Setup backlight pin as output and output to on state.
  572. // DDRx |= n
  573. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  574. #if BACKLIGHT_ON_STATE == 0
  575. // PORTx &= ~n
  576. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  577. #else
  578. // PORTx |= n
  579. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  580. #endif
  581. #ifndef NO_BACKLIGHT_CLOCK
  582. // Use full 16-bit resolution.
  583. ICR1 = 0xFFFF;
  584. // I could write a wall of text here to explain... but TL;DW
  585. // Go read the ATmega32u4 datasheet.
  586. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  587. // Pin PB7 = OCR1C (Timer 1, Channel C)
  588. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  589. // (i.e. start high, go low when counter matches.)
  590. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  591. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  592. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  593. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  594. #endif
  595. backlight_init();
  596. #ifdef BACKLIGHT_BREATHING
  597. breathing_defaults();
  598. #endif
  599. }
  600. __attribute__ ((weak))
  601. void backlight_set(uint8_t level)
  602. {
  603. // Prevent backlight blink on lowest level
  604. // #if BACKLIGHT_ON_STATE == 0
  605. // // PORTx &= ~n
  606. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  607. // #else
  608. // // PORTx |= n
  609. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  610. // #endif
  611. if ( level == 0 ) {
  612. #ifndef NO_BACKLIGHT_CLOCK
  613. // Turn off PWM control on backlight pin, revert to output low.
  614. TCCR1A &= ~(_BV(COM1x1));
  615. OCR1x = 0x0;
  616. #else
  617. // #if BACKLIGHT_ON_STATE == 0
  618. // // PORTx |= n
  619. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  620. // #else
  621. // // PORTx &= ~n
  622. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  623. // #endif
  624. #endif
  625. }
  626. #ifndef NO_BACKLIGHT_CLOCK
  627. else if ( level == BACKLIGHT_LEVELS ) {
  628. // Turn on PWM control of backlight pin
  629. TCCR1A |= _BV(COM1x1);
  630. // Set the brightness
  631. OCR1x = 0xFFFF;
  632. }
  633. else {
  634. // Turn on PWM control of backlight pin
  635. TCCR1A |= _BV(COM1x1);
  636. // Set the brightness
  637. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  638. }
  639. #endif
  640. #ifdef BACKLIGHT_BREATHING
  641. breathing_intensity_default();
  642. #endif
  643. }
  644. uint8_t backlight_tick = 0;
  645. void backlight_task(void) {
  646. #ifdef NO_BACKLIGHT_CLOCK
  647. if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  648. #if BACKLIGHT_ON_STATE == 0
  649. // PORTx &= ~n
  650. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  651. #else
  652. // PORTx |= n
  653. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  654. #endif
  655. } else {
  656. #if BACKLIGHT_ON_STATE == 0
  657. // PORTx |= n
  658. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  659. #else
  660. // PORTx &= ~n
  661. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  662. #endif
  663. }
  664. backlight_tick = (backlight_tick + 1) % 16;
  665. #endif
  666. }
  667. #ifdef BACKLIGHT_BREATHING
  668. #define BREATHING_NO_HALT 0
  669. #define BREATHING_HALT_OFF 1
  670. #define BREATHING_HALT_ON 2
  671. static uint8_t breath_intensity;
  672. static uint8_t breath_speed;
  673. static uint16_t breathing_index;
  674. static uint8_t breathing_halt;
  675. void breathing_enable(void)
  676. {
  677. if (get_backlight_level() == 0)
  678. {
  679. breathing_index = 0;
  680. }
  681. else
  682. {
  683. // Set breathing_index to be at the midpoint (brightest point)
  684. breathing_index = 0x20 << breath_speed;
  685. }
  686. breathing_halt = BREATHING_NO_HALT;
  687. // Enable breathing interrupt
  688. TIMSK1 |= _BV(OCIE1A);
  689. }
  690. void breathing_pulse(void)
  691. {
  692. if (get_backlight_level() == 0)
  693. {
  694. breathing_index = 0;
  695. }
  696. else
  697. {
  698. // Set breathing_index to be at the midpoint + 1 (brightest point)
  699. breathing_index = 0x21 << breath_speed;
  700. }
  701. breathing_halt = BREATHING_HALT_ON;
  702. // Enable breathing interrupt
  703. TIMSK1 |= _BV(OCIE1A);
  704. }
  705. void breathing_disable(void)
  706. {
  707. // Disable breathing interrupt
  708. TIMSK1 &= ~_BV(OCIE1A);
  709. backlight_set(get_backlight_level());
  710. }
  711. void breathing_self_disable(void)
  712. {
  713. if (get_backlight_level() == 0)
  714. {
  715. breathing_halt = BREATHING_HALT_OFF;
  716. }
  717. else
  718. {
  719. breathing_halt = BREATHING_HALT_ON;
  720. }
  721. //backlight_set(get_backlight_level());
  722. }
  723. void breathing_toggle(void)
  724. {
  725. if (!is_breathing())
  726. {
  727. if (get_backlight_level() == 0)
  728. {
  729. breathing_index = 0;
  730. }
  731. else
  732. {
  733. // Set breathing_index to be at the midpoint + 1 (brightest point)
  734. breathing_index = 0x21 << breath_speed;
  735. }
  736. breathing_halt = BREATHING_NO_HALT;
  737. }
  738. // Toggle breathing interrupt
  739. TIMSK1 ^= _BV(OCIE1A);
  740. // Restore backlight level
  741. if (!is_breathing())
  742. {
  743. backlight_set(get_backlight_level());
  744. }
  745. }
  746. bool is_breathing(void)
  747. {
  748. return (TIMSK1 && _BV(OCIE1A));
  749. }
  750. void breathing_intensity_default(void)
  751. {
  752. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  753. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  754. }
  755. void breathing_intensity_set(uint8_t value)
  756. {
  757. breath_intensity = value;
  758. }
  759. void breathing_speed_default(void)
  760. {
  761. breath_speed = 4;
  762. }
  763. void breathing_speed_set(uint8_t value)
  764. {
  765. bool is_breathing_now = is_breathing();
  766. uint8_t old_breath_speed = breath_speed;
  767. if (is_breathing_now)
  768. {
  769. // Disable breathing interrupt
  770. TIMSK1 &= ~_BV(OCIE1A);
  771. }
  772. breath_speed = value;
  773. if (is_breathing_now)
  774. {
  775. // Adjust index to account for new speed
  776. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  777. // Enable breathing interrupt
  778. TIMSK1 |= _BV(OCIE1A);
  779. }
  780. }
  781. void breathing_speed_inc(uint8_t value)
  782. {
  783. if ((uint16_t)(breath_speed - value) > 10 )
  784. {
  785. breathing_speed_set(0);
  786. }
  787. else
  788. {
  789. breathing_speed_set(breath_speed - value);
  790. }
  791. }
  792. void breathing_speed_dec(uint8_t value)
  793. {
  794. if ((uint16_t)(breath_speed + value) > 10 )
  795. {
  796. breathing_speed_set(10);
  797. }
  798. else
  799. {
  800. breathing_speed_set(breath_speed + value);
  801. }
  802. }
  803. void breathing_defaults(void)
  804. {
  805. breathing_intensity_default();
  806. breathing_speed_default();
  807. breathing_halt = BREATHING_NO_HALT;
  808. }
  809. /* Breathing Sleep LED brighness(PWM On period) table
  810. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  811. *
  812. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  813. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  814. */
  815. static const uint8_t breathing_table[64] PROGMEM = {
  816. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  817. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  818. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  819. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  820. };
  821. ISR(TIMER1_COMPA_vect)
  822. {
  823. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  824. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  825. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  826. {
  827. // Disable breathing interrupt
  828. TIMSK1 &= ~_BV(OCIE1A);
  829. }
  830. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  831. }
  832. #endif // breathing
  833. #else // backlight
  834. __attribute__ ((weak))
  835. void backlight_init_ports(void)
  836. {
  837. }
  838. __attribute__ ((weak))
  839. void backlight_set(uint8_t level)
  840. {
  841. }
  842. #endif // backlight
  843. // Functions for spitting out values
  844. //
  845. void send_dword(uint32_t number) { // this might not actually work
  846. uint16_t word = (number >> 16);
  847. send_word(word);
  848. send_word(number & 0xFFFFUL);
  849. }
  850. void send_word(uint16_t number) {
  851. uint8_t byte = number >> 8;
  852. send_byte(byte);
  853. send_byte(number & 0xFF);
  854. }
  855. void send_byte(uint8_t number) {
  856. uint8_t nibble = number >> 4;
  857. send_nibble(nibble);
  858. send_nibble(number & 0xF);
  859. }
  860. void send_nibble(uint8_t number) {
  861. switch (number) {
  862. case 0:
  863. register_code(KC_0);
  864. unregister_code(KC_0);
  865. break;
  866. case 1 ... 9:
  867. register_code(KC_1 + (number - 1));
  868. unregister_code(KC_1 + (number - 1));
  869. break;
  870. case 0xA ... 0xF:
  871. register_code(KC_A + (number - 0xA));
  872. unregister_code(KC_A + (number - 0xA));
  873. break;
  874. }
  875. }
  876. __attribute__((weak))
  877. uint16_t hex_to_keycode(uint8_t hex)
  878. {
  879. if (hex == 0x0) {
  880. return KC_0;
  881. } else if (hex < 0xA) {
  882. return KC_1 + (hex - 0x1);
  883. } else {
  884. return KC_A + (hex - 0xA);
  885. }
  886. }
  887. void api_send_unicode(uint32_t unicode) {
  888. #ifdef API_ENABLE
  889. uint8_t chunk[4];
  890. dword_to_bytes(unicode, chunk);
  891. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  892. #endif
  893. }
  894. __attribute__ ((weak))
  895. void led_set_user(uint8_t usb_led) {
  896. }
  897. __attribute__ ((weak))
  898. void led_set_kb(uint8_t usb_led) {
  899. led_set_user(usb_led);
  900. }
  901. __attribute__ ((weak))
  902. void led_init_ports(void)
  903. {
  904. }
  905. __attribute__ ((weak))
  906. void led_set(uint8_t usb_led)
  907. {
  908. // Example LED Code
  909. //
  910. // // Using PE6 Caps Lock LED
  911. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  912. // {
  913. // // Output high.
  914. // DDRE |= (1<<6);
  915. // PORTE |= (1<<6);
  916. // }
  917. // else
  918. // {
  919. // // Output low.
  920. // DDRE &= ~(1<<6);
  921. // PORTE &= ~(1<<6);
  922. // }
  923. led_set_kb(usb_led);
  924. }
  925. //------------------------------------------------------------------------------
  926. // Override these functions in your keymap file to play different tunes on
  927. // different events such as startup and bootloader jump
  928. __attribute__ ((weak))
  929. void startup_user() {}
  930. __attribute__ ((weak))
  931. void shutdown_user() {}
  932. //------------------------------------------------------------------------------