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.

109 lines
3.1 KiB

  1. /*
  2. Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
  3. Modified by Ryan Skidmore <rskeys@ryanskidmore.co.uk> (@ryanskidmore)
  4. to support the rskeys100.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdbool.h>
  17. #include "matrix.h"
  18. #include <util/delay.h>
  19. #include "quantum.h"
  20. static const uint32_t col_values[24] = SHR_COLS;
  21. static uint8_t read_rows(void);
  22. static void select_col(uint8_t col);
  23. static void shift_pulse(void);
  24. static void shift_out_single(uint8_t value);
  25. static void shift_out(uint32_t value);
  26. void matrix_init_custom(void) {
  27. setPinInput(ROW_A);
  28. setPinInput(ROW_B);
  29. setPinInput(ROW_C);
  30. setPinInput(ROW_D);
  31. setPinInput(ROW_E);
  32. setPinInput(ROW_F);
  33. setPinOutput(SHR_DATA);
  34. setPinOutput(SHR_LATCH);
  35. setPinOutput(SHR_CLOCK);
  36. }
  37. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  38. bool changed = false;
  39. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  40. select_col(col);
  41. _delay_us(1);
  42. uint8_t rows = read_rows();
  43. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  44. bool prev_bit = ((uint32_t)(current_matrix[row]) & (matrix_row_t)(1UL << col)) ? 1 : 0;
  45. bool curr_bit = ((uint32_t)rows & (uint32_t)(1UL << row)) ? 1 : 0;
  46. if (prev_bit != curr_bit) {
  47. current_matrix[row] = (uint32_t)(current_matrix[row]) ^ (uint32_t)(1UL << col);
  48. changed = true;
  49. }
  50. }
  51. }
  52. return changed;
  53. }
  54. static uint8_t read_rows(void) {
  55. return (readPin(ROW_F) << 5)
  56. | (readPin(ROW_E) << 4)
  57. | (readPin(ROW_D) << 3)
  58. | (readPin(ROW_C) << 2)
  59. | (readPin(ROW_B) << 1)
  60. | (readPin(ROW_A) );
  61. }
  62. static void select_col(uint8_t col) {
  63. shift_out(col_values[col]);
  64. }
  65. static void shift_out(uint32_t value) {
  66. writePinLow(SHR_LATCH);
  67. uint8_t first_byte = (value >> 16) & 0xFF;
  68. uint8_t second_byte = (value >> 8) & 0xFF;
  69. uint8_t third_byte = (uint8_t)(value & 0xFF);
  70. shift_out_single(first_byte);
  71. shift_out_single(second_byte);
  72. shift_out_single(third_byte);
  73. writePinHigh(SHR_LATCH);
  74. /* We delay here to prevent multiple consecutive keys being triggered with a single switch press */
  75. _delay_us(10);
  76. }
  77. static void shift_out_single(uint8_t value) {
  78. for (uint8_t i = 0; i < 8; i++) {
  79. if (value & 0b10000000) {
  80. writePinHigh(SHR_DATA);
  81. } else {
  82. writePinLow(SHR_DATA);
  83. }
  84. shift_pulse();
  85. value = value << 1;
  86. }
  87. }
  88. static inline void shift_pulse(void) {
  89. writePinHigh(SHR_CLOCK);
  90. writePinLow(SHR_CLOCK);
  91. }