aboutsummaryrefslogtreecommitdiff
path: root/keyboards/whitefox/matrix.c
diff options
context:
space:
mode:
authorKaleb Elwert <belak@coded.io>2017-01-03 23:41:52 -0800
committerKaleb Elwert <belak@coded.io>2017-01-03 23:41:52 -0800
commit8377d5fdc57ab0a4f1757d9f787592ede1b94cfe (patch)
tree592977342bf2e36d73742c5334a7c4d8da308050 /keyboards/whitefox/matrix.c
parent847377fb43356cc1d3dff20ff1d5030a833cca41 (diff)
downloadqmk_firmware-8377d5fdc57ab0a4f1757d9f787592ede1b94cfe.tar.gz
qmk_firmware-8377d5fdc57ab0a4f1757d9f787592ede1b94cfe.zip
Initial whitefox support
Diffstat (limited to 'keyboards/whitefox/matrix.c')
-rw-r--r--keyboards/whitefox/matrix.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/keyboards/whitefox/matrix.c b/keyboards/whitefox/matrix.c
new file mode 100644
index 000000000..ce35bca28
--- /dev/null
+++ b/keyboards/whitefox/matrix.c
@@ -0,0 +1,132 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <string.h>
4#include "hal.h"
5#include "timer.h"
6#include "wait.h"
7#include "print.h"
8#include "matrix.h"
9
10
11/*
12 * Matt3o's WhiteFox
13 * Column pins are input with internal pull-down. Row pins are output and strobe with high.
14 * Key is high or 1 when it turns on.
15 *
16 * col: { PTD0, PTD1, PTD4, PTD5, PTD6, PTD7, PTC1, PTC2 }
17 * row: { PTB2, PTB3, PTB18, PTB19, PTC0, PTC8, PTC9, PTC10, PTC11 }
18 */
19/* matrix state(1:on, 0:off) */
20static matrix_row_t matrix[MATRIX_ROWS];
21static matrix_row_t matrix_debouncing[MATRIX_ROWS];
22static bool debouncing = false;
23static uint16_t debouncing_time = 0;
24
25
26void matrix_init(void)
27{
28//debug_matrix = true;
29 /* Column(sense) */
30 palSetPadMode(GPIOD, 0, PAL_MODE_INPUT_PULLDOWN);
31 palSetPadMode(GPIOD, 1, PAL_MODE_INPUT_PULLDOWN);
32 palSetPadMode(GPIOD, 4, PAL_MODE_INPUT_PULLDOWN);
33 palSetPadMode(GPIOD, 5, PAL_MODE_INPUT_PULLDOWN);
34 palSetPadMode(GPIOD, 6, PAL_MODE_INPUT_PULLDOWN);
35 palSetPadMode(GPIOD, 7, PAL_MODE_INPUT_PULLDOWN);
36 palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_PULLDOWN);
37 palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_PULLDOWN);
38
39 /* Row(strobe) */
40 palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
41 palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL);
42 palSetPadMode(GPIOB, 18, PAL_MODE_OUTPUT_PUSHPULL);
43 palSetPadMode(GPIOB, 19, PAL_MODE_OUTPUT_PUSHPULL);
44 palSetPadMode(GPIOC, 0, PAL_MODE_OUTPUT_PUSHPULL);
45 palSetPadMode(GPIOC, 8, PAL_MODE_OUTPUT_PUSHPULL);
46 palSetPadMode(GPIOC, 9, PAL_MODE_OUTPUT_PUSHPULL);
47 palSetPadMode(GPIOC, 10, PAL_MODE_OUTPUT_PUSHPULL);
48 palSetPadMode(GPIOC, 11, PAL_MODE_OUTPUT_PUSHPULL);
49
50 memset(matrix, 0, MATRIX_ROWS);
51 memset(matrix_debouncing, 0, MATRIX_ROWS);
52}
53
54uint8_t matrix_scan(void)
55{
56 for (int row = 0; row < MATRIX_ROWS; row++) {
57 matrix_row_t data = 0;
58
59 // strobe row
60 switch (row) {
61 case 0: palSetPad(GPIOB, 2); break;
62 case 1: palSetPad(GPIOB, 3); break;
63 case 2: palSetPad(GPIOB, 18); break;
64 case 3: palSetPad(GPIOB, 19); break;
65 case 4: palSetPad(GPIOC, 0); break;
66 case 5: palSetPad(GPIOC, 8); break;
67 case 6: palSetPad(GPIOC, 9); break;
68 case 7: palSetPad(GPIOC, 10); break;
69 case 8: palSetPad(GPIOC, 11); break;
70 }
71
72 wait_us(1); // need wait to settle pin state
73
74 // read col data: { PTD0, PTD1, PTD4, PTD5, PTD6, PTD7, PTC1, PTC2 }
75 data = ((palReadPort(GPIOC) & 0x06UL) << 5) |
76 ((palReadPort(GPIOD) & 0xF0UL) >> 2) |
77 (palReadPort(GPIOD) & 0x03UL);
78
79 // un-strobe row
80 switch (row) {
81 case 0: palClearPad(GPIOB, 2); break;
82 case 1: palClearPad(GPIOB, 3); break;
83 case 2: palClearPad(GPIOB, 18); break;
84 case 3: palClearPad(GPIOB, 19); break;
85 case 4: palClearPad(GPIOC, 0); break;
86 case 5: palClearPad(GPIOC, 8); break;
87 case 6: palClearPad(GPIOC, 9); break;
88 case 7: palClearPad(GPIOC, 10); break;
89 case 8: palClearPad(GPIOC, 11); break;
90 }
91
92 if (matrix_debouncing[row] != data) {
93 matrix_debouncing[row] = data;
94 debouncing = true;
95 debouncing_time = timer_read();
96 }
97 }
98
99 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
100 for (int row = 0; row < MATRIX_ROWS; row++) {
101 matrix[row] = matrix_debouncing[row];
102 }
103 debouncing = false;
104 }
105 return 1;
106}
107
108bool matrix_is_on(uint8_t row, uint8_t col)
109{
110 return (matrix[row] & (1<<col));
111}
112
113matrix_row_t matrix_get_row(uint8_t row)
114{
115 return matrix[row];
116}
117
118void matrix_print(void)
119{
120 xprintf("\nr/c 01234567\n");
121 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
122 xprintf("%X0: ", row);
123 matrix_row_t data = matrix_get_row(row);
124 for (int col = 0; col < MATRIX_COLS; col++) {
125 if (data & (1<<col))
126 xprintf("1");
127 else
128 xprintf("0");
129 }
130 xprintf("\n");
131 }
132}