quantum.c 44 KB

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