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.

219 lines
4.5 KiB

  1. /*
  2. Copyright 2014 Warren Janssens <warren.janssens@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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "action_layer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "led.h"
  27. #include "config.h"
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. static uint8_t debouncing = DEBOUNCE;
  32. /* matrix state(1:on, 0:off) */
  33. static uint8_t matrix[MATRIX_ROWS];
  34. static uint8_t matrix_debouncing[MATRIX_ROWS];
  35. static matrix_row_t read_row(uint8_t row);
  36. static void unselect_rows(void);
  37. static void select_rows(uint8_t row);
  38. __attribute__ ((weak))
  39. void matrix_init_kb(void) {
  40. matrix_init_user();
  41. }
  42. __attribute__ ((weak))
  43. void matrix_scan_kb(void) {
  44. matrix_scan_user();
  45. }
  46. __attribute__ ((weak))
  47. void matrix_init_user(void) {
  48. }
  49. __attribute__ ((weak))
  50. void matrix_scan_user(void) {
  51. }
  52. inline
  53. uint8_t matrix_rows(void)
  54. {
  55. return MATRIX_ROWS;
  56. }
  57. inline
  58. uint8_t matrix_cols(void)
  59. {
  60. return MATRIX_COLS;
  61. }
  62. void matrix_init(void)
  63. {
  64. //debug_enable = true;
  65. //dprint("matrix_init"); dprintln();
  66. // output high (leds)
  67. DDRD = 0xFF;
  68. PORTD = 0xFF;
  69. // output low (multiplexers)
  70. DDRF = 0xFF;
  71. PORTF = 0x00;
  72. // input with pullup (matrix)
  73. DDRB = 0x00;
  74. PORTB = 0xFF;
  75. // input with pullup (program and keypad buttons)
  76. DDRC = 0x00;
  77. PORTC = 0xFF;
  78. // initialize row and col
  79. unselect_rows();
  80. // initialize matrix state: all keys off
  81. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  82. matrix[i] = 0;
  83. matrix_debouncing[i] = 0;
  84. }
  85. }
  86. uint8_t matrix_scan(void)
  87. {
  88. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  89. select_rows(i);
  90. uint8_t row = read_row(i);
  91. if (matrix_debouncing[i] != row) {
  92. matrix_debouncing[i] = row;
  93. if (debouncing) {
  94. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  95. }
  96. debouncing = DEBOUNCE;
  97. }
  98. unselect_rows();
  99. }
  100. if (debouncing) {
  101. if (--debouncing) {
  102. _delay_ms(1);
  103. } else {
  104. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  105. matrix[i] = matrix_debouncing[i];
  106. }
  107. }
  108. }
  109. matrix_scan_quantum();
  110. return 1;
  111. }
  112. bool matrix_is_modified(void)
  113. {
  114. if (debouncing) return false;
  115. return true;
  116. }
  117. inline
  118. bool matrix_is_on(uint8_t row, uint8_t col)
  119. {
  120. return (matrix[row] & ((matrix_row_t)1<<col));
  121. }
  122. inline
  123. matrix_row_t matrix_get_row(uint8_t row)
  124. {
  125. return matrix[row];
  126. }
  127. void matrix_print(void)
  128. {
  129. print("\nr/c 01234567\n");
  130. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  131. print_hex8(row); print(": ");
  132. print_bin_reverse8(matrix_get_row(row));
  133. print("\n");
  134. }
  135. }
  136. uint8_t matrix_key_count(void)
  137. {
  138. uint8_t count = 0;
  139. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  140. count += bitpop16(matrix[i]);
  141. }
  142. return count;
  143. }
  144. static matrix_row_t read_row(uint8_t row)
  145. {
  146. _delay_us(30); // without this wait read unstable value.
  147. //keypad and program buttons
  148. if (row == 12)
  149. {
  150. return ~(PINC | 0b00111111);
  151. }
  152. return ~PINB;
  153. }
  154. static void unselect_rows(void)
  155. {
  156. // set A,B,C,G to 0 (F4 - F7)
  157. PORTF &= 0x0F;
  158. }
  159. static void select_rows(uint8_t row)
  160. {
  161. // set A,B,C,G to row value
  162. PORTF |= row << 4;
  163. }
  164. /* Row pin configuration
  165. PF0 A
  166. PF1 B
  167. PF2 C
  168. PF3 G 0 = U4, 1 = U5
  169. 4y0 4y1 4y2 4y3 4y4 4y5 4y6 4y7 5y0 5y1 5y2 5y3 5y4 5y5 5y6 5y7
  170. r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16
  171. PB0 21 c1 f6 f8 f7 5 4 3 2 1 =+
  172. PB1 22 c2 f3 f5 f4 t r e w q TAB
  173. PB2 23 c3 ESC f2 f1 g f d s a CL
  174. PB3 24 c4 f9 f11 f10 b v c x z LS UP DN [{ ]}
  175. PB4 25 c5 f12 SL PS RT LT §± `~ 6 7 8 9 0 -_
  176. PB5 26 c6 PB PGM KPD y u i o p \
  177. PB6 27 c7 LC DL BS RC EN SP h j k l ;: '"
  178. PB7 28 c8 RA PU PD n m ,< .> /? RS
  179. */