quantum.c 43 KB

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