lfkpad.c 4.2 KB

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