Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "gpio_api.h"
  4. #include "timer.h"
  5. #include "wait.h"
  6. #include "matrix.h"
  7. #ifndef DEBOUNCE
  8. #define DEBOUNCE 5
  9. #endif
  10. /*
  11. * Infinity Pinusage:
  12. * Column pins are input with internal pull-down. Row pins are output and strobe with high.
  13. * Key is high or 1 when it turns on.
  14. *
  15. * col: { PTD1, PTD2, PTD3, PTD4, PTD5, PTD6, PTD7 }
  16. * row: { PTB0, PTB1, PTB2, PTB3, PTB16, PTB17, PTC4, PTC5, PTD0 }
  17. */
  18. static gpio_t col[MATRIX_COLS];
  19. static gpio_t row[MATRIX_ROWS];
  20. /* matrix state(1:on, 0:off) */
  21. static matrix_row_t matrix[MATRIX_ROWS];
  22. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  23. static bool debouncing = false;
  24. static uint16_t debouncing_time = 0;
  25. void matrix_init(void)
  26. {
  27. /* Column(sense) */
  28. gpio_init_in_ex(&col[0], PTD1, PullDown);
  29. gpio_init_in_ex(&col[1], PTD2, PullDown);
  30. gpio_init_in_ex(&col[2], PTD3, PullDown);
  31. gpio_init_in_ex(&col[3], PTD4, PullDown);
  32. gpio_init_in_ex(&col[4], PTD5, PullDown);
  33. gpio_init_in_ex(&col[5], PTD6, PullDown);
  34. gpio_init_in_ex(&col[6], PTD7, PullDown);
  35. #ifndef INFINITY_LED
  36. /* Row(strobe) */
  37. gpio_init_out_ex(&row[0], PTB0, 0);
  38. gpio_init_out_ex(&row[1], PTB1, 0);
  39. gpio_init_out_ex(&row[2], PTB2, 0);
  40. gpio_init_out_ex(&row[3], PTB3, 0);
  41. gpio_init_out_ex(&row[4], PTB16, 0);
  42. gpio_init_out_ex(&row[5], PTB17, 0);
  43. gpio_init_out_ex(&row[6], PTC4, 0);
  44. gpio_init_out_ex(&row[7], PTC5, 0);
  45. gpio_init_out_ex(&row[8], PTD0, 0);
  46. #else
  47. gpio_init_out_ex(&row[0], PTC0, 0);
  48. gpio_init_out_ex(&row[1], PTC1, 0);
  49. gpio_init_out_ex(&row[2], PTC2, 0);
  50. gpio_init_out_ex(&row[3], PTC3, 0);
  51. gpio_init_out_ex(&row[4], PTC4, 0);
  52. gpio_init_out_ex(&row[5], PTC5, 0);
  53. gpio_init_out_ex(&row[6], PTC6, 0);
  54. gpio_init_out_ex(&row[7], PTC7, 0);
  55. gpio_init_out_ex(&row[8], PTD0, 0);
  56. #endif
  57. }
  58. uint8_t matrix_scan(void)
  59. {
  60. for (int i = 0; i < MATRIX_ROWS; i++) {
  61. matrix_row_t r = 0;
  62. gpio_write(&row[i], 1);
  63. wait_us(1); // need wait to settle pin state
  64. for (int j = 0; j < MATRIX_COLS; j++) {
  65. if (gpio_read(&col[j])) {
  66. r |= (1<<j);
  67. }
  68. }
  69. gpio_write(&row[i], 0);
  70. if (matrix_debouncing[i] != r) {
  71. matrix_debouncing[i] = r;
  72. debouncing = true;
  73. debouncing_time = timer_read();
  74. }
  75. }
  76. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  77. for (int i = 0; i < MATRIX_ROWS; i++) {
  78. matrix[i] = matrix_debouncing[i];
  79. }
  80. debouncing = false;
  81. }
  82. /*
  83. if (debouncing) {
  84. if (--debouncing) {
  85. return 0;
  86. } else {
  87. for (int i = 0; i < MATRIX_ROWS; i++) {
  88. matrix[i] = matrix_debouncing[i];
  89. }
  90. }
  91. }
  92. */
  93. return 1;
  94. }
  95. bool matrix_is_on(uint8_t row, uint8_t col)
  96. {
  97. return (matrix[row] & (1<<col));
  98. }
  99. matrix_row_t matrix_get_row(uint8_t row)
  100. {
  101. return matrix[row];
  102. }
  103. void matrix_print(void)
  104. {
  105. }