]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/lets_split/uno_slave/uno-matrix.c
images, docks, clean-up [skip ci]
[qmk_firmware.git] / keyboards / lets_split / uno_slave / uno-matrix.c
1 #define F_CPU 16000000UL
2
3 #include <util/delay.h>
4 #include <avr/io.h>
5 #include <stdlib.h>
6
7 #include "uno-matrix.h"
8
9 #define debug(X) NULL
10 #define debug_hex(X) NULL
11
12 #ifndef DEBOUNCE
13 #   define DEBOUNCE  5
14 #endif
15
16 static uint8_t debouncing = DEBOUNCE;
17
18 /* matrix state(1:on, 0:off) */
19 static matrix_row_t matrix[MATRIX_ROWS];
20 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
21
22 static matrix_row_t read_cols(void);
23 static void init_cols(void);
24 static void unselect_rows(void);
25 static void select_row(uint8_t row);
26
27 inline
28 uint8_t matrix_rows(void)
29 {
30     return MATRIX_ROWS;
31 }
32
33 inline
34 uint8_t matrix_cols(void)
35 {
36     return MATRIX_COLS;
37 }
38
39 void matrix_init(void)
40 {
41     //debug_enable = true;
42     //debug_matrix = true;
43     //debug_mouse = true;
44     // initialize row and col
45     unselect_rows();
46     init_cols();
47
48     // initialize matrix state: all keys off
49     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
50         matrix[i] = 0;
51         matrix_debouncing[i] = 0;
52     }
53 }
54
55 uint8_t matrix_scan(void)
56 {
57     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
58         select_row(i);
59         _delay_us(30);  // without this wait read unstable value.
60         matrix_row_t cols = read_cols();
61         //Serial.println(cols, BIN);
62         if (matrix_debouncing[i] != cols) {
63             matrix_debouncing[i] = cols;
64             if (debouncing) {
65                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
66             }
67             debouncing = DEBOUNCE;
68         }
69         unselect_rows();
70     }
71
72     if (debouncing) {
73         if (--debouncing) {
74             _delay_ms(1);
75         } else {
76             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
77                 matrix[i] = matrix_debouncing[i];
78             }
79         }
80     }
81
82     return 1;
83 }
84
85 bool matrix_is_modified(void)
86 {
87     if (debouncing) return false;
88     return true;
89 }
90
91 inline
92 bool matrix_is_on(uint8_t row, uint8_t col)
93 {
94     return (matrix[row] & ((matrix_row_t)1<<col));
95 }
96
97 inline
98 matrix_row_t matrix_get_row(uint8_t row)
99 {
100     return matrix[row];
101 }
102
103 // TODO update this comment
104 /* Column pin configuration
105  * col: 0   1   2   3   4   5
106  * pin: D3  D4  D5  D6  D7  B0
107  */
108 static void  init_cols(void)
109 {
110     // Input with pull-up(DDR:0, PORT:1)
111   DDRD  &= ~(1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
112   PORTD |=  (1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
113
114   DDRB  &= ~(1<<0);
115   PORTB |=  (1<<0);
116 }
117
118 static matrix_row_t read_cols(void)
119 {
120     return (PIND&(1<<3) ? 0 : (1<<0)) |
121            (PIND&(1<<4) ? 0 : (1<<1)) |
122            (PIND&(1<<5) ? 0 : (1<<2)) |
123            (PIND&(1<<6) ? 0 : (1<<3)) |
124            (PIND&(1<<7) ? 0 : (1<<4)) |
125            (PINB&(1<<0) ? 0 : (1<<5));
126 }
127
128 /* Row pin configuration
129  * row: 0  1  2  3
130  * pin: C0 C1 C2 C3
131  */
132 static void unselect_rows(void)
133 {
134     // Hi-Z(DDR:0, PORT:0) to unselect
135     DDRC  &= ~0xF;
136     PORTC &= ~0xF;
137 }
138
139 static void select_row(uint8_t row)
140 {
141     // Output low(DDR:1, PORT:0) to select
142     switch (row) {
143         case 0:
144             DDRC  |= (1<<0);
145             PORTC &= ~(1<<0);
146             break;
147         case 1:
148             DDRC  |= (1<<1);
149             PORTC &= ~(1<<1);
150             break;
151         case 2:
152             DDRC  |= (1<<2);
153             PORTC &= ~(1<<2);
154             break;
155         case 3:
156             DDRC  |= (1<<3);
157             PORTC &= ~(1<<3);
158             break;
159     }
160 }