Subversion Repositories group.electronics

Rev

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