lfk78.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "lfk78.h"
  2. #include <avr/timer_avr.h>
  3. #include <avr/wdt.h>
  4. #include "audio.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. __attribute__((weak))
  12. const Layer_Info layer_info[] = {
  13. // Layer Mask Red Green Blue
  14. { 0x00000000, 0xFFFFFFFF, { 0x0000, 0x0FFF, 0x0000 } }, // base layer - green
  15. { 0x00000002, 0xFFFFFFFE, { 0x0000, 0x0000, 0x0FFF } }, // function layer - blue
  16. { 0x00000004, 0xFFFFFFFC, { 0x0FFF, 0x0000, 0x0FFF } }, // settings layer - magenta
  17. { 0xFFFFFFFF, 0xFFFFFFFF, { 0x0FFF, 0x0FFF, 0x0FFF } } // unknown layer - REQUIRED - white
  18. };
  19. void matrix_init_kb(void) {
  20. matrix_init_user();
  21. // Configure the Layer LED
  22. // Set up 16 bit PWM: Fast PWM, mode 14, inverted
  23. TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(COM1B1) | _BV(COM1B0) | _BV(COM1C1) | _BV(COM1C0) | _BV(WGM11);
  24. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
  25. ICR1 = 0xFFFF;
  26. // PWM values - 0xFFFF = off, 0x0000 = max
  27. OCR1A = 0x0FFF; // B5 - Red
  28. OCR1B = 0x0000; // B6 - Green
  29. OCR1C = 0x0000; // B7 - Blue
  30. // Set as output
  31. setPinOutput(B5);
  32. setPinOutput(B6);
  33. setPinOutput(B7);
  34. #ifndef AUDIO_ENABLE
  35. // If we're not using the audio pin, drive it low
  36. setPinOutput(C6);
  37. writePinLow(C6);
  38. #endif
  39. #ifdef ISSI_ENABLE
  40. issi_init();
  41. #endif
  42. #ifdef WATCHDOG_ENABLE
  43. // This is done after turning the layer LED red, if we're caught in a loop
  44. // we should get a flashing red light
  45. wdt_enable(WDTO_500MS);
  46. #endif
  47. }
  48. void matrix_scan_kb(void) {
  49. #ifdef WATCHDOG_ENABLE
  50. wdt_reset();
  51. #endif
  52. #ifdef ISSI_ENABLE
  53. // switch/underglow lighting update
  54. static uint32_t issi_device = 0;
  55. static uint32_t twi_last_ready = 0;
  56. if (twi_last_ready > 1000) {
  57. // It's been way too long since the last ISSI update, reset the I2C bus and start again
  58. dprintf("TWI failed to recover, TWI re-init\n");
  59. twi_last_ready = 0;
  60. TWIInit();
  61. force_issi_refresh();
  62. }
  63. if (isTWIReady()) {
  64. twi_last_ready = 0;
  65. // If the i2c bus is available, kick off the issi update, alternate between devices
  66. update_issi(issi_device, issi_device);
  67. if (issi_device) {
  68. issi_device = 0;
  69. } else {
  70. issi_device = 3;
  71. }
  72. } else {
  73. twi_last_ready++;
  74. }
  75. #endif
  76. // Update layer indicator LED
  77. //
  78. // Not sure how else to reliably do this... TMK has the 'hook_layer_change'
  79. // but can't find QMK equiv
  80. static uint32_t layer_indicator = -1;
  81. if (layer_indicator != layer_state) {
  82. for (uint32_t i = 0; ; i++) {
  83. // the layer_info list should end with layer 0xFFFFFFFF
  84. // it will break this out of the loop and define the unknown layer color
  85. if ((layer_info[i].layer == (layer_state & layer_info[i].mask)) || (layer_info[i].layer == 0xFFFFFFFF)) {
  86. OCR1A = layer_info[i].color.red;
  87. OCR1B = layer_info[i].color.green;
  88. OCR1C = layer_info[i].color.blue;
  89. layer_indicator = layer_state;
  90. break;
  91. }
  92. }
  93. }
  94. matrix_scan_user();
  95. }
  96. void click(uint16_t freq, uint16_t duration) {
  97. #ifdef AUDIO_ENABLE
  98. if (freq >= 100 && freq <= 20000 && duration < 100) {
  99. play_note(freq, 10);
  100. for (uint16_t i = 0; i < duration; i++) {
  101. _delay_ms(1);
  102. }
  103. stop_all_notes();
  104. }
  105. #endif
  106. }
  107. bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
  108. if (click_toggle && record->event.pressed) {
  109. click(click_hz, click_time);
  110. }
  111. if (keycode == RESET) {
  112. reset_keyboard_kb();
  113. }
  114. return process_record_user(keycode, record);
  115. }
  116. void action_function(keyrecord_t *event, uint8_t id, uint8_t opt) {
  117. #ifdef AUDIO_ENABLE
  118. int8_t sign = 1;
  119. #endif
  120. if (id == LFK_ESC_TILDE) {
  121. // Send ~ on shift-esc
  122. void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
  123. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  124. if (layer_state == 0) {
  125. method(shifted ? KC_GRAVE : KC_ESCAPE);
  126. } else {
  127. method(shifted ? KC_ESCAPE : KC_GRAVE);
  128. }
  129. send_keyboard_report();
  130. } else if (event->event.pressed) {
  131. switch (id) {
  132. case LFK_SET_DEFAULT_LAYER:
  133. // set/save the current base layer to eeprom, falls through to LFK_CLEAR
  134. eeconfig_update_default_layer(1UL << opt);
  135. default_layer_set(1UL << opt);
  136. case LFK_CLEAR:
  137. // Go back to default layer
  138. layer_clear();
  139. break;
  140. #ifdef ISSI_ENABLE
  141. case LFK_LED_TEST:
  142. led_test();
  143. break;
  144. #endif
  145. #ifdef AUDIO_ENABLE
  146. case LFK_CLICK_FREQ_LOWER:
  147. sign = -1; // continue to next statement
  148. case LFK_CLICK_FREQ_HIGHER:
  149. click_hz += sign * 100;
  150. click(click_hz, click_time);
  151. break;
  152. case LFK_CLICK_TOGGLE:
  153. if (click_toggle) {
  154. click_toggle = 0;
  155. click(4000, 100);
  156. click(1000, 100);
  157. } else {
  158. click_toggle = 1;
  159. click(1000, 100);
  160. click(4000, 100);
  161. }
  162. break;
  163. case LFK_CLICK_TIME_SHORTER:
  164. sign = -1; // continue to next statement
  165. case LFK_CLICK_TIME_LONGER:
  166. click_time += sign;
  167. click(click_hz, click_time);
  168. break;
  169. #endif
  170. case LFK_DEBUG_SETTINGS:
  171. dprintf("Click:\n");
  172. dprintf(" toggle: %d\n", click_toggle);
  173. dprintf(" freq(hz): %d\n", click_hz);
  174. dprintf(" duration(ms): %d\n", click_time);
  175. break;
  176. }
  177. }
  178. }
  179. void reset_keyboard_kb() {
  180. #ifdef WATCHDOG_ENABLE
  181. MCUSR = 0;
  182. wdt_disable();
  183. wdt_reset();
  184. #endif
  185. OCR1A = 0x0000; // B5 - Red
  186. OCR1B = 0x0FFF; // B6 - Green
  187. OCR1C = 0x0FFF; // B7 - Blue
  188. reset_keyboard();
  189. }
  190. void led_set_kb(uint8_t usb_led) {
  191. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  192. #ifdef ISSI_ENABLE
  193. # ifdef CAPSLOCK_LED
  194. if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
  195. activateLED(0, 3, 7, 255);
  196. } else {
  197. activateLED(0, 3, 7, 0);
  198. }
  199. # endif // CAPSLOCK_LED
  200. #endif // ISS_ENABLE
  201. led_set_user(usb_led);
  202. }
  203. // LFK lighting info
  204. const uint8_t switch_matrices[] = { 0, 1 };
  205. const uint8_t rgb_matrices[] = { 6, 7 };
  206. const uint8_t rgb_sequence[] = {
  207. 12, 11, 10, 9, 16, 32, 31, 30, 28, 25, 24, 22, 21,
  208. 20, 19, 18, 17, 1, 2, 3, 4, 5, 6, 7, 8, 14, 13
  209. };
  210. // Maps switch LEDs from Row/Col to ISSI matrix.
  211. // Value breakdown:
  212. // Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  213. // / \ ISSI Col | ISSI Row |
  214. // matrix idx
  215. const uint8_t switch_leds[MATRIX_ROWS][MATRIX_COLS] = LAYOUT(
  216. 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91,
  217. 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0xA9, 0xA8, 0xA7, 0xA6, 0xA5, 0xA4, 0xA3, 0xA2, 0xA1,
  218. 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0xB9, 0xB8, 0xB7, 0xB6, 0xB5, 0xB3,
  219. 0x49, 0x48, 0x47, 0x45, 0x44, 0x43, 0x42, 0x41, 0xC9, 0xC8, 0xC7, 0xC6, 0xC5, 0xC4, 0xC2,
  220. 0x59, 0x58, 0x57, 0x56, 0x55, 0x51, 0xD6, 0xE5, 0xE4, 0xE3, 0xE2, 0xE1
  221. );