aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi ISHII <2170248+mtei@users.noreply.github.com>2020-07-04 23:20:49 +0900
committerGitHub <noreply@github.com>2020-07-04 23:20:49 +0900
commitc2ca57c8f4defd8fc7b7911cc1ba1e49f3d483e1 (patch)
treedfa58840f11aeb4c2b71aed5f35f9e101a72a4d0
parent5c8b23ccffa0083752044f0459e6ac3114ce6e52 (diff)
downloadqmk_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.
-rw-r--r--docs/feature_dip_switch.md14
-rw-r--r--quantum/dip_switch.c52
-rw-r--r--quantum/matrix_common.c5
3 files changed, 67 insertions, 4 deletions
diff --git a/docs/feature_dip_switch.md b/docs/feature_dip_switch.md
index bce47fed8..15e449c4c 100644
--- a/docs/feature_dip_switch.md
+++ b/docs/feature_dip_switch.md
@@ -7,9 +7,17 @@ DIP switches are supported by adding this to your `rules.mk`:
7and this to your `config.h`: 7and this to your `config.h`:
8 8
9```c 9```c
10// Connects each switch in the dip switch to the GPIO pin of the MCU
10#define DIP_SWITCH_PINS { B14, A15, A10, B9 } 11#define DIP_SWITCH_PINS { B14, A15, A10, B9 }
11``` 12```
12 13
14or
15
16```c
17// Connect each switch in the DIP switch to an unused intersections in the key matrix.
18#define DIP_SWITCH_MATRIX_GRID { {0,6}, {1,6}, {2,6} } // List of row and col pairs
19```
20
13## Callbacks 21## Callbacks
14 22
15The callback functions can be inserted into your `<keyboard>.c`: 23The callback functions can be inserted into your `<keyboard>.c`:
@@ -87,4 +95,10 @@ void dip_switch_update_mask_user(uint32_t state) {
87 95
88## Hardware 96## Hardware
89 97
98### Connects each switch in the dip switch to the GPIO pin of the MCU
99
90One side of the DIP switch should be wired directly to the pin on the MCU, and the other side to ground. It should not matter which side is connected to which, as it should be functionally the same. 100One side of the DIP switch should be wired directly to the pin on the MCU, and the other side to ground. It should not matter which side is connected to which, as it should be functionally the same.
101
102### Connect each switch in the DIP switch to an unused intersections in the key matrix.
103
104As with the keyswitch, a diode and DIP switch connect the ROW line to the COL line.
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)
29static 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))
34static pin_t dip_switch_pad[] = DIP_SWITCH_PINS;
35#endif
36
37#ifdef DIP_SWITCH_MATRIX_GRID
38typedef 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))
44static matrix_index_t dip_switch_pad[] = DIP_SWITCH_MATRIX_GRID;
45extern bool peek_matrix(uint8_t row_index, uint8_t col_index, bool read_raw);
46static uint16_t scan_count;
47#endif /* DIP_SWITCH_MATRIX_GRID */
48
30static bool dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0}; 49static bool dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0};
31static bool last_dip_switch_state[NUMBER_OF_DIP_SWITCHES] = {0}; 50static 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
41void dip_switch_init(void) { 60void 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
48void dip_switch_read(bool forced) { 72void 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}