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.

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