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.

179 lines
4.8 KiB

  1. #include <avr/sfr_defs.h>
  2. #include <avr/timer_avr.h>
  3. #include <avr/wdt.h>
  4. #include "cu75.h"
  5. #include "keymap.h"
  6. #include "debug.h"
  7. #include "../lfkeyboards/issi.h"
  8. #include "../lfkeyboards/TWIlib.h"
  9. #include "../lfkeyboards/lighting.h"
  10. #ifdef AUDIO_ENABLE
  11. float test_sound[][2] = SONG(STARTUP_SOUND);
  12. #include <audio/audio.h>
  13. #endif
  14. uint16_t click_hz = CLICK_HZ;
  15. uint16_t click_time = CLICK_MS;
  16. uint8_t click_toggle = CLICK_ENABLED;
  17. void matrix_init_kb(void)
  18. {
  19. // put your keyboard start-up code here
  20. // runs once when the firmware starts up
  21. matrix_init_user();
  22. #ifdef AUDIO_ENABLE
  23. audio_init();
  24. PLAY_SONG(test_sound);
  25. // Fix port B5
  26. cbi(DDRB, 5);
  27. sbi(PORTB, 5);
  28. #else
  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. }
  37. void matrix_scan_kb(void)
  38. {
  39. #ifdef WATCHDOG_ENABLE
  40. wdt_reset();
  41. #endif
  42. #ifdef ISSI_ENABLE
  43. // switch/underglow lighting update
  44. static uint32_t issi_device = 0;
  45. static uint32_t twi_last_ready = 0;
  46. if(twi_last_ready > 1000){
  47. // Its been way too long since the last ISSI update, reset the I2C bus and start again
  48. dprintf("TWI failed to recover, TWI re-init\n");
  49. twi_last_ready = 0;
  50. TWIInit();
  51. force_issi_refresh();
  52. }
  53. if(isTWIReady()){
  54. twi_last_ready = 0;
  55. // If the i2c bus is available, kick off the issi update, alternate between devices
  56. update_issi(issi_device, issi_device);
  57. if(issi_device){
  58. issi_device = 0;
  59. }else{
  60. issi_device = 3;
  61. }
  62. }else{
  63. twi_last_ready++;
  64. }
  65. #endif
  66. matrix_scan_user();
  67. }
  68. void click(uint16_t freq, uint16_t duration){
  69. #ifdef AUDIO_ENABLE
  70. if(freq >= 100 && freq <= 20000 && duration < 100){
  71. play_note(freq, 10);
  72. for (uint16_t i = 0; i < duration; i++){
  73. _delay_ms(1);
  74. }
  75. stop_all_notes();
  76. }
  77. #endif
  78. }
  79. bool process_record_kb(uint16_t keycode, keyrecord_t* record)
  80. {
  81. // Test code that turns on the switch led for the key that is pressed
  82. // set_backlight_by_keymap(record->event.key.col, record->event.key.row);
  83. if (click_toggle && record->event.pressed){
  84. click(click_hz, click_time);
  85. }
  86. if (keycode == RESET) {
  87. reset_keyboard_kb();
  88. } else {
  89. }
  90. return process_record_user(keycode, record);
  91. }
  92. void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
  93. {
  94. #ifdef AUDIO_ENABLE
  95. int8_t sign = 1;
  96. #endif
  97. if(id == LFK_ESC_TILDE){
  98. // Send ~ on shift-esc
  99. void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
  100. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  101. method(shifted ? KC_GRAVE : KC_ESCAPE);
  102. send_keyboard_report();
  103. }else if(event->event.pressed){
  104. switch(id){
  105. case LFK_SET_DEFAULT_LAYER:
  106. // set/save the current base layer to eeprom, falls through to LFK_CLEAR
  107. eeconfig_update_default_layer(1UL << opt);
  108. default_layer_set(1UL << opt);
  109. case LFK_CLEAR:
  110. // Go back to default layer
  111. layer_clear();
  112. break;
  113. #ifdef AUDIO_ENABLE
  114. case LFK_CLICK_FREQ_LOWER:
  115. sign = -1; // continue to next statement
  116. case LFK_CLICK_FREQ_HIGHER:
  117. click_hz += sign * 100;
  118. click(click_hz, click_time);
  119. break;
  120. case LFK_CLICK_TOGGLE:
  121. if(click_toggle){
  122. click_toggle = 0;
  123. click(4000, 100);
  124. click(1000, 100);
  125. }else{
  126. click_toggle = 1;
  127. click(1000, 100);
  128. click(4000, 100);
  129. }
  130. break;
  131. case LFK_CLICK_TIME_SHORTER:
  132. sign = -1; // continue to next statement
  133. case LFK_CLICK_TIME_LONGER:
  134. click_time += sign;
  135. click(click_hz, click_time);
  136. break;
  137. #endif
  138. case LFK_DEBUG_SETTINGS:
  139. dprintf("Click:\n");
  140. dprintf(" toggle: %d\n", click_toggle);
  141. dprintf(" freq(hz): %d\n", click_hz);
  142. dprintf(" duration(ms): %d\n", click_time);
  143. break;
  144. }
  145. }
  146. }
  147. void reset_keyboard_kb(){
  148. #ifdef WATCHDOG_ENABLE
  149. MCUSR = 0;
  150. wdt_disable();
  151. wdt_reset();
  152. #endif
  153. reset_keyboard();
  154. }
  155. void led_set_kb(uint8_t usb_led)
  156. {
  157. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  158. led_set_user(usb_led);
  159. }
  160. // LFK lighting info
  161. const uint8_t switch_matrices[] = {0, 1};
  162. const uint8_t rgb_matrices[] = {6, 7};
  163. const uint8_t rgb_sequence[] = {
  164. 24, 23, 22, 21, 20, 19, 18, 17, 1, 2, 3, 4, 5,
  165. 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 9
  166. };