Subversion Repositories group.electronics

Rev

Rev 37 | Rev 40 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37 Rev 39
Line 11... Line 11...
11
#include <string.h>
11
#include <string.h>
12
 
12
 
13
#include "config.h"
13
#include "config.h"
14
#include "hiddesc.h"
14
#include "hiddesc.h"
15
 
15
 
16
#define ROTS_ATTACHED	2
16
#define ROTS_ATTACHED	1
17
#define STAT		0
17
#define STAT		0
18
#define SENT		1
18
#define SENT		1
19
 
19
 
-
 
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
 
20
/*
28
/*
21
 * Keyboard modifier codes
29
 * Keyboard modifier codes
22
 */
30
 */
23
#define MOD_CONTROL_LEFT    (1<<0)
31
#define MOD_CONTROL_LEFT    (1<<0)
24
#define MOD_SHIFT_LEFT      (1<<1)
32
#define MOD_SHIFT_LEFT      (1<<1)
Line 36... Line 44...
36
 
44
 
37
volatile uint8_t pcIntCurr[3] = {0,0,0};
45
volatile uint8_t pcIntCurr[3] = {0,0,0};
38
volatile uint8_t pcIntLast[3] = {0,0,0};
46
volatile uint8_t pcIntLast[3] = {0,0,0};
39
volatile uint8_t pcIntMask[3] = {0,0,0};
47
volatile uint8_t pcIntMask[3] = {0,0,0};
40
 
48
 
-
 
49
// switches = [detected][timer][debounced]
-
 
50
volatile uint8_t switches[1][3] = { {0,0,0} };
-
 
51
 
41
// rotdata = [rot#][(stat|sent)]
52
// rotdata = [rot#][(stat|sent)]
42
volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };
53
volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };
43
 
54
 
44
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
55
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
45
			0x21, 0x22, 0x23,
56
			0x21, 0x22, 0x23,
46
			0x24, 0x25, 0x26,
57
			0x24, 0x25, 0x26,
47
			0x20, 0x27, 0x25 };
58
			0x25, 0x27, 0x20 };
-
 
59
 
-
 
60
uint8_t keySelect = 1;
48
 
61
 
49
struct {
62
struct {
50
	uint8_t report_id;
63
	uint8_t report_id;
51
	uint8_t modifier;
64
	uint8_t modifier;
52
	uint8_t reserved;
-
 
53
	uint8_t keycode[6];
65
	uint8_t keycode;
54
} reportKeyboard;
66
} reportKeyboard;
55
 
67
 
56
struct{
68
struct{
57
	uint8_t report_id;
69
	uint8_t report_id;
58
  union {
70
  union {
Line 73... Line 85...
73
	uint16_t rot1b:1;
85
	uint16_t rot1b:1;
74
    };
86
    };
75
  };
87
  };
76
} reportJoystick;
88
} reportJoystick;
77
 
89
 
-
 
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
 
78
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
102
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
79
	usbRequest_t *rq = (void *)data;
103
	usbRequest_t *rq = (void *)data;
80
 
104
 
