quantum.c 38 KB

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