]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/jm60/matrix.c
several improvements for mitosis:datagrok (#1960)
[qmk_firmware.git] / keyboards / jm60 / matrix.c
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  * JM60
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: { PTA15, PTC10, PTC11, PTC12, PTD2, PTB3, PTB4, PTB5, PTB6, PTB7, PTB8, PTB9, PTA2, PTA3 }
17  *     row: { PTB11, PTB10, PTB2, PTB1, PTB0}
18  */
19 /* matrix state(1:on, 0:off) */
20 static matrix_row_t matrix[MATRIX_ROWS];
21 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
22 static bool debouncing = false;
23 static uint16_t debouncing_time = 0;
24
25
26 void matrix_init(void)
27 {
28 //debug_matrix = true;
29     /* Column(sense) */
30     palSetPadMode(GPIOA, 15,  PAL_MODE_INPUT_PULLDOWN);
31     palSetPadMode(GPIOC, 10,  PAL_MODE_INPUT_PULLDOWN);
32     palSetPadMode(GPIOC, 11,  PAL_MODE_INPUT_PULLDOWN);
33     palSetPadMode(GPIOC, 12,  PAL_MODE_INPUT_PULLDOWN);
34     palSetPadMode(GPIOD, 2,  PAL_MODE_INPUT_PULLDOWN);
35     palSetPadMode(GPIOB, 3,  PAL_MODE_INPUT_PULLDOWN);
36     palSetPadMode(GPIOB, 4,  PAL_MODE_INPUT_PULLDOWN);
37     palSetPadMode(GPIOB, 5,  PAL_MODE_INPUT_PULLDOWN);
38     palSetPadMode(GPIOB, 6,  PAL_MODE_INPUT_PULLDOWN);
39     palSetPadMode(GPIOB, 7,  PAL_MODE_INPUT_PULLDOWN);
40     palSetPadMode(GPIOB, 8,  PAL_MODE_INPUT_PULLDOWN);
41     palSetPadMode(GPIOB, 9,  PAL_MODE_INPUT_PULLDOWN);
42     palSetPadMode(GPIOA, 2,  PAL_MODE_INPUT_PULLDOWN);
43     palSetPadMode(GPIOA, 3,  PAL_MODE_INPUT_PULLDOWN);
44
45     /* Row(strobe) */
46     palSetPadMode(GPIOB, 11,  PAL_MODE_OUTPUT_PUSHPULL);
47     palSetPadMode(GPIOB, 10,  PAL_MODE_OUTPUT_PUSHPULL);
48     palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL);
49     palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL);
50     palSetPadMode(GPIOB, 0,  PAL_MODE_OUTPUT_PUSHPULL);
51
52     memset(matrix, 0, MATRIX_ROWS);
53     memset(matrix_debouncing, 0, MATRIX_ROWS);
54 }
55
56 uint8_t matrix_scan(void)
57 {
58     for (int row = 0; row < MATRIX_ROWS; row++) {
59         matrix_row_t data = 0;
60
61         // strobe row
62         switch (row) {
63             case 0: palSetPad(GPIOB, 11);    break;
64             case 1: palSetPad(GPIOB, 10);    break;
65             case 2: palSetPad(GPIOB, 2);        break;
66             case 3: palSetPad(GPIOB, 1);   break;
67             case 4: palSetPad(GPIOB, 0);    break;
68         }
69
70         wait_us(20); // need wait to settle pin state
71
72         // read col data: { PTA15, PTC10, PTC11, PTC12, PTD2, PTB3, PTB4, PTB5, PTB6, PTB7, PTB8, PTB9, PTA2, PTA3 }
73         data = ((palReadPort(GPIOA) & 0x8000UL) >> 15) |        // 0
74                    ((palReadPort(GPIOC) & 0x1C00UL) >> 9) |             // 1, 2, 3
75                            ((palReadPort(GPIOD) & 0x0004UL) << 2) |             // 4
76                            ((palReadPort(GPIOB) & 0x03F8UL) << 2) |             // 5, 6, 7, 8, 9, 10, 11
77                            ((palReadPort(GPIOA) & 0x000CUL) << 10);    // 12, 13
78
79         // un-strobe row
80         switch (row) {
81                 case 0: palClearPad(GPIOB, 11);    break;
82                 case 1: palClearPad(GPIOB, 10);    break;
83                 case 2: palClearPad(GPIOB, 2);    break;
84                 case 3: palClearPad(GPIOB, 1);   break;
85                 case 4: palClearPad(GPIOB, 0);    break;
86         }
87
88         if (matrix_debouncing[row] != data) {
89             matrix_debouncing[row] = data;
90             debouncing = true;
91             debouncing_time = timer_read();
92         }
93     }
94
95     if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
96         for (int row = 0; row < MATRIX_ROWS; row++) {
97             matrix[row] = matrix_debouncing[row];
98         }
99         debouncing = false;
100     }
101     return 1;
102 }
103
104 bool matrix_is_on(uint8_t row, uint8_t col)
105 {
106     return (matrix[row] & (1<<col));
107 }
108
109 matrix_row_t matrix_get_row(uint8_t row)
110 {
111     return matrix[row];
112 }
113
114 void matrix_print(void)
115 {
116     xprintf("\nr/c 01234567\n");
117     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
118         xprintf("%X0: ", row);
119         matrix_row_t data = matrix_get_row(row);
120         for (int col = 0; col < MATRIX_COLS; col++) {
121             if (data & (1<<col))
122                 xprintf("1");
123             else
124                 xprintf("0");
125         }
126         xprintf("\n");
127     }
128 }