lfkpad.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <avr/sfr_defs.h>
  2. #include <avr/timer_avr.h>
  3. #include <avr/wdt.h>
  4. #include "lfkpad.h"
  5. #include "keymap.h"
  6. #include "issi.h"
  7. #include "TWIlib.h"
  8. #include "lighting.h"
  9. #include "debug.h"
  10. #include "quantum.h"
  11. uint16_t click_hz = CLICK_HZ;
  12. uint16_t click_time = CLICK_MS;
  13. uint8_t click_toggle = CLICK_ENABLED;
  14. void matrix_init_kb(void)
  15. {
  16. matrix_init_user();
  17. #ifndef AUDIO_ENABLE
  18. // If we're not using the audio pin, drive it low
  19. sbi(DDRC, 6);
  20. cbi(PORTC, 6);
  21. #endif
  22. #ifdef ISSI_ENABLE
  23. issi_init();
  24. #endif
  25. #ifdef WATCHDOG_ENABLE
  26. // This is done after turning the layer LED red, if we're caught in a loop
  27. // we should get a flashing red light
  28. wdt_enable(WDTO_500MS);
  29. #endif
  30. }
  31. void matrix_scan_kb(void)
  32. {
  33. #ifdef WATCHDOG_ENABLE
  34. wdt_reset();
  35. #endif
  36. #ifdef ISSI_ENABLE
  37. // switch/underglow lighting update
  38. static uint32_t issi_device = 0;
  39. static uint32_t twi_last_ready = 0;
  40. if(twi_last_ready > 1000){
  41. // Its been way too long since the last ISSI update, reset the I2C bus and start again
  42. dprintf("TWI failed to recover, TWI re-init\n");
  43. twi_last_ready = 0;
  44. TWIInit();
  45. force_issi_refresh();
  46. }
  47. if(isTWIReady()){
  48. twi_last_ready = 0;
  49. // If the i2c bus is available, kick off the issi update, alternate between devices
  50. update_issi(issi_device, issi_device);
  51. if(issi_device){
  52. issi_device = 0;
  53. }else{
  54. issi_device = 3;
  55. }
  56. }else{
  57. twi_last_ready++;
  58. }
  59. #endif
  60. matrix_scan_user();
  61. }
  62. void click(uint16_t freq, uint16_t duration){
  63. #ifdef AUDIO_ENABLE
  64. if(freq >= 100 && freq <= 20000 && duration < 100){
  65. play_note(freq, 10);
  66. for (uint16_t i = 0; i < duration; i++){
  67. _delay_ms(1);
  68. }
  69. stop_all_notes();
  70. }
  71. #endif
  72. }
  73. bool process_record_kb(uint16_t keycode, keyrecord_t* record)
  74. {
  75. if (click_toggle && record->event.pressed){
  76. click(click_hz, click_time);
  77. }
  78. if (keycode == RESET) {
  79. reset_keyboard_kb();
  80. } else {
  81. }
  82. return process_record_user(keycode, record);
  83. }
  84. void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
  85. {
  86. #ifdef AUDIO_ENABLE
  87. int8_t sign = 1;
  88. #endif
  89. if(id == LFK_ESC_TILDE){
  90. // Send ~ on shift-esc
  91. void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
  92. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  93. if(layer_state == 0){
  94. method(shifted ? KC_GRAVE : KC_ESCAPE);
  95. }else{
  96. method(shifted ? KC_ESCAPE : KC_GRAVE);
  97. }
  98. send_keyboard_report();
  99. }else if(event->event.pressed){
  100. switch(id){
  101. case LFK_SET_DEFAULT_LAYER:
  102. // set/save the current base layer to eeprom, falls through to LFK_CLEAR
  103. eeconfig_update_default_layer(1UL << opt);
  104. default_layer_set(1UL << opt);
  105. case LFK_CLEAR:
  106. // Go back to default layer
  107. layer_clear();
  108. break;
  109. #ifdef AUDIO_ENABLE
  110. case LFK_CLICK_FREQ_LOWER:
  111. sign = -1; // continue to next statement
  112. case LFK_CLICK_FREQ_HIGHER:
  113. click_hz += sign * 100;
  114. click(click_hz, click_time);
  115. break;
  116. case LFK_CLICK_TOGGLE:
  117. if(click_toggle){
  118. click_toggle = 0;
  119. click(4000, 100);
  120. click(1000, 100);
  121. }else{
  122. click_toggle = 1;
  123. click(1000, 100);
  124. click(4000, 100);
  125. }
  126. break;
  127. case LFK_CLICK_TIME_SHORTER:
  128. sign = -1; // continue to next statement
  129. case LFK_CLICK_TIME_LONGER:
  130. click_time += sign;
  131. click(click_hz, click_time);
  132. break;
  133. #endif
  134. }
  135. }
  136. }
  137. void reset_keyboard_kb(){
  138. #ifdef WATCHDOG_ENABLE
  139. MCUSR = 0;
  140. wdt_disable();
  141. wdt_reset();
  142. #endif
  143. reset_keyboard();
  144. }
  145. void led_set_kb(uint8_t usb_led)
  146. {
  147. led_set_user(usb_led);
  148. }
  149. // LFK lighting info
  150. const uint8_t rgb_matrices[] = {0, 1};
  151. const uint8_t rgb_sequence[] = {
  152. 32, 1, 2, 3,
  153. 31, 30, 5, 6,
  154. 28, 27, 7,
  155. 17, 18, 9, 8,
  156. 19, 21, 11,
  157. 22, 14, 12,
  158. 16, 26,
  159. 4, 25,
  160. 13, 24,
  161. 20
  162. };