quantum.c 45 KB

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