meira.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright 2017 Cole Markham, WoodKeys.click
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "meira.h"
  17. #include "issi.h"
  18. #include "TWIlib.h"
  19. #include "lighting.h"
  20. #include "quantum.h"
  21. #define BACKLIGHT_BREATHING
  22. extern void backlight_set(uint8_t level);
  23. #ifdef AUDIO_ENABLE
  24. float tone_startup[][2] = SONG(STARTUP_SOUND);
  25. float tone_goodbye[][2] = SONG(GOODBYE_SOUND);
  26. #endif
  27. void shutdown_user(void) {
  28. #ifdef AUDIO_ENABLE
  29. PLAY_NOTE_ARRAY(tone_goodbye, false, 0);
  30. _delay_ms(150);
  31. stop_all_notes();
  32. #endif
  33. }
  34. void matrix_init_kb(void)
  35. {
  36. debug_enable=true;
  37. print("meira matrix_init_kb\n");
  38. #ifdef AUDIO_ENABLE
  39. _delay_ms(20); // gets rid of tick
  40. PLAY_NOTE_ARRAY(tone_startup, false, 0);
  41. #endif
  42. #ifdef ISSI_ENABLE
  43. issi_init();
  44. #endif
  45. backlight_set(5);
  46. #ifdef WATCHDOG_ENABLE
  47. // This is done after turning the layer LED red, if we're caught in a loop
  48. // we should get a flashing red light
  49. wdt_enable(WDTO_500MS);
  50. #endif
  51. // put your keyboard start-up code here
  52. // runs once when the firmware starts up
  53. matrix_init_user();
  54. }
  55. void matrix_scan_kb(void)
  56. {
  57. #ifdef WATCHDOG_ENABLE
  58. wdt_reset();
  59. #endif
  60. #ifdef ISSI_ENABLE
  61. // switch/underglow lighting update
  62. static uint32_t issi_device = 0;
  63. static uint32_t twi_last_ready = 0;
  64. if(twi_last_ready > 1000){
  65. // Its been way too long since the last ISSI update, reset the I2C bus and start again
  66. xprintf("TWI failed to recover, TWI re-init\n");
  67. twi_last_ready = 0;
  68. TWIInit();
  69. force_issi_refresh();
  70. }
  71. if(isTWIReady()){
  72. twi_last_ready = 0;
  73. // If the i2c bus is available, kick off the issi update, alternate between devices
  74. update_issi(issi_device, issi_device);
  75. if(issi_device){
  76. issi_device = 0;
  77. }else{
  78. issi_device = 3;
  79. }
  80. }else{
  81. twi_last_ready++;
  82. }
  83. #endif
  84. matrix_scan_user();
  85. }
  86. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  87. // Test code that turns on the switch led for the key that is pressed
  88. // set_backlight_by_keymap(record->event.key.col, record->event.key.row);
  89. if (keycode == RESET) {
  90. reset_keyboard_kb();
  91. } else {
  92. }
  93. return process_record_user(keycode, record);
  94. }
  95. void led_set_kb(uint8_t usb_led) {
  96. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  97. led_set_user(usb_led);
  98. }
  99. //void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
  100. //{
  101. //#ifdef AUDIO_ENABLE
  102. // int8_t sign = 1;
  103. //#endif
  104. // if(id == LFK_ESC_TILDE){
  105. // // Send ~ on shift-esc
  106. // void (*method)(uint8_t) = (event->event.pressed) ? &add_key : &del_key;
  107. // uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT));
  108. // method(shifted ? KC_GRAVE : KC_ESCAPE);
  109. // send_keyboard_report();
  110. // }else if(event->event.pressed){
  111. // switch(id){
  112. // case LFK_CLEAR:
  113. // // Go back to default layer
  114. // layer_clear();
  115. // break;
  116. //#ifdef ISSI_ENABLE
  117. // case LFK_LED_TEST:
  118. // led_test();
  119. // break;
  120. //#endif
  121. // }
  122. // }
  123. //}
  124. void reset_keyboard_kb(){
  125. #ifdef WATCHDOG_ENABLE
  126. MCUSR = 0;
  127. wdt_disable();
  128. wdt_reset();
  129. #endif
  130. xprintf("programming!\n");
  131. reset_keyboard();
  132. }