Subversion Repositories group.electronics

Rev

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