81
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
105
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
82
		switch (rq->bRequest) {
106
		switch (rq->bRequest) {
Line 101... Line 125...
101
 
125
 
102
int main(void) {
126
int main(void) {
103
 
127
 
104
  ACSR |= (1<<ACD); // Disable analog comparator
128
  ACSR |= (1<<ACD); // Disable analog comparator
105
 
129
 
-
 
130
  /*
-
 
131
	Setup ADC
-
 
132
	ADMUX: 8 bit mode, Avcc ref
-
 
133
	ADCSRA: Enable, 128 prescale
-
 
134
  */
106
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
135
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
107
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
136
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
108
 
137
 
109
  /*
138
  /*
110
  DDR : 1 = Output, 0 = Input
139
  DDR : 1 = Output, 0 = Input
111
  PORT: 1 = Pullup for Input, otherwise set output
140
  PORT: 1 = Pullup for Input, otherwise set output
112
  PIN : Read input pin
141
  PIN : Read input pin
113
  */
142
  */
114
 
143
 
115
  /*
144
  /*
116
	PB0	- Output 		- Keypad 1
145
	PB0	- Output 		- Keypad 2
117
	PB1	- Output 		- Keypad 2
146
	PB1	- Output 		- Keypad 7
118
	PB2	- Output 		- Keypad 3
147
	PB2	- Output 		- Keypad 6
119
	PB3	- Output 		- Keypad 4
148
	PB3	- Output 		- Keypad 4
120
	PB4	- Input, Pullup		- Function select
149
	PB4	- Input, Pullup		- Function select
121
	PB5	- Input, Pullup		- Function select
150
	PB5	- Input, Pullup		- Function select
122
  */
151
  */
123
  DDRB		= 0B00001111;
152
  DDRB		= 0B00001111;
Line 127... Line 156...
127
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
156
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
128
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
157
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
129
 
158
 
130
 
159
 
131
	PD4	- Output		- Keypad select status led
160
	PD4	- Output		- Keypad select status led
132
	PD5	- Input, Pullup		- Keypad 6
161
	PD5	- Input, Pullup		- Keypad 3
133
	PD6	- Input, Pullup		- Keypad 7
162
	PD6	- Input, Pullup		- Keypad 1
134
	PD7	- Input, Pullup		- Keypad 8
163
	PD7	- Input, Pullup		- Keypad 5
135
  */
164
  */
136
  DDRD		= 0B00010000;
165
  DDRD		= 0B00010000;
137
  PORTD		= 0B11100011;
166
  PORTD		= 0B11100011;
138
 
167
 
139
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
168
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
140
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
169
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
141
 
170
 
142
  // Timers not used for the moment
171
  // Timers not used for the moment
143
  // Setup timer0 - Enable overflow, 8 times prescaler
172
  // Setup timer0 - Enable overflow, 8 times prescaler
144
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
173
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
145
  //TCNT0 = 0x00;				// Set Timer0 to 0
174
	TCNT0 = 0x00;				// Set Timer0 to 0
146
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
175
	TCCR0B = (1<< CS01) ;			// /8 prescaler
147
 
176
 
148
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
177
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
149
  _delay_ms(500);
178
  _delay_ms(500);
150
  usbDeviceConnect();
179
  usbDeviceConnect();
151
 
180
 
152
  wdt_enable(WDTO_1S);
181
  wdt_enable(WDTO_1S);
153
  usbInit();
182
  usbInit();
154
  sei();
183
  sei();
155
 
184
 
156
	reportKeyboard.report_id = 1;
185
	reportKeyboard.report_id = 1;
157
	reportKeyboard.reserved = 0x00;
-
 
158
 
-
 
159
	reportJoystick.report_id = 2;
186
	reportJoystick.report_id = 2;
160
 
187
 
161
 
-
 
162
  for(;;) {
188
  for(;;) {
163
    wdt_reset();
189
    wdt_reset();
164
    usbPoll();
190
    usbPoll();
165
 
191
 
-
 
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
 
166
    if(usbInterruptIsReady()){
242
    if(usbInterruptIsReady()){
167
	reportJoystick.data1[0] = (-128 + analogRead(0));
243
	reportJoystick.data1[0] = (-128 + analogRead(0));
168
	reportJoystick.data1[1] = (-128 + analogRead(1));
244
	reportJoystick.data1[1] = (-128 + analogRead(1));
169
	reportJoystick.data2 = 0x0000;				// Clear all the buttons
245
	reportJoystick.data2 = 0x0000;				// Clear all the buttons
170
 
246
 
171
	reportKeyboard.modifier = 0x00;
247
	reportKeyboard.modifier = 0x00;
172
	reportKeyboard.keycode[0] = 0x00;
248
	reportKeyboard.keycode = 0x00;
173
 
249
 
174
	uint8_t key = getKey();
250
	uint8_t key = getKey();
175
	if (rbi(PINB, PB5)) {
251
	if (rbi(keySelect, 0)) {
176
		cbi(PORTD, PD4);
-
 
177
		// Keypad is joystick
252
		// Keypad is joystick
178
		if (key > 0)
253
		if (key > 0)
179
			reportJoystick.data2 |= (1 << (key -1));
254
			reportJoystick.data2 |= (1 << (--key));
180
	} else {
255
	} else {
181
		sbi(PORTD, PD4);
-
 
182
		// Keypad is keyboard
256
		// Keypad is keyboard
183
		if (key > 0) {
257
		if (key > 0) {
184
			key--;
-
 
185
			//if (key==0x20 || key==0x25)
258
			if (key==10 || key==12)
186
			//	reportKeyboard.modifier |= (1<<1);	//Left shif
259
				reportKeyboard.modifier |= (1<<1);	//Left shift
187
			//reportKeyboard.keycode = keyMap[key];
260
			reportKeyboard.keycode = keyMap[--key];
188
			reportKeyboard.keycode[0] = 0x30;
-
 
189
 
-
 
190
		}
261
		}
191
	}
262
	}
192
 
263
 
193
	// Now work out what rotary to send, if any
264
	// Now work out what rotary to send, if any
194
	// Also record if we sent a positive response, 
265
	// Also record if we sent a positive response, 
Line 216... Line 287...
216
	        if (rbi(PINB, PB4))
287
	        if (rbi(PINB, PB4))
217
        	        rotdata[rot][SENT] = 0;
288
        	        rotdata[rot][SENT] = 0;
218
	}
289
	}
219
 
290
 
220
      /* called after every poll of the interrupt endpoint */
291
      /* called after every poll of the interrupt endpoint */
221
      usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
292
      //usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
222
      usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
293
      //usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
-
 
294
 
-
 
295
	usbSendHidReport(&reportKeyboard, sizeof(reportKeyboard));
-
 
296
	usbSendHidReport(&reportJoystick, sizeof(reportJoystick));
-
 
297
 
223
    }
298
    }
224
  }
299
  }
225
}
300
}
226
 
301
 
227
uint8_t analogRead(uint8_t pin) {
302
uint8_t analogRead(uint8_t pin) {
Line 289... Line 364...
289
 
364
 
290
	// Clear the mask so we know we've delth with it
365
	// Clear the mask so we know we've delth with it
291
	pcIntMask[pcint] = 0;
366
	pcIntMask[pcint] = 0;
292
}
367
}
293
 
368
 
294
/* Not used for the moment
-
 
295
ISR(TIMER0_OVF_vect) {
369
ISR(TIMER0_OVF_vect) {
-
 
370
	if (switches[0][DETECTED] && switches[0][TIMER])
296
	timer0_ovf++;
371
		switches[0][TIMER]--;
297
}
372
}
298
*/
-
 
299
 
373
 
300
ISR(PCINT1_vect)
374
ISR(PCINT1_vect)
301
{
375
{
302
        // Save the state and work out which pin caused
376
        // Save the state and work out which pin caused
303
        //  the interrupt to occur
377
        //  the interrupt to occur