quantum.c 40 KB

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