Subversion Repositories group.electronics

Rev

Rev 34 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 pfowler 1
#include <avr/io.h>
2
#include <avr/pgmspace.h>
3
#include <avr/interrupt.h>
4
 
5
#define F_CPU 12000000
6
#include <util/delay.h>
7
#include <avr/wdt.h>
8
#include <usbdrv.h>
9
 
10
#include <stdlib.h>
11
#include <string.h>
12
 
13
#include "config.h"
14
#include "hiddesc.h"
15
 
16
#define ROTS_ATTACHED	2
17
#define STAT		0
18
#define SENT		1
19
 
20
 
21
void doInt(uint8_t pcint);
22
uint8_t getKey(void);
23
uint8_t analogRead(uint8_t pin);
24
 
25
volatile uint8_t pcIntCurr[3] = {0,0,0};
26
volatile uint8_t pcIntLast[3] = {0,0,0};
27
volatile uint8_t pcIntMask[3] = {0,0,0};
28
 
29
// rotdata = [rot#][(stat|sent)]
30
volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };
31
 
32
uint8_t keyMap[] = { 	'1', '2', '3',
33
			'4', '5', '6',
34
			'7', '8', '9',
35
			'*', '0', '#' };
36
 
37
struct {
38
	uint8_t report_id;
39
	uint8_t modifier;
40
	uint8_t reserved;
41
	uint8_t keycodes[6];
42
} reportKeyboard;
43
 
44
struct{
45
	uint8_t report_id;
46
  union {
47
    uint8_t data1[2];
48
    struct {
49
	uint8_t rx:8;
50
	uint8_t ry:8;
51
    };
52
  };
53
 
54
  union {
55
    uint16_t data2;
56
    struct {
57
	uint16_t buttons:12;
58
	uint16_t rot2a:1;
59
	uint16_t rot2b:1;
60
	uint16_t rot1a:1;
61
	uint16_t rot1b:1;
62
    };
63
  };
64
} reportJoystick;
65
 
66
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
67
	usbRequest_t *rq = (void *)data;
68
 
69
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
70
		switch (rq->bRequest) {
71
			case USBRQ_HID_GET_REPORT:
72
				if (rq->wValue.bytes[0] == 1)
73
					return sizeof(reportKeyboard);
74
				else if (rq->wValue.bytes[0] == 2)
75
					return sizeof(reportJoystick);
76
				else
77
					return 0;
78
			case USBRQ_HID_GET_IDLE:
79
				return 1;
80
			default:
81
				return 0;
82
		}
83
	}
84
	return 0;
85
}
86
 
87
void hadUsbReset(void) {
88
}
89
 
90
int main(void) {
91
 
92
  ACSR |= (1<<ACD); // Disable analog comparator
93
 
94
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
95
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
96
 
97
  /*
98
  DDR : 1 = Output, 0 = Input
99
  PORT: 1 = Pullup for Input, otherwise set output
100
  PIN : Read input pin
101
  */
102
 
103
  /*
104
	PB0	- Output 		- Keypad 1
105
	PB1	- Output 		- Keypad 2
106
	PB2	- Output 		- Keypad 3
107
	PB3	- Output 		- Keypad 4
108
	PB4	- Input, Pullup		- Function select
109
  */
110
  DDRB		= 0B00001111;
111
  PORTB 	= 0B00011111;
112
 
113
  /*
114
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
115
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
116
 
117
 
118
	PD4	- Input, Pullup		- Keypad 5
119
	PD5	- Input, Pullup		- Keypad 6
120
	PD6	- Input, Pullup		- Keypad 7
121
	PD7	- Input, Pullup		- Keypad 8
122
  */
123
  DDRD		= 0B00000000;
124
  PORTD		= 0B11110011;
125
 
126
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
127
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
128
 
129
  // Timers not used for the moment
130
  // Setup timer0 - Enable overflow, 8 times prescaler
131
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
132
  //TCNT0 = 0x00;				// Set Timer0 to 0
133
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
134
 
135
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
136
  _delay_ms(500);
137
  usbDeviceConnect();
138
 
139
  wdt_enable(WDTO_1S);
140
  usbInit();
141
  sei();
142
 
143
	reportKeyboard.report_id = 1;
144
	reportJoystick.report_id = 2;
145
 
146
  for(;;) {
147
    wdt_reset();
148
    usbPoll();
149
 
150
    if(usbInterruptIsReady()){
151
	reportJoystick.data1[0] = (-128 + analogRead(0));
152
	reportJoystick.data1[1] = (-128 + analogRead(1));
153
	reportJoystick.data2 = 0x0000;
154
 
155
 
156
	uint8_t key = getKey();
157
	if (rbi(PINB, PB5)) {
158
		// Keypad is joystick
159
		if (key > 0)
160
			reportJoystick.data2 |= (1 << (key -1));
161
	} else {
162
		// Keypad is keyboard
163
		if (key > 0) {
164
 
165
		}
166
	}
167
 
168
	// Now work out what rotary to send, if any
169
	// Also record if we sent a positive response, 
170
	//  so we can send a '0' next time (if selected on PD4)
171
	// rotdata = [rot#][(stat|sent)]
172
	uint8_t rot = 0;
173
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
174
	        if (rotdata[rot][STAT] == 0x01 && rotdata[rot][SENT] == 0) {
175
                	rotdata[rot][SENT] = 1;
176
			switch (rot) {
177
				case(0):	reportJoystick.rot1a = 1; break;
178
				case(1):	reportJoystick.rot2a = 1; break;
179
			}
180
	        } else if (rotdata[rot][STAT] == 0x02 && rotdata[rot][SENT] == 0) {
181
                	rotdata[rot][SENT] = 1;
182
                        switch (rot) {
183
                                case(0):      reportJoystick.rot1b = 1; break;
184
                                case(1):      reportJoystick.rot2b = 1; break;
185
                        }
186
	        } else {
187
        	        rotdata[rot][SENT] = 0;
188
	        }
189
		rotdata[rot][STAT] = 0;
190
 
191
	        if (rbi(PINB, PB4))
192
        	        rotdata[rot][SENT] = 0;
193
	}
194
 
195
/*	
196
	if (rot_stat[0] == 0x01 && rot_sent[0] == 0) {
197
		report.rot1a = 1;
198
		rot_sent[0] = 1;
199
	} else if (rot_stat[0] == 0x02 && rot_sent[0] == 0) {
200
		report.rot1b = 1;
201
		rot_sent[0] = 1;
202
	} else {
203
		rot_sent[0] = 0;
204
	}
205
 
206
	// Reset our stat so ready for next turn
207
	rot_stat[0] = 0;
208
 
209
	// If our function select is set, dont bother
210
	//  sending a 'o' between consequtive 1's.
211
	if (rbi(PINB, PB4))
212
		rot_sent[0] = 0;
213
*/
214
 
215
      /* called after every poll of the interrupt endpoint */
216
      usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
217
      usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
218
    }
219
  }
