Subversion Repositories group.electronics

Rev

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