quantum.c 23 KB

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