logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (3256B)


  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 "matrix.h"
  17. #include <util/delay.h>
  18. static const uint32_t col_values[24] = SHR_COLS;
  19. static uint8_t read_rows(void);
  20. static void select_col(uint8_t col);
  21. static void shift_pulse(void);
  22. static void shift_out_single(uint8_t value);
  23. static void shift_out(uint32_t value);
  24. void matrix_init_custom(void) {
  25. gpio_set_pin_input(ROW_A);
  26. gpio_set_pin_input(ROW_B);
  27. gpio_set_pin_input(ROW_C);
  28. gpio_set_pin_input(ROW_D);
  29. gpio_set_pin_input(ROW_E);
  30. gpio_set_pin_input(ROW_F);
  31. gpio_set_pin_output(SHR_DATA);
  32. gpio_set_pin_output(SHR_LATCH);
  33. gpio_set_pin_output(SHR_CLOCK);
  34. }
  35. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  36. bool changed = false;
  37. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  38. select_col(col);
  39. _delay_us(1);
  40. uint8_t rows = read_rows();
  41. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  42. bool prev_bit = ((uint32_t)(current_matrix[row]) & (matrix_row_t)(1UL << col)) ? 1 : 0;
  43. bool curr_bit = ((uint32_t)rows & (uint32_t)(1UL << row)) ? 1 : 0;
  44. if (prev_bit != curr_bit) {
  45. current_matrix[row] = (uint32_t)(current_matrix[row]) ^ (uint32_t)(1UL << col);
  46. changed = true;
  47. }
  48. }
  49. }
  50. return changed;
  51. }
  52. static uint8_t read_rows(void) {
  53. return (gpio_read_pin(ROW_F) << 5)
  54. | (gpio_read_pin(ROW_E) << 4)
  55. | (gpio_read_pin(ROW_D) << 3)
  56. | (gpio_read_pin(ROW_C) << 2)
  57. | (gpio_read_pin(ROW_B) << 1)
  58. | (gpio_read_pin(ROW_A) );
  59. }
  60. static void select_col(uint8_t col) {
  61. shift_out(col_values[col]);
  62. }
  63. static void shift_out(uint32_t value) {
  64. gpio_write_pin_low(SHR_LATCH);
  65. uint8_t first_byte = (value >> 16) & 0xFF;
  66. uint8_t second_byte = (value >> 8) & 0xFF;
  67. uint8_t third_byte = (uint8_t)(value & 0xFF);
  68. shift_out_single(first_byte);
  69. shift_out_single(second_byte);
  70. shift_out_single(third_byte);
  71. gpio_write_pin_high(SHR_LATCH);
  72. /* We delay here to prevent multiple consecutive keys being triggered with a single switch press */
  73. _delay_us(10);
  74. }
  75. static void shift_out_single(uint8_t value) {
  76. for (uint8_t i = 0; i < 8; i++) {
  77. if (value & 0b10000000) {
  78. gpio_write_pin_high(SHR_DATA);
  79. } else {
  80. gpio_write_pin_low(SHR_DATA);
  81. }
  82. shift_pulse();
  83. value = value << 1;
  84. }
  85. }
  86. static inline void shift_pulse(void) {
  87. gpio_write_pin_high(SHR_CLOCK);
  88. gpio_write_pin_low(SHR_CLOCK);
  89. }