logo

qmk_firmware

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

dip_switch.c (4841B)


  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. # include "wait.h"
  55. # ifndef DIP_SWITCH_MAP_KEY_DELAY
  56. # define DIP_SWITCH_MAP_KEY_DELAY TAP_CODE_DELAY
  57. # endif
  58. static void dip_switch_exec_mapping(uint8_t index, bool on) {
  59. // The delays below cater for Windows and its wonderful requirements.
  60. action_exec(on ? MAKE_DIPSWITCH_ON_EVENT(index, true) : MAKE_DIPSWITCH_OFF_EVENT(index, true));
  61. # if DIP_SWITCH_MAP_KEY_DELAY > 0
  62. wait_ms(DIP_SWITCH_MAP_KEY_DELAY);
  63. # endif // DIP_SWITCH_MAP_KEY_DELAY > 0
  64. action_exec(on ? MAKE_DIPSWITCH_ON_EVENT(index, false) : MAKE_DIPSWITCH_OFF_EVENT(index, false));
  65. # if DIP_SWITCH_MAP_KEY_DELAY > 0
  66. wait_ms(DIP_SWITCH_MAP_KEY_DELAY);
  67. # endif // DIP_SWITCH_MAP_KEY_DELAY > 0
  68. }
  69. #endif // DIP_SWITCH_MAP_ENABLE
  70. void dip_switch_init(void) {
  71. #ifdef DIP_SWITCH_PINS
  72. # if defined(SPLIT_KEYBOARD) && defined(DIP_SWITCH_PINS_RIGHT)
  73. if (!isLeftHand) {
  74. const pin_t dip_switch_pad_right[] = DIP_SWITCH_PINS_RIGHT;
  75. for (uint8_t i = 0; i < NUM_DIP_SWITCHES; i++) {
  76. dip_switch_pad[i] = dip_switch_pad_right[i];
  77. }
  78. }
  79. # endif
  80. for (uint8_t i = 0; i < NUM_DIP_SWITCHES; i++) {
  81. gpio_set_pin_input_high(dip_switch_pad[i]);
  82. }
  83. dip_switch_read(true);
  84. #endif
  85. #ifdef DIP_SWITCH_MATRIX_GRID
  86. scan_count = 0;
  87. #endif
  88. }
  89. void dip_switch_read(bool forced) {
  90. bool has_dip_state_changed = false;
  91. uint32_t dip_switch_mask = 0;
  92. #ifdef DIP_SWITCH_MATRIX_GRID
  93. bool read_raw = false;
  94. if (scan_count < 500) {
  95. scan_count++;
  96. if (scan_count == 10) {
  97. read_raw = true;
  98. forced = true; /* First reading of the dip switch */
  99. } else {
  100. return;
  101. }
  102. }
  103. #endif
  104. for (uint8_t i = 0; i < NUM_DIP_SWITCHES; i++) {
  105. #ifdef DIP_SWITCH_PINS
  106. dip_switch_state[i] = !gpio_read_pin(dip_switch_pad[i]);
  107. #endif
  108. #ifdef DIP_SWITCH_MATRIX_GRID
  109. dip_switch_state[i] = peek_matrix(dip_switch_pad[i].row, dip_switch_pad[i].col, read_raw);
  110. #endif
  111. dip_switch_mask |= dip_switch_state[i] << i;
  112. if (last_dip_switch_state[i] != dip_switch_state[i] || forced) {
  113. has_dip_state_changed = true;
  114. #ifndef DIP_SWITCH_MAP_ENABLE
  115. dip_switch_update_kb(i, dip_switch_state[i]);
  116. #else
  117. dip_switch_exec_mapping(i, dip_switch_state[i]);
  118. #endif
  119. }
  120. }
  121. if (has_dip_state_changed) {
  122. #ifndef DIP_SWITCH_MAP_ENABLE
  123. dip_switch_update_mask_kb(dip_switch_mask);
  124. #endif
  125. memcpy(last_dip_switch_state, dip_switch_state, sizeof(dip_switch_state));
  126. }
  127. }
  128. void dip_switch_task(void) {
  129. dip_switch_read(false);
  130. }