smk65.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <avr/sfr_defs.h>
  2. #include <avr/timer_avr.h>
  3. #include <avr/wdt.h>
  4. #include "smk65.h"
  5. #include "keymap.h"
  6. #include "debug.h"
  7. #include "issi.h"
  8. #include "TWIlib.h"
  9. #include "lighting.h"
  10. uint16_t click_hz = CLICK_HZ;
  11. uint16_t click_time = CLICK_MS;
  12. uint8_t click_toggle = CLICK_ENABLED;
  13. void matrix_init_kb(void)
  14. {
  15. matrix_init_user();
  16. #ifdef AUDIO_ENABLE
  17. // audio_init() sets PB5 to output and drives it low, which breaks our matrix
  18. // so reset PB5 to input
  19. cbi(DDRB, 5);
  20. sbi(PORTB, 5);
  21. #else
  22. // If we're not using the audio pin, drive it low
  23. sbi(DDRC, 6);
  24. cbi(PORTC, 6);
  25. #endif
  26. #ifdef ISSI_ENABLE
  27. issi_init();
  28. #endif
  29. }
  30. void matrix_scan_kb(void)
  31. {
  32. #ifdef WATCHDOG_ENABLE
  33. wdt_reset();
  34. #endif
  35. matrix_scan_user();
  36. }
  37. void click(uint16_t freq, uint16_t duration){
  38. #ifdef AUDIO_ENABLE
  39. if(freq >= 100 && freq <= 20000 && duration < 100){
  40. play_note(freq, 10);
  41. for (uint16_t i = 0; i < duration; i++){
  42. _delay_ms(1);
  43. }
  44. stop_all_notes();
  45. }
  46. #endif
  47. }
  48. bool process_record_kb(uint16_t keycode, keyrecord_t* record)
  49. {
  50. // Test code that turns on the switch led for the key that is pressed
  51. // set_backlight_by_keymap(record->event.key.col, record->event.key.row);
  52. if (click_toggle && record->event.pressed){
  53. click(click_hz, click_time);
  54. }
  55. if (keycode == RESET) {
  56. reset_keyboard_kb();
  57. } else {
  58. }
  59. return process_record_user(keycode, record);
  60. }
  61. void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
  62. {
  63. #ifdef AUDIO_ENABLE
  64. int8_t sign = 1;
  65. #endif
  66. if(id == LFK_ESC_TILDE){
  67. // Send ~ on shift-esc
  68. void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
  69. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  70. method(shifted ? KC_GRAVE : KC_ESCAPE);
  71. send_keyboard_report();
  72. }else if(event->event.pressed){
  73. switch(id){
  74. case LFK_SET_DEFAULT_LAYER:
  75. // set/save the current base layer to eeprom, falls through to LFK_CLEAR
  76. eeconfig_update_default_layer(1UL << opt);
  77. default_layer_set(1UL << opt);
  78. case LFK_CLEAR:
  79. // Go back to default layer
  80. layer_clear();
  81. break;
  82. #ifdef AUDIO_ENABLE
  83. case LFK_CLICK_FREQ_LOWER:
  84. sign = -1; // continue to next statement
  85. case LFK_CLICK_FREQ_HIGHER:
  86. click_hz += sign * 100;
  87. click(click_hz, click_time);
  88. break;
  89. case LFK_CLICK_TOGGLE:
  90. if(click_toggle){
  91. click_toggle = 0;
  92. click(4000, 100);
  93. click(1000, 100);
  94. }else{
  95. click_toggle = 1;
  96. click(1000, 100);
  97. click(4000, 100);
  98. }
  99. break;
  100. case LFK_CLICK_TIME_SHORTER:
  101. sign = -1; // continue to next statement
  102. case LFK_CLICK_TIME_LONGER:
  103. click_time += sign;
  104. click(click_hz, click_time);
  105. break;
  106. #endif
  107. case LFK_DEBUG_SETTINGS:
  108. dprintf("Click:\n");
  109. dprintf(" toggle: %d\n", click_toggle);
  110. dprintf(" freq(hz): %d\n", click_hz);
  111. dprintf(" duration(ms): %d\n", click_time);
  112. break;
  113. }
  114. }
  115. }
  116. void reset_keyboard_kb(){
  117. #ifdef WATCHDOG_ENABLE
  118. MCUSR = 0;
  119. wdt_disable();
  120. wdt_reset();
  121. #endif
  122. reset_keyboard();
  123. }
  124. void led_set_kb(uint8_t usb_led)
  125. {
  126. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  127. led_set_user(usb_led);
  128. }
  129. // LFK lighting info
  130. const uint8_t switch_matrices[] = {0, 1};
  131. const uint8_t rgb_matrices[] = {6, 7};
  132. // const uint8_t rgb_sequence[] = {
  133. // 14, 24, 23, 22, 21, 20, 19, 18, 26, 25, 28, 29,
  134. // 30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
  135. // };
  136. const uint8_t rgb_sequence[] = {
  137. 25, 28, 29,
  138. 30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  139. };
  140. // Maps switch LEDs from Row/Col to ISSI matrix.
  141. // Value breakdown:
  142. // Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  143. // / \ ISSI Col | ISSI Row |
  144. // matrix idx
  145. // const uint8_t switch_leds[MATRIX_ROWS][MATRIX_COLS] =
  146. // KEYMAP(
  147. // 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91,
  148. // 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0xA9, 0xA8, 0xA7, 0xA6, 0xA5, 0xA4, 0xA3, 0xA2, 0xA1,
  149. // 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0xB9, 0xB8, 0xB7, 0xB6, 0xB5, 0xB3,
  150. // 0x49, 0x48, 0x47, 0x45, 0x44, 0x43, 0x42, 0x41, 0xC9, 0xC8, 0xC7, 0xC6, 0xC5, 0xC4, 0xC2,
  151. // 0x59, 0x58, 0x57, 0x56, 0x55, 0x51, 0xD6, 0xE5, 0xE4, 0xE3, 0xE2, 0xE1);