quantum.c 42 KB

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