Statistics
| Branch: | Tag: | Revision:

root / MatrixScan.c @ 12653b5a

History | View | Annotate | Download (5.11 KB)

1
#include "MatrixScan.h"
2
#include <util/delay.h>
3
void setupMatrix(){
4
        // TODO
5
        // setup input pins
6
        DATAINDDR = ((0<<DIN_7) | (0<<DIN_6) | (0<<DIN_5) | (0<<DIN_4) | (0<<DIN_3) | (0<<DIN_2) | (0<<DIN_1) | (0<<DIN_0));
7
        // setup shift register clock and data lines
8
        SHIFTREGDDR |= (1<<SRCLKPIN) | (1<<SRDATAPIN);
9
        // setup reset line for shiftregisters
10
        SHIFTREGRESETDDR |= (1<<SHIFTREGRESETPIN);
11
        SHIFTREGRESETPORT |= (1<<SHIFTREGRESETPIN);
12
        clearSR();
13
}
14

    
15
void clockSR(){
16
        SHIFTREGPORT |= (1<<SRCLKPIN);
17
        // TODO add delay here?
18
        SHIFTREGPORT &= ~(1<<SRCLKPIN);
19
}
20

    
21
void pushBit(uint8_t bit){
22
        if(bit != 0){
23
                SHIFTREGPORT |= (1<<SRDATAPIN);
24
                clockSR();
25
                SHIFTREGPORT &= ~(1<<SRDATAPIN);
26
        }
27
        else{
28
                clockSR();
29
        }
30
}  
31

    
32
void clearSR(){
33
        //SHIFTREGPORT &= ~(1<<SHIFTREGRESETPIN);
34
        //_delay_us(1);
35
        //SHIFTREGPORT |= (1<<SHIFTREGRESETPIN);
36
        for(uint8_t i = 0; i < 25; i++){
37
                pushBit(0);
38
        }
39
}
40

    
41
void decrementKeystrokes(){
42
        for(uint8_t i = 0; i < 30; i++){
43
                if(detectedKeypress[i][0] > 0){
44
                        detectedKeypress[i][0] = detectedKeypress[i][0]-1;
45
                }
46
        }
47
}
48

    
49
void addKeystroke(uint8_t row, uint8_t col){
50
        // check if the key is already inside the list
51
        for(uint8_t i = 0; i < 30; i++){
52
                if(detectedKeypress[i][0] > 0){
53
                        if(detectedKeypress[i][1] == row && detectedKeypress[i][2] == col){
54
                                if(detectedKeypress[i][0] < MAX_KEYPRESS_COUNT){
55
                                        detectedKeypress[i][0] = detectedKeypress[i][0] + 2;
56
                                }
57
                                return;
58
                        }
59
                }
60
        }
61
        // key not found, find space for the key
62
        for(uint8_t i = 0; i < 30; i++){
63
                if(detectedKeypress[i][0] == 0){
64
                        detectedKeypress[i][0] = 2;
65
                        detectedKeypress[i][1] = row;
66
                        detectedKeypress[i][2] = col;
67
                        return;
68
                }
69
        }
70
}
71

    
72
void setReportData(USB_KeyboardReport_Data_t* const ReportData){
73
//        // push initial 1
74
//        pushBit(1);
75
//        //for(uint8_t i = 0; i < 7; i++){
76
//        //        pushBit(0); // to get the 1 on the first output
77
//        //}
78
//        // scan matrix
79
//        uint8_t somethingPressed = 0;
80
//        for(uint8_t i = 0; i < 19; i++){
81
//                _delay_us(100);
82
//                matrixState[i] = DATAINPIN;
83
//                if(matrixState[i] != 0){
84
//                        ReportData->KeyCode[0] = HID_KEYBOARD_SC_A + i;
85
//                        for(uint8_t bit = 0; bit < 8; bit++){
86
//                                if(matrixState[i] & (1<<bit)){
87
//                                        ReportData->KeyCode[1] = HID_KEYBOARD_SC_1_AND_EXCLAMATION + bit;
88
//                                }
89
//                        }
90
//                }
91
//                pushBit(0);
92
//        }
93
//        // set mods (check if >= 0xe0)
94
//        clearSR();
95
//        return;
96

    
97

    
98

    
99
        // push initial 1
100
        pushBit(1);
101
        // set afterglow before we miss it later in case of ghosting
102
        ReportData->Modifier |= modAfterglow;
103
        modAfterglow = 0;
104
        // scan matrix
105
        uint8_t pressedKeys = 0;
106
        for(uint8_t i = 0; i < 19; i++){
107
                _delay_us(15);
108
                matrixState[i] = DATAINPIN;
109
                pushBit(0);
110
        }
111
        // check for ghosting
112
        for(uint8_t i = 0; i < 19-1; i++){
113
                if(matrixState[i] != 0){
114
                        for(uint8_t j = i+1; j < 19; j++){
115
                                if(matrixState[i] == matrixState[j] && !((matrixState[i] == (1<<0)) ||
116
                                 (matrixState[i] == (1<<1)) ||
117
                                 (matrixState[i] == (1<<2)) ||
118
                                 (matrixState[i] == (1<<3)) ||
119
                                 (matrixState[i] == (1<<4)) ||
120
                                 (matrixState[i] == (1<<5)) ||
121
                                 (matrixState[i] == (1<<6)) ||
122
                                 (matrixState[i] == (1<<7)))){
123
                                        return; // ghosting found
124
                                }
125
                        }
126
                }
127
        }
128
        // handle queue
129
        for(uint8_t i = 0; i < 19; i++){
130
                if(matrixState[i] != 0){
131
                        for(uint8_t bit = 0; bit < 8; bit++){
132
                                if(matrixState[i] & (1<<bit)){
133
                                        addKeystroke(bit, i);
134
                                }
135
                        }
136
                }
137
        }
138
        // decrement all keystrokes (we added two before)
139
        decrementKeystrokes();
140
        // check for mods
141
        uint8_t modOffset = 0;
142
        for(uint8_t i = 0; i < 30; i++){
143
                if(detectedKeypress[i][0] > KEYPRESS_TRIGGER_COUNT){
144
                        switch (pgm_read_byte(&mapping[detectedKeypress[i][2]][detectedKeypress[i][1]][0][0])){
145
                                case HID_KEYBOARD_SC_LEFT_CONTROL:
146
                                        ReportData->Modifier |= (1<<0);
147
                                        break;
148
                                case HID_KEYBOARD_SC_LEFT_SHIFT:
149
                                        ReportData->Modifier |= (1<<1);
150
                                        if(modOffset == 0){
151
                                                modOffset = 1;
152
                                        }
153
                                        break;
154
                                case HID_KEYBOARD_SC_LEFT_ALT:
155
                                        ReportData->Modifier |= (1<<2);
156
                                        break;
157
                                case HID_KEYBOARD_SC_LEFT_GUI:
158
                                        ReportData->Modifier |= (1<<3);
159
                                        break;
160
                                case HID_KEYBOARD_SC_RIGHT_CONTROL:
161
                                        ReportData->Modifier |= (1<<4);
162
                                        break;
163
                                case HID_KEYBOARD_SC_RIGHT_SHIFT:
164
                                        ReportData->Modifier |= (1<<5);
165
                                        if(modOffset == 0){
166
                                                modOffset = 1;
167
                                        }
168
                                        break;
169
                                case HID_KEYBOARD_SC_RIGHT_ALT:
170
                                        modOffset = 3;
171
                                        break;
172
                                case HID_KEYBOARD_SC_RIGHT_GUI:
173
                                        ReportData->Modifier |= (1<<7);
174
                                        break;
175
                                case HID_KEYBOARD_SC_MOD3:
176
                                        modOffset = 2;
177
                                        break;
178
                                case HID_KEYBOARD_SC_MOD4:
179
                                        modOffset = 3;
180
                                        break;
181
                                default:
182
                                        break;
183
                        }
184
                }
185
        }
186
        // build report
187
        for(uint8_t i = 0; i < 30; i++){
188
                if(detectedKeypress[i][0] > KEYPRESS_TRIGGER_COUNT){
189
                        if(pressedKeys < 6){
190
                                uint8_t possibleMod = pgm_read_byte(&mapping[detectedKeypress[i][2]][detectedKeypress[i][1]][modOffset][0]);
191
                                if(possibleMod >= HID_KEYBOARD_SC_LEFT_CONTROL && possibleMod <= HID_KEYBOARD_SC_RIGHT_GUI){
192
                                        modAfterglow |= (1<<(possibleMod-0xe0));
193
                                        ReportData->Modifier |= modAfterglow;
194
                                }
195
                                uint8_t possibleKey = pgm_read_byte(&mapping[detectedKeypress[i][2]][detectedKeypress[i][1]][modOffset][1]);
196
                                if(possibleKey != 0){
197
                                        ReportData->KeyCode[pressedKeys++] = possibleKey;
198
                                }
199
                        }
200
                }
201
        }
202
        clearSR();
203
        return;
204
}