quantum.c 32 KB

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