quantum.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. #include "backlight.h"
  24. extern backlight_config_t backlight_config;
  25. #ifdef FAUXCLICKY_ENABLE
  26. #include "fauxclicky.h"
  27. #endif
  28. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  29. switch (code) {
  30. case QK_MODS ... QK_MODS_MAX:
  31. break;
  32. default:
  33. return;
  34. }
  35. if (code & QK_LCTL)
  36. f(KC_LCTL);
  37. if (code & QK_LSFT)
  38. f(KC_LSFT);
  39. if (code & QK_LALT)
  40. f(KC_LALT);
  41. if (code & QK_LGUI)
  42. f(KC_LGUI);
  43. if (code < QK_RMODS_MIN) return;
  44. if (code & QK_RCTL)
  45. f(KC_RCTL);
  46. if (code & QK_RSFT)
  47. f(KC_RSFT);
  48. if (code & QK_RALT)
  49. f(KC_RALT);
  50. if (code & QK_RGUI)
  51. f(KC_RGUI);
  52. }
  53. static inline void qk_register_weak_mods(uint8_t kc) {
  54. add_weak_mods(MOD_BIT(kc));
  55. send_keyboard_report();
  56. }
  57. static inline void qk_unregister_weak_mods(uint8_t kc) {
  58. del_weak_mods(MOD_BIT(kc));
  59. send_keyboard_report();
  60. }
  61. static inline void qk_register_mods(uint8_t kc) {
  62. add_weak_mods(MOD_BIT(kc));
  63. send_keyboard_report();
  64. }
  65. static inline void qk_unregister_mods(uint8_t kc) {
  66. del_weak_mods(MOD_BIT(kc));
  67. send_keyboard_report();
  68. }
  69. void register_code16 (uint16_t code) {
  70. if (IS_MOD(code) || code == KC_NO) {
  71. do_code16 (code, qk_register_mods);
  72. } else {
  73. do_code16 (code, qk_register_weak_mods);
  74. }
  75. register_code (code);
  76. }
  77. void unregister_code16 (uint16_t code) {
  78. unregister_code (code);
  79. if (IS_MOD(code) || code == KC_NO) {
  80. do_code16 (code, qk_unregister_mods);
  81. } else {
  82. do_code16 (code, qk_unregister_weak_mods);
  83. }
  84. }
  85. __attribute__ ((weak))
  86. bool process_action_kb(keyrecord_t *record) {
  87. return true;
  88. }
  89. __attribute__ ((weak))
  90. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  91. return process_record_user(keycode, record);
  92. }
  93. __attribute__ ((weak))
  94. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  95. return true;
  96. }
  97. void reset_keyboard(void) {
  98. clear_keyboard();
  99. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
  100. music_all_notes_off();
  101. shutdown_user();
  102. #endif
  103. wait_ms(250);
  104. #ifdef CATERINA_BOOTLOADER
  105. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  106. #endif
  107. bootloader_jump();
  108. }
  109. // Shift / paren setup
  110. #ifndef LSPO_KEY
  111. #define LSPO_KEY KC_9
  112. #endif
  113. #ifndef RSPC_KEY
  114. #define RSPC_KEY KC_0
  115. #endif
  116. static bool shift_interrupted[2] = {0, 0};
  117. static uint16_t scs_timer[2] = {0, 0};
  118. bool process_record_quantum(keyrecord_t *record) {
  119. /* This gets the keycode from the key pressed */
  120. keypos_t key = record->event.key;
  121. uint16_t keycode;
  122. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  123. /* TODO: Use store_or_get_action() or a similar function. */
  124. if (!disable_action_cache) {
  125. uint8_t layer;
  126. if (record->event.pressed) {
  127. layer = layer_switch_get_layer(key);
  128. update_source_layers_cache(key, layer);
  129. } else {
  130. layer = read_source_layers_cache(key);
  131. }
  132. keycode = keymap_key_to_keycode(layer, key);
  133. } else
  134. #endif
  135. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  136. // This is how you use actions here
  137. // if (keycode == KC_LEAD) {
  138. // action_t action;
  139. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  140. // process_action(record, action);
  141. // return false;
  142. // }
  143. if (!(
  144. process_record_kb(keycode, record) &&
  145. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  146. process_midi(keycode, record) &&
  147. #endif
  148. #ifdef AUDIO_ENABLE
  149. process_audio(keycode, record) &&
  150. #endif
  151. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  152. process_music(keycode, record) &&
  153. #endif
  154. #ifdef TAP_DANCE_ENABLE
  155. process_tap_dance(keycode, record) &&
  156. #endif
  157. #ifndef DISABLE_LEADER
  158. process_leader(keycode, record) &&
  159. #endif
  160. #ifndef DISABLE_CHORDING
  161. process_chording(keycode, record) &&
  162. #endif
  163. #ifdef COMBO_ENABLE
  164. process_combo(keycode, record) &&
  165. #endif
  166. #ifdef UNICODE_ENABLE
  167. process_unicode(keycode, record) &&
  168. #endif
  169. #ifdef UCIS_ENABLE
  170. process_ucis(keycode, record) &&
  171. #endif
  172. #ifdef PRINTING_ENABLE
  173. process_printer(keycode, record) &&
  174. #endif
  175. #ifdef UNICODEMAP_ENABLE
  176. process_unicode_map(keycode, record) &&
  177. #endif
  178. true)) {
  179. return false;
  180. }
  181. // Shift / paren setup
  182. switch(keycode) {
  183. case RESET:
  184. if (record->event.pressed) {
  185. reset_keyboard();
  186. }
  187. return false;
  188. break;
  189. case DEBUG:
  190. if (record->event.pressed) {
  191. print("\nDEBUG: enabled.\n");
  192. debug_enable = true;
  193. }
  194. return false;
  195. break;
  196. #ifdef FAUXCLICKY_ENABLE
  197. case FC_TOG:
  198. if (record->event.pressed) {
  199. FAUXCLICKY_TOGGLE;
  200. }
  201. return false;
  202. break;
  203. case FC_ON:
  204. if (record->event.pressed) {
  205. FAUXCLICKY_ON;
  206. }
  207. return false;
  208. break;
  209. case FC_OFF:
  210. if (record->event.pressed) {
  211. FAUXCLICKY_OFF;
  212. }
  213. return false;
  214. break;
  215. #endif
  216. #ifdef RGBLIGHT_ENABLE
  217. case RGB_TOG:
  218. if (record->event.pressed) {
  219. rgblight_toggle();
  220. }
  221. return false;
  222. break;
  223. case RGB_MOD:
  224. if (record->event.pressed) {
  225. rgblight_step();
  226. }
  227. return false;
  228. break;
  229. case RGB_HUI:
  230. if (record->event.pressed) {
  231. rgblight_increase_hue();
  232. }
  233. return false;
  234. break;
  235. case RGB_HUD:
  236. if (record->event.pressed) {
  237. rgblight_decrease_hue();
  238. }
  239. return false;
  240. break;
  241. case RGB_SAI:
  242. if (record->event.pressed) {
  243. rgblight_increase_sat();
  244. }
  245. return false;
  246. break;
  247. case RGB_SAD:
  248. if (record->event.pressed) {
  249. rgblight_decrease_sat();
  250. }
  251. return false;
  252. break;
  253. case RGB_VAI:
  254. if (record->event.pressed) {
  255. rgblight_increase_val();
  256. }
  257. return false;
  258. break;
  259. case RGB_VAD:
  260. if (record->event.pressed) {
  261. rgblight_decrease_val();
  262. }
  263. return false;
  264. break;
  265. #endif
  266. #ifdef PROTOCOL_LUFA
  267. case OUT_AUTO:
  268. if (record->event.pressed) {
  269. set_output(OUTPUT_AUTO);
  270. }
  271. return false;
  272. break;
  273. case OUT_USB:
  274. if (record->event.pressed) {
  275. set_output(OUTPUT_USB);
  276. }
  277. return false;
  278. break;
  279. #ifdef BLUETOOTH_ENABLE
  280. case OUT_BT:
  281. if (record->event.pressed) {
  282. set_output(OUTPUT_BLUETOOTH);
  283. }
  284. return false;
  285. break;
  286. #endif
  287. #endif
  288. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  289. if (record->event.pressed) {
  290. // MAGIC actions (BOOTMAGIC without the boot)
  291. if (!eeconfig_is_enabled()) {
  292. eeconfig_init();
  293. }
  294. /* keymap config */
  295. keymap_config.raw = eeconfig_read_keymap();
  296. switch (keycode)
  297. {
  298. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  299. keymap_config.swap_control_capslock = true;
  300. break;
  301. case MAGIC_CAPSLOCK_TO_CONTROL:
  302. keymap_config.capslock_to_control = true;
  303. break;
  304. case MAGIC_SWAP_LALT_LGUI:
  305. keymap_config.swap_lalt_lgui = true;
  306. break;
  307. case MAGIC_SWAP_RALT_RGUI:
  308. keymap_config.swap_ralt_rgui = true;
  309. break;
  310. case MAGIC_NO_GUI:
  311. keymap_config.no_gui = true;
  312. break;
  313. case MAGIC_SWAP_GRAVE_ESC:
  314. keymap_config.swap_grave_esc = true;
  315. break;
  316. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  317. keymap_config.swap_backslash_backspace = true;
  318. break;
  319. case MAGIC_HOST_NKRO:
  320. keymap_config.nkro = true;
  321. break;
  322. case MAGIC_SWAP_ALT_GUI:
  323. keymap_config.swap_lalt_lgui = true;
  324. keymap_config.swap_ralt_rgui = true;
  325. break;
  326. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  327. keymap_config.swap_control_capslock = false;
  328. break;
  329. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  330. keymap_config.capslock_to_control = false;
  331. break;
  332. case MAGIC_UNSWAP_LALT_LGUI:
  333. keymap_config.swap_lalt_lgui = false;
  334. break;
  335. case MAGIC_UNSWAP_RALT_RGUI:
  336. keymap_config.swap_ralt_rgui = false;
  337. break;
  338. case MAGIC_UNNO_GUI:
  339. keymap_config.no_gui = false;
  340. break;
  341. case MAGIC_UNSWAP_GRAVE_ESC:
  342. keymap_config.swap_grave_esc = false;
  343. break;
  344. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  345. keymap_config.swap_backslash_backspace = false;
  346. break;
  347. case MAGIC_UNHOST_NKRO:
  348. keymap_config.nkro = false;
  349. break;
  350. case MAGIC_UNSWAP_ALT_GUI:
  351. keymap_config.swap_lalt_lgui = false;
  352. keymap_config.swap_ralt_rgui = false;
  353. break;
  354. case MAGIC_TOGGLE_NKRO:
  355. keymap_config.nkro = !keymap_config.nkro;
  356. break;
  357. default:
  358. break;
  359. }
  360. eeconfig_update_keymap(keymap_config.raw);
  361. clear_keyboard(); // clear to prevent stuck keys
  362. return false;
  363. }
  364. break;
  365. case KC_LSPO: {
  366. if (record->event.pressed) {
  367. shift_interrupted[0] = false;
  368. scs_timer[0] = timer_read ();
  369. register_mods(MOD_BIT(KC_LSFT));
  370. }
  371. else {
  372. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  373. if (get_mods() & MOD_BIT(KC_RSFT)) {
  374. shift_interrupted[0] = true;
  375. shift_interrupted[1] = true;
  376. }
  377. #endif
  378. if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
  379. register_code(LSPO_KEY);
  380. unregister_code(LSPO_KEY);
  381. }
  382. unregister_mods(MOD_BIT(KC_LSFT));
  383. }
  384. return false;
  385. // break;
  386. }
  387. case KC_RSPC: {
  388. if (record->event.pressed) {
  389. shift_interrupted[1] = false;
  390. scs_timer[1] = timer_read ();
  391. register_mods(MOD_BIT(KC_RSFT));
  392. }
  393. else {
  394. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  395. if (get_mods() & MOD_BIT(KC_LSFT)) {
  396. shift_interrupted[0] = true;
  397. shift_interrupted[1] = true;
  398. }
  399. #endif
  400. if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  401. register_code(RSPC_KEY);
  402. unregister_code(RSPC_KEY);
  403. }
  404. unregister_mods(MOD_BIT(KC_RSFT));
  405. }
  406. return false;
  407. // break;
  408. }
  409. case GRAVE_ESC: {
  410. void (*method)(uint8_t) = (record->event.pressed) ? &add_key : &del_key;
  411. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  412. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  413. method(shifted ? KC_GRAVE : KC_ESCAPE);
  414. send_keyboard_report();
  415. }
  416. default: {
  417. shift_interrupted[0] = true;
  418. shift_interrupted[1] = true;
  419. break;
  420. }
  421. }
  422. return process_action_kb(record);
  423. }
  424. #if defined SENDSTRING_JIS_KEYCODE
  425. /* for users with JIS keyboards */
  426. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  427. 0, 0, 0, 0, 0, 0, 0, 0,
  428. 0, 0, 0, 0, 0, 0, 0, 0,
  429. 0, 0, 0, 0, 0, 0, 0, 0,
  430. 0, 0, 0, 0, 0, 0, 0, 0,
  431. 0, 1, 1, 1, 1, 1, 1, 1,
  432. 1, 1, 1, 1, 0, 0, 0, 0,
  433. 0, 0, 0, 0, 0, 0, 0, 0,
  434. 0, 0, 0, 0, 1, 1, 1, 1,
  435. 0, 1, 1, 1, 1, 1, 1, 1,
  436. 1, 1, 1, 1, 1, 1, 1, 1,
  437. 1, 1, 1, 1, 1, 1, 1, 1,
  438. 1, 1, 1, 0, 0, 0, 0, 1,
  439. 1, 0, 0, 0, 0, 0, 0, 0,
  440. 0, 0, 0, 0, 0, 0, 0, 0,
  441. 0, 0, 0, 0, 0, 0, 0, 0,
  442. 0, 0, 0, 1, 1, 1, 1, 0
  443. };
  444. #else
  445. /* for standard keycodes */
  446. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  447. 0, 0, 0, 0, 0, 0, 0, 0,
  448. 0, 0, 0, 0, 0, 0, 0, 0,
  449. 0, 0, 0, 0, 0, 0, 0, 0,
  450. 0, 0, 0, 0, 0, 0, 0, 0,
  451. 0, 1, 1, 1, 1, 1, 1, 0,
  452. 1, 1, 1, 1, 0, 0, 0, 0,
  453. 0, 0, 0, 0, 0, 0, 0, 0,
  454. 0, 0, 1, 0, 1, 0, 1, 1,
  455. 1, 1, 1, 1, 1, 1, 1, 1,
  456. 1, 1, 1, 1, 1, 1, 1, 1,
  457. 1, 1, 1, 1, 1, 1, 1, 1,
  458. 1, 1, 1, 0, 0, 0, 1, 1,
  459. 0, 0, 0, 0, 0, 0, 0, 0,
  460. 0, 0, 0, 0, 0, 0, 0, 0,
  461. 0, 0, 0, 0, 0, 0, 0, 0,
  462. 0, 0, 0, 1, 1, 1, 1, 0
  463. };
  464. #endif
  465. #if defined SENDSTRING_JIS_KEYCODE
  466. /* for users with JIS keyboards */
  467. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  468. 0, 0, 0, 0, 0, 0, 0, 0,
  469. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  470. 0, 0, 0, 0, 0, 0, 0, 0,
  471. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  472. KC_SPC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  473. KC_8, KC_9, KC_QUOT, KC_SCLN, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  474. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  475. KC_8, KC_9, KC_QUOT, KC_SCLN, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  476. KC_LBRC, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  477. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  478. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  479. KC_X, KC_Y, KC_Z, KC_RBRC, KC_JYEN, KC_BSLS, KC_EQL, KC_RO,
  480. KC_LBRC, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  481. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  482. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  483. KC_X, KC_Y, KC_Z, KC_RBRC, KC_JYEN, KC_BSLS, KC_EQL, KC_DEL,
  484. };
  485. #elif defined SENDSTRING_COLEMAK_KEYCODE
  486. /* for users whose OSes are set to Colemak */
  487. #include "keymap_colemak.h"
  488. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  489. 0, 0, 0, 0, 0, 0, 0, 0,
  490. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  491. 0, 0, 0, 0, 0, 0, 0, 0,
  492. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  493. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  494. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  495. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  496. KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  497. KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  498. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  499. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  500. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  501. KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  502. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  503. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  504. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  505. };
  506. #elif defined SENDSTRING_DVORAK_KEYCODE
  507. /* for users whose OSes are set to Dvorak */
  508. #include "keymap_dvorak.h"
  509. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  510. 0, 0, 0, 0, 0, 0, 0, 0,
  511. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  512. 0, 0, 0, 0, 0, 0, 0, 0,
  513. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  514. KC_SPC, DV_1, DV_QUOT, DV_3, DV_4, DV_5, DV_7, DV_QUOT,
  515. DV_9, DV_0, DV_8, DV_EQL, DV_COMM, DV_MINS, DV_DOT, DV_SLSH,
  516. DV_0, DV_1, DV_2, DV_3, DV_4, DV_5, DV_6, DV_7,
  517. DV_8, DV_9, DV_SCLN, DV_SCLN, DV_COMM, DV_EQL, DV_DOT, DV_SLSH,
  518. DV_2, DV_A, DV_B, DV_C, DV_D, DV_E, DV_F, DV_G,
  519. DV_H, DV_I, DV_J, DV_K, DV_L, DV_M, DV_N, DV_O,
  520. DV_P, DV_Q, DV_R, DV_S, DV_T, DV_U, DV_V, DV_W,
  521. DV_X, DV_Y, DV_Z, DV_LBRC, DV_BSLS, DV_RBRC, DV_6, DV_MINS,
  522. DV_GRV, DV_A, DV_B, DV_C, DV_D, DV_E, DV_F, DV_G,
  523. DV_H, DV_I, DV_J, DV_K, DV_L, DV_M, DV_N, DV_O,
  524. DV_P, DV_Q, DV_R, DV_S, DV_T, DV_U, DV_V, DV_W,
  525. DV_X, DV_Y, DV_Z, DV_LBRC, DV_BSLS, DV_RBRC, DV_GRV, KC_DEL
  526. };
  527. #else
  528. /* For users with default keyboard layout in OS */
  529. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  530. 0, 0, 0, 0, 0, 0, 0, 0,
  531. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  532. 0, 0, 0, 0, 0, 0, 0, 0,
  533. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  534. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  535. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  536. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  537. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  538. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  539. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  540. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  541. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  542. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  543. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  544. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  545. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  546. };
  547. #endif
  548. void send_string(const char *str) {
  549. send_string_with_delay(str, 0);
  550. }
  551. void send_string_with_delay(const char *str, uint8_t interval) {
  552. while (1) {
  553. uint8_t keycode;
  554. uint8_t ascii_code = pgm_read_byte(str);
  555. if (!ascii_code) break;
  556. keycode = pgm_read_byte(&ascii_to_keycode_lut[ascii_code]);
  557. if (pgm_read_byte(&ascii_to_shift_lut[ascii_code])) {
  558. register_code(KC_LSFT);
  559. register_code(keycode);
  560. unregister_code(keycode);
  561. unregister_code(KC_LSFT);
  562. }
  563. else {
  564. register_code(keycode);
  565. unregister_code(keycode);
  566. }
  567. ++str;
  568. // interval
  569. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  570. }
  571. }
  572. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  573. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  574. layer_on(layer3);
  575. } else {
  576. layer_off(layer3);
  577. }
  578. }
  579. void tap_random_base64(void) {
  580. #if defined(__AVR_ATmega32U4__)
  581. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  582. #else
  583. uint8_t key = rand() % 64;
  584. #endif
  585. switch (key) {
  586. case 0 ... 25:
  587. register_code(KC_LSFT);
  588. register_code(key + KC_A);
  589. unregister_code(key + KC_A);
  590. unregister_code(KC_LSFT);
  591. break;
  592. case 26 ... 51:
  593. register_code(key - 26 + KC_A);
  594. unregister_code(key - 26 + KC_A);
  595. break;
  596. case 52:
  597. register_code(KC_0);
  598. unregister_code(KC_0);
  599. break;
  600. case 53 ... 61:
  601. register_code(key - 53 + KC_1);
  602. unregister_code(key - 53 + KC_1);
  603. break;
  604. case 62:
  605. register_code(KC_LSFT);
  606. register_code(KC_EQL);
  607. unregister_code(KC_EQL);
  608. unregister_code(KC_LSFT);
  609. break;
  610. case 63:
  611. register_code(KC_SLSH);
  612. unregister_code(KC_SLSH);
  613. break;
  614. }
  615. }
  616. void matrix_init_quantum() {
  617. #ifdef BACKLIGHT_ENABLE
  618. backlight_init_ports();
  619. #endif
  620. matrix_init_kb();
  621. }
  622. void matrix_scan_quantum() {
  623. #ifdef AUDIO_ENABLE
  624. matrix_scan_music();
  625. #endif
  626. #ifdef TAP_DANCE_ENABLE
  627. matrix_scan_tap_dance();
  628. #endif
  629. #ifdef COMBO_ENABLE
  630. matrix_scan_combo();
  631. #endif
  632. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  633. backlight_task();
  634. #endif
  635. matrix_scan_kb();
  636. }
  637. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  638. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  639. #if BACKLIGHT_PIN == B7
  640. # define COM1x1 COM1C1
  641. # define OCR1x OCR1C
  642. #elif BACKLIGHT_PIN == B6
  643. # define COM1x1 COM1B1
  644. # define OCR1x OCR1B
  645. #elif BACKLIGHT_PIN == B5
  646. # define COM1x1 COM1A1
  647. # define OCR1x OCR1A
  648. #else
  649. # define NO_BACKLIGHT_CLOCK
  650. #endif
  651. #ifndef BACKLIGHT_ON_STATE
  652. #define BACKLIGHT_ON_STATE 0
  653. #endif
  654. __attribute__ ((weak))
  655. void backlight_init_ports(void)
  656. {
  657. // Setup backlight pin as output and output to on state.
  658. // DDRx |= n
  659. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  660. #if BACKLIGHT_ON_STATE == 0
  661. // PORTx &= ~n
  662. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  663. #else
  664. // PORTx |= n
  665. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  666. #endif
  667. #ifndef NO_BACKLIGHT_CLOCK
  668. // Use full 16-bit resolution.
  669. ICR1 = 0xFFFF;
  670. // I could write a wall of text here to explain... but TL;DW
  671. // Go read the ATmega32u4 datasheet.
  672. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  673. // Pin PB7 = OCR1C (Timer 1, Channel C)
  674. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  675. // (i.e. start high, go low when counter matches.)
  676. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  677. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  678. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  679. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  680. #endif
  681. backlight_init();
  682. #ifdef BACKLIGHT_BREATHING
  683. breathing_defaults();
  684. #endif
  685. }
  686. __attribute__ ((weak))
  687. void backlight_set(uint8_t level)
  688. {
  689. // Prevent backlight blink on lowest level
  690. // #if BACKLIGHT_ON_STATE == 0
  691. // // PORTx &= ~n
  692. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  693. // #else
  694. // // PORTx |= n
  695. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  696. // #endif
  697. if ( level == 0 ) {
  698. #ifndef NO_BACKLIGHT_CLOCK
  699. // Turn off PWM control on backlight pin, revert to output low.
  700. TCCR1A &= ~(_BV(COM1x1));
  701. OCR1x = 0x0;
  702. #else
  703. // #if BACKLIGHT_ON_STATE == 0
  704. // // PORTx |= n
  705. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  706. // #else
  707. // // PORTx &= ~n
  708. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  709. // #endif
  710. #endif
  711. }
  712. #ifndef NO_BACKLIGHT_CLOCK
  713. else if ( level == BACKLIGHT_LEVELS ) {
  714. // Turn on PWM control of backlight pin
  715. TCCR1A |= _BV(COM1x1);
  716. // Set the brightness
  717. OCR1x = 0xFFFF;
  718. }
  719. else {
  720. // Turn on PWM control of backlight pin
  721. TCCR1A |= _BV(COM1x1);
  722. // Set the brightness
  723. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  724. }
  725. #endif
  726. #ifdef BACKLIGHT_BREATHING
  727. breathing_intensity_default();
  728. #endif
  729. }
  730. uint8_t backlight_tick = 0;
  731. void backlight_task(void) {
  732. #ifdef NO_BACKLIGHT_CLOCK
  733. if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  734. #if BACKLIGHT_ON_STATE == 0
  735. // PORTx &= ~n
  736. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  737. #else
  738. // PORTx |= n
  739. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  740. #endif
  741. } else {
  742. #if BACKLIGHT_ON_STATE == 0
  743. // PORTx |= n
  744. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  745. #else
  746. // PORTx &= ~n
  747. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  748. #endif
  749. }
  750. backlight_tick = (backlight_tick + 1) % 16;
  751. #endif
  752. }
  753. #ifdef BACKLIGHT_BREATHING
  754. #define BREATHING_NO_HALT 0
  755. #define BREATHING_HALT_OFF 1
  756. #define BREATHING_HALT_ON 2
  757. static uint8_t breath_intensity;
  758. static uint8_t breath_speed;
  759. static uint16_t breathing_index;
  760. static uint8_t breathing_halt;
  761. void breathing_enable(void)
  762. {
  763. if (get_backlight_level() == 0)
  764. {
  765. breathing_index = 0;
  766. }
  767. else
  768. {
  769. // Set breathing_index to be at the midpoint (brightest point)
  770. breathing_index = 0x20 << breath_speed;
  771. }
  772. breathing_halt = BREATHING_NO_HALT;
  773. // Enable breathing interrupt
  774. TIMSK1 |= _BV(OCIE1A);
  775. }
  776. void breathing_pulse(void)
  777. {
  778. if (get_backlight_level() == 0)
  779. {
  780. breathing_index = 0;
  781. }
  782. else
  783. {
  784. // Set breathing_index to be at the midpoint + 1 (brightest point)
  785. breathing_index = 0x21 << breath_speed;
  786. }
  787. breathing_halt = BREATHING_HALT_ON;
  788. // Enable breathing interrupt
  789. TIMSK1 |= _BV(OCIE1A);
  790. }
  791. void breathing_disable(void)
  792. {
  793. // Disable breathing interrupt
  794. TIMSK1 &= ~_BV(OCIE1A);
  795. backlight_set(get_backlight_level());
  796. }
  797. void breathing_self_disable(void)
  798. {
  799. if (get_backlight_level() == 0)
  800. {
  801. breathing_halt = BREATHING_HALT_OFF;
  802. }
  803. else
  804. {
  805. breathing_halt = BREATHING_HALT_ON;
  806. }
  807. //backlight_set(get_backlight_level());
  808. }
  809. void breathing_toggle(void)
  810. {
  811. if (!is_breathing())
  812. {
  813. if (get_backlight_level() == 0)
  814. {
  815. breathing_index = 0;
  816. }
  817. else
  818. {
  819. // Set breathing_index to be at the midpoint + 1 (brightest point)
  820. breathing_index = 0x21 << breath_speed;
  821. }
  822. breathing_halt = BREATHING_NO_HALT;
  823. }
  824. // Toggle breathing interrupt
  825. TIMSK1 ^= _BV(OCIE1A);
  826. // Restore backlight level
  827. if (!is_breathing())
  828. {
  829. backlight_set(get_backlight_level());
  830. }
  831. }
  832. bool is_breathing(void)
  833. {
  834. return (TIMSK1 && _BV(OCIE1A));
  835. }
  836. void breathing_intensity_default(void)
  837. {
  838. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  839. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  840. }
  841. void breathing_intensity_set(uint8_t value)
  842. {
  843. breath_intensity = value;
  844. }
  845. void breathing_speed_default(void)
  846. {
  847. breath_speed = 4;
  848. }
  849. void breathing_speed_set(uint8_t value)
  850. {
  851. bool is_breathing_now = is_breathing();
  852. uint8_t old_breath_speed = breath_speed;
  853. if (is_breathing_now)
  854. {
  855. // Disable breathing interrupt
  856. TIMSK1 &= ~_BV(OCIE1A);
  857. }
  858. breath_speed = value;
  859. if (is_breathing_now)
  860. {
  861. // Adjust index to account for new speed
  862. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  863. // Enable breathing interrupt
  864. TIMSK1 |= _BV(OCIE1A);
  865. }
  866. }
  867. void breathing_speed_inc(uint8_t value)
  868. {
  869. if ((uint16_t)(breath_speed - value) > 10 )
  870. {
  871. breathing_speed_set(0);
  872. }
  873. else
  874. {
  875. breathing_speed_set(breath_speed - value);
  876. }
  877. }
  878. void breathing_speed_dec(uint8_t value)
  879. {
  880. if ((uint16_t)(breath_speed + value) > 10 )
  881. {
  882. breathing_speed_set(10);
  883. }
  884. else
  885. {
  886. breathing_speed_set(breath_speed + value);
  887. }
  888. }
  889. void breathing_defaults(void)
  890. {
  891. breathing_intensity_default();
  892. breathing_speed_default();
  893. breathing_halt = BREATHING_NO_HALT;
  894. }
  895. /* Breathing Sleep LED brighness(PWM On period) table
  896. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  897. *
  898. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  899. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  900. */
  901. static const uint8_t breathing_table[64] PROGMEM = {
  902. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  903. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  904. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  905. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  906. };
  907. ISR(TIMER1_COMPA_vect)
  908. {
  909. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  910. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  911. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  912. {
  913. // Disable breathing interrupt
  914. TIMSK1 &= ~_BV(OCIE1A);
  915. }
  916. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  917. }
  918. #endif // breathing
  919. #else // backlight
  920. __attribute__ ((weak))
  921. void backlight_init_ports(void)
  922. {
  923. }
  924. __attribute__ ((weak))
  925. void backlight_set(uint8_t level)
  926. {
  927. }
  928. #endif // backlight
  929. // Functions for spitting out values
  930. //
  931. void send_dword(uint32_t number) { // this might not actually work
  932. uint16_t word = (number >> 16);
  933. send_word(word);
  934. send_word(number & 0xFFFFUL);
  935. }
  936. void send_word(uint16_t number) {
  937. uint8_t byte = number >> 8;
  938. send_byte(byte);
  939. send_byte(number & 0xFF);
  940. }
  941. void send_byte(uint8_t number) {
  942. uint8_t nibble = number >> 4;
  943. send_nibble(nibble);
  944. send_nibble(number & 0xF);
  945. }
  946. void send_nibble(uint8_t number) {
  947. switch (number) {
  948. case 0:
  949. register_code(KC_0);
  950. unregister_code(KC_0);
  951. break;
  952. case 1 ... 9:
  953. register_code(KC_1 + (number - 1));
  954. unregister_code(KC_1 + (number - 1));
  955. break;
  956. case 0xA ... 0xF:
  957. register_code(KC_A + (number - 0xA));
  958. unregister_code(KC_A + (number - 0xA));
  959. break;
  960. }
  961. }
  962. __attribute__((weak))
  963. uint16_t hex_to_keycode(uint8_t hex)
  964. {
  965. if (hex == 0x0) {
  966. return KC_0;
  967. } else if (hex < 0xA) {
  968. return KC_1 + (hex - 0x1);
  969. } else {
  970. return KC_A + (hex - 0xA);
  971. }
  972. }
  973. void api_send_unicode(uint32_t unicode) {
  974. #ifdef API_ENABLE
  975. uint8_t chunk[4];
  976. dword_to_bytes(unicode, chunk);
  977. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  978. #endif
  979. }
  980. __attribute__ ((weak))
  981. void led_set_user(uint8_t usb_led) {
  982. }
  983. __attribute__ ((weak))
  984. void led_set_kb(uint8_t usb_led) {
  985. led_set_user(usb_led);
  986. }
  987. __attribute__ ((weak))
  988. void led_init_ports(void)
  989. {
  990. }
  991. __attribute__ ((weak))
  992. void led_set(uint8_t usb_led)
  993. {
  994. // Example LED Code
  995. //
  996. // // Using PE6 Caps Lock LED
  997. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  998. // {
  999. // // Output high.
  1000. // DDRE |= (1<<6);
  1001. // PORTE |= (1<<6);
  1002. // }
  1003. // else
  1004. // {
  1005. // // Output low.
  1006. // DDRE &= ~(1<<6);
  1007. // PORTE &= ~(1<<6);
  1008. // }
  1009. led_set_kb(usb_led);
  1010. }
  1011. //------------------------------------------------------------------------------
  1012. // Override these functions in your keymap file to play different tunes on
  1013. // different events such as startup and bootloader jump
  1014. __attribute__ ((weak))
  1015. void startup_user() {}
  1016. __attribute__ ((weak))
  1017. void shutdown_user() {}
  1018. //------------------------------------------------------------------------------