Revision 47e7c7ed MatrixScan.c

View differences:

MatrixScan.c
32 32
	}
33 33
}
34 34

  
35
void decrementKeystrokes(){
36
	for(uint8_t i = 0; i < 30; i++){
37
		if(detectedKeypress[i][0] > 0){
38
			detectedKeypress[i][0] = detectedKeypress[i][0]-1;
39
		}
40
	}
41
}
42

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

  
35 66
void setReportData(USB_KeyboardReport_Data_t* const ReportData){
36 67
	// push initial 1
37 68
	pushBit(1);
......
58 89
				 (matrixState[i] == (1<<5)) ||
59 90
				 (matrixState[i] == (1<<6)) ||
60 91
				 (matrixState[i] == (1<<7)))){
61
				 return; // ghosting found
92
					return; // ghosting found
62 93
				}
63 94
			}
64 95
		}
65 96
	}
66
	// check for mods
67
	uint8_t modOffset = 0;
97
	// handle queue
68 98
	for(uint8_t i = 0; i < 19; i++){
69 99
		if(matrixState[i] != 0){
70 100
			for(uint8_t bit = 0; bit < 8; bit++){
71 101
				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
					}
102
					addKeystroke(bit, i);
111 103
				}
112 104
			}
113 105
		}
114 106
	}
107
	// decrement all keystrokes (we added two before)
108
	decrementKeystrokes();
109
	// check for mods
110
	uint8_t modOffset = 0;
111
	for(uint8_t i = 0; i < 30; i++){
112
		if(detectedKeypress[i][0] > KEYPRESS_TRIGGER_COUNT){
113
			switch (pgm_read_byte(&mapping[detectedKeypress[i][2]][detectedKeypress[i][1]][0][0])){
114
				case HID_KEYBOARD_SC_LEFT_CONTROL:
115
					ReportData->Modifier |= (1<<0);
116
					break;
117
				case HID_KEYBOARD_SC_LEFT_SHIFT:
118
					ReportData->Modifier |= (1<<1);
119
					modOffset = 1;
120
					break;
121
				case HID_KEYBOARD_SC_LEFT_ALT:
122
					ReportData->Modifier |= (1<<2);
123
					break;
124
				case HID_KEYBOARD_SC_LEFT_GUI:
125
					ReportData->Modifier |= (1<<3);
126
					break;
127
				case HID_KEYBOARD_SC_RIGHT_CONTROL:
128
					ReportData->Modifier |= (1<<4);
129
					break;
130
				case HID_KEYBOARD_SC_RIGHT_SHIFT:
131
					ReportData->Modifier |= (1<<5);
132
					modOffset = 1;
133
					break;
134
				case HID_KEYBOARD_SC_RIGHT_ALT:
135
					modOffset = 3;
136
					break;
137
				case HID_KEYBOARD_SC_RIGHT_GUI:
138
					ReportData->Modifier |= (1<<7);
139
					break;
140
				case HID_KEYBOARD_SC_MOD3:
141
					modOffset = 2;
142
					break;
143
				case HID_KEYBOARD_SC_MOD4:
144
					modOffset = 3;
145
					break;
146
				default:
147
					break;
148
			}
149
		}
150
	}
115 151
	// 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
					}
152
	for(uint8_t i = 0; i < 30; i++){
153
		if(detectedKeypress[i][0] > KEYPRESS_TRIGGER_COUNT){
154
			if(pressedKeys < 6){
155
				uint8_t possibleMod = pgm_read_byte(&mapping[detectedKeypress[i][2]][detectedKeypress[i][1]][modOffset][0]);
156
				if(possibleMod >= HID_KEYBOARD_SC_LEFT_CONTROL && possibleMod <= HID_KEYBOARD_SC_RIGHT_GUI){
157
					modAfterglow |= (1<<(possibleMod-0xe0));
158
					ReportData->Modifier |= modAfterglow;
159
				}
160
				uint8_t possibleKey = pgm_read_byte(&mapping[detectedKeypress[i][2]][detectedKeypress[i][1]][modOffset][1]);
161
				if(possibleKey != 0){
162
					ReportData->KeyCode[pressedKeys++] = possibleKey;
128 163
				}
129 164
			}
130 165
		}

Also available in: Unified diff