matrix.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  36. __attribute__ ((weak))
  37. void matrix_scan_kb(void) {
  38. }
  39. inline
  40. uint8_t matrix_rows(void)
  41. {
  42. return MATRIX_ROWS;
  43. }
  44. inline
  45. uint8_t matrix_cols(void)
  46. {
  47. return MATRIX_COLS;
  48. }
  49. void matrix_init(void)
  50. {
  51. DDRC &= ~(1<<7);
  52. PORTC |= (1<<7);
  53. DDRB &= ~(1<<7 | 1<<5);
  54. PORTB |= (1<<7 | 1<<5);
  55. DDRD &= ~(1<<6 | 1<<4 | 1<<1);
  56. PORTD |= (1<<6 | 1<<4 | 1<<1);
  57. matrix_init_kb();
  58. }
  59. uint8_t matrix_scan(void)
  60. {
  61. matrix[0] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2));
  62. matrix[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2));
  63. matrix_scan_kb();
  64. return 1;
  65. }
  66. bool matrix_is_modified(void)
  67. {
  68. return true;
  69. }
  70. inline
  71. bool matrix_is_on(uint8_t row, uint8_t col)
  72. {
  73. return (matrix[row] & ((matrix_row_t)1<<col));
  74. }
  75. inline
  76. matrix_row_t matrix_get_row(uint8_t row)
  77. {
  78. return matrix[row];
  79. }
  80. void matrix_print(void)
  81. {
  82. print("\nr/c 0123456789ABCDEF\n");
  83. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  84. phex(row); print(": ");
  85. pbin_reverse16(matrix_get_row(row));
  86. print("\n");
  87. }
  88. }
  89. uint8_t matrix_key_count(void)
  90. {
  91. uint8_t count = 0;
  92. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  93. count += bitpop16(matrix[i]);
  94. }
  95. return count;
  96. }