matrix.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright 2018 listofoptions <listofoptions@gmail.com>
  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 <string.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #endif
  20. #include <util/delay.h>
  21. #include "wait.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "timer.h"
  27. #include "config.h"
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. #define print_matrix_header() print("\nr/c 01234567\n")
  32. #define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  33. #define matrix_bitpop(i) bitpop(matrix[i])
  34. #define ROW_SHIFTER ((uint8_t)1)
  35. #define check_bit(var,pos) ((var) & (1<<(pos)))
  36. #define NUM_ROW_PINS 5
  37. #define NUM_COL_PINS 2
  38. static const uint8_t row_pins [NUM_ROW_PINS] = MATRIX_ROW_PINS ;
  39. static const uint8_t col_pins [NUM_ROW_PINS] = MATRIX_COL_PINS ;
  40. #if ( DEBOUNCE > 0 )
  41. static uint16_t debouncing_time ;
  42. static bool debouncing = false ;
  43. #endif
  44. static uint8_t matrix [MATRIX_ROWS] = {0};
  45. #if ( DEBOUNCE > 0 )
  46. static uint8_t matrix_debounce [MATRIX_ROWS] = {0};
  47. #endif
  48. static
  49. inline
  50. void toggle_led(void) {
  51. uint8_t pin = LED_PIN ;
  52. _SFR_IO8((pin >> 4) + 2) ^= _BV(pin & 0xF);
  53. }
  54. static
  55. inline
  56. void init_led(void) {
  57. uint8_t pin = LED_PIN ;
  58. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  59. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  60. }
  61. static
  62. inline
  63. void init_data(void) {
  64. uint8_t pin = MATRIX_DATA_PIN ;
  65. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  66. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // LO
  67. }
  68. static
  69. inline
  70. void init_strobe(void) {
  71. uint8_t pin = MATRIX_STROBE_PIN ;
  72. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  73. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  74. }
  75. static
  76. inline
  77. void init_rows(void) {
  78. for ( uint8_t i = 0 ; i < NUM_ROW_PINS; ++i ) {
  79. uint8_t pin = row_pins[i];
  80. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  81. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  82. }
  83. }
  84. static
  85. inline
  86. void init_cols(void) {
  87. for ( uint8_t i = 0 ; i < NUM_COL_PINS; ++i ) {
  88. uint8_t pin = col_pins[i];
  89. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  90. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  91. }
  92. }
  93. static
  94. inline
  95. void select_row(uint8_t current_row) {
  96. for ( uint8_t i = 0 ; i < NUM_ROW_PINS; ++i ) {
  97. uint8_t pin = row_pins[i] ;
  98. if ( check_bit( current_row, i ) ) {
  99. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  100. } else {
  101. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  102. }
  103. }
  104. wait_us(30) ;
  105. }
  106. static
  107. inline
  108. void select_col(uint8_t current_col) {
  109. for ( uint8_t i = 0 ; i < NUM_COL_PINS; ++i ) {
  110. uint8_t pin = col_pins[i] ;
  111. if ( check_bit( current_col, i ) ) {
  112. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  113. } else {
  114. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  115. }
  116. }
  117. wait_us(30) ;
  118. }
  119. static
  120. inline
  121. uint8_t matrix_strobe(uint8_t col_index) {
  122. uint8_t strobe_pin = MATRIX_STROBE_PIN ;
  123. uint8_t data_pin = MATRIX_DATA_PIN ;
  124. // set strobe pin low
  125. _SFR_IO8((strobe_pin >> 4) + 2) &= ~_BV(strobe_pin & 0xF);
  126. wait_us(30) ;
  127. // read data
  128. uint8_t data = (_SFR_IO8(data_pin >> 4) & _BV(data_pin & 0xF)) ;
  129. // set strobe pin hi
  130. _SFR_IO8((strobe_pin >> 4) + 2) |= _BV(strobe_pin & 0xF);
  131. uint8_t out = data ? (1 << col_index) : 0 ;
  132. return out ;
  133. }
  134. static
  135. bool matrix_read(uint8_t current_matrix[], uint8_t current_row) {
  136. // Store last value of row prior to reading
  137. uint8_t last_row_value = current_matrix[current_row];
  138. // Clear data in matrix row
  139. current_matrix[current_row] = 0;
  140. select_row(current_row);
  141. // For each col...
  142. for(uint8_t col_index = 0; col_index < MATRIX_COLS; ++col_index) {
  143. select_col(col_index) ;
  144. // strobe the matrix
  145. // Populate the matrix row with the state of the data pin
  146. current_matrix[current_row] |= matrix_strobe(col_index) ;
  147. }
  148. bool test = last_row_value != current_matrix[current_row] ;
  149. return test ;
  150. }
  151. __attribute__ ((weak))
  152. void matrix_init_quantum(void) {
  153. matrix_init_kb();
  154. }
  155. __attribute__ ((weak))
  156. void matrix_scan_quantum(void) {
  157. matrix_scan_kb();
  158. }
  159. __attribute__ ((weak))
  160. void matrix_init_kb(void) {
  161. matrix_init_user();
  162. }
  163. __attribute__ ((weak))
  164. void matrix_scan_kb(void) {
  165. matrix_scan_user();
  166. }
  167. __attribute__ ((weak))
  168. void matrix_init_user(void) {
  169. }
  170. __attribute__ ((weak))
  171. void matrix_scan_user(void) {
  172. }
  173. inline
  174. uint8_t matrix_rows(void) {
  175. return MATRIX_ROWS;
  176. }
  177. inline
  178. uint8_t matrix_cols(void) {
  179. return MATRIX_COLS;
  180. }
  181. inline
  182. uint8_t matrix_get_row(uint8_t row) {
  183. return matrix[row];
  184. }
  185. void matrix_init(void) {
  186. init_led() ;
  187. init_rows() ;
  188. init_cols() ;
  189. init_data() ;
  190. init_strobe() ;
  191. // initialize matrix state: all keys off
  192. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  193. matrix[i] = 0;
  194. # if (DEBOUNCE > 0)
  195. matrix_debounce [i] = 0;
  196. # endif
  197. }
  198. matrix_init_quantum() ;
  199. }
  200. uint8_t matrix_scan(void) {
  201. for ( uint8_t current_row = 0; current_row < MATRIX_ROWS; ++current_row ) {
  202. # if (DEBOUNCE > 0)
  203. bool matrix_changed = matrix_read(matrix_debounce, current_row);
  204. if (matrix_changed) {
  205. debouncing = true ;
  206. debouncing_time = timer_read();
  207. }
  208. # else
  209. matrix_read(matrix, current_row);
  210. # endif
  211. }
  212. # if (DEBOUNCE > 0)
  213. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  214. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  215. matrix[i] = matrix_debounce[i];
  216. }
  217. debouncing = false;
  218. }
  219. # endif
  220. matrix_scan_quantum();
  221. return 1;
  222. }
  223. void matrix_print(void) {
  224. print_matrix_header();
  225. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  226. phex(row); print(": ");
  227. print_matrix_row(row);
  228. print("\n");
  229. }
  230. }