Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /* Row(strobe) */
  36. gpio_init_out_ex(&row[0], PTB0, 0);
  37. gpio_init_out_ex(&row[1], PTB1, 0);
  38. gpio_init_out_ex(&row[2], PTB2, 0);
  39. gpio_init_out_ex(&row[3], PTB3, 0);
  40. gpio_init_out_ex(&row[4], PTB16, 0);
  41. gpio_init_out_ex(&row[5], PTB17, 0);
  42. gpio_init_out_ex(&row[6], PTC4, 0);
  43. gpio_init_out_ex(&row[7], PTC5, 0);
  44. gpio_init_out_ex(&row[8], PTD0, 0);
  45. }
  46. uint8_t matrix_scan(void)
  47. {
  48. for (int i = 0; i < MATRIX_ROWS; i++) {
  49. matrix_row_t r = 0;
  50. gpio_write(&row[i], 1);
  51. wait_us(1); // need wait to settle pin state
  52. for (int j = 0; j < MATRIX_COLS; j++) {
  53. if (gpio_read(&col[j])) {
  54. r |= (1<<j);
  55. }
  56. }
  57. gpio_write(&row[i], 0);
  58. if (matrix_debouncing[i] != r) {
  59. matrix_debouncing[i] = r;
  60. debouncing = true;
  61. debouncing_time = timer_read();
  62. }
  63. }
  64. if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
  65. for (int i = 0; i < MATRIX_ROWS; i++) {
  66. matrix[i] = matrix_debouncing[i];
  67. }
  68. debouncing = false;
  69. }
  70. /*
  71. if (debouncing) {
  72. if (--debouncing) {
  73. return 0;
  74. } else {
  75. for (int i = 0; i < MATRIX_ROWS; i++) {
  76. matrix[i] = matrix_debouncing[i];
  77. }
  78. }
  79. }
  80. */
  81. return 1;
  82. }
  83. bool matrix_is_on(uint8_t row, uint8_t col)
  84. {
  85. return (matrix[row] & (1<<col));
  86. }
  87. matrix_row_t matrix_get_row(uint8_t row)
  88. {
  89. return matrix[row];
  90. }
  91. void matrix_print(void)
  92. {
  93. }