quantum.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. #include "quantum.h"
  2. #include "outputselect.h"
  3. #ifndef TAPPING_TERM
  4. #define TAPPING_TERM 200
  5. #endif
  6. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  7. switch (code) {
  8. case QK_MODS ... QK_MODS_MAX:
  9. break;
  10. default:
  11. return;
  12. }
  13. if (code & QK_LCTL)
  14. f(KC_LCTL);
  15. if (code & QK_LSFT)
  16. f(KC_LSFT);
  17. if (code & QK_LALT)
  18. f(KC_LALT);
  19. if (code & QK_LGUI)
  20. f(KC_LGUI);
  21. if (code < QK_RMODS_MIN) return;
  22. if (code & QK_RCTL)
  23. f(KC_RCTL);
  24. if (code & QK_RSFT)
  25. f(KC_RSFT);
  26. if (code & QK_RALT)
  27. f(KC_RALT);
  28. if (code & QK_RGUI)
  29. f(KC_RGUI);
  30. }
  31. void register_code16 (uint16_t code) {
  32. do_code16 (code, register_code);
  33. register_code (code);
  34. }
  35. void unregister_code16 (uint16_t code) {
  36. unregister_code (code);
  37. do_code16 (code, unregister_code);
  38. }
  39. __attribute__ ((weak))
  40. bool process_action_kb(keyrecord_t *record) {
  41. return true;
  42. }
  43. __attribute__ ((weak))
  44. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  45. return process_record_user(keycode, record);
  46. }
  47. __attribute__ ((weak))
  48. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  49. return true;
  50. }
  51. void reset_keyboard(void) {
  52. clear_keyboard();
  53. #ifdef AUDIO_ENABLE
  54. stop_all_notes();
  55. shutdown_user();
  56. #endif
  57. wait_ms(250);
  58. #ifdef CATERINA_BOOTLOADER
  59. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  60. #endif
  61. bootloader_jump();
  62. }
  63. // Shift / paren setup
  64. #ifndef LSPO_KEY
  65. #define LSPO_KEY KC_9
  66. #endif
  67. #ifndef RSPC_KEY
  68. #define RSPC_KEY KC_0
  69. #endif
  70. static bool shift_interrupted[2] = {0, 0};
  71. static uint16_t scs_timer = 0;
  72. bool process_record_quantum(keyrecord_t *record) {
  73. /* This gets the keycode from the key pressed */
  74. keypos_t key = record->event.key;
  75. uint16_t keycode;
  76. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  77. /* TODO: Use store_or_get_action() or a similar function. */
  78. if (!disable_action_cache) {
  79. uint8_t layer;
  80. if (record->event.pressed) {
  81. layer = layer_switch_get_layer(key);
  82. update_source_layers_cache(key, layer);
  83. } else {
  84. layer = read_source_layers_cache(key);
  85. }
  86. keycode = keymap_key_to_keycode(layer, key);
  87. } else
  88. #endif
  89. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  90. // This is how you use actions here
  91. // if (keycode == KC_LEAD) {
  92. // action_t action;
  93. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  94. // process_action(record, action);
  95. // return false;
  96. // }
  97. if (!(
  98. process_record_kb(keycode, record) &&
  99. #ifdef MIDI_ENABLE
  100. process_midi(keycode, record) &&
  101. #endif
  102. #ifdef AUDIO_ENABLE
  103. process_music(keycode, record) &&
  104. #endif
  105. #ifdef TAP_DANCE_ENABLE
  106. process_tap_dance(keycode, record) &&
  107. #endif
  108. #ifndef DISABLE_LEADER
  109. process_leader(keycode, record) &&
  110. #endif
  111. #ifndef DISABLE_CHORDING
  112. process_chording(keycode, record) &&
  113. #endif
  114. #ifdef UNICODE_ENABLE
  115. process_unicode(keycode, record) &&
  116. #endif
  117. #ifdef UCIS_ENABLE
  118. process_ucis(keycode, record) &&
  119. #endif
  120. #ifdef PRINTING_ENABLE
  121. process_printer(keycode, record) &&
  122. #endif
  123. #ifdef UNICODEMAP_ENABLE
  124. process_unicode_map(keycode, record) &&
  125. #endif
  126. true)) {
  127. return false;
  128. }
  129. // Shift / paren setup
  130. switch(keycode) {
  131. case RESET:
  132. if (record->event.pressed) {
  133. reset_keyboard();
  134. }
  135. return false;
  136. break;
  137. case DEBUG:
  138. if (record->event.pressed) {
  139. print("\nDEBUG: enabled.\n");
  140. debug_enable = true;
  141. }
  142. return false;
  143. break;
  144. #ifdef RGBLIGHT_ENABLE
  145. case RGB_TOG:
  146. if (record->event.pressed) {
  147. rgblight_toggle();
  148. }
  149. return false;
  150. break;
  151. case RGB_MOD:
  152. if (record->event.pressed) {
  153. rgblight_step();
  154. }
  155. return false;
  156. break;
  157. case RGB_HUI:
  158. if (record->event.pressed) {
  159. rgblight_increase_hue();
  160. }
  161. return false;
  162. break;
  163. case RGB_HUD:
  164. if (record->event.pressed) {
  165. rgblight_decrease_hue();
  166. }
  167. return false;
  168. break;
  169. case RGB_SAI:
  170. if (record->event.pressed) {
  171. rgblight_increase_sat();
  172. }
  173. return false;
  174. break;
  175. case RGB_SAD:
  176. if (record->event.pressed) {
  177. rgblight_decrease_sat();
  178. }
  179. return false;
  180. break;
  181. case RGB_VAI:
  182. if (record->event.pressed) {
  183. rgblight_increase_val();
  184. }
  185. return false;
  186. break;
  187. case RGB_VAD:
  188. if (record->event.pressed) {
  189. rgblight_decrease_val();
  190. }
  191. return false;
  192. break;
  193. #endif
  194. case OUT_AUTO:
  195. if (record->event.pressed) {
  196. set_output(OUTPUT_AUTO);
  197. }
  198. return false;
  199. break;
  200. case OUT_USB:
  201. if (record->event.pressed) {
  202. set_output(OUTPUT_USB);
  203. }
  204. return false;
  205. break;
  206. #ifdef BLUETOOTH_ENABLE
  207. case OUT_BT:
  208. if (record->event.pressed) {
  209. set_output(OUTPUT_BLUETOOTH);
  210. }
  211. return false;
  212. break;
  213. #endif
  214. #ifdef ADAFRUIT_BLE_ENABLE
  215. case OUT_BLE:
  216. if (record->event.pressed) {
  217. set_output(OUTPUT_ADAFRUIT_BLE);
  218. }
  219. return false;
  220. break;
  221. #endif
  222. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  223. if (record->event.pressed) {
  224. // MAGIC actions (BOOTMAGIC without the boot)
  225. if (!eeconfig_is_enabled()) {
  226. eeconfig_init();
  227. }
  228. /* keymap config */
  229. keymap_config.raw = eeconfig_read_keymap();
  230. switch (keycode)
  231. {
  232. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  233. keymap_config.swap_control_capslock = true;
  234. break;
  235. case MAGIC_CAPSLOCK_TO_CONTROL:
  236. keymap_config.capslock_to_control = true;
  237. break;
  238. case MAGIC_SWAP_LALT_LGUI:
  239. keymap_config.swap_lalt_lgui = true;
  240. break;
  241. case MAGIC_SWAP_RALT_RGUI:
  242. keymap_config.swap_ralt_rgui = true;
  243. break;
  244. case MAGIC_NO_GUI:
  245. keymap_config.no_gui = true;
  246. break;
  247. case MAGIC_SWAP_GRAVE_ESC:
  248. keymap_config.swap_grave_esc = true;
  249. break;
  250. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  251. keymap_config.swap_backslash_backspace = true;
  252. break;
  253. case MAGIC_HOST_NKRO:
  254. keymap_config.nkro = true;
  255. break;
  256. case MAGIC_SWAP_ALT_GUI:
  257. keymap_config.swap_lalt_lgui = true;
  258. keymap_config.swap_ralt_rgui = true;
  259. break;
  260. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  261. keymap_config.swap_control_capslock = false;
  262. break;
  263. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  264. keymap_config.capslock_to_control = false;
  265. break;
  266. case MAGIC_UNSWAP_LALT_LGUI:
  267. keymap_config.swap_lalt_lgui = false;
  268. break;
  269. case MAGIC_UNSWAP_RALT_RGUI:
  270. keymap_config.swap_ralt_rgui = false;
  271. break;
  272. case MAGIC_UNNO_GUI:
  273. keymap_config.no_gui = false;
  274. break;
  275. case MAGIC_UNSWAP_GRAVE_ESC:
  276. keymap_config.swap_grave_esc = false;
  277. break;
  278. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  279. keymap_config.swap_backslash_backspace = false;
  280. break;
  281. case MAGIC_UNHOST_NKRO:
  282. keymap_config.nkro = false;
  283. break;
  284. case MAGIC_UNSWAP_ALT_GUI:
  285. keymap_config.swap_lalt_lgui = false;
  286. keymap_config.swap_ralt_rgui = false;
  287. break;
  288. case MAGIC_TOGGLE_NKRO:
  289. keymap_config.nkro = !keymap_config.nkro;
  290. break;
  291. default:
  292. break;
  293. }
  294. eeconfig_update_keymap(keymap_config.raw);
  295. clear_keyboard(); // clear to prevent stuck keys
  296. return false;
  297. }
  298. break;
  299. case KC_LSPO: {
  300. if (record->event.pressed) {
  301. shift_interrupted[0] = false;
  302. scs_timer = timer_read ();
  303. register_mods(MOD_BIT(KC_LSFT));
  304. }
  305. else {
  306. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  307. if (get_mods() & MOD_BIT(KC_RSFT)) {
  308. shift_interrupted[0] = true;
  309. shift_interrupted[1] = true;
  310. }
  311. #endif
  312. if (!shift_interrupted[0] && timer_elapsed(scs_timer) < TAPPING_TERM) {
  313. register_code(LSPO_KEY);
  314. unregister_code(LSPO_KEY);
  315. }
  316. unregister_mods(MOD_BIT(KC_LSFT));
  317. }
  318. return false;
  319. // break;
  320. }
  321. case KC_RSPC: {
  322. if (record->event.pressed) {
  323. shift_interrupted[1] = false;
  324. scs_timer = timer_read ();
  325. register_mods(MOD_BIT(KC_RSFT));
  326. }
  327. else {
  328. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  329. if (get_mods() & MOD_BIT(KC_LSFT)) {
  330. shift_interrupted[0] = true;
  331. shift_interrupted[1] = true;
  332. }
  333. #endif
  334. if (!shift_interrupted[1] && timer_elapsed(scs_timer) < TAPPING_TERM) {
  335. register_code(RSPC_KEY);
  336. unregister_code(RSPC_KEY);
  337. }
  338. unregister_mods(MOD_BIT(KC_RSFT));
  339. }
  340. return false;
  341. // break;
  342. }
  343. default: {
  344. shift_interrupted[0] = true;
  345. shift_interrupted[1] = true;
  346. break;
  347. }
  348. }
  349. return process_action_kb(record);
  350. }
  351. const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
  352. 0, 0, 0, 0, 0, 0, 0, 0,
  353. 0, 0, 0, 0, 0, 0, 0, 0,
  354. 0, 0, 0, 0, 0, 0, 0, 0,
  355. 0, 0, 0, 0, 0, 0, 0, 0,
  356. 0, 1, 1, 1, 1, 1, 1, 0,
  357. 1, 1, 1, 1, 0, 0, 0, 0,
  358. 0, 0, 0, 0, 0, 0, 0, 0,
  359. 0, 0, 1, 0, 1, 0, 1, 1,
  360. 1, 1, 1, 1, 1, 1, 1, 1,
  361. 1, 1, 1, 1, 1, 1, 1, 1,
  362. 1, 1, 1, 1, 1, 1, 1, 1,
  363. 1, 1, 1, 0, 0, 0, 1, 1,
  364. 0, 0, 0, 0, 0, 0, 0, 0,
  365. 0, 0, 0, 0, 0, 0, 0, 0,
  366. 0, 0, 0, 0, 0, 0, 0, 0,
  367. 0, 0, 0, 1, 1, 1, 1, 0
  368. };
  369. const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
  370. 0, 0, 0, 0, 0, 0, 0, 0,
  371. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  372. 0, 0, 0, 0, 0, 0, 0, 0,
  373. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  374. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  375. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  376. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  377. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  378. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  379. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  380. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  381. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  382. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  383. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  384. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  385. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  386. };
  387. /* for users whose OSes are set to Colemak */
  388. #if 0
  389. #include "keymap_colemak.h"
  390. const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
  391. 0, 0, 0, 0, 0, 0, 0, 0,
  392. 0, 0, 0, 0, 0, 0, 0, 0,
  393. 0, 0, 0, 0, 0, 0, 0, 0,
  394. 0, 0, 0, 0, 0, 0, 0, 0,
  395. 0, 1, 1, 1, 1, 1, 1, 0,
  396. 1, 1, 1, 1, 0, 0, 0, 0,
  397. 0, 0, 0, 0, 0, 0, 0, 0,
  398. 0, 0, 1, 0, 1, 0, 1, 1,
  399. 1, 1, 1, 1, 1, 1, 1, 1,
  400. 1, 1, 1, 1, 1, 1, 1, 1,
  401. 1, 1, 1, 1, 1, 1, 1, 1,
  402. 1, 1, 1, 0, 0, 0, 1, 1,
  403. 0, 0, 0, 0, 0, 0, 0, 0,
  404. 0, 0, 0, 0, 0, 0, 0, 0,
  405. 0, 0, 0, 0, 0, 0, 0, 0,
  406. 0, 0, 0, 1, 1, 1, 1, 0
  407. };
  408. const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
  409. 0, 0, 0, 0, 0, 0, 0, 0,
  410. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  411. 0, 0, 0, 0, 0, 0, 0, 0,
  412. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  413. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  414. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  415. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  416. KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  417. KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  418. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  419. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  420. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  421. KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  422. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  423. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  424. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  425. };
  426. #endif
  427. void send_string(const char *str) {
  428. while (1) {
  429. uint8_t keycode;
  430. uint8_t ascii_code = pgm_read_byte(str);
  431. if (!ascii_code) break;
  432. keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
  433. if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
  434. register_code(KC_LSFT);
  435. register_code(keycode);
  436. unregister_code(keycode);
  437. unregister_code(KC_LSFT);
  438. }
  439. else {
  440. register_code(keycode);
  441. unregister_code(keycode);
  442. }
  443. ++str;
  444. }
  445. }
  446. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  447. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  448. layer_on(layer3);
  449. } else {
  450. layer_off(layer3);
  451. }
  452. }
  453. void tap_random_base64(void) {
  454. #if defined(__AVR_ATmega32U4__)
  455. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  456. #else
  457. uint8_t key = rand() % 64;
  458. #endif
  459. switch (key) {
  460. case 0 ... 25:
  461. register_code(KC_LSFT);
  462. register_code(key + KC_A);
  463. unregister_code(key + KC_A);
  464. unregister_code(KC_LSFT);
  465. break;
  466. case 26 ... 51:
  467. register_code(key - 26 + KC_A);
  468. unregister_code(key - 26 + KC_A);
  469. break;
  470. case 52:
  471. register_code(KC_0);
  472. unregister_code(KC_0);
  473. break;
  474. case 53 ... 61:
  475. register_code(key - 53 + KC_1);
  476. unregister_code(key - 53 + KC_1);
  477. break;
  478. case 62:
  479. register_code(KC_LSFT);
  480. register_code(KC_EQL);
  481. unregister_code(KC_EQL);
  482. unregister_code(KC_LSFT);
  483. break;
  484. case 63:
  485. register_code(KC_SLSH);
  486. unregister_code(KC_SLSH);
  487. break;
  488. }
  489. }
  490. void matrix_init_quantum() {
  491. #ifdef BACKLIGHT_ENABLE
  492. backlight_init_ports();
  493. #endif
  494. matrix_init_kb();
  495. }
  496. void matrix_scan_quantum() {
  497. #ifdef AUDIO_ENABLE
  498. matrix_scan_music();
  499. #endif
  500. #ifdef TAP_DANCE_ENABLE
  501. matrix_scan_tap_dance();
  502. #endif
  503. matrix_scan_kb();
  504. }
  505. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  506. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  507. #if BACKLIGHT_PIN == B7
  508. # define COM1x1 COM1C1
  509. # define OCR1x OCR1C
  510. #elif BACKLIGHT_PIN == B6
  511. # define COM1x1 COM1B1
  512. # define OCR1x OCR1B
  513. #elif BACKLIGHT_PIN == B5
  514. # define COM1x1 COM1A1
  515. # define OCR1x OCR1A
  516. #else
  517. # error "Backlight pin not supported - use B5, B6, or B7"
  518. #endif
  519. __attribute__ ((weak))
  520. void backlight_init_ports(void)
  521. {
  522. // Setup backlight pin as output and output low.
  523. // DDRx |= n
  524. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  525. // PORTx &= ~n
  526. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  527. // Use full 16-bit resolution.
  528. ICR1 = 0xFFFF;
  529. // I could write a wall of text here to explain... but TL;DW
  530. // Go read the ATmega32u4 datasheet.
  531. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  532. // Pin PB7 = OCR1C (Timer 1, Channel C)
  533. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  534. // (i.e. start high, go low when counter matches.)
  535. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  536. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  537. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  538. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  539. backlight_init();
  540. #ifdef BACKLIGHT_BREATHING
  541. breathing_defaults();
  542. #endif
  543. }
  544. __attribute__ ((weak))
  545. void backlight_set(uint8_t level)
  546. {
  547. // Prevent backlight blink on lowest level
  548. // PORTx &= ~n
  549. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  550. if ( level == 0 ) {
  551. // Turn off PWM control on backlight pin, revert to output low.
  552. TCCR1A &= ~(_BV(COM1x1));
  553. OCR1x = 0x0;
  554. } else if ( level == BACKLIGHT_LEVELS ) {
  555. // Turn on PWM control of backlight pin
  556. TCCR1A |= _BV(COM1x1);
  557. // Set the brightness
  558. OCR1x = 0xFFFF;
  559. } else {
  560. // Turn on PWM control of backlight pin
  561. TCCR1A |= _BV(COM1x1);
  562. // Set the brightness
  563. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  564. }
  565. #ifdef BACKLIGHT_BREATHING
  566. breathing_intensity_default();
  567. #endif
  568. }
  569. #ifdef BACKLIGHT_BREATHING
  570. #define BREATHING_NO_HALT 0
  571. #define BREATHING_HALT_OFF 1
  572. #define BREATHING_HALT_ON 2
  573. static uint8_t breath_intensity;
  574. static uint8_t breath_speed;
  575. static uint16_t breathing_index;
  576. static uint8_t breathing_halt;
  577. void breathing_enable(void)
  578. {
  579. if (get_backlight_level() == 0)
  580. {
  581. breathing_index = 0;
  582. }
  583. else
  584. {
  585. // Set breathing_index to be at the midpoint (brightest point)
  586. breathing_index = 0x20 << breath_speed;
  587. }
  588. breathing_halt = BREATHING_NO_HALT;
  589. // Enable breathing interrupt
  590. TIMSK1 |= _BV(OCIE1A);
  591. }
  592. void breathing_pulse(void)
  593. {
  594. if (get_backlight_level() == 0)
  595. {
  596. breathing_index = 0;
  597. }
  598. else
  599. {
  600. // Set breathing_index to be at the midpoint + 1 (brightest point)
  601. breathing_index = 0x21 << breath_speed;
  602. }
  603. breathing_halt = BREATHING_HALT_ON;
  604. // Enable breathing interrupt
  605. TIMSK1 |= _BV(OCIE1A);
  606. }
  607. void breathing_disable(void)
  608. {
  609. // Disable breathing interrupt
  610. TIMSK1 &= ~_BV(OCIE1A);
  611. backlight_set(get_backlight_level());
  612. }
  613. void breathing_self_disable(void)
  614. {
  615. if (get_backlight_level() == 0)
  616. {
  617. breathing_halt = BREATHING_HALT_OFF;
  618. }
  619. else
  620. {
  621. breathing_halt = BREATHING_HALT_ON;
  622. }
  623. //backlight_set(get_backlight_level());
  624. }
  625. void breathing_toggle(void)
  626. {
  627. if (!is_breathing())
  628. {
  629. if (get_backlight_level() == 0)
  630. {
  631. breathing_index = 0;
  632. }
  633. else
  634. {
  635. // Set breathing_index to be at the midpoint + 1 (brightest point)
  636. breathing_index = 0x21 << breath_speed;
  637. }
  638. breathing_halt = BREATHING_NO_HALT;
  639. }
  640. // Toggle breathing interrupt
  641. TIMSK1 ^= _BV(OCIE1A);
  642. // Restore backlight level
  643. if (!is_breathing())
  644. {
  645. backlight_set(get_backlight_level());
  646. }
  647. }
  648. bool is_breathing(void)
  649. {
  650. return (TIMSK1 && _BV(OCIE1A));
  651. }
  652. void breathing_intensity_default(void)
  653. {
  654. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  655. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  656. }
  657. void breathing_intensity_set(uint8_t value)
  658. {
  659. breath_intensity = value;
  660. }
  661. void breathing_speed_default(void)
  662. {
  663. breath_speed = 4;
  664. }
  665. void breathing_speed_set(uint8_t value)
  666. {
  667. bool is_breathing_now = is_breathing();
  668. uint8_t old_breath_speed = breath_speed;
  669. if (is_breathing_now)
  670. {
  671. // Disable breathing interrupt
  672. TIMSK1 &= ~_BV(OCIE1A);
  673. }
  674. breath_speed = value;
  675. if (is_breathing_now)
  676. {
  677. // Adjust index to account for new speed
  678. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  679. // Enable breathing interrupt
  680. TIMSK1 |= _BV(OCIE1A);
  681. }
  682. }
  683. void breathing_speed_inc(uint8_t value)
  684. {
  685. if ((uint16_t)(breath_speed - value) > 10 )
  686. {
  687. breathing_speed_set(0);
  688. }
  689. else
  690. {
  691. breathing_speed_set(breath_speed - value);
  692. }
  693. }
  694. void breathing_speed_dec(uint8_t value)
  695. {
  696. if ((uint16_t)(breath_speed + value) > 10 )
  697. {
  698. breathing_speed_set(10);
  699. }
  700. else
  701. {
  702. breathing_speed_set(breath_speed + value);
  703. }
  704. }
  705. void breathing_defaults(void)
  706. {
  707. breathing_intensity_default();
  708. breathing_speed_default();
  709. breathing_halt = BREATHING_NO_HALT;
  710. }
  711. /* Breathing Sleep LED brighness(PWM On period) table
  712. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  713. *
  714. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  715. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  716. */
  717. static const uint8_t breathing_table[64] PROGMEM = {
  718. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  719. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  720. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  721. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  722. };
  723. ISR(TIMER1_COMPA_vect)
  724. {
  725. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  726. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  727. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  728. {
  729. // Disable breathing interrupt
  730. TIMSK1 &= ~_BV(OCIE1A);
  731. }
  732. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  733. }
  734. #endif // breathing
  735. #else // backlight
  736. __attribute__ ((weak))
  737. void backlight_init_ports(void)
  738. {
  739. }
  740. __attribute__ ((weak))
  741. void backlight_set(uint8_t level)
  742. {
  743. }
  744. #endif // backlight
  745. // Functions for spitting out values
  746. //
  747. void send_dword(uint32_t number) { // this might not actually work
  748. uint16_t word = (number >> 16);
  749. send_word(word);
  750. send_word(number & 0xFFFFUL);
  751. }
  752. void send_word(uint16_t number) {
  753. uint8_t byte = number >> 8;
  754. send_byte(byte);
  755. send_byte(number & 0xFF);
  756. }
  757. void send_byte(uint8_t number) {
  758. uint8_t nibble = number >> 4;
  759. send_nibble(nibble);
  760. send_nibble(number & 0xF);
  761. }
  762. void send_nibble(uint8_t number) {
  763. switch (number) {
  764. case 0:
  765. register_code(KC_0);
  766. unregister_code(KC_0);
  767. break;
  768. case 1 ... 9:
  769. register_code(KC_1 + (number - 1));
  770. unregister_code(KC_1 + (number - 1));
  771. break;
  772. case 0xA ... 0xF:
  773. register_code(KC_A + (number - 0xA));
  774. unregister_code(KC_A + (number - 0xA));
  775. break;
  776. }
  777. }
  778. void api_send_unicode(uint32_t unicode) {
  779. #ifdef API_ENABLE
  780. uint8_t chunk[4];
  781. dword_to_bytes(unicode, chunk);
  782. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  783. #endif
  784. }
  785. __attribute__ ((weak))
  786. void led_set_user(uint8_t usb_led) {
  787. }
  788. __attribute__ ((weak))
  789. void led_set_kb(uint8_t usb_led) {
  790. led_set_user(usb_led);
  791. }
  792. __attribute__ ((weak))
  793. void led_init_ports(void)
  794. {
  795. }
  796. __attribute__ ((weak))
  797. void led_set(uint8_t usb_led)
  798. {
  799. // Example LED Code
  800. //
  801. // // Using PE6 Caps Lock LED
  802. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  803. // {
  804. // // Output high.
  805. // DDRE |= (1<<6);
  806. // PORTE |= (1<<6);
  807. // }
  808. // else
  809. // {
  810. // // Output low.
  811. // DDRE &= ~(1<<6);
  812. // PORTE &= ~(1<<6);
  813. // }
  814. led_set_kb(usb_led);
  815. }
  816. //------------------------------------------------------------------------------
  817. // Override these functions in your keymap file to play different tunes on
  818. // different events such as startup and bootloader jump
  819. __attribute__ ((weak))
  820. void startup_user() {}
  821. __attribute__ ((weak))
  822. void shutdown_user() {}
  823. //------------------------------------------------------------------------------