cu75.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.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. setPinInput(B5);
  27. writePinHigh(B5);
  28. #else
  29. // If we're not using the audio pin, drive it low
  30. setPinOutput(C6);
  31. writePinLow(C6);
  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 == QK_BOOT) {
  87. reset_keyboard_kb();
  88. } else {
  89. }
  90. return process_record_user(keycode, record);
  91. }
  92. void reset_keyboard_kb(void){
  93. #ifdef WATCHDOG_ENABLE
  94. MCUSR = 0;
  95. wdt_disable();
  96. wdt_reset();
  97. #endif
  98. reset_keyboard();
  99. }
  100. // LFK lighting info
  101. const uint8_t switch_matrices[] = {0, 1};
  102. const uint8_t rgb_matrices[] = {6, 7};
  103. const uint8_t rgb_sequence[] = {
  104. 24, 23, 22, 21, 20, 19, 18, 17, 1, 2, 3, 4, 5,
  105. 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 9
  106. };