Subversion Repositories group.electronics

Rev

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

Rev 27 Rev 28
Line 12... Line 12...
12
 
12
 
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(uint8_t pcint);
18
int getKey(void);
18
int getKey(void);
-
 
19
uint8_t analogRead(uint8_t pin);
19
 
20
 
20
volatile uint8_t pcIntCurr = 0;
21
volatile uint8_t pcIntCurr[3] = {0,0,0};
21
volatile uint8_t pcIntLast = 0;
22
volatile uint8_t pcIntLast[3] = {0,0,0};
22
volatile uint8_t pcIntMask = 0;
23
volatile uint8_t pcIntMask[3] = {0,0,0};
23
 
24
 
24
volatile uint8_t rot_stat = 0;
25
volatile uint8_t rot_stat[2] = {0,0};
25
volatile uint8_t rot_sent = 0;
26
volatile uint8_t rot_sent[2] = {0,0};
26
 
27
 
27
struct{
28
struct{
28
  union {
29
  union {
29
    uint8_t data1[2];	// Rotaries
30
    uint8_t data1[2];	// Rotaries
30
    struct {
31
    struct {
Line 76... Line 77...
76
 
77
 
77
int main(void) {
78
int main(void) {
78
 
79
 
79
  ACSR |= (1<<ACD); // Disable analog comparator
80
  ACSR |= (1<<ACD); // Disable analog comparator
80
 
81
 
-
 
82
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (0<<REFS1);
-
 
83
  ADCSRA = (1<<ADEN) | (0<<ADSC);
-
 
84
 
81
  /*
85
  /*
82
  DDR : 1 = Output, 0 = Input
86
  DDR : 1 = Output, 0 = Input
83
  PORT: 1 = Pullup for Input, otherwise set output
87
  PORT: 1 = Pullup for Input, otherwise set output
84
  PIN : Read input pin
88
  PIN : Read input pin
85
  */
89
  */
Line 128... Line 132...
128
  for(;;) {
132
  for(;;) {
129
    wdt_reset();
133
    wdt_reset();
130
    usbPoll();
134
    usbPoll();
131
 
135
 
132
    if(usbInterruptIsReady()){
136
    if(usbInterruptIsReady()){
133
	report.data1[0] = 0x05;
137
	report.data1[0] = analogRead(0);
134
	report.data1[1] = 0x05;
138
	report.data1[1] = analogRead(1);
135
	report.data2 = 0x0000;
139
	report.data2 = 0x0000;
136
 
140
 
-
 
141
 
137
	int key = getKey();
142
	int key = getKey();
138
	if (key > -1)
143
	if (key > -1)
139
		report.data2 |= (1 << key);
144
		report.data2 |= (1 << key);
140
 
145
 
141
	// Now work out what rotary to send, if any
146
	// Now work out what rotary to send, if any
142
	// Also record if we sent a positive response, 
147
	// Also record if we sent a positive response, 
143
	//  so we can send a '0' next time (if selected on PD4)
148
	//  so we can send a '0' next time (if selected on PD4)
144
	
149
	
145
	if (rot_stat == 0x01 && rot_sent == 0) {
150
	if (rot_stat[0] == 0x01 && rot_sent[0] == 0) {
146
		report.rot1a = 1;
151
		report.rot1a = 1;
147
		rot_sent = 1;
152
		rot_sent[0] = 1;
148
	} else if (rot_stat == 0x02 && rot_sent == 0) {
153
	} else if (rot_stat[0] == 0x02 && rot_sent[0] == 0) {
149
		report.rot1b = 1;
154
		report.rot1b = 1;
150
		rot_sent = 1;
155
		rot_sent[0] = 1;
151
	} else {
156
	} else {
152
		rot_sent = 0;
157
		rot_sent[0] = 0;
153
	}
158
	}
154
 
159
 
155
	// Reset our stat so ready for next turn
160
	// Reset our stat so ready for next turn
156
	rot_stat = 0;
161
	rot_stat[0] = 0;
157
 
162
 
158
	// If our function select is set, dont bother
163
	// If our function select is set, dont bother
159
	//  sending a 'o' between consequtive 1's.
164
	//  sending a 'o' between consequtive 1's.
160
	if (rbi(PINB, PB4))
165
	if (rbi(PINB, PB4))
161
		rot_sent = 0;
166
		rot_sent[0] = 0;
162
 
167
 
163
      /* called after every poll of the interrupt endpoint */
168
      /* called after every poll of the interrupt endpoint */
164
      usbSetInterrupt(&report, sizeof(report));
169
      usbSetInterrupt(&report, sizeof(report));
165
    }
170
    }
166
  }
171
  }
167
}
172
}
168
 
173
 
-
 
174
uint8_t analogRead(uint8_t pin) {
-
 
175
	ADMUX |= (1<<pin);
-
 
176
	ADCSRA |= (1<<ADSC);		// Start converting
-
 
177
 
-
 
178
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
-
 
179
	uint8_t result = ADCH;
-
 
180
	ADCSRA |= (0<<ADSC);		// Stop converting
-
 
181
 
-
 
182
	// result = result * 5/255;		// wtf?
-
 
183
	return result;
-
 
184
}
-
 
185
 
169
int getKey() {
186
int getKey() {
170
	uint8_t col, row = 0;
187
	uint8_t row = 0;
171
	int key = -1;
188
	int key = -1;
172
	uint8_t n = 0;
189
	uint8_t n = 0;
173
 
190
 
174
	for (row=0; row<=3; row++) {
191
	for (row=0; row<=3; row++) {
175
		//cbi(DDRB, row);
-
 
176
		cbi(PORTB, row);
192
		cbi(PORTB, row);
177
		_delay_us(100);
-
 
178
		//for (col=4; col<=7; col++); {
-
 
179
			if (rbi(PIND, 4) == 0) key = n;
193
		if (rbi(PIND, 4) == 0) key = n; n++;
180
			n++;
-
 
181
		        if (rbi(PIND, 5) == 0) key = n;
194
		if (rbi(PIND, 5) == 0) key = n; n++;
182
			n++;
-
 
183
		        if (rbi(PIND, 6) == 0) key = n;
195
		if (rbi(PIND, 6) == 0) key = n; n++;
184
			n++;
-
 
185
		        if (rbi(PIND, 7) == 0) key = n;
196
		if (rbi(PIND, 7) == 0) key = n; n++;
186
			n++;
-
 
187
 
-
 
188
			//if (rbi(PIND, col) == 0)
-
 
189
			//	key = n;
-
 
190
		//}
-
 
191
		//sbi(DDRB, row);
-
 
192
		sbi(PORTB, row);
197
		sbi(PORTB, row);
193
	}
198
	}
194
	return key;
199
	return key;
195
}
200
}
196
 
201
 
-
 
202
/*
-
 
203
 *
-
 
204
 * Process the Pin Change Interrupt.
-
 
205
 * pcint provides what bank caused the interrupt
-
 
206
 *
-
 
207
 */
197
void doInt() {
208
void doInt(uint8_t pcint) {
-
 
209
 
-
 
210
	// Select what rotary we are dealing with
-
 
211
	//   based on the pc interrupt that fired.
-
 
212
	uint8_t rotnum = 0;
-
 
213
	if (pcint == 1) 
-
 
214
		rotnum = 1;
-
 
215
 
198
	// If rot_stat is not 0, we havn't sent
216
	// If rot_stat is not 0, we havn't sent
199
	//  our last results yet. Skip this click.
217
	//  our last results yet. Skip this click.
200
	if (rot_stat != 0) {
218
	if (rot_stat[rotnum] != 0) {
201
		pcIntMask = 0;
219
		pcIntMask[pcint] = 0;
202
		return;
220
		return;
203
	}
221
	}
204
 
-
 
205
	// Check which pin caused the interrupt. If they both
222
	// Check which pin caused the interrupt. If they both
206
	//  equal 0, the pin that interrupted is the direction
223
	//  equal 0, the pin that interrupted is the direction
207
  	if (rbi(pcIntCurr, PCINT17) == 0 
224
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
208
		&& rbi(pcIntCurr, PCINT17) == 0 
225
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
209
		&& rbi(pcIntMask, PCINT16) ) {
226
		&& rbi(pcIntMask[pcint], PCINT16) ) {
210
			rot_stat = 1;
227
			rot_stat[rotnum] = 1;
211
  	} else if (rbi(pcIntCurr, PCINT16) == 0 
228
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
212
		&& rbi(pcIntCurr, PCINT17) == 0 
229
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
213
		&& rbi(pcIntMask, PCINT17) ) {
230
		&& rbi(pcIntMask[pcint], PCINT17) ) {
214
			rot_stat = 2;
231
			rot_stat[rotnum] = 2;
215
  	}
232
  	}
216
 
233
 
217
	// Clear the mask so we know we've delth with it
234
	// Clear the mask so we know we've delth with it
218
	pcIntMask = 0;
235
	pcIntMask[pcint] = 0;
219
}
236
}
220
 
237
 
221
/* Not used for the moment
238
/* Not used for the moment
222
ISR(TIMER0_OVF_vect) {
239
ISR(TIMER0_OVF_vect) {
223
	timer0_ovf++;
240
	timer0_ovf++;
224
}
241
}
225
*/
242
*/
226
 
243
 
-
 
244
ISR(PCINT1_vect)
-
 
245
{
-
 
246
        // Save the state and work out which pin caused
-
 
247
        //  the interrupt to occur
-
 
248
        pcIntCurr[1] = PIND;
-
 
249
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
-
 
250
        pcIntLast[1] = pcIntCurr[1];
-
 
251
        doInt(1);
-
 
252
}
-
 
253
 
227
ISR(PCINT2_vect)
254
ISR(PCINT2_vect)
228
{
255
{
229
	// Save the state and work out which pin caused
256
	// Save the state and work out which pin caused
230
	//  the interrupt to occur
257
	//  the interrupt to occur
231
	pcIntCurr = PIND;
258
	pcIntCurr[2] = PIND;
232
	pcIntMask = pcIntCurr ^ pcIntLast;
259
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
233
	pcIntLast = pcIntCurr;
260
	pcIntLast[2] = pcIntCurr[2];
234
	doInt();
261
	doInt(2);
235
}
262
}
236
 
263