matrix.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #ifndef CONFIG_SPECIFIC_H
  23. #define CONFIG_SPECIFIC_H
  24. #define CONFIG_LED_IO \
  25. DDRB |= (1<<7); \
  26. DDRC |= (1<<5) | (1<<6);
  27. #define USB_LED_CAPS_LOCK_ON PORTC &= ~(1<<5)
  28. #define USB_LED_CAPS_LOCK_OFF PORTC |= (1<<5)
  29. #define USB_LED_NUM_LOCK_ON PORTB &= ~(1<<7)
  30. #define USB_LED_NUM_LOCK_OFF PORTB |= (1<<7)
  31. #define USB_LED_SCROLL_LOCK_ON PORTC &= ~(1<<6)
  32. #define USB_LED_SCROLL_LOCK_OFF PORTC |= (1<<6)
  33. #define CONFIG_MATRIX_IO \
  34. /* Column output pins */ \
  35. DDRD |= 0b01111011; \
  36. /* Row input pins */ \
  37. DDRC &= ~0b10000000; \
  38. DDRB &= ~0b01111111; \
  39. PORTC |= 0b10000000; \
  40. PORTB |= 0b01111111;
  41. #define MATRIX_ROW_SCAN \
  42. (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) | \
  43. (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) | \
  44. (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) | \
  45. (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) | \
  46. (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) | \
  47. (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) | \
  48. (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) | \
  49. (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7))
  50. #define MATRIX_ROW_SELECT \
  51. case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break; \
  52. case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break; \
  53. case 2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break; \
  54. case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break; \
  55. case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break; \
  56. case 5: PORTD = (PORTD & ~0b01111011) | 0b01110001; break; \
  57. case 6: PORTD = (PORTD & ~0b01111011) | 0b01100001; break; \
  58. case 7: PORTD = (PORTD & ~0b01111011) | 0b01110000; break; \
  59. case 8: PORTD = (PORTD & ~0b01111011) | 0b01100000; break; \
  60. case 9: PORTD = (PORTD & ~0b01111011) | 0b01101000; break; \
  61. case 10: PORTD = (PORTD & ~0b01111011) | 0b00101011; break; \
  62. case 11: PORTD = (PORTD & ~0b01111011) | 0b00110011; break; \
  63. case 12: PORTD = (PORTD & ~0b01111011) | 0b00100011; break; \
  64. case 13: PORTD = (PORTD & ~0b01111011) | 0b01111000; break; \
  65. case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break; \
  66. case 15: PORTD = (PORTD & ~0b01111011) | 0b01101001; break; \
  67. case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break; \
  68. case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
  69. #endif
  70. #ifndef DEBOUNCING_DELAY
  71. # define DEBOUNCING_DELAY 0
  72. #endif
  73. static uint8_t debouncing = DEBOUNCING_DELAY;
  74. static matrix_row_t matrix[MATRIX_ROWS];
  75. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  76. static matrix_row_t scan_row(void);
  77. static void select_row(uint8_t row);
  78. inline uint8_t matrix_rows(void) {
  79. return MATRIX_ROWS;
  80. }
  81. inline uint8_t matrix_cols(void) {
  82. return MATRIX_COLS;
  83. }
  84. void matrix_init(void) {
  85. CONFIG_MATRIX_IO;
  86. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  87. matrix[i] = 0;
  88. matrix_debouncing[i] = 0;
  89. }
  90. matrix_init_quantum();
  91. }
  92. uint8_t matrix_scan(void) {
  93. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  94. select_row(row);
  95. _delay_us(3);
  96. matrix_row_t row_scan = scan_row();
  97. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  98. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  99. bool curr_bit = row_scan & (1<<col);
  100. if (prev_bit != curr_bit) {
  101. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  102. debouncing = DEBOUNCING_DELAY;
  103. }
  104. }
  105. }
  106. if (debouncing) {
  107. if (--debouncing)
  108. _delay_ms(1);
  109. else
  110. for (uint8_t i = 0; i < MATRIX_ROWS; i++)
  111. matrix[i] = matrix_debouncing[i];
  112. }
  113. matrix_scan_quantum();
  114. return 1;
  115. }
  116. bool matrix_is_modified(void) {
  117. if (debouncing)
  118. return false;
  119. else
  120. return true;
  121. }
  122. inline bool matrix_is_on(uint8_t row, uint8_t col) {
  123. return (matrix[row] & ((matrix_row_t)1<<col));
  124. }
  125. inline matrix_row_t matrix_get_row(uint8_t row) {
  126. return matrix[row];
  127. }
  128. void matrix_print(void) {
  129. print("\nr/c 01234567\n");
  130. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  131. matrix_row_t row_scan = matrix_get_row(row);
  132. xprintf("%02X: ", row);
  133. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  134. bool curr_bit = row_scan & (1<<col);
  135. xprintf("%c", curr_bit ? '*' : '.');
  136. }
  137. print("\n");
  138. }
  139. }
  140. uint8_t matrix_key_count(void) {
  141. uint8_t count = 0;
  142. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  143. count += bitpop32(matrix[i]);
  144. }
  145. return count;
  146. }
  147. static matrix_row_t scan_row(void) {
  148. return MATRIX_ROW_SCAN;
  149. }
  150. static void select_row(uint8_t row) {
  151. switch (row) {
  152. MATRIX_ROW_SELECT;
  153. }
  154. }