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.

128 lines
4.0 KiB

  1. /*
  2. * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
  3. * Copyright 2019 Drashna Jaelre (Christopher Courtney) <drashna@live.com>
  4. *
  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. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  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. #include <string.h> // for memcpy
  19. #include "dip_switch.h"
  20. #include "gpio.h"
  21. #include "util.h"
  22. #ifdef SPLIT_KEYBOARD
  23. # include "split_common/split_util.h"
  24. #endif
  25. #if !defined(DIP_SWITCH_PINS) && !defined(DIP_SWITCH_MATRIX_GRID)
  26. # error "Either DIP_SWITCH_PINS or DIP_SWITCH_MATRIX_GRID must be defined."
  27. #endif
  28. #if defined(DIP_SWITCH_PINS) && defined(DIP_SWITCH_MATRIX_GRID)
  29. # error "Both DIP_SWITCH_PINS and DIP_SWITCH_MATRIX_GRID are defined."
  30. #endif
  31. #ifdef DIP_SWITCH_PINS
  32. # define NUMBER_OF_DIP_SWITCHES (ARRAY_SIZE(dip_switch_pad))
  33. static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
  34. #endif
  35. #ifdef DIP_SWITCH_MATRIX_GRID
  36. typedef struct matrix_index_t {
  37. uint8_t row;
  38. uint8_t col;
  39. } matrix_index_t;
  40. # define NUMBER_OF_DIP_SWITCHES (ARRAY_SIZE(dip_switch_pad))
  41. static matrix_index_t dip_switch_pad[] = DIP_SWITCH_MATRIX_GRID;
  42. extern bool peek_matrix(uint8_t row_index, uint8_t col_index, bool read_raw);
  43. static uint16_t scan_count;
  44. #endif /* DIP_SWITCH_MATRIX_GRID */
  45. static bool dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0};
  46. static bool last_dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0};
  47. __attribute__((weak)) bool dip_switch_update_user(uint8_t index, bool active) {
  48. return true;
  49. }
  50. __attribute__((weak)) bool dip_switch_update_kb(uint8_t index, bool active) {
  51. return dip_switch_update_user(index, active);
  52. }
  53. __attribute__((weak)) bool dip_switch_update_mask_user(uint32_t state) {
  54. return true;
  55. }
  56. __attribute__((weak)) bool dip_switch_update_mask_kb(uint32_t state) {
  57. return dip_switch_update_mask_user(state);
  58. }
  59. void dip_switch_init(void) {
  60. #ifdef DIP_SWITCH_PINS
  61. # if defined(SPLIT_KEYBOARD) && defined(DIP_SWITCH_PINS_RIGHT)
  62. if (!isLeftHand) {
  63. const pin_t dip_switch_pad_right[] = DIP_SWITCH_PINS_RIGHT;
  64. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  65. dip_switch_pad[i] = dip_switch_pad_right[i];
  66. }
  67. }
  68. # endif
  69. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  70. setPinInputHigh(dip_switch_pad[i]);
  71. }
  72. dip_switch_read(true);
  73. #endif
  74. #ifdef DIP_SWITCH_MATRIX_GRID
  75. scan_count = 0;
  76. #endif
  77. }
  78. void dip_switch_read(bool forced) {
  79. bool has_dip_state_changed = false;
  80. uint32_t dip_switch_mask = 0;
  81. #ifdef DIP_SWITCH_MATRIX_GRID
  82. bool read_raw = false;
  83. if (scan_count < 500) {
  84. scan_count++;
  85. if (scan_count == 10) {
  86. read_raw = true;
  87. forced = true; /* First reading of the dip switch */
  88. } else {
  89. return;
  90. }
  91. }
  92. #endif
  93. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  94. #ifdef DIP_SWITCH_PINS
  95. dip_switch_state[i] = !readPin(dip_switch_pad[i]);
  96. #endif
  97. #ifdef DIP_SWITCH_MATRIX_GRID
  98. dip_switch_state[i] = peek_matrix(dip_switch_pad[i].row, dip_switch_pad[i].col, read_raw);
  99. #endif
  100. dip_switch_mask |= dip_switch_state[i] << i;
  101. if (last_dip_switch_state[i] != dip_switch_state[i] || forced) {
  102. has_dip_state_changed = true;
  103. dip_switch_update_kb(i, dip_switch_state[i]);
  104. }
  105. }
  106. if (has_dip_state_changed) {
  107. dip_switch_update_mask_kb(dip_switch_mask);
  108. memcpy(last_dip_switch_state, dip_switch_state, sizeof(dip_switch_state));
  109. }
  110. }