quantum.c 43 KB

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