quantum.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. #ifndef BREATHING_PERIOD
  24. #define BREATHING_PERIOD 6
  25. #endif
  26. #include "backlight.h"
  27. extern backlight_config_t backlight_config;
  28. #ifdef FAUXCLICKY_ENABLE
  29. #include "fauxclicky.h"
  30. #endif
  31. #ifdef API_ENABLE
  32. #include "api.h"
  33. #endif
  34. #ifdef MIDI_ENABLE
  35. #include "process_midi.h"
  36. #endif
  37. #ifdef AUDIO_ENABLE
  38. #ifndef GOODBYE_SONG
  39. #define GOODBYE_SONG SONG(GOODBYE_SOUND)
  40. #endif
  41. #ifndef AG_NORM_SONG
  42. #define AG_NORM_SONG SONG(AG_NORM_SOUND)
  43. #endif
  44. #ifndef AG_SWAP_SONG
  45. #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
  46. #endif
  47. float goodbye_song[][2] = GOODBYE_SONG;
  48. float ag_norm_song[][2] = AG_NORM_SONG;
  49. float ag_swap_song[][2] = AG_SWAP_SONG;
  50. #ifdef DEFAULT_LAYER_SONGS
  51. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  52. #endif
  53. #endif
  54. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  55. switch (code) {
  56. case QK_MODS ... QK_MODS_MAX:
  57. break;
  58. default:
  59. return;
  60. }
  61. if (code & QK_LCTL)
  62. f(KC_LCTL);
  63. if (code & QK_LSFT)
  64. f(KC_LSFT);
  65. if (code & QK_LALT)
  66. f(KC_LALT);
  67. if (code & QK_LGUI)
  68. f(KC_LGUI);
  69. if (code < QK_RMODS_MIN) return;
  70. if (code & QK_RCTL)
  71. f(KC_RCTL);
  72. if (code & QK_RSFT)
  73. f(KC_RSFT);
  74. if (code & QK_RALT)
  75. f(KC_RALT);
  76. if (code & QK_RGUI)
  77. f(KC_RGUI);
  78. }
  79. static inline void qk_register_weak_mods(uint8_t kc) {
  80. add_weak_mods(MOD_BIT(kc));
  81. send_keyboard_report();
  82. }
  83. static inline void qk_unregister_weak_mods(uint8_t kc) {
  84. del_weak_mods(MOD_BIT(kc));
  85. send_keyboard_report();
  86. }
  87. static inline void qk_register_mods(uint8_t kc) {
  88. add_weak_mods(MOD_BIT(kc));
  89. send_keyboard_report();
  90. }
  91. static inline void qk_unregister_mods(uint8_t kc) {
  92. del_weak_mods(MOD_BIT(kc));
  93. send_keyboard_report();
  94. }
  95. void register_code16 (uint16_t code) {
  96. if (IS_MOD(code) || code == KC_NO) {
  97. do_code16 (code, qk_register_mods);
  98. } else {
  99. do_code16 (code, qk_register_weak_mods);
  100. }
  101. register_code (code);
  102. }
  103. void unregister_code16 (uint16_t code) {
  104. unregister_code (code);
  105. if (IS_MOD(code) || code == KC_NO) {
  106. do_code16 (code, qk_unregister_mods);
  107. } else {
  108. do_code16 (code, qk_unregister_weak_mods);
  109. }
  110. }
  111. __attribute__ ((weak))
  112. bool process_action_kb(keyrecord_t *record) {
  113. return true;
  114. }
  115. __attribute__ ((weak))
  116. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  117. return process_record_user(keycode, record);
  118. }
  119. __attribute__ ((weak))
  120. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  121. return true;
  122. }
  123. void reset_keyboard(void) {
  124. clear_keyboard();
  125. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  126. process_midi_all_notes_off();
  127. #endif
  128. #ifdef AUDIO_ENABLE
  129. #ifndef NO_MUSIC_MODE
  130. music_all_notes_off();
  131. #endif
  132. uint16_t timer_start = timer_read();
  133. PLAY_SONG(goodbye_song);
  134. shutdown_user();
  135. while(timer_elapsed(timer_start) < 250)
  136. wait_ms(1);
  137. stop_all_notes();
  138. #else
  139. shutdown_user();
  140. wait_ms(250);
  141. #endif
  142. // this is also done later in bootloader.c - not sure if it's neccesary here
  143. #ifdef BOOTLOADER_CATERINA
  144. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  145. #endif
  146. bootloader_jump();
  147. }
  148. // Shift / paren setup
  149. #ifndef LSPO_KEY
  150. #define LSPO_KEY KC_9
  151. #endif
  152. #ifndef RSPC_KEY
  153. #define RSPC_KEY KC_0
  154. #endif
  155. // Shift / Enter setup
  156. #ifndef SFTENT_KEY
  157. #define SFTENT_KEY KC_ENT
  158. #endif
  159. static bool shift_interrupted[2] = {0, 0};
  160. static uint16_t scs_timer[2] = {0, 0};
  161. /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
  162. * Used to ensure that the correct keycode is released if the key is released.
  163. */
  164. static bool grave_esc_was_shifted = false;
  165. bool process_record_quantum(keyrecord_t *record) {
  166. /* This gets the keycode from the key pressed */
  167. keypos_t key = record->event.key;
  168. uint16_t keycode;
  169. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  170. /* TODO: Use store_or_get_action() or a similar function. */
  171. if (!disable_action_cache) {
  172. uint8_t layer;
  173. if (record->event.pressed) {
  174. layer = layer_switch_get_layer(key);
  175. update_source_layers_cache(key, layer);
  176. } else {
  177. layer = read_source_layers_cache(key);
  178. }
  179. keycode = keymap_key_to_keycode(layer, key);
  180. } else
  181. #endif
  182. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  183. // This is how you use actions here
  184. // if (keycode == KC_LEAD) {
  185. // action_t action;
  186. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  187. // process_action(record, action);
  188. // return false;
  189. // }
  190. #ifdef TAP_DANCE_ENABLE
  191. preprocess_tap_dance(keycode, record);
  192. #endif
  193. if (!(
  194. #if defined(KEY_LOCK_ENABLE)
  195. // Must run first to be able to mask key_up events.
  196. process_key_lock(&keycode, record) &&
  197. #endif
  198. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  199. process_clicky(keycode, record) &&
  200. #endif //AUDIO_CLICKY
  201. process_record_kb(keycode, record) &&
  202. #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYPRESSES)
  203. process_rgb_matrix(keycode, record) &&
  204. #endif
  205. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  206. process_midi(keycode, record) &&
  207. #endif
  208. #ifdef AUDIO_ENABLE
  209. process_audio(keycode, record) &&
  210. #endif
  211. #ifdef STENO_ENABLE
  212. process_steno(keycode, record) &&
  213. #endif
  214. #if ( defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  215. process_music(keycode, record) &&
  216. #endif
  217. #ifdef TAP_DANCE_ENABLE
  218. process_tap_dance(keycode, record) &&
  219. #endif
  220. #ifdef LEADER_ENABLE
  221. process_leader(keycode, record) &&
  222. #endif
  223. #ifdef COMBO_ENABLE
  224. process_combo(keycode, record) &&
  225. #endif
  226. #ifdef UNICODE_ENABLE
  227. process_unicode(keycode, record) &&
  228. #endif
  229. #ifdef UCIS_ENABLE
  230. process_ucis(keycode, record) &&
  231. #endif
  232. #ifdef PRINTING_ENABLE
  233. process_printer(keycode, record) &&
  234. #endif
  235. #ifdef AUTO_SHIFT_ENABLE
  236. process_auto_shift(keycode, record) &&
  237. #endif
  238. #ifdef UNICODEMAP_ENABLE
  239. process_unicode_map(keycode, record) &&
  240. #endif
  241. #ifdef TERMINAL_ENABLE
  242. process_terminal(keycode, record) &&
  243. #endif
  244. true)) {
  245. return false;
  246. }
  247. // Shift / paren setup
  248. switch(keycode) {
  249. case RESET:
  250. if (record->event.pressed) {
  251. reset_keyboard();
  252. }
  253. return false;
  254. case DEBUG:
  255. if (record->event.pressed) {
  256. debug_enable = true;
  257. print("DEBUG: enabled.\n");
  258. }
  259. return false;
  260. #ifdef FAUXCLICKY_ENABLE
  261. case FC_TOG:
  262. if (record->event.pressed) {
  263. FAUXCLICKY_TOGGLE;
  264. }
  265. return false;
  266. case FC_ON:
  267. if (record->event.pressed) {
  268. FAUXCLICKY_ON;
  269. }
  270. return false;
  271. case FC_OFF:
  272. if (record->event.pressed) {
  273. FAUXCLICKY_OFF;
  274. }
  275. return false;
  276. #endif
  277. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  278. case RGB_TOG:
  279. // Split keyboards need to trigger on key-up for edge-case issue
  280. #ifndef SPLIT_KEYBOARD
  281. if (record->event.pressed) {
  282. #else
  283. if (!record->event.pressed) {
  284. #endif
  285. rgblight_toggle();
  286. #ifdef SPLIT_KEYBOARD
  287. RGB_DIRTY = true;
  288. #endif
  289. }
  290. return false;
  291. case RGB_MODE_FORWARD:
  292. if (record->event.pressed) {
  293. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  294. if(shifted) {
  295. rgblight_step_reverse();
  296. }
  297. else {
  298. rgblight_step();
  299. }
  300. #ifdef SPLIT_KEYBOARD
  301. RGB_DIRTY = true;
  302. #endif
  303. }
  304. return false;
  305. case RGB_MODE_REVERSE:
  306. if (record->event.pressed) {
  307. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  308. if(shifted) {
  309. rgblight_step();
  310. }
  311. else {
  312. rgblight_step_reverse();
  313. }
  314. #ifdef SPLIT_KEYBOARD
  315. RGB_DIRTY = true;
  316. #endif
  317. }
  318. return false;
  319. case RGB_HUI:
  320. // Split keyboards need to trigger on key-up for edge-case issue
  321. #ifndef SPLIT_KEYBOARD
  322. if (record->event.pressed) {
  323. #else
  324. if (!record->event.pressed) {
  325. #endif
  326. rgblight_increase_hue();
  327. #ifdef SPLIT_KEYBOARD
  328. RGB_DIRTY = true;
  329. #endif
  330. }
  331. return false;
  332. case RGB_HUD:
  333. // Split keyboards need to trigger on key-up for edge-case issue
  334. #ifndef SPLIT_KEYBOARD
  335. if (record->event.pressed) {
  336. #else
  337. if (!record->event.pressed) {
  338. #endif
  339. rgblight_decrease_hue();
  340. #ifdef SPLIT_KEYBOARD
  341. RGB_DIRTY = true;
  342. #endif
  343. }
  344. return false;
  345. case RGB_SAI:
  346. // Split keyboards need to trigger on key-up for edge-case issue
  347. #ifndef SPLIT_KEYBOARD
  348. if (record->event.pressed) {
  349. #else
  350. if (!record->event.pressed) {
  351. #endif
  352. rgblight_increase_sat();
  353. #ifdef SPLIT_KEYBOARD
  354. RGB_DIRTY = true;
  355. #endif
  356. }
  357. return false;
  358. case RGB_SAD:
  359. // Split keyboards need to trigger on key-up for edge-case issue
  360. #ifndef SPLIT_KEYBOARD
  361. if (record->event.pressed) {
  362. #else
  363. if (!record->event.pressed) {
  364. #endif
  365. rgblight_decrease_sat();
  366. #ifdef SPLIT_KEYBOARD
  367. RGB_DIRTY = true;
  368. #endif
  369. }
  370. return false;
  371. case RGB_VAI:
  372. // Split keyboards need to trigger on key-up for edge-case issue
  373. #ifndef SPLIT_KEYBOARD
  374. if (record->event.pressed) {
  375. #else
  376. if (!record->event.pressed) {
  377. #endif
  378. rgblight_increase_val();
  379. #ifdef SPLIT_KEYBOARD
  380. RGB_DIRTY = true;
  381. #endif
  382. }
  383. return false;
  384. case RGB_VAD:
  385. // Split keyboards need to trigger on key-up for edge-case issue
  386. #ifndef SPLIT_KEYBOARD
  387. if (record->event.pressed) {
  388. #else
  389. if (!record->event.pressed) {
  390. #endif
  391. rgblight_decrease_val();
  392. #ifdef SPLIT_KEYBOARD
  393. RGB_DIRTY = true;
  394. #endif
  395. }
  396. return false;
  397. case RGB_SPI:
  398. if (record->event.pressed) {
  399. rgblight_increase_speed();
  400. }
  401. return false;
  402. case RGB_SPD:
  403. if (record->event.pressed) {
  404. rgblight_decrease_speed();
  405. }
  406. return false;
  407. case RGB_MODE_PLAIN:
  408. if (record->event.pressed) {
  409. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  410. #ifdef SPLIT_KEYBOARD
  411. RGB_DIRTY = true;
  412. #endif
  413. }
  414. return false;
  415. case RGB_MODE_BREATHE:
  416. #ifdef RGBLIGHT_EFFECT_BREATHING
  417. if (record->event.pressed) {
  418. if ((RGBLIGHT_MODE_BREATHING <= rgblight_get_mode()) &&
  419. (rgblight_get_mode() < RGBLIGHT_MODE_BREATHING_end)) {
  420. rgblight_step();
  421. } else {
  422. rgblight_mode(RGBLIGHT_MODE_BREATHING);
  423. }
  424. }
  425. #endif
  426. return false;
  427. case RGB_MODE_RAINBOW:
  428. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  429. if (record->event.pressed) {
  430. if ((RGBLIGHT_MODE_RAINBOW_MOOD <= rgblight_get_mode()) &&
  431. (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_MOOD_end)) {
  432. rgblight_step();
  433. } else {
  434. rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
  435. }
  436. }
  437. #endif
  438. return false;
  439. case RGB_MODE_SWIRL:
  440. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  441. if (record->event.pressed) {
  442. if ((RGBLIGHT_MODE_RAINBOW_SWIRL <= rgblight_get_mode()) &&
  443. (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_SWIRL_end)) {
  444. rgblight_step();
  445. } else {
  446. rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
  447. }
  448. }
  449. #endif
  450. return false;
  451. case RGB_MODE_SNAKE:
  452. #ifdef RGBLIGHT_EFFECT_SNAKE
  453. if (record->event.pressed) {
  454. if ((RGBLIGHT_MODE_SNAKE <= rgblight_get_mode()) &&
  455. (rgblight_get_mode() < RGBLIGHT_MODE_SNAKE_end)) {
  456. rgblight_step();
  457. } else {
  458. rgblight_mode(RGBLIGHT_MODE_SNAKE);
  459. }
  460. }
  461. #endif
  462. return false;
  463. case RGB_MODE_KNIGHT:
  464. #ifdef RGBLIGHT_EFFECT_KNIGHT
  465. if (record->event.pressed) {
  466. if ((RGBLIGHT_MODE_KNIGHT <= rgblight_get_mode()) &&
  467. (rgblight_get_mode() < RGBLIGHT_MODE_KNIGHT_end)) {
  468. rgblight_step();
  469. } else {
  470. rgblight_mode(RGBLIGHT_MODE_KNIGHT);
  471. }
  472. }
  473. #endif
  474. return false;
  475. case RGB_MODE_XMAS:
  476. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  477. if (record->event.pressed) {
  478. rgblight_mode(RGBLIGHT_MODE_CHRISTMAS);
  479. }
  480. #endif
  481. return false;
  482. case RGB_MODE_GRADIENT:
  483. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  484. if (record->event.pressed) {
  485. if ((RGBLIGHT_MODE_STATIC_GRADIENT <= rgblight_get_mode()) &&
  486. (rgblight_get_mode() < RGBLIGHT_MODE_STATIC_GRADIENT_end)) {
  487. rgblight_step();
  488. } else {
  489. rgblight_mode(RGBLIGHT_MODE_STATIC_GRADIENT);
  490. }
  491. }
  492. #endif
  493. return false;
  494. case RGB_MODE_RGBTEST:
  495. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  496. if (record->event.pressed) {
  497. rgblight_mode(RGBLIGHT_MODE_RGB_TEST);
  498. }
  499. #endif
  500. return false;
  501. #endif // defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  502. #ifdef PROTOCOL_LUFA
  503. case OUT_AUTO:
  504. if (record->event.pressed) {
  505. set_output(OUTPUT_AUTO);
  506. }
  507. return false;
  508. case OUT_USB:
  509. if (record->event.pressed) {
  510. set_output(OUTPUT_USB);
  511. }
  512. return false;
  513. #ifdef BLUETOOTH_ENABLE
  514. case OUT_BT:
  515. if (record->event.pressed) {
  516. set_output(OUTPUT_BLUETOOTH);
  517. }
  518. return false;
  519. #endif
  520. #endif
  521. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  522. if (record->event.pressed) {
  523. // MAGIC actions (BOOTMAGIC without the boot)
  524. if (!eeconfig_is_enabled()) {
  525. eeconfig_init();
  526. }
  527. /* keymap config */
  528. keymap_config.raw = eeconfig_read_keymap();
  529. switch (keycode)
  530. {
  531. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  532. keymap_config.swap_control_capslock = true;
  533. break;
  534. case MAGIC_CAPSLOCK_TO_CONTROL:
  535. keymap_config.capslock_to_control = true;
  536. break;
  537. case MAGIC_SWAP_LALT_LGUI:
  538. keymap_config.swap_lalt_lgui = true;
  539. break;
  540. case MAGIC_SWAP_RALT_RGUI:
  541. keymap_config.swap_ralt_rgui = true;
  542. break;
  543. case MAGIC_NO_GUI:
  544. keymap_config.no_gui = true;
  545. break;
  546. case MAGIC_SWAP_GRAVE_ESC:
  547. keymap_config.swap_grave_esc = true;
  548. break;
  549. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  550. keymap_config.swap_backslash_backspace = true;
  551. break;
  552. case MAGIC_HOST_NKRO:
  553. keymap_config.nkro = true;
  554. break;
  555. case MAGIC_SWAP_ALT_GUI:
  556. keymap_config.swap_lalt_lgui = true;
  557. keymap_config.swap_ralt_rgui = true;
  558. #ifdef AUDIO_ENABLE
  559. PLAY_SONG(ag_swap_song);
  560. #endif
  561. break;
  562. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  563. keymap_config.swap_control_capslock = false;
  564. break;
  565. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  566. keymap_config.capslock_to_control = false;
  567. break;
  568. case MAGIC_UNSWAP_LALT_LGUI:
  569. keymap_config.swap_lalt_lgui = false;
  570. break;
  571. case MAGIC_UNSWAP_RALT_RGUI:
  572. keymap_config.swap_ralt_rgui = false;
  573. break;
  574. case MAGIC_UNNO_GUI:
  575. keymap_config.no_gui = false;
  576. break;
  577. case MAGIC_UNSWAP_GRAVE_ESC:
  578. keymap_config.swap_grave_esc = false;
  579. break;
  580. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  581. keymap_config.swap_backslash_backspace = false;
  582. break;
  583. case MAGIC_UNHOST_NKRO:
  584. keymap_config.nkro = false;
  585. break;
  586. case MAGIC_UNSWAP_ALT_GUI:
  587. keymap_config.swap_lalt_lgui = false;
  588. keymap_config.swap_ralt_rgui = false;
  589. #ifdef AUDIO_ENABLE
  590. PLAY_SONG(ag_norm_song);
  591. #endif
  592. break;
  593. case MAGIC_TOGGLE_NKRO:
  594. keymap_config.nkro = !keymap_config.nkro;
  595. break;
  596. default:
  597. break;
  598. }
  599. eeconfig_update_keymap(keymap_config.raw);
  600. clear_keyboard(); // clear to prevent stuck keys
  601. return false;
  602. }
  603. break;
  604. case KC_LSPO: {
  605. if (record->event.pressed) {
  606. shift_interrupted[0] = false;
  607. scs_timer[0] = timer_read ();
  608. register_mods(MOD_BIT(KC_LSFT));
  609. }
  610. else {
  611. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  612. if (get_mods() & MOD_BIT(KC_RSFT)) {
  613. shift_interrupted[0] = true;
  614. shift_interrupted[1] = true;
  615. }
  616. #endif
  617. if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
  618. register_code(LSPO_KEY);
  619. unregister_code(LSPO_KEY);
  620. }
  621. unregister_mods(MOD_BIT(KC_LSFT));
  622. }
  623. return false;
  624. }
  625. case KC_RSPC: {
  626. if (record->event.pressed) {
  627. shift_interrupted[1] = false;
  628. scs_timer[1] = timer_read ();
  629. register_mods(MOD_BIT(KC_RSFT));
  630. }
  631. else {
  632. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  633. if (get_mods() & MOD_BIT(KC_LSFT)) {
  634. shift_interrupted[0] = true;
  635. shift_interrupted[1] = true;
  636. }
  637. #endif
  638. if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  639. register_code(RSPC_KEY);
  640. unregister_code(RSPC_KEY);
  641. }
  642. unregister_mods(MOD_BIT(KC_RSFT));
  643. }
  644. return false;
  645. }
  646. case KC_SFTENT: {
  647. if (record->event.pressed) {
  648. shift_interrupted[1] = false;
  649. scs_timer[1] = timer_read ();
  650. register_mods(MOD_BIT(KC_RSFT));
  651. }
  652. else if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  653. unregister_mods(MOD_BIT(KC_RSFT));
  654. register_code(SFTENT_KEY);
  655. unregister_code(SFTENT_KEY);
  656. }
  657. else {
  658. unregister_mods(MOD_BIT(KC_RSFT));
  659. }
  660. return false;
  661. }
  662. case GRAVE_ESC: {
  663. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  664. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  665. #ifdef GRAVE_ESC_ALT_OVERRIDE
  666. // if ALT is pressed, ESC is always sent
  667. // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
  668. if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
  669. shifted = 0;
  670. }
  671. #endif
  672. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  673. // if CTRL is pressed, ESC is always sent
  674. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  675. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
  676. shifted = 0;
  677. }
  678. #endif
  679. #ifdef GRAVE_ESC_GUI_OVERRIDE
  680. // if GUI is pressed, ESC is always sent
  681. if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
  682. shifted = 0;
  683. }
  684. #endif
  685. #ifdef GRAVE_ESC_SHIFT_OVERRIDE
  686. // if SHIFT is pressed, ESC is always sent
  687. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  688. shifted = 0;
  689. }
  690. #endif
  691. if (record->event.pressed) {
  692. grave_esc_was_shifted = shifted;
  693. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  694. }
  695. else {
  696. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  697. }
  698. send_keyboard_report();
  699. return false;
  700. }
  701. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
  702. case BL_BRTG: {
  703. if (record->event.pressed)
  704. breathing_toggle();
  705. return false;
  706. }
  707. #endif
  708. default: {
  709. shift_interrupted[0] = true;
  710. shift_interrupted[1] = true;
  711. break;
  712. }
  713. }
  714. return process_action_kb(record);
  715. }
  716. __attribute__ ((weak))
  717. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  718. 0, 0, 0, 0, 0, 0, 0, 0,
  719. 0, 0, 0, 0, 0, 0, 0, 0,
  720. 0, 0, 0, 0, 0, 0, 0, 0,
  721. 0, 0, 0, 0, 0, 0, 0, 0,
  722. 0, 1, 1, 1, 1, 1, 1, 0,
  723. 1, 1, 1, 1, 0, 0, 0, 0,
  724. 0, 0, 0, 0, 0, 0, 0, 0,
  725. 0, 0, 1, 0, 1, 0, 1, 1,
  726. 1, 1, 1, 1, 1, 1, 1, 1,
  727. 1, 1, 1, 1, 1, 1, 1, 1,
  728. 1, 1, 1, 1, 1, 1, 1, 1,
  729. 1, 1, 1, 0, 0, 0, 1, 1,
  730. 0, 0, 0, 0, 0, 0, 0, 0,
  731. 0, 0, 0, 0, 0, 0, 0, 0,
  732. 0, 0, 0, 0, 0, 0, 0, 0,
  733. 0, 0, 0, 1, 1, 1, 1, 0
  734. };
  735. __attribute__ ((weak))
  736. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  737. 0, 0, 0, 0, 0, 0, 0, 0,
  738. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  739. 0, 0, 0, 0, 0, 0, 0, 0,
  740. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  741. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  742. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  743. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  744. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  745. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  746. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  747. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  748. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  749. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  750. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  751. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  752. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  753. };
  754. void send_string(const char *str) {
  755. send_string_with_delay(str, 0);
  756. }
  757. void send_string_P(const char *str) {
  758. send_string_with_delay_P(str, 0);
  759. }
  760. void send_string_with_delay(const char *str, uint8_t interval) {
  761. while (1) {
  762. char ascii_code = *str;
  763. if (!ascii_code) break;
  764. if (ascii_code == 1) {
  765. // tap
  766. uint8_t keycode = *(++str);
  767. register_code(keycode);
  768. unregister_code(keycode);
  769. } else if (ascii_code == 2) {
  770. // down
  771. uint8_t keycode = *(++str);
  772. register_code(keycode);
  773. } else if (ascii_code == 3) {
  774. // up
  775. uint8_t keycode = *(++str);
  776. unregister_code(keycode);
  777. } else {
  778. send_char(ascii_code);
  779. }
  780. ++str;
  781. // interval
  782. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  783. }
  784. }
  785. void send_string_with_delay_P(const char *str, uint8_t interval) {
  786. while (1) {
  787. char ascii_code = pgm_read_byte(str);
  788. if (!ascii_code) break;
  789. if (ascii_code == 1) {
  790. // tap
  791. uint8_t keycode = pgm_read_byte(++str);
  792. register_code(keycode);
  793. unregister_code(keycode);
  794. } else if (ascii_code == 2) {
  795. // down
  796. uint8_t keycode = pgm_read_byte(++str);
  797. register_code(keycode);
  798. } else if (ascii_code == 3) {
  799. // up
  800. uint8_t keycode = pgm_read_byte(++str);
  801. unregister_code(keycode);
  802. } else {
  803. send_char(ascii_code);
  804. }
  805. ++str;
  806. // interval
  807. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  808. }
  809. }
  810. void send_char(char ascii_code) {
  811. uint8_t keycode;
  812. keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  813. if (pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code])) {
  814. register_code(KC_LSFT);
  815. register_code(keycode);
  816. unregister_code(keycode);
  817. unregister_code(KC_LSFT);
  818. } else {
  819. register_code(keycode);
  820. unregister_code(keycode);
  821. }
  822. }
  823. void set_single_persistent_default_layer(uint8_t default_layer) {
  824. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  825. PLAY_SONG(default_layer_songs[default_layer]);
  826. #endif
  827. eeconfig_update_default_layer(1U<<default_layer);
  828. default_layer_set(1U<<default_layer);
  829. }
  830. uint32_t update_tri_layer_state(uint32_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  831. uint32_t mask12 = (1UL << layer1) | (1UL << layer2);
  832. uint32_t mask3 = 1UL << layer3;
  833. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  834. }
  835. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  836. layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
  837. }
  838. void tap_random_base64(void) {
  839. #if defined(__AVR_ATmega32U4__)
  840. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  841. #else
  842. uint8_t key = rand() % 64;
  843. #endif
  844. switch (key) {
  845. case 0 ... 25:
  846. register_code(KC_LSFT);
  847. register_code(key + KC_A);
  848. unregister_code(key + KC_A);
  849. unregister_code(KC_LSFT);
  850. break;
  851. case 26 ... 51:
  852. register_code(key - 26 + KC_A);
  853. unregister_code(key - 26 + KC_A);
  854. break;
  855. case 52:
  856. register_code(KC_0);
  857. unregister_code(KC_0);
  858. break;
  859. case 53 ... 61:
  860. register_code(key - 53 + KC_1);
  861. unregister_code(key - 53 + KC_1);
  862. break;
  863. case 62:
  864. register_code(KC_LSFT);
  865. register_code(KC_EQL);
  866. unregister_code(KC_EQL);
  867. unregister_code(KC_LSFT);
  868. break;
  869. case 63:
  870. register_code(KC_SLSH);
  871. unregister_code(KC_SLSH);
  872. break;
  873. }
  874. }
  875. void matrix_init_quantum() {
  876. #ifdef BACKLIGHT_ENABLE
  877. backlight_init_ports();
  878. #endif
  879. #ifdef AUDIO_ENABLE
  880. audio_init();
  881. #endif
  882. #ifdef RGB_MATRIX_ENABLE
  883. rgb_matrix_init();
  884. #endif
  885. matrix_init_kb();
  886. }
  887. uint8_t rgb_matrix_task_counter = 0;
  888. #ifndef RGB_MATRIX_SKIP_FRAMES
  889. #define RGB_MATRIX_SKIP_FRAMES 1
  890. #endif
  891. void matrix_scan_quantum() {
  892. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  893. matrix_scan_music();
  894. #endif
  895. #ifdef TAP_DANCE_ENABLE
  896. matrix_scan_tap_dance();
  897. #endif
  898. #ifdef COMBO_ENABLE
  899. matrix_scan_combo();
  900. #endif
  901. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  902. backlight_task();
  903. #endif
  904. #ifdef RGB_MATRIX_ENABLE
  905. rgb_matrix_task();
  906. if (rgb_matrix_task_counter == 0) {
  907. rgb_matrix_update_pwm_buffers();
  908. }
  909. rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % (RGB_MATRIX_SKIP_FRAMES + 1));
  910. #endif
  911. matrix_scan_kb();
  912. }
  913. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  914. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  915. // depending on the pin, we use a different output compare unit
  916. #if BACKLIGHT_PIN == B7
  917. # define TCCRxA TCCR1A
  918. # define TCCRxB TCCR1B
  919. # define COMxx1 COM1C1
  920. # define OCRxx OCR1C
  921. # define ICRx ICR1
  922. #elif BACKLIGHT_PIN == B6
  923. # define TCCRxA TCCR1A
  924. # define TCCRxB TCCR1B
  925. # define COMxx1 COM1B1
  926. # define OCRxx OCR1B
  927. # define ICRx ICR1
  928. #elif BACKLIGHT_PIN == B5
  929. # define TCCRxA TCCR1A
  930. # define TCCRxB TCCR1B
  931. # define COMxx1 COM1A1
  932. # define OCRxx OCR1A
  933. # define ICRx ICR1
  934. #elif BACKLIGHT_PIN == C6
  935. # define TCCRxA TCCR3A
  936. # define TCCRxB TCCR3B
  937. # define COMxx1 COM1A1
  938. # define OCRxx OCR3A
  939. # define ICRx ICR3
  940. #else
  941. # define NO_HARDWARE_PWM
  942. #endif
  943. #ifndef BACKLIGHT_ON_STATE
  944. #define BACKLIGHT_ON_STATE 0
  945. #endif
  946. #ifdef NO_HARDWARE_PWM // pwm through software
  947. __attribute__ ((weak))
  948. void backlight_init_ports(void)
  949. {
  950. // Setup backlight pin as output and output to on state.
  951. // DDRx |= n
  952. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  953. #if BACKLIGHT_ON_STATE == 0
  954. // PORTx &= ~n
  955. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  956. #else
  957. // PORTx |= n
  958. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  959. #endif
  960. }
  961. __attribute__ ((weak))
  962. void backlight_set(uint8_t level) {}
  963. uint8_t backlight_tick = 0;
  964. #ifndef BACKLIGHT_CUSTOM_DRIVER
  965. void backlight_task(void) {
  966. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  967. #if BACKLIGHT_ON_STATE == 0
  968. // PORTx &= ~n
  969. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  970. #else
  971. // PORTx |= n
  972. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  973. #endif
  974. } else {
  975. #if BACKLIGHT_ON_STATE == 0
  976. // PORTx |= n
  977. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  978. #else
  979. // PORTx &= ~n
  980. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  981. #endif
  982. }
  983. backlight_tick = (backlight_tick + 1) % 16;
  984. }
  985. #endif
  986. #ifdef BACKLIGHT_BREATHING
  987. #ifndef BACKLIGHT_CUSTOM_DRIVER
  988. #error "Backlight breathing only available with hardware PWM. Please disable."
  989. #endif
  990. #endif
  991. #else // pwm through timer
  992. #define TIMER_TOP 0xFFFFU
  993. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  994. static uint16_t cie_lightness(uint16_t v) {
  995. if (v <= 5243) // if below 8% of max
  996. return v / 9; // same as dividing by 900%
  997. else {
  998. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  999. // to get a useful result with integer division, we shift left in the expression above
  1000. // and revert what we've done again after squaring.
  1001. y = y * y * y >> 8;
  1002. if (y > 0xFFFFUL) // prevent overflow
  1003. return 0xFFFFU;
  1004. else
  1005. return (uint16_t) y;
  1006. }
  1007. }
  1008. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  1009. static inline void set_pwm(uint16_t val) {
  1010. OCRxx = val;
  1011. }
  1012. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1013. __attribute__ ((weak))
  1014. void backlight_set(uint8_t level) {
  1015. if (level > BACKLIGHT_LEVELS)
  1016. level = BACKLIGHT_LEVELS;
  1017. if (level == 0) {
  1018. // Turn off PWM control on backlight pin
  1019. TCCRxA &= ~(_BV(COMxx1));
  1020. } else {
  1021. // Turn on PWM control of backlight pin
  1022. TCCRxA |= _BV(COMxx1);
  1023. }
  1024. // Set the brightness
  1025. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  1026. }
  1027. void backlight_task(void) {}
  1028. #endif // BACKLIGHT_CUSTOM_DRIVER
  1029. #ifdef BACKLIGHT_BREATHING
  1030. #define BREATHING_NO_HALT 0
  1031. #define BREATHING_HALT_OFF 1
  1032. #define BREATHING_HALT_ON 2
  1033. #define BREATHING_STEPS 128
  1034. static uint8_t breathing_period = BREATHING_PERIOD;
  1035. static uint8_t breathing_halt = BREATHING_NO_HALT;
  1036. static uint16_t breathing_counter = 0;
  1037. bool is_breathing(void) {
  1038. return !!(TIMSK1 & _BV(TOIE1));
  1039. }
  1040. #define breathing_interrupt_enable() do {TIMSK1 |= _BV(TOIE1);} while (0)
  1041. #define breathing_interrupt_disable() do {TIMSK1 &= ~_BV(TOIE1);} while (0)
  1042. #define breathing_min() do {breathing_counter = 0;} while (0)
  1043. #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
  1044. void breathing_enable(void)
  1045. {
  1046. breathing_counter = 0;
  1047. breathing_halt = BREATHING_NO_HALT;
  1048. breathing_interrupt_enable();
  1049. }
  1050. void breathing_pulse(void)
  1051. {
  1052. if (get_backlight_level() == 0)
  1053. breathing_min();
  1054. else
  1055. breathing_max();
  1056. breathing_halt = BREATHING_HALT_ON;
  1057. breathing_interrupt_enable();
  1058. }
  1059. void breathing_disable(void)
  1060. {
  1061. breathing_interrupt_disable();
  1062. // Restore backlight level
  1063. backlight_set(get_backlight_level());
  1064. }
  1065. void breathing_self_disable(void)
  1066. {
  1067. if (get_backlight_level() == 0)
  1068. breathing_halt = BREATHING_HALT_OFF;
  1069. else
  1070. breathing_halt = BREATHING_HALT_ON;
  1071. }
  1072. void breathing_toggle(void) {
  1073. if (is_breathing())
  1074. breathing_disable();
  1075. else
  1076. breathing_enable();
  1077. }
  1078. void breathing_period_set(uint8_t value)
  1079. {
  1080. if (!value)
  1081. value = 1;
  1082. breathing_period = value;
  1083. }
  1084. void breathing_period_default(void) {
  1085. breathing_period_set(BREATHING_PERIOD);
  1086. }
  1087. void breathing_period_inc(void)
  1088. {
  1089. breathing_period_set(breathing_period+1);
  1090. }
  1091. void breathing_period_dec(void)
  1092. {
  1093. breathing_period_set(breathing_period-1);
  1094. }
  1095. /* To generate breathing curve in python:
  1096. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  1097. */
  1098. static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1099. // Use this before the cie_lightness function.
  1100. static inline uint16_t scale_backlight(uint16_t v) {
  1101. return v / BACKLIGHT_LEVELS * get_backlight_level();
  1102. }
  1103. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  1104. * about 244 times per second.
  1105. */
  1106. ISR(TIMER1_OVF_vect)
  1107. {
  1108. uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
  1109. // resetting after one period to prevent ugly reset at overflow.
  1110. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  1111. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  1112. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  1113. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  1114. {
  1115. breathing_interrupt_disable();
  1116. }
  1117. set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  1118. }
  1119. #endif // BACKLIGHT_BREATHING
  1120. __attribute__ ((weak))
  1121. void backlight_init_ports(void)
  1122. {
  1123. // Setup backlight pin as output and output to on state.
  1124. // DDRx |= n
  1125. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  1126. #if BACKLIGHT_ON_STATE == 0
  1127. // PORTx &= ~n
  1128. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  1129. #else
  1130. // PORTx |= n
  1131. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  1132. #endif
  1133. // I could write a wall of text here to explain... but TL;DW
  1134. // Go read the ATmega32u4 datasheet.
  1135. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  1136. // Pin PB7 = OCR1C (Timer 1, Channel C)
  1137. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  1138. // (i.e. start high, go low when counter matches.)
  1139. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  1140. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  1141. /*
  1142. 14.8.3:
  1143. "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
  1144. "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
  1145. */
  1146. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  1147. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1148. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  1149. ICRx = TIMER_TOP;
  1150. backlight_init();
  1151. #ifdef BACKLIGHT_BREATHING
  1152. breathing_enable();
  1153. #endif
  1154. }
  1155. #endif // NO_HARDWARE_PWM
  1156. #else // backlight
  1157. __attribute__ ((weak))
  1158. void backlight_init_ports(void) {}
  1159. __attribute__ ((weak))
  1160. void backlight_set(uint8_t level) {}
  1161. #endif // backlight
  1162. #ifdef HD44780_ENABLED
  1163. #include "hd44780.h"
  1164. #endif
  1165. // Functions for spitting out values
  1166. //
  1167. void send_dword(uint32_t number) { // this might not actually work
  1168. uint16_t word = (number >> 16);
  1169. send_word(word);
  1170. send_word(number & 0xFFFFUL);
  1171. }
  1172. void send_word(uint16_t number) {
  1173. uint8_t byte = number >> 8;
  1174. send_byte(byte);
  1175. send_byte(number & 0xFF);
  1176. }
  1177. void send_byte(uint8_t number) {
  1178. uint8_t nibble = number >> 4;
  1179. send_nibble(nibble);
  1180. send_nibble(number & 0xF);
  1181. }
  1182. void send_nibble(uint8_t number) {
  1183. switch (number) {
  1184. case 0:
  1185. register_code(KC_0);
  1186. unregister_code(KC_0);
  1187. break;
  1188. case 1 ... 9:
  1189. register_code(KC_1 + (number - 1));
  1190. unregister_code(KC_1 + (number - 1));
  1191. break;
  1192. case 0xA ... 0xF:
  1193. register_code(KC_A + (number - 0xA));
  1194. unregister_code(KC_A + (number - 0xA));
  1195. break;
  1196. }
  1197. }
  1198. __attribute__((weak))
  1199. uint16_t hex_to_keycode(uint8_t hex)
  1200. {
  1201. hex = hex & 0xF;
  1202. if (hex == 0x0) {
  1203. return KC_0;
  1204. } else if (hex < 0xA) {
  1205. return KC_1 + (hex - 0x1);
  1206. } else {
  1207. return KC_A + (hex - 0xA);
  1208. }
  1209. }
  1210. void api_send_unicode(uint32_t unicode) {
  1211. #ifdef API_ENABLE
  1212. uint8_t chunk[4];
  1213. dword_to_bytes(unicode, chunk);
  1214. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1215. #endif
  1216. }
  1217. __attribute__ ((weak))
  1218. void led_set_user(uint8_t usb_led) {
  1219. }
  1220. __attribute__ ((weak))
  1221. void led_set_kb(uint8_t usb_led) {
  1222. led_set_user(usb_led);
  1223. }
  1224. __attribute__ ((weak))
  1225. void led_init_ports(void)
  1226. {
  1227. }
  1228. __attribute__ ((weak))
  1229. void led_set(uint8_t usb_led)
  1230. {
  1231. // Example LED Code
  1232. //
  1233. // // Using PE6 Caps Lock LED
  1234. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  1235. // {
  1236. // // Output high.
  1237. // DDRE |= (1<<6);
  1238. // PORTE |= (1<<6);
  1239. // }
  1240. // else
  1241. // {
  1242. // // Output low.
  1243. // DDRE &= ~(1<<6);
  1244. // PORTE &= ~(1<<6);
  1245. // }
  1246. led_set_kb(usb_led);
  1247. }
  1248. //------------------------------------------------------------------------------
  1249. // Override these functions in your keymap file to play different tunes on
  1250. // different events such as startup and bootloader jump
  1251. __attribute__ ((weak))
  1252. void startup_user() {}
  1253. __attribute__ ((weak))
  1254. void shutdown_user() {}
  1255. //------------------------------------------------------------------------------