Subversion Repositories group.electronics

Rev

Rev 35 | Rev 37 | 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
36 pfowler 120
	PB5	- Input, Pullup		- Function select
33 pfowler 121
  */
122
  DDRB		= 0B00001111;
36 pfowler 123
  PORTB 	= 0B00111111;
33 pfowler 124
 
125
  /*
126
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
127
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
128
 
129
 
35 pfowler 130
	PD4	- Output		- Keypad select status led
33 pfowler 131
	PD5	- Input, Pullup		- Keypad 6
132
	PD6	- Input, Pullup		- Keypad 7
133
	PD7	- Input, Pullup		- Keypad 8
134
  */
35 pfowler 135
  DDRD		= 0B00010000;
136
  PORTD		= 0B11100011;
33 pfowler 137
 
138
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
139
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
140
 
141
  // Timers not used for the moment
142
  // Setup timer0 - Enable overflow, 8 times prescaler
143
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
144
  //TCNT0 = 0x00;				// Set Timer0 to 0
145
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
146
 
147
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
148
  _delay_ms(500);
149
  usbDeviceConnect();
150
 
151
  wdt_enable(WDTO_1S);
152
  usbInit();
153
  sei();
154
 
155
	reportKeyboard.report_id = 1;
156
	reportJoystick.report_id = 2;
157
 
158
  for(;;) {
159
    wdt_reset();
160
    usbPoll();
161
 
162
    if(usbInterruptIsReady()){
163
	reportJoystick.data1[0] = (-128 + analogRead(0));
164
	reportJoystick.data1[1] = (-128 + analogRead(1));
36 pfowler 165
	reportJoystick.data2 = 0x0000;				// Clear all the buttons
33 pfowler 166
 
36 pfowler 167
	reportKeyboard.keycode = 0x00;
168
	reportKeyboard.modifier = 0x00;
33 pfowler 169
 
170
	uint8_t key = getKey();
171
	if (rbi(PINB, PB5)) {
36 pfowler 172
		cbi(PORTD, PD4);
33 pfowler 173
		// Keypad is joystick
174
		if (key > 0)
175
			reportJoystick.data2 |= (1 << (key -1));
176
	} else {
36 pfowler 177
		sbi(PORTD, PD4);
33 pfowler 178
		// Keypad is keyboard
179
		if (key > 0) {
36 pfowler 180
			key--;
181
			//if (key==10 || key==12)
182
			//	reportKeyboard.modifier |= (1<<1);	//Left shif
183
			//reportKeyboard.keycode = keyMap[key];
184
			reportKeyboard.keycode = 0x30;
185
 
33 pfowler 186
		}
187
	}
188
 
189
	// Now work out what rotary to send, if any
190
	// Also record if we sent a positive response, 
191
	//  so we can send a '0' next time (if selected on PD4)
192
	// rotdata = [rot#][(stat|sent)]
193
	uint8_t rot = 0;
194
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
195
	        if (rotdata[rot][STAT] == 0x01 && rotdata[rot][SENT] == 0) {
196
                	rotdata[rot][SENT] = 1;
197
			switch (rot) {
198
				case(0):	reportJoystick.rot1a = 1; break;
199
				case(1):	reportJoystick.rot2a = 1; break;
200
			}
201
	        } else if (rotdata[rot][STAT] == 0x02 && rotdata[rot][SENT] == 0) {
202
                	rotdata[rot][SENT] = 1;
203
                        switch (rot) {
204
                                case(0):      reportJoystick.rot1b = 1; break;
205
                                case(1):      reportJoystick.rot2b = 1; break;
206
                        }
207
	        } else {
208
        	        rotdata[rot][SENT] = 0;
209
	        }
210
		rotdata[rot][STAT] = 0;
211
 
212
	        if (rbi(PINB, PB4))
213
        	        rotdata[rot][SENT] = 0;
214
	}
215
 
216
      /* called after every poll of the interrupt endpoint */
217
      usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
218
      usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
219
    }
220
  }
221
}
222
 
223
uint8_t analogRead(uint8_t pin) {
224
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
225
	ADCSRA |= (1<<ADSC);		// Start converting
226
 
227
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
228
	uint8_t result = ADCH;
229
	//ADCSRA |= (0<<ADSC);		// Stop converting
230
 
231
	return result;
232
}
233
 
234
uint8_t getKey() {
235
	uint8_t col, row = 0;
236
	uint8_t key = 0;
237
	uint8_t n = 1;
238
 
239
	for (row=0; row<=3; row++) {
240
		cbi(PORTB, row);
241
		_delay_us(10);				// Wait for the port to change
242
 
243
		for (col=5; col<=7; col++) {
244
			if (rbi(PIND, col) == 0)
245
				key = n;
246
			n++;
247
		}	
248
 
249
		sbi(PORTB, row);
250
	}
251
	return key;
252
}
253
 
254
/*
255
 *
256
 * Process the Pin Change Interrupt.
257
 * pcint provides what bank caused the interrupt
258
 *
259
 */
260
void doInt(uint8_t pcint) {
261
 
262
	// Select what rotary we are dealing with
263
	//   based on the pc interrupt that fired.
264
	uint8_t rot = 0;
265
	if (pcint == 1) 
266
		rot = 1;
267
 
268
	// If rot stat is not 0, we havn't sent
269
	//  our last results yet. Skip this click.
270
	if (rotdata[rot][STAT] != 0) {
271
		pcIntMask[pcint] = 0;
272
		return;
273
	}
274
	// Check which pin caused the interrupt. If they both
275
	//  equal 0, the pin that interrupted is the direction
276
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
277
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
278
		&& rbi(pcIntMask[pcint], PCINT16) ) {
279
			rotdata[rot][STAT] = 1;
280
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
281
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
282
		&& rbi(pcIntMask[pcint], PCINT17) ) {
283
			rotdata[rot][STAT] = 2;
284
  	}
285
 
286
	// Clear the mask so we know we've delth with it
287
	pcIntMask[pcint] = 0;
288
}
289
 
290
/* Not used for the moment
291
ISR(TIMER0_OVF_vect) {
292
	timer0_ovf++;
293
}
294
*/
295
 
296
ISR(PCINT1_vect)
297
{
298
        // Save the state and work out which pin caused
299
        //  the interrupt to occur
300
        pcIntCurr[1] = PIND;
301
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
302
        pcIntLast[1] = pcIntCurr[1];
303
        doInt(1);
304
}
305
 
306
ISR(PCINT2_vect)
307
{
308
	// Save the state and work out which pin caused
309
	//  the interrupt to occur
310
	pcIntCurr[2] = PIND;
311
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
312
	pcIntLast[2] = pcIntCurr[2];
313
	doInt(2);
314
}
315