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.

118 lines
3.9 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) { return true; }
  47. __attribute__((weak)) bool dip_switch_update_kb(uint8_t index, bool active) { return dip_switch_update_user(index, active); }
  48. __attribute__((weak)) bool dip_switch_update_mask_user(uint32_t state) { return true; }
  49. __attribute__((weak)) bool dip_switch_update_mask_kb(uint32_t state) { return dip_switch_update_mask_user(state); }
  50. void dip_switch_init(void) {
  51. #ifdef DIP_SWITCH_PINS
  52. # if defined(SPLIT_KEYBOARD) && defined(DIP_SWITCH_PINS_RIGHT)
  53. if (!isLeftHand) {
  54. const pin_t dip_switch_pad_right[] = DIP_SWITCH_PINS_RIGHT;
  55. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  56. dip_switch_pad[i] = dip_switch_pad_right[i];
  57. }
  58. }
  59. # endif
  60. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  61. setPinInputHigh(dip_switch_pad[i]);
  62. }
  63. dip_switch_read(true);
  64. #endif
  65. #ifdef DIP_SWITCH_MATRIX_GRID
  66. scan_count = 0;
  67. #endif
  68. }
  69. void dip_switch_read(bool forced) {
  70. bool has_dip_state_changed = false;
  71. uint32_t dip_switch_mask = 0;
  72. #ifdef DIP_SWITCH_MATRIX_GRID
  73. bool read_raw = false;
  74. if (scan_count < 500) {
  75. scan_count++;
  76. if (scan_count == 10) {
  77. read_raw = true;
  78. forced = true; /* First reading of the dip switch */
  79. } else {
  80. return;
  81. }
  82. }
  83. #endif
  84. for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) {
  85. #ifdef DIP_SWITCH_PINS
  86. dip_switch_state[i] = !readPin(dip_switch_pad[i]);
  87. #endif
  88. #ifdef DIP_SWITCH_MATRIX_GRID
  89. dip_switch_state[i] = peek_matrix(dip_switch_pad[i].row, dip_switch_pad[i].col, read_raw);
  90. #endif
  91. dip_switch_mask |= dip_switch_state[i] << i;
  92. if (last_dip_switch_state[i] != dip_switch_state[i] || forced) {
  93. has_dip_state_changed = true;
  94. dip_switch_update_kb(i, dip_switch_state[i]);
  95. }
  96. }
  97. if (has_dip_state_changed) {
  98. dip_switch_update_mask_kb(dip_switch_mask);
  99. memcpy(last_dip_switch_state, dip_switch_state, sizeof(dip_switch_state));
  100. }
  101. }