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.

125 lines
2.9 KiB

  1. /* Copyright 2017-2019 Nikolaus Wittenstein <nikolaus.wittenstein@gmail.com>
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  8. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. * FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  12. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. * PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "datahand.h"
  16. #include "matrix.h"
  17. #include "action.h"
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <avr/io.h>
  21. static matrix_row_t matrix[MATRIX_ROWS];
  22. static matrix_row_t read_cols(void);
  23. static void select_row(uint8_t row);
  24. void matrix_init(void) {
  25. /* See datahand.h for more detail on pins. */
  26. /* 7 - matrix scan; 6-3 - mode LEDs */
  27. DDRB = 0b11111000;
  28. /* 1-0 - matrix scan */
  29. DDRD = 0b00000011;
  30. /* 6 - matrix scan */
  31. DDRE = 0b01000000;
  32. /* 7-4 - lock LEDs */
  33. DDRF = 0b11110000;
  34. /* Turn off the non-Normal LEDs (they're active low). */
  35. PORTB |= LED_TENKEY | LED_FN | LED_NAS;
  36. /* Turn off the lock LEDs. */
  37. PORTF |= LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK | LED_MOUSE_LOCK;
  38. matrix_init_user();
  39. }
  40. uint8_t matrix_scan(void) {
  41. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  42. select_row(row);
  43. /* The default hardware works down to at least 100us, but I have a replacement
  44. * photodiode that responds a little more slowly. Cranking it up to 1000us fixes
  45. * shadowing issues.
  46. */
  47. _delay_us(1000);
  48. matrix[row] = read_cols();
  49. }
  50. matrix_scan_user();
  51. return 1;
  52. }
  53. matrix_row_t matrix_get_row(uint8_t row) {
  54. return matrix[row];
  55. }
  56. void matrix_print(void) {
  57. print("\nr/c 01234567\n");
  58. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  59. print_hex8(row);
  60. print(": ");
  61. print_bin_reverse8(matrix_get_row(row));
  62. print("\n");
  63. }
  64. }
  65. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  66. return process_record_user(keycode, record);
  67. }
  68. static void select_row(uint8_t row) {
  69. /* Original 8051: P1 bits 0-3 (pins 1-4)
  70. * Teensy++: PE0, PB7, PD0, PD1
  71. */
  72. if (row & (1<<0)) {
  73. PORTE |= (1<<6);
  74. } else {
  75. PORTE &= ~(1<<6);
  76. }
  77. if (row & (1<<1)) {
  78. PORTB |= (1<<7);
  79. } else {
  80. PORTB &= ~(1<<7);
  81. }
  82. if (row & (1<<2)) {
  83. PORTD |= (1<<0);
  84. } else {
  85. PORTD &= ~(1<<0);
  86. }
  87. if (row & (1<<3)) {
  88. PORTD |= (1<<1);
  89. } else {
  90. PORTD &= ~(1<<1);
  91. }
  92. }
  93. static matrix_row_t read_cols(void) {
  94. /* Original 8051: P1 bits 4-7 (pins 5-8)
  95. * Teensy++: PD bits 2-5
  96. */
  97. return (PIND & 0b00111100) >> 2;
  98. }