quantum.c 46 KB

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