You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.4 KiB

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