Subversion Repositories group.electronics

Rev

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

Rev 60 Rev 62
Line 41... Line 41...
41
 
41
 
42
 
42
 
43
void doInt(uint8_t pcint);
43
void doInt(uint8_t pcint);
44
uint8_t getKey(void);
44
uint8_t getKey(void);
45
 
45
 
46
volatile uint8_t pcIntCurr[3] = {0,0,0};
46
volatile struct {
47
volatile uint8_t pcIntLast[3] = {0,0,0};
47
	uint8_t current;
-
 
48
	uint8_t last;
48
volatile uint8_t pcIntMask[3] = {0,0,0};
49
	uint8_t mask;
-
 
50
} pcInt[3];
49
 
51
 
50
// switches = [detected][timer][debounced]
52
// switches = [detected][timer][debounced]
51
volatile uint8_t switches[1][3] = { {0,0,0} };
53
volatile uint8_t switches[1][3] = { {0,0,0} };
52
 
54
 
53
// rotdata = [rot#][(stat|sent)]
55
// rotData = [rot#][(stat|sent)]
54
volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };
56
//volatile uint8_t rotData[2][2] = { {0,0}, {0,0} };
-
 
57
 
-
 
58
volatile struct {
-
 
59
	union {
-
 
60
		uint8_t data;
-
 
61
		struct {
-
 
62
			uint8_t stat:4;
-
 
63
			uint8_t sent:4;
-
 
64
		};
-
 
65
	};
-
 
66
} rotData[2];
55
 
67
 
56
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
68
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
57
			0x21, 0x22, 0x23,
69
			0x21, 0x22, 0x23,
58
			0x24, 0x25, 0x26,
70
			0x24, 0x25, 0x26,
59
			0x25, 0x27, 0x20 };
71
			0x25, 0x27, 0x20 };
Line 123... Line 135...
123
 
135
 
124
void hadUsbReset(void) {
136
void hadUsbReset(void) {
125
}
137
}
126
 
138
 
