quantum.c 38 KB

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