quantum.c 36 KB

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