Subversion Repositories group.electronics

Rev

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