quantum.c 34 KB

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