Statistics
| Branch: | Tag: | Revision:

root / MatrixScan.c @ f255c38f

History | View | Annotate | Download (3.36 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
8
        SHIFTREGDDR |= (1<<SRCLKPIN) | (1<<SRDATAPIN);
9
        clearSR();
10
}
11

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

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

    
29
void clearSR(){
30
        for(uint8_t i = 0; i < 21; i++){
31
                pushBit(0);
32
        }
33
}
34

    
35
void setReportData(USB_KeyboardReport_Data_t* const ReportData){
36
        // push initial 1
37
        pushBit(1);
38
        pushBit(0); // to get the 1 on the first output
39
        // set afterglow before we miss it later in case of ghosting
40
        ReportData->Modifier |= modAfterglow;
41
        modAfterglow = 0;
42
        // scan matrix
43
        uint8_t pressedKeys = 0;
44
        for(uint8_t i = 0; i < 19; i++){
45
                _delay_us(15);
46
                matrixState[i] = DATAINPIN;
47
                pushBit(0);
48
        }
49
        // check for ghosting
50
        for(uint8_t i = 0; i < 19-1; i++){
51
                if(matrixState[i] != 0){
52
                        for(uint8_t j = i+1; j < 19; j++){
53
                                if(matrixState[i] == matrixState[j] && !((matrixState[i] == (1<<0)) ||
54
                                 (matrixState[i] == (1<<1)) ||
55
                                 (matrixState[i] == (1<<2)) ||
56
                                 (matrixState[i] == (1<<3)) ||
57
                                 (matrixState[i] == (1<<4)) ||
58
                                 (matrixState[i] == (1<<5)) ||
59
                                 (matrixState[i] == (1<<6)) ||
60
                                 (matrixState[i] == (1<<7)))){
61
                                 return; // ghosting found
62
                                }
63
                        }
64
                }
65
        }
66
        // check for mods
67
        uint8_t modOffset = 0;
68
        for(uint8_t i = 0; i < 19; i++){
69
                if(matrixState[i] != 0){
70
                        for(uint8_t bit = 0; bit < 8; bit++){
71
                                if(matrixState[i] & (1<<bit)){
72
                                        switch pgm_read_byte(&mapping[i][bit][0][0]){
73
                                                case HID_KEYBOARD_SC_LEFT_CONTROL:
74
                                                        ReportData->Modifier |= (1<<0);
75
                                                        break;
76
                                                case HID_KEYBOARD_SC_LEFT_SHIFT:
77
                                                        ReportData->Modifier |= (1<<1);
78
                                                        modOffset = 1;
79
                                                        break;
80
                                                case HID_KEYBOARD_SC_LEFT_ALT:
81
                                                        ReportData->Modifier |= (1<<2);
82
                                                        break;
83
                                                case HID_KEYBOARD_SC_LEFT_GUI:
84
                                                        ReportData->Modifier |= (1<<3);
85
                                                        break;
86
                                                case HID_KEYBOARD_SC_RIGHT_CONTROL:
87
                                                        ReportData->Modifier |= (1<<4);
88
                                                        break;
89
                                                case HID_KEYBOARD_SC_RIGHT_SHIFT:
90
                                                        ReportData->Modifier |= (1<<5);
91
                                                        modOffset = 1;
92
                                                        break;
93
                                                case HID_KEYBOARD_SC_RIGHT_ALT:
94
                                                        modOffset = 3;
95
                                                        matrixState[i] &= ~(1<<bit);
96
                                                        break;
97
                                                case HID_KEYBOARD_SC_RIGHT_GUI:
98
                                                        ReportData->Modifier |= (1<<7);
99
                                                        break;
100
                                                case HID_KEYBOARD_SC_MOD3:
101
                                                        modOffset = 2;
102
                                                        matrixState[i] &= ~(1<<bit);
103
                                                        break;
104
                                                case HID_KEYBOARD_SC_MOD4:
105
                                                        modOffset = 3;
106
                                                        matrixState[i] &= ~(1<<bit);
107
                                                        break;
108
                                                default:
109
                                                        break;
110
                                        }
111
                                }
112
                        }
113
                }
114
        }
115
        // build report
116
        for(uint8_t i = 0; i < 19; i++){
117
                if(matrixState[i] != 0){
118
                        for(uint8_t bit = 0; bit < 8; bit++){
119
                                if(matrixState[i] & (1<<bit)){
120
                                        if(mapping[i][bit] != 0 && pressedKeys < 6){
121
                                                uint8_t possibleMod = pgm_read_byte(&mapping[i][bit][modOffset][0]);
122
                                                if(possibleMod >= HID_KEYBOARD_SC_LEFT_CONTROL && possibleMod <= HID_KEYBOARD_SC_RIGHT_GUI){
123
                                                        modAfterglow |= (1<<(possibleMod-0xe0));
124
                                                        ReportData->Modifier |= modAfterglow;
125
                                                }
126
                                                ReportData->KeyCode[pressedKeys++] = pgm_read_byte(&mapping[i][bit][modOffset][1]);
127
                                        }
128
                                }
129
                        }
130
                }
131
        }
132
        return;
133
}