Subversion Repositories group.electronics

Rev

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