diff options
| author | Takeshi ISHII <2170248+mtei@users.noreply.github.com> | 2020-07-04 23:20:49 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-04 23:20:49 +0900 |
| commit | c2ca57c8f4defd8fc7b7911cc1ba1e49f3d483e1 (patch) | |
| tree | dfa58840f11aeb4c2b71aed5f35f9e101a72a4d0 /quantum | |
| parent | 5c8b23ccffa0083752044f0459e6ac3114ce6e52 (diff) | |
| download | qmk_firmware-c2ca57c8f4defd8fc7b7911cc1ba1e49f3d483e1.tar.gz qmk_firmware-c2ca57c8f4defd8fc7b7911cc1ba1e49f3d483e1.zip | |
add DIP_SWITCH_MATRIX_GRID support (#8772)
* dipsw test on helix/rev2/sc/back:five_rows
* add peek_matrix() to matrix_common.c
* add DIP_SWITCH_MATRIX_GRID support to quantum/dip_switch.c
* update docs/feature_dip_switch.md about DIP_SWITCH_MATRIX_GRID
* Test end. remove test code. Revert "dipsw test on helix/rev2/sc/back:five_rows"
This reverts commit 6d4304c74557597c9fb4d324f79c3ae4793ae874.
Diffstat (limited to 'quantum')
| -rw-r--r-- | quantum/dip_switch.c | 52 | ||||
| -rw-r--r-- | quantum/matrix_common.c | 5 |
2 files changed, 53 insertions, 4 deletions
diff --git a/quantum/dip_switch.c b/quantum/dip_switch.c index 66c166ce4..879326d21 100644 --- a/quantum/dip_switch.c +++ b/quantum/dip_switch.c | |||
| @@ -21,12 +21,31 @@ | |||
| 21 | // for memcpy | 21 | // for memcpy |
| 22 | #include <string.h> | 22 | #include <string.h> |
| 23 | 23 | ||
| 24 | #if !defined(DIP_SWITCH_PINS) | 24 | #if !defined(DIP_SWITCH_PINS) && !defined(DIP_SWITCH_MATRIX_GRID) |
| 25 | # error "No DIP switch pads defined by DIP_SWITCH_PINS" | 25 | # error "Either DIP_SWITCH_PINS or DIP_SWITCH_MATRIX_GRID must be defined." |
| 26 | #endif | 26 | #endif |
| 27 | 27 | ||
| 28 | #define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t)) | 28 | #if defined(DIP_SWITCH_PINS) && defined(DIP_SWITCH_MATRIX_GRID) |
| 29 | static pin_t dip_switch_pad[] = DIP_SWITCH_PINS; | 29 | # error "Both DIP_SWITCH_PINS and DIP_SWITCH_MATRIX_GRID are defined." |
| 30 | #endif | ||
| 31 | |||
| 32 | #ifdef DIP_SWITCH_PINS | ||
| 33 | # define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t)) | ||
| 34 | static pin_t dip_switch_pad[] = DIP_SWITCH_PINS; | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #ifdef DIP_SWITCH_MATRIX_GRID | ||
| 38 | typedef struct matrix_index_t { | ||
| 39 | uint8_t row; | ||
| 40 | uint8_t col; | ||
| 41 | } matrix_index_t; | ||
| 42 | |||
| 43 | # define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(matrix_index_t)) | ||
| 44 | static matrix_index_t dip_switch_pad[] = DIP_SWITCH_MATRIX_GRID; | ||
| 45 | extern bool peek_matrix(uint8_t row_index, uint8_t col_index, bool read_raw); | ||
| 46 | static uint16_t scan_count; | ||
| 47 | #endif /* DIP_SWITCH_MATRIX_GRID */ | ||
| 48 | |||
| 30 | static bool dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0}; | 49 | static bool dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0}; |
| 31 | static bool last_dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0}; | 50 | static bool last_dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0}; |
| 32 | 51 | ||
| @@ -39,18 +58,43 @@ __attribute__((weak)) void dip_switch_update_mask_user(uint32_t state) {} | |||
| 39 | __attribute__((weak)) void dip_switch_update_mask_kb(uint32_t state) { dip_switch_update_mask_user(state); } | 58 | __attribute__((weak)) void dip_switch_update_mask_kb(uint32_t state) { dip_switch_update_mask_user(state); } |
| 40 | 59 | ||
| 41 | void dip_switch_init(void) { | 60 | void dip_switch_init(void) { |
| 61 | #ifdef DIP_SWITCH_PINS | ||
| 42 | for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) { | 62 | for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) { |
| 43 | setPinInputHigh(dip_switch_pad[i]); | 63 | setPinInputHigh(dip_switch_pad[i]); |
| 44 | } | 64 | } |
| 45 | dip_switch_read(true); | 65 | dip_switch_read(true); |
| 66 | #endif | ||
| 67 | #ifdef DIP_SWITCH_MATRIX_GRID | ||
| 68 | scan_count = 0; | ||
| 69 | #endif | ||
| 46 | } | 70 | } |
| 47 | 71 | ||
| 48 | void dip_switch_read(bool forced) { | 72 | void dip_switch_read(bool forced) { |
| 49 | bool has_dip_state_changed = false; | 73 | bool has_dip_state_changed = false; |
| 50 | uint32_t dip_switch_mask = 0; | 74 | uint32_t dip_switch_mask = 0; |
| 51 | 75 | ||
| 76 | #ifdef DIP_SWITCH_MATRIX_GRID | ||
| 77 | bool read_raw = false; | ||
| 78 | |||
| 79 | if (scan_count < 500) { | ||
| 80 | scan_count ++; | ||
| 81 | if (scan_count == 10) { | ||
| 82 | read_raw = true; | ||
| 83 | forced = true; /* First reading of the dip switch */ | ||
| 84 | } else { | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | #endif | ||
| 89 | |||
| 52 | for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) { | 90 | for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) { |
| 91 | #ifdef DIP_SWITCH_PINS | ||
| 53 | dip_switch_state[i] = !readPin(dip_switch_pad[i]); | 92 | dip_switch_state[i] = !readPin(dip_switch_pad[i]); |
| 93 | #endif | ||
| 94 | #ifdef DIP_SWITCH_MATRIX_GRID | ||
| 95 | dip_switch_state[i] = peek_matrix(dip_switch_pad[i].row, dip_switch_pad[i].col, | ||
| 96 | read_raw); | ||
| 97 | #endif | ||
| 54 | dip_switch_mask |= dip_switch_state[i] << i; | 98 | dip_switch_mask |= dip_switch_state[i] << i; |
| 55 | if (last_dip_switch_state[i] != dip_switch_state[i] || forced) { | 99 | if (last_dip_switch_state[i] != dip_switch_state[i] || forced) { |
| 56 | has_dip_state_changed = true; | 100 | has_dip_state_changed = true; |
diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index de62b8070..e7d2dbb29 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c | |||
| @@ -112,3 +112,8 @@ __attribute__((weak)) uint8_t matrix_scan(void) { | |||
| 112 | matrix_scan_quantum(); | 112 | matrix_scan_quantum(); |
| 113 | return changed; | 113 | return changed; |
| 114 | } | 114 | } |
| 115 | |||
| 116 | __attribute__((weak)) bool peek_matrix(uint8_t row_index, uint8_t col_index, bool raw) { | ||
| 117 | return 0 != ( (raw? raw_matrix[row_index]:matrix[row_index]) | ||
| 118 | & (MATRIX_ROW_SHIFTER << col_index)); | ||
| 119 | } | ||
