Revision de87a8c5

View differences:

Matrix.h
1
#include <inttypes.h>
2

  
3
#ifndef __matrix_h__
4
#define __matrix_h__
5

  
6
volatile uint8_t matrixState[19];
7
volatile uint8_t mapping[8][19];
8

  
9

  
10
#endif
MatrixScan.c
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
}
MatrixScan.h
1
#include "Matrix.h"
2
#include <LUFA/Drivers/USB/USB.h>
3

  
4
#ifndef __matrixscan_h__
5
#define __matrixscan_h__
6

  
7

  
8
#define SHIFTREGPORT PORTC
9
#define SHIFTREGDDR DDRC
10
#define SRDATAPIN PC7
11
#define SRCLKPIN PC6
12
#define DATAINPORT PORTD
13
#define DATAINPIN PIND
14
#define DATAINDDR DDRD
15
#define DIN_0 PD0
16
#define DIN_1 PD1
17
#define DIN_2 PD2
18
#define DIN_3 PD3
19
#define DIN_4 PD4
20
#define DIN_5 PD5
21
#define DIN_6 PD6
22
#define DIN_7 PD7
23

  
24

  
25
void clearSR(void);
26
void clockSR(void);
27
void pushBit(uint8_t bit);
28
void setupMatrix(void);
29
void setReportData(USB_KeyboardReport_Data_t* const ReportData);
30

  
31

  
32
#endif

Also available in: Unified diff