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.

126 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 "dip_switch.h"
  19. #ifdef SPLIT_KEYBOARD
  20. # include "split_common/split_util.h"
  21. #endif
  22. // for memcpy
  23. #include <string.h>
  24. #if !defined(DIP_SWITCH_PINS) && !defined(DIP_SWITCH_MATRIX_GRID)
  25. # error "Either DIP_SWITCH_PINS or DIP_SWITCH_MATRIX_GRID must be defined."
  26. #endif
  27. #if defined(DIP_SWITCH_PINS) && defined(DIP_SWITCH_MATRIX_GRID)
  28. # error "Both DIP_SWITCH_PINS and DIP_SWITCH_MATRIX_GRID are defined."
  29. #endif
  30. #ifdef DIP_SWITCH_PINS
  31. # define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t))
  32. static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
  33. #endif
  34. #ifdef DIP_SWITCH_MATRIX_GRID
  35. typedef struct matrix_index_t {
  36. uint8_t row;
  37. uint8_t col;
  38. } matrix_index_t;
  39. # define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(matrix_index_t))
  40. static matrix_index_t dip_switch_pad[] = DIP_SWITCH_MATRIX_GRID;
  41. extern bool peek_matrix(uint8_t row_index, uint8_t col_index, bool read_raw);
  42. static uint16_t scan_count;
  43. #endif /* DIP_SWITCH_MATRIX_GRID */
  44. static bool dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0};
  45. static bool last_dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0};
  46. __attribute__((weak)) bool dip_switch_update_user(uint8_t index, bool active) {
  47. return true;
  48. }
  49. __attribute__((weak)) bool dip_switch_update_kb(uint8_t index, bool active) {
  50. return dip_switch_update_user(index, active);
  51. }
  52. __attribute__((weak)) bool dip_switch_update_mask_user(uint32_t state) {
  53. return true;
  54. }
  55. __attribute__((weak)) bool dip_switch_update_mask_kb(uint32_t state) {
  56. return dip_switch_update_mask_user(state);
  57. }
  58. void dip_switch_init(void) {
  59. #ifdef DIP_SWITCH_PINS
  60. # if defined(SPLIT_KEYBOARD) && defined(DIP_SWITCH_PINS_RIGHT)
  61. if (!isLeftHand) {
  62. const pin_t dip_switch_pad_right[] = DIP_SWITCH_PINS_RIGHT;
  63. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  64. dip_switch_pad[i] = dip_switch_pad_right[i];
  65. }
  66. }
  67. # endif
  68. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  69. setPinInputHigh(dip_switch_pad[i]);
  70. }
  71. dip_switch_read(true);
  72. #endif
  73. #ifdef DIP_SWITCH_MATRIX_GRID
  74. scan_count = 0;
  75. #endif
  76. }
  77. void dip_switch_read(bool forced) {
  78. bool has_dip_state_changed = false;
  79. uint32_t dip_switch_mask = 0;
  80. #ifdef DIP_SWITCH_MATRIX_GRID
  81. bool read_raw = false;
  82. if (scan_count < 500) {
  83. scan_count++;
  84. if (scan_count == 10) {
  85. read_raw = true;
  86. forced = true; /* First reading of the dip switch */
  87. } else {
  88. return;
  89. }
  90. }
  91. #endif
  92. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  93. #ifdef DIP_SWITCH_PINS
  94. dip_switch_state[i] = !readPin(dip_switch_pad[i]);
  95. #endif
  96. #ifdef DIP_SWITCH_MATRIX_GRID
  97. dip_switch_state[i] = peek_matrix(dip_switch_pad[i].row, dip_switch_pad[i].col, read_raw);
  98. #endif
  99. dip_switch_mask |= dip_switch_state[i] << i;
  100. if (last_dip_switch_state[i] != dip_switch_state[i] || forced) {
  101. has_dip_state_changed = true;
  102. dip_switch_update_kb(i, dip_switch_state[i]);
  103. }
  104. }
  105. if (has_dip_state_changed) {
  106. dip_switch_update_mask_kb(dip_switch_mask);
  107. memcpy(last_dip_switch_state, dip_switch_state, sizeof(dip_switch_state));
  108. }
  109. }