Statistics
| Branch: | Tag: | Revision:

root / MatrixScan.c @ de87a8c5

History | View | Annotate | Download (1.14 KB)

1
#include "MatrixScan.h"
2
void setupMatrix(){
3
        // TODO
4
        // setup input pins
5
        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));
6
        // setup shift register
7
        SHIFTREGDDR |= (1<<SRCLKPIN) | (1<<SRDATAPIN);
8
        clearSR();
9
}
10

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

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

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

    
34
void setReportData(USB_KeyboardReport_Data_t* const ReportData){
35
        // push initial 1
36
        pushBit(1);
37
        pushBit(0); // to get the 1 on the first output
38
        // scan matrix
39
        uint8_t somethingPressed = 0;
40
        for(uint8_t i = 0; i < 19; i++){
41
                matrixState[i] = DATAINPIN;
42
                if(matrixState[i] != 0){
43
                        ReportData->KeyCode[0] = HID_KEYBOARD_SC_A + i;
44
                        for(uint8_t bit = 0; bit < 8; bit++){
45
                                if(matrixState[i] & (1<<bit)){
46
                                        ReportData->KeyCode[1] = HID_KEYBOARD_SC_1_AND_EXCLAMATION + bit;
47
                                }
48
                        }
49
                }
50
                pushBit(0);
51
        }
52
        // set mods (check if >= 0xe0)
53
        return;
54
}