127
int main(void) {
139
int main(void) {
128
 
-
 
129
  ACSR |= (1<<ACD); // Disable analog comparator
-
 
130
 
-
 
131
  /*
-
 
132
	Setup ADC
140
	analogInit();
133
	ADMUX: 8 bit mode, Avcc ref
-
 
134
	ADCSRA: Enable, 128 prescale
-
 
135
  */
-
 
136
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
-
 
137
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
-
 
138
 
141
 
139
  /*
142
  /*
140
  DDR : 1 = Output, 0 = Input
143
  DDR : 1 = Output, 0 = Input
141
  PORT: 1 = Pullup for Input, otherwise set output
144
  PORT: 1 = Pullup for Input, otherwise set output
142
  PIN : Read input pin
145
  PIN : Read input pin
Line 174... Line 177...
174
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
177
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
175
	TCNT0 = 0x00;				// Set Timer0 to 0
178
	TCNT0 = 0x00;				// Set Timer0 to 0
176
	TCCR0B = (1<< CS01) ;			// /8 prescaler
179
	TCCR0B = (1<< CS01) ;			// /8 prescaler
177
 
180
 
178
	i2c_master();
181
	i2c_master();
179
	i2c_beginTransmission(0x27);
-
 
180
	i2c_writeByte(0x01);
-
 
181
	i2c_endTransmission(1);
-
 
182
 
-
 
183
 
182
 
184
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
183
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
185
  _delay_ms(500);
184
  _delay_ms(500);
186
  usbDeviceConnect();
185
  usbDeviceConnect();
187
 
186
 
Line 269... Line 268...
269
	}
268
	}
270
 
269
 
271
	// Now work out what rotary to send, if any
270
	// Now work out what rotary to send, if any
272
	// Also record if we sent a positive response, 
271
	// Also record if we sent a positive response, 
273
	//  so we can send a '0' next time (if selected on PD4)
272
	//  so we can send a '0' next time (if selected on PD4)
274
	// rotdata = [rot#][(stat|sent)]
273
	// rotData = [rot#][(stat|sent)]
275
	uint8_t rot = 0;
274
	uint8_t rot = 0;
276
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
275
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
277
	        if (rotdata[rot][STAT] == 0x01 && rotdata[rot][SENT] == 0) {
276
	        if (rotData[rot].stat == 0x01 && rotData[rot].sent == 0) {
278
                	rotdata[rot][SENT] = 1;
277
                	rotData[rot].sent = 1;
279
			switch (rot) {
278
			switch (rot) {
280
				case(0):	reportJoystick.rot1a = 1; break;
279
				case(0):	reportJoystick.rot1a = 1; break;
281
				case(1):	reportJoystick.rot2a = 1; break;
280
				case(1):	reportJoystick.rot2a = 1; break;
282
			}
281
			}
283
	        } else if (rotdata[rot][STAT] == 0x02 && rotdata[rot][SENT] == 0) {
282
	        } else if (rotData[rot].stat == 0x02 && rotData[rot].sent == 0) {
284
                	rotdata[rot][SENT] = 1;
283
                	rotData[rot].sent = 1;
285
                        switch (rot) {
284
                        switch (rot) {
286
                                case(0):      reportJoystick.rot1b = 1; break;
285
                                case(0):      reportJoystick.rot1b = 1; break;
287
                                case(1):      reportJoystick.rot2b = 1; break;
286
                                case(1):      reportJoystick.rot2b = 1; break;
288
                        }
287
                        }
289
	        } else {
288
	        } else {
290
        	        rotdata[rot][SENT] = 0;
289
        	        rotData[rot].sent = 0;
291
	        }
290
	        }
292
		rotdata[rot][STAT] = 0;
291
		rotData[rot].stat = 0;
293
 
292
 
294
	        if (rbi(PINB, PB4))
293
	        if (rbi(PINB, PB4))
295
        	        rotdata[rot][SENT] = 0;
294
        	        rotData[rot].sent = 0;
296
	}
295
	}
297
 
296
 
298
      /* called after every poll of the interrupt endpoint */
297
      /* called after every poll of the interrupt endpoint */
299
      //usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
298
      //usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
300
      //usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
299
      //usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
Line 330... Line 329...
330
 *
329
 *
331
 * Process the Pin Change Interrupt.
330
 * Process the Pin Change Interrupt.
332
 * pcint provides what bank caused the interrupt
331
 * pcint provides what bank caused the interrupt
333
 *
332
 *
334
 */
333
 */
335
void doInt(uint8_t pcint) {
334
void pcInterrupt(uint8_t pcint) {
-
 
335
 
-
 
336
	switch (pcint) {
-
 
337
		case 0:	pcInt[pcint].current = PINB; break;
-
 
338
		case 1:	pcInt[pcint].current = PIND; break;
-
 
339
		case 2:	pcInt[pcint].current = PINC; break;
-
 
340
	}
-
 
341
	pcInt[pcint].current = PIND;
-
 
342
	pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
-
 
343
	pcInt[pcint].last = pcInt[pcint].current;
336
 
344
 
337
	// Select what rotary we are dealing with
345
	// Select what rotary we are dealing with
338
	//   based on the pc interrupt that fired.
346
	//   based on the pc interrupt that fired.
339
	uint8_t rot = 0;
347
	uint8_t rot = 0;
340
	if (pcint == 1) 
348
	if (pcint == 1) 
341
		rot = 1;
349
		rot = 1;
342
 
350
 
343
	// If rot stat is not 0, we havn't sent
351
	// If rot stat is not 0, we havn't sent
344
	//  our last results yet. Skip this click.
352
	//  our last results yet. Skip this click.
345
	if (rotdata[rot][STAT] != 0) {
353
	if (rotData[rot].stat != 0) {
346
		pcIntMask[pcint] = 0;
354
		pcInt[pcint].mask = 0;
347
		return;
355
		return;
348
	}
356
	}
349
	// Check which pin caused the interrupt. If they both
357
	// Check which pin caused the interrupt. If they both
350
	//  equal 0, the pin that interrupted is the direction
358
	//  equal 0, the pin that interrupted is the direction
351
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
359
  	if (rbi(pcInt[pcint].current, PCINT17) == 0 
352
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
360
		&& rbi(pcInt[pcint].current, PCINT17) == 0 
353
		&& rbi(pcIntMask[pcint], PCINT16) ) {
361
		&& rbi(pcInt[pcint].mask, PCINT16) ) {
354
			rotdata[rot][STAT] = 1;
362
			rotData[rot].stat = 1;
355
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
363
  	} else if (rbi(pcInt[pcint].current, PCINT16) == 0 
356
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
364
		&& rbi(pcInt[pcint].current, PCINT17) == 0 
357
		&& rbi(pcIntMask[pcint], PCINT17) ) {
365
		&& rbi(pcInt[pcint].mask, PCINT17) ) {
358
			rotdata[rot][STAT] = 2;
366
			rotData[rot].stat = 2;
359
  	}
367
  	}
360
 
368
 
361
	// Clear the mask so we know we've delth with it
369
	// Clear the mask so we know we've delth with it
362
	pcIntMask[pcint] = 0;
370
	pcInt[pcint].mask = 0;
363
}
371
}
364
 
372
 
365
ISR(TIMER0_OVF_vect) {
373
ISR(TIMER0_OVF_vect) {
366
	if (switches[0][DETECTED] && switches[0][TIMER])
374
	if (switches[0][DETECTED] && switches[0][TIMER])
367
		switches[0][TIMER]--;
375
		switches[0][TIMER]--;
368
}
376
}
369
 
377
 
370
ISR(PCINT1_vect)
378
ISR(PCINT0_vect) {
-
 
379
	pcInterrupt(0);
371
{
380
}
372
        // Save the state and work out which pin caused
-
 
373
        //  the interrupt to occur
-
 
-
 
381
 
374
        pcIntCurr[1] = PIND;
382
ISR(PCINT1_vect) {
375
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
-
 
376
        pcIntLast[1] = pcIntCurr[1];
-
 
377
        doInt(1);
383
	pcInterrupt(1);
378
}
384
}
379
 
385
 
380
ISR(PCINT2_vect)
386
ISR(PCINT2_vect) {
381
{
-
 
382
	// Save the state and work out which pin caused
-
 
383
	//  the interrupt to occur
-
 
384
	pcIntCurr[2] = PIND;
-
 
385
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
-
 
386
	pcIntLast[2] = pcIntCurr[2];
-
 
387
	doInt(2);
387
	pcInterrupt(2);
388
}
388
}
389
 
389