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.

173 lines
5.4 KiB

  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. Copyright 2020 Kan-Ru Chen <kanru@kanru.info>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "quantum.h"
  16. #ifdef BLUETOOTH_ENABLE
  17. # include "adafruit_ble.h"
  18. #endif
  19. #define RELAX_TIME_US 5
  20. #define ADC_READ_TIME_US 5
  21. uint8_t power_save_level;
  22. static uint32_t matrix_last_modified = 0;
  23. static inline void key_strobe_high(void) { writePinLow(B6); }
  24. static inline void key_strobe_low(void) { gpio_write_pin_high(B6); }
  25. static inline bool key_state(void) { return gpio_read_pin(D7); }
  26. static inline void key_prev_on(void) { gpio_write_pin_high(B7); }
  27. static inline void key_prev_off(void) { writePinLow(B7); }
  28. static inline bool key_power_state(void) { return !gpio_read_pin(D6); }
  29. static inline void suspend_power_down_longer(void) {
  30. uint8_t times = 60;
  31. while (--times) suspend_power_down();
  32. }
  33. void matrix_power_up(void) {
  34. dprint("[matrix_on]\n");
  35. // change pins output
  36. DDRB = 0xFF;
  37. PORTB = 0x40;
  38. // switch MOS FET on
  39. setPinOutput(D6);
  40. writePinLow(D6);
  41. }
  42. void matrix_power_down(void) {
  43. dprint("[matrix_off]\n");
  44. // input with pull-up consumes less than without it when pin is open
  45. DDRB = 0x00;
  46. PORTB = 0xFF;
  47. // switch MOS FET off
  48. setPinOutput(D6);
  49. gpio_write_pin_high(D6);
  50. }
  51. static inline void key_select_row(uint8_t row) { PORTB = (PORTB & 0b11111000) | ((row)&0b111); }
  52. static inline void key_select_col(uint8_t col) { PORTB = (PORTB & 0b11000111) | (((col)&0b111) << 3); }
  53. static inline bool key_prev_was_on(matrix_row_t matrix[], uint8_t row, uint8_t col) { return matrix[row] & (1 << col); }
  54. void matrix_init_custom(void) { power_save_level = 0; }
  55. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  56. bool matrix_has_changed = false;
  57. // power on
  58. if (!key_power_state()) {
  59. matrix_power_up();
  60. }
  61. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  62. matrix_row_t last_row_value = current_matrix[row];
  63. key_select_row(row);
  64. wait_us(RELAX_TIME_US);
  65. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  66. // Hysteresis control: assert(1) when previous key state is on
  67. if (key_prev_was_on(current_matrix, row, col)) {
  68. key_prev_on();
  69. } else {
  70. key_prev_off();
  71. }
  72. // Disable interrupts to encure the ADC timing is correct
  73. cli();
  74. // strobe
  75. key_select_col(col);
  76. key_strobe_high();
  77. // Wait for ADC to outputs its value.
  78. // 1us was ok on one HHKB, but not worked on another.
  79. // no wait doesn't work on Teensy++ with pro(1us works)
  80. // no wait does work on tmk PCB(8MHz) with pro2
  81. // 1us wait does work on both of above
  82. // 1us wait doesn't work on tmk(16MHz)
  83. // 5us wait does work on tmk(16MHz)
  84. // 5us wait does work on tmk(16MHz/2)
  85. // 5us wait does work on tmk(8MHz)
  86. // 10us wait does work on Teensy++ with pro
  87. // 10us wait does work on 328p+iwrap with pro
  88. // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
  89. wait_us(ADC_READ_TIME_US);
  90. if (key_state()) {
  91. current_matrix[row] &= ~(1 << col);
  92. } else {
  93. current_matrix[row] |= (1 << col);
  94. }
  95. key_strobe_low();
  96. sei();
  97. // Make sure enough time has elapsed since the last call
  98. // This is to ensure the matrix voltages have relaxed
  99. wait_us(RELAX_TIME_US);
  100. }
  101. if (current_matrix[row] ^ last_row_value) {
  102. matrix_has_changed = true;
  103. matrix_last_modified = timer_read32();
  104. }
  105. }
  106. // Power saving
  107. uint32_t time_diff = timer_elapsed32(matrix_last_modified);
  108. if (time_diff > MATRIX_POWER_SAVE_TIMEOUT_L3_MS) {
  109. power_save_level = 3;
  110. suspend_power_down_longer();
  111. } else if (time_diff > MATRIX_POWER_SAVE_TIMEOUT_L2_MS) {
  112. power_save_level = 2;
  113. #ifdef BLUETOOTH_ENABLE
  114. if (!adafruit_ble_is_connected()) {
  115. power_save_level = 3;
  116. }
  117. #endif
  118. suspend_power_down_longer();
  119. } else if (time_diff > MATRIX_POWER_SAVE_TIMEOUT_MS) {
  120. power_save_level = 1;
  121. suspend_power_down();
  122. } else {
  123. if (power_save_level != 0) {
  124. power_save_level = 0;
  125. suspend_wakeup_init();
  126. }
  127. }
  128. return matrix_has_changed;
  129. }
  130. bool adafruit_ble_delbonds(void);
  131. bool adafruit_ble_reconnect(void);
  132. bool command_extra(uint8_t code) {
  133. switch (code) {
  134. #ifdef BLUETOOTH_ENABLE
  135. case KC_R:
  136. adafruit_ble_delbonds();
  137. return true;
  138. case KC_S:
  139. adafruit_ble_reconnect();
  140. return true;
  141. #endif
  142. default:
  143. return false;
  144. }
  145. }