Subversion Repositories group.electronics

Rev

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