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.

151 lines
4.7 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. #ifdef SPLIT_KEYBOARD
  21. # include "split_common/split_util.h"
  22. #endif
  23. #if !defined(DIP_SWITCH_PINS) && !defined(DIP_SWITCH_MATRIX_GRID)
  24. # error "Either DIP_SWITCH_PINS or DIP_SWITCH_MATRIX_GRID must be defined."
  25. #endif
  26. #if defined(DIP_SWITCH_PINS) && defined(DIP_SWITCH_MATRIX_GRID)
  27. # error "Both DIP_SWITCH_PINS and DIP_SWITCH_MATRIX_GRID are defined."
  28. #endif
  29. #ifdef DIP_SWITCH_PINS
  30. static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
  31. #endif
  32. #ifdef DIP_SWITCH_MATRIX_GRID
  33. static matrix_intersection_t dip_switch_pad[] = DIP_SWITCH_MATRIX_GRID;
  34. extern bool peek_matrix(uint8_t row_index, uint8_t col_index, bool read_raw);
  35. static uint16_t scan_count;
  36. #endif /* DIP_SWITCH_MATRIX_GRID */
  37. static bool dip_switch_state[NUM_DIP_SWITCHES] = {0};
  38. static bool last_dip_switch_state[NUM_DIP_SWITCHES] = {0};
  39. __attribute__((weak)) bool dip_switch_update_user(uint8_t index, bool active) {
  40. return true;
  41. }
  42. __attribute__((weak)) bool dip_switch_update_kb(uint8_t index, bool active) {
  43. return dip_switch_update_user(index, active);
  44. }
  45. __attribute__((weak)) bool dip_switch_update_mask_user(uint32_t state) {
  46. return true;
  47. }
  48. __attribute__((weak)) bool dip_switch_update_mask_kb(uint32_t state) {
  49. return dip_switch_update_mask_user(state);
  50. }
  51. #ifdef DIP_SWITCH_MAP_ENABLE
  52. # include "keymap_introspection.h"
  53. # include "action.h"
  54. # ifndef DIP_SWITCH_MAP_KEY_DELAY
  55. # define DIP_SWITCH_MAP_KEY_DELAY TAP_CODE_DELAY
  56. # endif
  57. static void dip_switch_exec_mapping(uint8_t index, bool on) {
  58. // The delays below cater for Windows and its wonderful requirements.
  59. action_exec(on ? MAKE_DIPSWITCH_ON_EVENT(index, true) : MAKE_DIPSWITCH_OFF_EVENT(index, true));
  60. # if DIP_SWITCH_MAP_KEY_DELAY > 0
  61. wait_ms(DIP_SWITCH_MAP_KEY_DELAY);
  62. # endif // DIP_SWITCH_MAP_KEY_DELAY > 0
  63. action_exec(on ? MAKE_DIPSWITCH_ON_EVENT(index, false) : MAKE_DIPSWITCH_OFF_EVENT(index, false));
  64. # if DIP_SWITCH_MAP_KEY_DELAY > 0
  65. wait_ms(DIP_SWITCH_MAP_KEY_DELAY);
  66. # endif // DIP_SWITCH_MAP_KEY_DELAY > 0
  67. }
  68. #endif // DIP_SWITCH_MAP_ENABLE
  69. void dip_switch_init(void) {
  70. #ifdef DIP_SWITCH_PINS
  71. # if defined(SPLIT_KEYBOARD) && defined(DIP_SWITCH_PINS_RIGHT)
  72. if (!isLeftHand) {
  73. const pin_t dip_switch_pad_right[] = DIP_SWITCH_PINS_RIGHT;
  74. for (uint8_t i = 0; i < NUM_DIP_SWITCHES; i++) {
  75. dip_switch_pad[i] = dip_switch_pad_right[i];
  76. }
  77. }
  78. # endif
  79. for (uint8_t i = 0; i < NUM_DIP_SWITCHES; i++) {
  80. gpio_set_pin_input_high(dip_switch_pad[i]);
  81. }
  82. dip_switch_read(true);
  83. #endif
  84. #ifdef DIP_SWITCH_MATRIX_GRID
  85. scan_count = 0;
  86. #endif
  87. }
  88. void dip_switch_read(bool forced) {
  89. bool has_dip_state_changed = false;
  90. uint32_t dip_switch_mask = 0;
  91. #ifdef DIP_SWITCH_MATRIX_GRID
  92. bool read_raw = false;
  93. if (scan_count < 500) {
  94. scan_count++;
  95. if (scan_count == 10) {
  96. read_raw = true;
  97. forced = true; /* First reading of the dip switch */
  98. } else {
  99. return;
  100. }
  101. }
  102. #endif
  103. for (uint8_t i = 0; i < NUM_DIP_SWITCHES; i++) {
  104. #ifdef DIP_SWITCH_PINS
  105. dip_switch_state[i] = !gpio_read_pin(dip_switch_pad[i]);
  106. #endif
  107. #ifdef DIP_SWITCH_MATRIX_GRID
  108. dip_switch_state[i] = peek_matrix(dip_switch_pad[i].row, dip_switch_pad[i].col, read_raw);
  109. #endif
  110. dip_switch_mask |= dip_switch_state[i] << i;
  111. if (last_dip_switch_state[i] != dip_switch_state[i] || forced) {
  112. has_dip_state_changed = true;
  113. #ifndef DIP_SWITCH_MAP_ENABLE
  114. dip_switch_update_kb(i, dip_switch_state[i]);
  115. #else
  116. dip_switch_exec_mapping(i, dip_switch_state[i]);
  117. #endif
  118. }
  119. }
  120. if (has_dip_state_changed) {
  121. #ifndef DIP_SWITCH_MAP_ENABLE
  122. dip_switch_update_mask_kb(dip_switch_mask);
  123. #endif
  124. memcpy(last_dip_switch_state, dip_switch_state, sizeof(dip_switch_state));
  125. }
  126. }
  127. void dip_switch_task(void) {
  128. dip_switch_read(false);
  129. }