quantum.c 48 KB

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