matrix.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Note for ErgoDox EZ customizers: Here be dragons!
  3. This is not a file you want to be messing with.
  4. All of the interesting stuff for you is under keymaps/ :)
  5. Love, Erez
  6. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * scan matrix
  20. */
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <avr/io.h>
  24. #include <util/delay.h>
  25. #include "action_layer.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "util.h"
  29. #include "matrix.h"
  30. #include "sixkeyboard.h"
  31. /* matrix state(1:on, 0:off) */
  32. static matrix_row_t matrix[MATRIX_ROWS];
  33. __attribute__ ((weak))
  34. void matrix_init_kb(void) {
  35. matrix_init_user();
  36. }
  37. __attribute__ ((weak))
  38. void matrix_scan_kb(void) {
  39. matrix_scan_user();
  40. }
  41. __attribute__ ((weak))
  42. void matrix_init_user(void) {
  43. }
  44. __attribute__ ((weak))
  45. void matrix_scan_user(void) {
  46. }
  47. inline
  48. uint8_t matrix_rows(void)
  49. {
  50. return MATRIX_ROWS;
  51. }
  52. inline
  53. uint8_t matrix_cols(void)
  54. {
  55. return MATRIX_COLS;
  56. }
  57. void matrix_init(void)
  58. {
  59. DDRC &= ~(1<<7);
  60. PORTC |= (1<<7);
  61. DDRB &= ~(1<<7 | 1<<5);
  62. PORTB |= (1<<7 | 1<<5);
  63. DDRD &= ~(1<<6 | 1<<4 | 1<<1);
  64. PORTD |= (1<<6 | 1<<4 | 1<<1);
  65. matrix_init_kb();
  66. }
  67. uint8_t matrix_scan(void)
  68. {
  69. matrix[0] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2));
  70. matrix[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2));
  71. matrix_scan_quantum();
  72. return 1;
  73. }
  74. bool matrix_is_modified(void)
  75. {
  76. return true;
  77. }
  78. inline
  79. bool matrix_is_on(uint8_t row, uint8_t col)
  80. {
  81. return (matrix[row] & ((matrix_row_t)1<<col));
  82. }
  83. inline
  84. matrix_row_t matrix_get_row(uint8_t row)
  85. {
  86. return matrix[row];
  87. }
  88. void matrix_print(void)
  89. {
  90. print("\nr/c 0123456789ABCDEF\n");
  91. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  92. phex(row); print(": ");
  93. pbin_reverse16(matrix_get_row(row));
  94. print("\n");
  95. }
  96. }
  97. uint8_t matrix_key_count(void)
  98. {
  99. uint8_t count = 0;
  100. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  101. count += bitpop16(matrix[i]);
  102. }
  103. return count;
  104. }