quantum.c 40 KB

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