Subversion Repositories group.electronics

Rev

Rev 23 | Rev 26 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23 Rev 24
Line 13... Line 13...
13
#include "config.h"
13
#include "config.h"
14
#include "hiddesc.h"
14
#include "hiddesc.h"
15
 
15
 
16
 
16
 
17
void doInt(void);
17
void doInt(void);
-
 
18
uint8_t getKey(void);
18
 
19
 
19
volatile uint8_t pcIntCurr = 0;
20
volatile uint8_t pcIntCurr = 0;
20
volatile uint8_t pcIntLast = 0;
21
volatile uint8_t pcIntLast = 0;
21
volatile uint8_t pcIntMask = 0;
22
volatile uint8_t pcIntMask = 0;
22
 
23
 
23
volatile uint8_t rot_stat = 0;
24
volatile uint8_t rot_stat = 0;
24
volatile uint8_t rot_sent = 0;
25
volatile uint8_t rot_sent = 0;
25
 
26
 
26
struct{
27
struct{
-
 
28
  int data1;	// X axis
-
 
29
  int data2;	// Y axis
27
  union {
30
  union {
28
    uint8_t data;
31
    uint8_t data3;	// Rotaries
29
    struct {
32
    struct {
-
 
33
	uint8_t rot1a:1;
-
 
34
	uint8_t rot1b:1;
-
 
35
	uint8_t rot2a:1;
-
 
36
	uint8_t rot2b:1;
-
 
37
    };
-
 
38
  };
-
 
39
  union {
-
 
40
    uint16_t data4;	// 16 buttons
-
 
41
    struct {
30
      uint8_t X:2;
42
      uint8_t b00:1;
-
 
43
      uint8_t b01:1;
31
      uint8_t Y:2;
44
      uint8_t b02:1;
-
 
45
      uint8_t b03:1;
-
 
46
      uint8_t b04:1;
-
 
47
      uint8_t b05:1;
-
 
48
      uint8_t b06:1;
-
 
49
      uint8_t b07:1;
-
 
50
      uint8_t b08:1;
32
      uint8_t B:1;
51
      uint8_t b09:1;
33
      uint8_t A:1;
52
      uint8_t b10:1;
34
      uint8_t rot1:1;
53
      uint8_t b11:1;
35
      uint8_t rot2:1;
54
      uint8_t b12:1;
-
 
55
      uint8_t b13:1;
-
 
56
      uint8_t b14:1;
-
 
57
      uint8_t b15:1;
36
    };
58
    };
37
  };
59
  };
38
} report;
60
} report;
39
 
61
 
40
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
62
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
Line 63... Line 85...
63
  PORT: 1 = Pullup for Input, otherwise set output
85
  PORT: 1 = Pullup for Input, otherwise set output
64
  PIN : Read input pin
86
  PIN : Read input pin
65
  */
87
  */
66
 
88
 
67
  /*
89
  /*
68
	PB0	- Output		- Status LED
90
	PB0	- Input	 		- Keypad 5
69
	PB1	- Input, Pullup, PCINT1	- Rotary 1
91
	PB1	- Input	 		- Keypad 6
-
 
92
	PB2	- Input	 		- Keypad 7
-
 
93
	PB3	- Input	 		- Keypad 8
70
	PB2	- Input, Pullup, PCINT2	- Rotary 2
94
	PB4	- Input, Pullup		- Function select
71
  */
95
  */
72
  DDRB		= 0B00000001;
96
  DDRB		= 0B00000000;
73
  PORTB 	= 0B00000110;
97
  PORTB 	= 0B00010000;
74
 
98
 
75
  /*
99
  /*
76
	PD4	- Input, Pullup 	- Rotary function select
100
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
77
	PD5	- Input, Pullup 	- Button
101
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
-
 
102
 
-
 
103
 
-
 
104
	PD4	- Output			- Keypad 1
-
 
105
	PD5	- Output			- Keypad 2
78
	PD6	- Input, Pullup 	- Button
106
	PD6	- Output			- Keypad 3
79
	PD7	- Output		- Status LED
107
	PD7	- Output			- Keypad 4
80
  */
108
  */
81
  DDRD		= 0B10000000;
109
  DDRD		= 0B11110000;
82
  PORTD		= 0B01110000;
110
  PORTD		= 0B00000011;
83
 
111
 
84
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
112
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
85
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
113
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
86
 
114
 
87
  // Timers not used for the moment
115
  // Timers not used for the moment
88
  // Setup timer0 - Enable overflow, 8 times prescaler
116
  // Setup timer0 - Enable overflow, 8 times prescaler
89
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
117
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
90
  //TCNT0 = 0x00;				// Set Timer0 to 0
118
  //TCNT0 = 0x00;				// Set Timer0 to 0
