quantum.c 36 KB

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