lfk78.c 7.0 KB

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