220
}
221
 
222
uint8_t analogRead(uint8_t pin) {
223
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
224
	ADCSRA |= (1<<ADSC);		// Start converting
225
 
226
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
227
	uint8_t result = ADCH;
228
	//ADCSRA |= (0<<ADSC);		// Stop converting
229
 
230
	return result;
231
}
232
 
233
uint8_t getKey() {
234
	uint8_t col, row = 0;
235
	uint8_t key = 0;
236
	uint8_t n = 1;
237
 
238
	for (row=0; row<=3; row++) {
239
		cbi(PORTB, row);
240
		_delay_us(10);				// Wait for the port to change
241
 
242
		for (col=5; col<=7; col++) {
243
			if (rbi(PIND, col) == 0)
244
				key = n;
245
			n++;
246
		}	
247
 
248
		sbi(PORTB, row);
249
	}
250
	return key;
251
}
252
 
253
/*
254
 *
255
 * Process the Pin Change Interrupt.
256
 * pcint provides what bank caused the interrupt
257
 *
258
 */
259
void doInt(uint8_t pcint) {
260
 
261
	// Select what rotary we are dealing with
262
	//   based on the pc interrupt that fired.
263
	uint8_t rot = 0;
264
	if (pcint == 1) 
265
		rot = 1;
266
 
267
	// If rot stat is not 0, we havn't sent
268
	//  our last results yet. Skip this click.
269
	if (rotdata[rot][STAT] != 0) {
270
		pcIntMask[pcint] = 0;
271
		return;
272
	}
273
	// Check which pin caused the interrupt. If they both
274
	//  equal 0, the pin that interrupted is the direction
275
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
276
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
277
		&& rbi(pcIntMask[pcint], PCINT16) ) {
278
			rotdata[rot][STAT] = 1;
279
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
280
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
281
		&& rbi(pcIntMask[pcint], PCINT17) ) {
282
			rotdata[rot][STAT] = 2;
283
  	}
284
 
285
	// Clear the mask so we know we've delth with it
286
	pcIntMask[pcint] = 0;
287
}
288
 
289
/* Not used for the moment
290
ISR(TIMER0_OVF_vect) {
291
	timer0_ovf++;
292
}
293
*/
294
 
295
ISR(PCINT1_vect)
296
{
297
        // Save the state and work out which pin caused
298
        //  the interrupt to occur
299
        pcIntCurr[1] = PIND;
300
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
301
        pcIntLast[1] = pcIntCurr[1];
302
        doInt(1);
303
}
304
 
305
ISR(PCINT2_vect)
306
{
307
	// Save the state and work out which pin caused
308
	//  the interrupt to occur
309
	pcIntCurr[2] = PIND;
310
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
311
	pcIntLast[2] = pcIntCurr[2];
312
	doInt(2);
313
}
314