Line 102... Line 130...
102
  for(;;) {
130
  for(;;) {
103
    wdt_reset();
131
    wdt_reset();
104
    usbPoll();
132
    usbPoll();
105
 
133
 
106
    if(usbInterruptIsReady()){
134
    if(usbInterruptIsReady()){
-
 
135
        report.data1 = 0; // Center X
107
        report.data = 0x05; // Center pad, little endian
136
        report.data2 = 0; // Center Y
-
 
137
 
108
 
138
 
109
	// Send our buttons 
-
 
110
	if (bit_is_clear(PIND, PD5))
-
 
111
		report.A = 1;
-
 
112
	if (bit_is_clear(PIND, PD6))
-
 
113
		report.B = 1;
139
	report.data4 |= (1 << getKey());
114
 
140
 
115
	// Now work out what rotary to send, if any
141
	// Now work out what rotary to send, if any
116
	// Also record if we sent a positive response, 
142
	// Also record if we sent a positive response, 
117
	//  so we can send a '0' next time (if selected on PD4)
143
	//  so we can send a '0' next time (if selected on PD4)
118
	if (rot_stat == 0x01 && rot_sent == 0) {
144
	if (rot_stat == 0x01 && rot_sent == 0) {
119
		report.rot1 = 1;
145
		report.rot1a = 1;
120
		rot_sent = 1;
146
		rot_sent = 1;
121
	} else if (rot_stat == 0x02 && rot_sent == 0) {
147
	} else if (rot_stat == 0x02 && rot_sent == 0) {
122
		report.rot2 = 1;
148
		report.rot1b = 1;
123
		rot_sent = 1;
149
		rot_sent = 1;
124
	} else {
150
	} else {
125
		rot_sent = 0;
151
		rot_sent = 0;
126
	}
152
	}
127
 
153
 
128
	// Reset our stat so ready for next turn
154
	// Reset our stat so ready for next turn
129
	rot_stat = 0;
155
	rot_stat = 0;
130
 
156
 
131
	// If our function select is set, dont bother
157
	// If our function select is set, dont bother
132
	//  sending a 'o' between consequtive 1's.
158
	//  sending a 'o' between consequtive 1's.
133
	if (rbi(PIND, PD4))
159
	if (rbi(PINB, PB4))
134
		rot_sent = 0;
160
		rot_sent = 0;
135
 
161
 
136
      /* called after every poll of the interrupt endpoint */
162
      /* called after every poll of the interrupt endpoint */
137
      usbSetInterrupt(&report, sizeof(report));
163
      usbSetInterrupt(&report, sizeof(report));
138
    }
164
    }
139
  }
165
  }
140
}
166
}
141
 
167
 
-
 
168
uint8_t getKey() {
-
 
169
	uint8_t col, row = 0;
-
 
170
	uint8_t key = 0;
-
 
171
	uint8_t n = 0;
-
 
172
 
-
 
173
	for (col=4; col<=7; col++) {
-
 
174
		sbi(PORTD, col);
-
 
175
		for (row=0; row<=3; row++); {
-
 
176
			if (rbi(PINB, row))
-
 
177
				key = n;
-
 
178
			n++;
-
 
179
		}
-
 
180
		cbi(PORTD, col);
-
 
181
	}
-
 
182
	return key;
-
 
183
}
-
 
184
 
142
void doInt() {
185
void doInt() {
143
	// If rot_stat is not 0, we havn't sent
186
	// If rot_stat is not 0, we havn't sent
144
	//  our last results yet. Skip this click.
187
	//  our last results yet. Skip this click.
145
	if (rot_stat != 0) {
188
	if (rot_stat != 0) {
146
		pcIntMask = 0;
189
		pcIntMask = 0;
147
		return;
190
		return;
148
	}
191
	}
149
 
192
 
150
	// Check which pin caused the interrupt. If they both
193
	// Check which pin caused the interrupt. If they both
151
	//  equal 0, the pin that interrupted is the direction
194
	//  equal 0, the pin that interrupted is the direction
152
  	if (rbi(pcIntCurr, PCINT1) == 0 
195
  	if (rbi(pcIntCurr, PCINT17) == 0 
153
		&& rbi(pcIntCurr, PCINT2) == 0 
196
		&& rbi(pcIntCurr, PCINT17) == 0 
154
		&& rbi(pcIntMask, PCINT1) ) {
197
		&& rbi(pcIntMask, PCINT16) ) {
155
			rot_stat = 1;
198
			rot_stat = 1;
156
  	} else if (rbi(pcIntCurr, PCINT1) == 0 
199
  	} else if (rbi(pcIntCurr, PCINT16) == 0 
157
		&& rbi(pcIntCurr, PCINT2) == 0 
200
		&& rbi(pcIntCurr, PCINT17) == 0 
158
		&& rbi(pcIntMask, PCINT2) ) {
201
		&& rbi(pcIntMask, PCINT17) ) {
159
			rot_stat = 2;
202
			rot_stat = 2;
160
  	}
203
  	}
161
 
204
 
162
	// Clear the mask so we know we've delth with it
205
	// Clear the mask so we know we've delth with it
163
	pcIntMask = 0;
206
	pcIntMask = 0;
Line 167... Line 210...
167
ISR(TIMER0_OVF_vect) {
210
ISR(TIMER0_OVF_vect) {
168
	timer0_ovf++;
211
	timer0_ovf++;
169
}
212
}
170
*/
213
*/
171
 
214
 
172
ISR(PCINT0_vect)
215
ISR(PCINT2_vect)
173
{
216
{
174
	// Save the state and work out which pin caused
217
	// Save the state and work out which pin caused
175
	//  the interrupt to occur
218
	//  the interrupt to occur
176
	pcIntCurr = PINB;
219
	pcIntCurr = PIND;
177
	pcIntMask = pcIntCurr ^ pcIntLast;
220
	pcIntMask = pcIntCurr ^ pcIntLast;
178
	pcIntLast = pcIntCurr;
221
	pcIntLast = pcIntCurr;
179
	doInt();
222
	doInt();
180
}
223
}
181
 
224