quantum.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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) || defined(BACKLIGHT_PINS))
  1056. // The logic is a bit complex, we support 3 setups:
  1057. // 1. hardware PWM when backlight is wired to a PWM pin
  1058. // depending on this pin, we use a different output compare unit
  1059. // 2. software PWM with hardware timers, but the used timer depends
  1060. // on the audio setup (audio wins other backlight)
  1061. // 3. full software PWM
  1062. #if BACKLIGHT_PIN == B7
  1063. # define HARDWARE_PWM
  1064. # define TCCRxA TCCR1A
  1065. # define TCCRxB TCCR1B
  1066. # define COMxx1 COM1C1
  1067. # define OCRxx OCR1C
  1068. # define ICRx ICR1
  1069. #elif BACKLIGHT_PIN == B6
  1070. # define HARDWARE_PWM
  1071. # define TCCRxA TCCR1A
  1072. # define TCCRxB TCCR1B
  1073. # define COMxx1 COM1B1
  1074. # define OCRxx OCR1B
  1075. # define ICRx ICR1
  1076. #elif BACKLIGHT_PIN == B5
  1077. # define HARDWARE_PWM
  1078. # define TCCRxA TCCR1A
  1079. # define TCCRxB TCCR1B
  1080. # define COMxx1 COM1A1
  1081. # define OCRxx OCR1A
  1082. # define ICRx ICR1
  1083. #elif BACKLIGHT_PIN == C6
  1084. # define HARDWARE_PWM
  1085. # define TCCRxA TCCR3A
  1086. # define TCCRxB TCCR3B
  1087. # define COMxx1 COM1A1
  1088. # define OCRxx OCR3A
  1089. # define ICRx ICR3
  1090. #elif defined(__AVR_ATmega32A__) && BACKLIGHT_PIN == D4
  1091. # define TCCRxA TCCR1A
  1092. # define TCCRxB TCCR1B
  1093. # define COMxx1 COM1B1
  1094. # define OCRxx OCR1B
  1095. # define ICRx ICR1
  1096. # define TIMSK1 TIMSK
  1097. #else
  1098. # if !defined(BACKLIGHT_CUSTOM_DRIVER)
  1099. # if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
  1100. // timer 1 is not used by audio , backlight can use it
  1101. #pragma message "Using hardware timer 1 with software PWM"
  1102. # define HARDWARE_PWM
  1103. # define BACKLIGHT_PWM_TIMER
  1104. # define TCCRxA TCCR1A
  1105. # define TCCRxB TCCR1B
  1106. # define OCRxx OCR1A
  1107. # define OCRxAH OCR1AH
  1108. # define OCRxAL OCR1AL
  1109. # define TIMERx_COMPA_vect TIMER1_COMPA_vect
  1110. # define TIMERx_OVF_vect TIMER1_OVF_vect
  1111. # define OCIExA OCIE1A
  1112. # define TOIEx TOIE1
  1113. # define ICRx ICR1
  1114. # ifndef TIMSK
  1115. # define TIMSK TIMSK1
  1116. # endif
  1117. # elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
  1118. #pragma message "Using hardware timer 3 with software PWM"
  1119. // timer 3 is not used by audio, backlight can use it
  1120. # define HARDWARE_PWM
  1121. # define BACKLIGHT_PWM_TIMER
  1122. # define TCCRxA TCCR3A
  1123. # define TCCRxB TCCR3B
  1124. # define OCRxx OCR3A
  1125. # define OCRxAH OCR3AH
  1126. # define OCRxAL OCR3AL
  1127. # define TIMERx_COMPA_vect TIMER3_COMPA_vect
  1128. # define TIMERx_OVF_vect TIMER3_OVF_vect
  1129. # define OCIExA OCIE3A
  1130. # define TOIEx TOIE3
  1131. # define ICRx ICR1
  1132. # ifndef TIMSK
  1133. # define TIMSK TIMSK3
  1134. # endif
  1135. # else
  1136. #pragma message "Audio in use - using pure software PWM"
  1137. #define NO_HARDWARE_PWM
  1138. # endif
  1139. # else
  1140. #pragma message "Custom driver defined - using pure software PWM"
  1141. #define NO_HARDWARE_PWM
  1142. # endif
  1143. #endif
  1144. #ifndef BACKLIGHT_ON_STATE
  1145. #define BACKLIGHT_ON_STATE 0
  1146. #endif
  1147. void backlight_on(uint8_t backlight_pin) {
  1148. #if BACKLIGHT_ON_STATE == 0
  1149. writePinLow(backlight_pin);
  1150. #else
  1151. writePinHigh(backlight_pin);
  1152. #endif
  1153. }
  1154. void backlight_off(uint8_t backlight_pin) {
  1155. #if BACKLIGHT_ON_STATE == 0
  1156. writePinHigh(backlight_pin);
  1157. #else
  1158. writePinLow(backlight_pin);
  1159. #endif
  1160. }
  1161. #if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software
  1162. // we support multiple backlight pins
  1163. #ifndef BACKLIGHT_LED_COUNT
  1164. #define BACKLIGHT_LED_COUNT 1
  1165. #endif
  1166. #if BACKLIGHT_LED_COUNT == 1
  1167. #define BACKLIGHT_PIN_INIT { BACKLIGHT_PIN }
  1168. #else
  1169. #define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
  1170. #endif
  1171. #define FOR_EACH_LED(x) \
  1172. for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) \
  1173. { \
  1174. uint8_t backlight_pin = backlight_pins[i]; \
  1175. { \
  1176. x \
  1177. } \
  1178. }
  1179. static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
  1180. #else // full hardware PWM
  1181. // we support only one backlight pin
  1182. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  1183. #define FOR_EACH_LED(x) x
  1184. #endif
  1185. #ifdef NO_HARDWARE_PWM
  1186. __attribute__((weak))
  1187. void backlight_init_ports(void)
  1188. {
  1189. // Setup backlight pin as output and output to on state.
  1190. FOR_EACH_LED(
  1191. setPinOutput(backlight_pin);
  1192. backlight_on(backlight_pin);
  1193. )
  1194. }
  1195. __attribute__ ((weak))
  1196. void backlight_set(uint8_t level) {}
  1197. uint8_t backlight_tick = 0;
  1198. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1199. void backlight_task(void) {
  1200. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  1201. FOR_EACH_LED(
  1202. backlight_on(backlight_pin);
  1203. )
  1204. }
  1205. else {
  1206. FOR_EACH_LED(
  1207. backlight_off(backlight_pin);
  1208. )
  1209. }
  1210. backlight_tick = (backlight_tick + 1) % 16;
  1211. }
  1212. #endif
  1213. #ifdef BACKLIGHT_BREATHING
  1214. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1215. #error "Backlight breathing only available with hardware PWM. Please disable."
  1216. #endif
  1217. #endif
  1218. #else // hardware pwm through timer
  1219. #ifdef BACKLIGHT_PWM_TIMER
  1220. // The idea of software PWM assisted by hardware timers is the following
  1221. // we use the hardware timer in fast PWM mode like for hardware PWM, but
  1222. // instead of letting the Output Match Comparator control the led pin
  1223. // (which is not possible since the backlight is not wired to PWM pins on the
  1224. // CPU), we do the LED on/off by oursleves.
  1225. // The timer is setup to count up to 0xFFFF, and we set the Output Compare
  1226. // register to the current 16bits backlight level (after CIE correction).
  1227. // This means the CPU will trigger a compare match interrupt when the counter
  1228. // reaches the backlight level, where we turn off the LEDs,
  1229. // but also an overflow interrupt when the counter rolls back to 0,
  1230. // in which we're going to turn on the LEDs.
  1231. // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
  1232. // Triggered when the counter reaches the OCRx value
  1233. ISR(TIMERx_COMPA_vect) {
  1234. FOR_EACH_LED(
  1235. backlight_off(backlight_pin);
  1236. )
  1237. }
  1238. // Triggered when the counter reaches the TOP value
  1239. // this one triggers at F_CPU/65536 =~ 244 Hz
  1240. ISR(TIMERx_OVF_vect) {
  1241. #ifdef BACKLIGHT_BREATHING
  1242. breathing_task();
  1243. #endif
  1244. // for very small values of OCRxx (or backlight level)
  1245. // we can't guarantee this whole code won't execute
  1246. // at the same time as the compare match interrupt
  1247. // which means that we might turn on the leds while
  1248. // trying to turn them off, leading to flickering
  1249. // artifacts (especially while breathing, because breathing_task
  1250. // takes many computation cycles).
  1251. // so better not turn them on while the counter TOP is very low.
  1252. if (OCRxx > 256) {
  1253. FOR_EACH_LED(
  1254. backlight_on(backlight_pin);
  1255. )
  1256. }
  1257. }
  1258. #endif
  1259. #define TIMER_TOP 0xFFFFU
  1260. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  1261. static uint16_t cie_lightness(uint16_t v) {
  1262. if (v <= 5243) // if below 8% of max
  1263. return v / 9; // same as dividing by 900%
  1264. else {
  1265. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  1266. // to get a useful result with integer division, we shift left in the expression above
  1267. // and revert what we've done again after squaring.
  1268. y = y * y * y >> 8;
  1269. if (y > 0xFFFFUL) // prevent overflow
  1270. return 0xFFFFU;
  1271. else
  1272. return (uint16_t) y;
  1273. }
  1274. }
  1275. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  1276. static inline void set_pwm(uint16_t val) {
  1277. OCRxx = val;
  1278. }
  1279. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1280. __attribute__ ((weak))
  1281. void backlight_set(uint8_t level) {
  1282. if (level > BACKLIGHT_LEVELS)
  1283. level = BACKLIGHT_LEVELS;
  1284. if (level == 0) {
  1285. #ifdef BACKLIGHT_PWM_TIMER
  1286. if (OCRxx) {
  1287. TIMSK &= ~(_BV(OCIExA));
  1288. TIMSK &= ~(_BV(TOIEx));
  1289. FOR_EACH_LED(
  1290. backlight_off(backlight_pin);
  1291. )
  1292. }
  1293. #else
  1294. // Turn off PWM control on backlight pin
  1295. TCCRxA &= ~(_BV(COMxx1));
  1296. #endif
  1297. } else {
  1298. #ifdef BACKLIGHT_PWM_TIMER
  1299. if (!OCRxx) {
  1300. TIMSK |= _BV(OCIExA);
  1301. TIMSK |= _BV(TOIEx);
  1302. }
  1303. #else
  1304. // Turn on PWM control of backlight pin
  1305. TCCRxA |= _BV(COMxx1);
  1306. #endif
  1307. }
  1308. // Set the brightness
  1309. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  1310. }
  1311. void backlight_task(void) {}
  1312. #endif // BACKLIGHT_CUSTOM_DRIVER
  1313. #ifdef BACKLIGHT_BREATHING
  1314. #define BREATHING_NO_HALT 0
  1315. #define BREATHING_HALT_OFF 1
  1316. #define BREATHING_HALT_ON 2
  1317. #define BREATHING_STEPS 128
  1318. static uint8_t breathing_period = BREATHING_PERIOD;
  1319. static uint8_t breathing_halt = BREATHING_NO_HALT;
  1320. static uint16_t breathing_counter = 0;
  1321. #ifdef BACKLIGHT_PWM_TIMER
  1322. static bool breathing = false;
  1323. bool is_breathing(void) {
  1324. return breathing;
  1325. }
  1326. #define breathing_interrupt_enable() do { breathing = true; } while (0)
  1327. #define breathing_interrupt_disable() do { breathing = false; } while (0)
  1328. #else
  1329. bool is_breathing(void) {
  1330. return !!(TIMSK1 & _BV(TOIE1));
  1331. }
  1332. #define breathing_interrupt_enable() do {TIMSK1 |= _BV(TOIE1);} while (0)
  1333. #define breathing_interrupt_disable() do {TIMSK1 &= ~_BV(TOIE1);} while (0)
  1334. #endif
  1335. #define breathing_min() do {breathing_counter = 0;} while (0)
  1336. #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
  1337. void breathing_enable(void)
  1338. {
  1339. breathing_counter = 0;
  1340. breathing_halt = BREATHING_NO_HALT;
  1341. breathing_interrupt_enable();
  1342. }
  1343. void breathing_pulse(void)
  1344. {
  1345. if (get_backlight_level() == 0)
  1346. breathing_min();
  1347. else
  1348. breathing_max();
  1349. breathing_halt = BREATHING_HALT_ON;
  1350. breathing_interrupt_enable();
  1351. }
  1352. void breathing_disable(void)
  1353. {
  1354. breathing_interrupt_disable();
  1355. // Restore backlight level
  1356. backlight_set(get_backlight_level());
  1357. }
  1358. void breathing_self_disable(void)
  1359. {
  1360. if (get_backlight_level() == 0)
  1361. breathing_halt = BREATHING_HALT_OFF;
  1362. else
  1363. breathing_halt = BREATHING_HALT_ON;
  1364. }
  1365. void breathing_toggle(void) {
  1366. if (is_breathing())
  1367. breathing_disable();
  1368. else
  1369. breathing_enable();
  1370. }
  1371. void breathing_period_set(uint8_t value)
  1372. {
  1373. if (!value)
  1374. value = 1;
  1375. breathing_period = value;
  1376. }
  1377. void breathing_period_default(void) {
  1378. breathing_period_set(BREATHING_PERIOD);
  1379. }
  1380. void breathing_period_inc(void)
  1381. {
  1382. breathing_period_set(breathing_period+1);
  1383. }
  1384. void breathing_period_dec(void)
  1385. {
  1386. breathing_period_set(breathing_period-1);
  1387. }
  1388. /* To generate breathing curve in python:
  1389. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  1390. */
  1391. 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};
  1392. // Use this before the cie_lightness function.
  1393. static inline uint16_t scale_backlight(uint16_t v) {
  1394. return v / BACKLIGHT_LEVELS * get_backlight_level();
  1395. }
  1396. #ifdef BACKLIGHT_PWM_TIMER
  1397. void breathing_task(void)
  1398. #else
  1399. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  1400. * about 244 times per second.
  1401. */
  1402. ISR(TIMER1_OVF_vect)
  1403. #endif
  1404. {
  1405. uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
  1406. // resetting after one period to prevent ugly reset at overflow.
  1407. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  1408. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  1409. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  1410. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  1411. {
  1412. breathing_interrupt_disable();
  1413. }
  1414. set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  1415. }
  1416. #endif // BACKLIGHT_BREATHING
  1417. __attribute__ ((weak))
  1418. void backlight_init_ports(void)
  1419. {
  1420. // Setup backlight pin as output and output to on state.
  1421. FOR_EACH_LED(
  1422. setPinOutput(backlight_pin);
  1423. backlight_on(backlight_pin);
  1424. )
  1425. // I could write a wall of text here to explain... but TL;DW
  1426. // Go read the ATmega32u4 datasheet.
  1427. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  1428. #ifdef BACKLIGHT_PWM_TIMER
  1429. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  1430. TCCRxA = _BV(WGM11); // = 0b00000010;
  1431. // clock select clk/1
  1432. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1433. #else // hardware PWM
  1434. // Pin PB7 = OCR1C (Timer 1, Channel C)
  1435. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  1436. // (i.e. start high, go low when counter matches.)
  1437. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  1438. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  1439. /*
  1440. 14.8.3:
  1441. "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 [..]."
  1442. "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)."
  1443. */
  1444. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  1445. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1446. #endif
  1447. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  1448. ICRx = TIMER_TOP;
  1449. backlight_init();
  1450. #ifdef BACKLIGHT_BREATHING
  1451. breathing_enable();
  1452. #endif
  1453. }
  1454. #endif // hardware backlight
  1455. #else // no backlight
  1456. __attribute__ ((weak))
  1457. void backlight_init_ports(void) {}
  1458. __attribute__ ((weak))
  1459. void backlight_set(uint8_t level) {}
  1460. #endif // backlight
  1461. #ifdef HD44780_ENABLED
  1462. #include "hd44780.h"
  1463. #endif
  1464. // Functions for spitting out values
  1465. //
  1466. void send_dword(uint32_t number) { // this might not actually work
  1467. uint16_t word = (number >> 16);
  1468. send_word(word);
  1469. send_word(number & 0xFFFFUL);
  1470. }
  1471. void send_word(uint16_t number) {
  1472. uint8_t byte = number >> 8;
  1473. send_byte(byte);
  1474. send_byte(number & 0xFF);
  1475. }
  1476. void send_byte(uint8_t number) {
  1477. uint8_t nibble = number >> 4;
  1478. send_nibble(nibble);
  1479. send_nibble(number & 0xF);
  1480. }
  1481. void send_nibble(uint8_t number) {
  1482. switch (number) {
  1483. case 0:
  1484. register_code(KC_0);
  1485. unregister_code(KC_0);
  1486. break;
  1487. case 1 ... 9:
  1488. register_code(KC_1 + (number - 1));
  1489. unregister_code(KC_1 + (number - 1));
  1490. break;
  1491. case 0xA ... 0xF:
  1492. register_code(KC_A + (number - 0xA));
  1493. unregister_code(KC_A + (number - 0xA));
  1494. break;
  1495. }
  1496. }
  1497. __attribute__((weak))
  1498. uint16_t hex_to_keycode(uint8_t hex)
  1499. {
  1500. hex = hex & 0xF;
  1501. if (hex == 0x0) {
  1502. return KC_0;
  1503. } else if (hex < 0xA) {
  1504. return KC_1 + (hex - 0x1);
  1505. } else {
  1506. return KC_A + (hex - 0xA);
  1507. }
  1508. }
  1509. void api_send_unicode(uint32_t unicode) {
  1510. #ifdef API_ENABLE
  1511. uint8_t chunk[4];
  1512. dword_to_bytes(unicode, chunk);
  1513. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1514. #endif
  1515. }
  1516. __attribute__ ((weak))
  1517. void led_set_user(uint8_t usb_led) {
  1518. }
  1519. __attribute__ ((weak))
  1520. void led_set_kb(uint8_t usb_led) {
  1521. led_set_user(usb_led);
  1522. }
  1523. __attribute__ ((weak))
  1524. void led_init_ports(void)
  1525. {
  1526. }
  1527. __attribute__ ((weak))
  1528. void led_set(uint8_t usb_led)
  1529. {
  1530. // Example LED Code
  1531. //
  1532. // // Using PE6 Caps Lock LED
  1533. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  1534. // {
  1535. // // Output high.
  1536. // DDRE |= (1<<6);
  1537. // PORTE |= (1<<6);
  1538. // }
  1539. // else
  1540. // {
  1541. // // Output low.
  1542. // DDRE &= ~(1<<6);
  1543. // PORTE &= ~(1<<6);
  1544. // }
  1545. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  1546. // Use backlight as Caps Lock indicator
  1547. uint8_t bl_toggle_lvl = 0;
  1548. if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
  1549. // Turning Caps Lock ON and backlight is disabled in config
  1550. // Toggling backlight to the brightest level
  1551. bl_toggle_lvl = BACKLIGHT_LEVELS;
  1552. } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
  1553. // Turning Caps Lock OFF and backlight is enabled in config
  1554. // Toggling backlight and restoring config level
  1555. bl_toggle_lvl = backlight_config.level;
  1556. }
  1557. // Set level without modify backlight_config to keep ability to restore state
  1558. backlight_set(bl_toggle_lvl);
  1559. #endif
  1560. led_set_kb(usb_led);
  1561. }
  1562. //------------------------------------------------------------------------------
  1563. // Override these functions in your keymap file to play different tunes on
  1564. // different events such as startup and bootloader jump
  1565. __attribute__ ((weak))
  1566. void startup_user() {}
  1567. __attribute__ ((weak))
  1568. void shutdown_user() {}
  1569. //------------------------------------------------------------------------------