Subversion Repositories group.electronics

Rev

Rev 33 | Rev 35 | 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
 
129
	PD4	- Input, Pullup		- Keypad 5
130
	PD5	- Input, Pullup		- Keypad 6
131
	PD6	- Input, Pullup		- Keypad 7
132
	PD7	- Input, Pullup		- Keypad 8
133
  */
134
  DDRD		= 0B00000000;
135
  PORTD		= 0B11110011;
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)) {
169
		// Keypad is joystick
170
		if (key > 0)
171
			reportJoystick.data2 |= (1 << (key -1));
172
	} else {
173
		// Keypad is keyboard
174
		if (key > 0) {
34 pfowler 175
			reportKeyboard.modifier = 0x00;
176
			if (key==10 || key==12)
177
				reportKeyboard.modifier |= (1<<1);	//Left shif
178
			reportKeyboard.keycode = keyMap[key];
33 pfowler 179
		}
180
	}
181
 
182
	// Now work out what rotary to send, if any
183
	// Also record if we sent a positive response, 
184
	//  so we can send a '0' next time (if selected on PD4)
185
	// rotdata = [rot#][(stat|sent)]
186
	uint8_t rot = 0;
187
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
188
	        if (rotdata[rot][STAT] == 0x01 && rotdata[rot][SENT] == 0) {
189
                	rotdata[rot][SENT] = 1;
190
			switch (rot) {
191
				case(0):	reportJoystick.rot1a = 1; break;
192
				case(1):	reportJoystick.rot2a = 1; break;
193
			}
194
	        } else if (rotdata[rot][STAT] == 0x02 && rotdata[rot][SENT] == 0) {
195
                	rotdata[rot][SENT] = 1;
196
                        switch (rot) {
197
                                case(0):      reportJoystick.rot1b = 1; break;
198
                                case(1):      reportJoystick.rot2b = 1; break;
199
                        }
200
	        } else {
201
        	        rotdata[rot][SENT] = 0;
202
	        }
203
		rotdata[rot][STAT] = 0;
204
 
205
	        if (rbi(PINB, PB4))
206
        	        rotdata[rot][SENT] = 0;
207
	}
208
 
209
/*	
210
	if (rot_stat[0] == 0x01 && rot_sent[0] == 0) {
211
		report.rot1a = 1;
212
		rot_sent[0] = 1;
213
	} else if (rot_stat[0] == 0x02 && rot_sent[0] == 0) {
214
		report.rot1b = 1;
215
		rot_sent[0] = 1;
216
	} else {
217
		rot_sent[0] = 0;
218
	}
219
 
220
	// Reset our stat so ready for next turn
221
	rot_stat[0] = 0;
222
 
223
	// If our function select is set, dont bother
224
	//  sending a 'o' between consequtive 1's.
225
	if (rbi(PINB, PB4))
226
		rot_sent[0] = 0;
227
*/
228
 
229
      /* called after every poll of the interrupt endpoint */
230
      usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
231
      usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
232
    }
233
  }
234
}
235
 
236
uint8_t analogRead(uint8_t pin) {
237
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
238
	ADCSRA |= (1<<ADSC);		// Start converting
239
 
240
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
241
	uint8_t result = ADCH;
242
	//ADCSRA |= (0<<ADSC);		// Stop converting
243
 
244
	return result;
245
}
246
 
247
uint8_t getKey() {
248
	uint8_t col, row = 0;
249
	uint8_t key = 0;
250
	uint8_t n = 1;
251
 
252
	for (row=0; row<=3; row++) {
253
		cbi(PORTB, row);
254
		_delay_us(10);				// Wait for the port to change
255
 
256
		for (col=5; col<=7; col++) {
257
			if (rbi(PIND, col) == 0)
258
				key = n;
259
			n++;
260
		}	
261
 
262
		sbi(PORTB, row);
263
	}
264
	return key;
265
}
266
 
267
/*
268
 *
269
 * Process the Pin Change Interrupt.
270
 * pcint provides what bank caused the interrupt
271
 *
272
 */
273
void doInt(uint8_t pcint) {
274
 
275
	// Select what rotary we are dealing with
276
	//   based on the pc interrupt that fired.
277
	uint8_t rot = 0;
278
	if (pcint == 1) 
279
		rot = 1;
280
 
281
	// If rot stat is not 0, we havn't sent
282
	//  our last results yet. Skip this click.
283
	if (rotdata[rot][STAT] != 0) {
284
		pcIntMask[pcint] = 0;
285
		return;
286
	}
287
	// Check which pin caused the interrupt. If they both
288
	//  equal 0, the pin that interrupted is the direction
289
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
290
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
291
		&& rbi(pcIntMask[pcint], PCINT16) ) {
292
			rotdata[rot][STAT] = 1;
293
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
294
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
295
		&& rbi(pcIntMask[pcint], PCINT17) ) {
296
			rotdata[rot][STAT] = 2;
297
  	}
298
 
299
	// Clear the mask so we know we've delth with it
300
	pcIntMask[pcint] = 0;
301
}
302
 
303
/* Not used for the moment
304
ISR(TIMER0_OVF_vect) {
305
	timer0_ovf++;
306
}
307
*/
308
 
309
ISR(PCINT1_vect)
310
{
311
        // Save the state and work out which pin caused
312
        //  the interrupt to occur
313
        pcIntCurr[1] = PIND;
314
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
315
        pcIntLast[1] = pcIntCurr[1];
316
        doInt(1);
317
}
318
 
319
ISR(PCINT2_vect)
320
{
321
	// Save the state and work out which pin caused
322
	//  the interrupt to occur
323
	pcIntCurr[2] = PIND;
324
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
325
	pcIntLast[2] = pcIntCurr[2];
326
	doInt(2);
327
}
328