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.

uno-matrix.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #define F_CPU 16000000UL
  2. #include <util/delay.h>
  3. #include <avr/io.h>
  4. #include <stdlib.h>
  5. #include "uno-matrix.h"
  6. #define debug(X) NULL
  7. #define debug_hex(X) NULL
  8. #ifndef DEBOUNCE
  9. # define DEBOUNCE 5
  10. #endif
  11. static uint8_t debouncing = DEBOUNCE;
  12. /* matrix state(1:on, 0:off) */
  13. static matrix_row_t matrix[MATRIX_ROWS];
  14. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  15. static matrix_row_t read_cols(void);
  16. static void init_cols(void);
  17. static void unselect_rows(void);
  18. static void select_row(uint8_t row);
  19. inline
  20. uint8_t matrix_rows(void)
  21. {
  22. return MATRIX_ROWS;
  23. }
  24. inline
  25. uint8_t matrix_cols(void)
  26. {
  27. return MATRIX_COLS;
  28. }
  29. void matrix_init(void)
  30. {
  31. //debug_enable = true;
  32. //debug_matrix = true;
  33. //debug_mouse = true;
  34. // initialize row and col
  35. unselect_rows();
  36. init_cols();
  37. // initialize matrix state: all keys off
  38. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  39. matrix[i] = 0;
  40. matrix_debouncing[i] = 0;
  41. }
  42. }
  43. uint8_t matrix_scan(void)
  44. {
  45. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  46. select_row(i);
  47. _delay_us(30); // without this wait read unstable value.
  48. matrix_row_t cols = read_cols();
  49. //Serial.println(cols, BIN);
  50. if (matrix_debouncing[i] != cols) {
  51. matrix_debouncing[i] = cols;
  52. if (debouncing) {
  53. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  54. }
  55. debouncing = DEBOUNCE;
  56. }
  57. unselect_rows();
  58. }
  59. if (debouncing) {
  60. if (--debouncing) {
  61. _delay_ms(1);
  62. } else {
  63. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = matrix_debouncing[i];
  65. }
  66. }
  67. }
  68. return 1;
  69. }
  70. bool matrix_is_modified(void)
  71. {
  72. if (debouncing) return false;
  73. return true;
  74. }
  75. inline
  76. bool matrix_is_on(uint8_t row, uint8_t col)
  77. {
  78. return (matrix[row] & ((matrix_row_t)1<<col));
  79. }
  80. inline
  81. matrix_row_t matrix_get_row(uint8_t row)
  82. {
  83. return matrix[row];
  84. }
  85. // TODO update this comment
  86. /* Column pin configuration
  87. * col: 0 1 2 3 4 5
  88. * pin: D3 D4 D5 D6 D7 B0
  89. */
  90. static void init_cols(void)
  91. {
  92. // Input with pull-up(DDR:0, PORT:1)
  93. DDRD &= ~(1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  94. PORTD |= (1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  95. DDRB &= ~(1<<0);
  96. PORTB |= (1<<0);
  97. }
  98. static matrix_row_t read_cols(void)
  99. {
  100. return (PIND&(1<<3) ? 0 : (1<<0)) |
  101. (PIND&(1<<4) ? 0 : (1<<1)) |
  102. (PIND&(1<<5) ? 0 : (1<<2)) |
  103. (PIND&(1<<6) ? 0 : (1<<3)) |
  104. (PIND&(1<<7) ? 0 : (1<<4)) |
  105. (PINB&(1<<0) ? 0 : (1<<5));
  106. }
  107. /* Row pin configuration
  108. * row: 0 1 2 3
  109. * pin: C0 C1 C2 C3
  110. */
  111. static void unselect_rows(void)
  112. {
  113. // Hi-Z(DDR:0, PORT:0) to unselect
  114. DDRC &= ~0xF;
  115. PORTC &= ~0xF;
  116. }
  117. static void select_row(uint8_t row)
  118. {
  119. // Output low(DDR:1, PORT:0) to select
  120. switch (row) {
  121. case 0:
  122. DDRC |= (1<<0);
  123. PORTC &= ~(1<<0);
  124. break;
  125. case 1:
  126. DDRC |= (1<<1);
  127. PORTC &= ~(1<<1);
  128. break;
  129. case 2:
  130. DDRC |= (1<<2);
  131. PORTC &= ~(1<<2);
  132. break;
  133. case 3:
  134. DDRC |= (1<<3);
  135. PORTC &= ~(1<<3);
  136. break;
  137. }
  138. }