quantum.c 38 KB

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