revb.c 5.3 KB

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