Subversion Repositories group.electronics

Rev

Rev 63 | Rev 94 | 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;
64 pfowler 188
  PORTD		= 0B11110011;
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
 
64 pfowler 203
	sei();
40 pfowler 204
	i2c_master();
63 pfowler 205
	lcd_init();
206
	lcd_createChar(0x00, emblock);
40 pfowler 207
 
63 pfowler 208
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
209
        lcd_setCursor(0, 1);
210
        lcd_print(strTime);
33 pfowler 211
 
63 pfowler 212
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
213
	_delay_ms(500);
214
	usbDeviceConnect();
215
 
216
	wdt_enable(WDTO_1S);
217
	usbInit();
64 pfowler 218
	//sei();
33 pfowler 219
 
220
	reportKeyboard.report_id = 1;
221
	reportJoystick.report_id = 2;
222
 
64 pfowler 223
	cbi(PORTD, PD4);
33 pfowler 224
  for(;;) {
225
    wdt_reset();
226
    usbPoll();
227
 
39 pfowler 228
	/*
229
	 * This is some really bad deboucing code
230
	 */
231
 
232
	// Detect the button press, wait 100 timer cycles (1.7ms / cycle)
233
	if (rbi(PINB, PB5) == 0 && switches[0][DETECTED]==0) {
234
		switches[0][DETECTED] = 1;
235
		switches[0][TIMER] = 100;
236
	}
237
 
238
	// After timer is zero, check switch again.
239
	//  If switch still pressed, its debounced
240
	//  Otherwise, reset the debounce
241
	if (switches[0][DETECTED] == 1 && switches[0][TIMER] == 0 && switches[0][DEBOUNCED]==0) {
242
		if (rbi(PINB, PB5) == 0)	
243
			switches[0][DEBOUNCED] = 1;
244
		else {
245
			switches[0][DETECTED] = 0;
246
			switches[0][TIMER] = 0;
247
		}
248
	}
249
 
250
	// If the switch has come up, do another debounce
251
	if (rbi(PINB, PB5) && switches[0][DETECTED]==2) {
252
		switches[0][TIMER] = 100;
253
		switches[0][DETECTED] = 3;
254
	}
255
 
256
	// After the up switch timer is zero, do the debounce check
257
	// Otherwise, assume switch is still down
258
	if (switches[0][DETECTED] == 3 && switches[0][TIMER] == 0) {
259
		if (rbi(PINB, PB5))
260
			switches[0][DETECTED] = 0;
261
	}
262
 
263
	// Process the switch
264
	if (switches[0][DEBOUNCED] == 1) {
265
		xbi(keySelect, 0);
266
 
267
		if (keySelect == 0)
268
			sbi(PORTD, PD4);
269
		else
270
			cbi(PORTD, PD4);
271
 
272
		switches[0][DETECTED] = 2;
273
		switches[0][DEBOUNCED] = 0;
274
	}
275
 
63 pfowler 276
	usbPoll();	
39 pfowler 277
 
33 pfowler 278
    if(usbInterruptIsReady()){
279
	reportJoystick.data1[0] = (-128 + analogRead(0));
280
	reportJoystick.data1[1] = (-128 + analogRead(1));
36 pfowler 281
	reportJoystick.data2 = 0x0000;				// Clear all the buttons
33 pfowler 282
 
36 pfowler 283
	reportKeyboard.modifier = 0x00;
39 pfowler 284
	reportKeyboard.keycode = 0x00;
33 pfowler 285
 
286
	uint8_t key = getKey();
39 pfowler 287
	if (rbi(keySelect, 0)) {
33 pfowler 288
		// Keypad is joystick
289
		if (key > 0)
39 pfowler 290
			reportJoystick.data2 |= (1 << (--key));
33 pfowler 291
	} else {
292
		// Keypad is keyboard
293
		if (key > 0) {
39 pfowler 294
			if (key==10 || key==12)
295
				reportKeyboard.modifier |= (1<<1);	//Left shift
296
			reportKeyboard.keycode = keyMap[--key];
33 pfowler 297
		}
298
	}
299
 
300
	// Now work out what rotary to send, if any
301
	// Also record if we sent a positive response, 
302
	//  so we can send a '0' next time (if selected on PD4)
62 pfowler 303
	// rotData = [rot#][(stat|sent)]
33 pfowler 304
	uint8_t rot = 0;
305
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
62 pfowler 306
	        if (rotData[rot].stat == 0x01 && rotData[rot].sent == 0) {
307
                	rotData[rot].sent = 1;
33 pfowler 308
			switch (rot) {
309
				case(0):	reportJoystick.rot1a = 1; break;
310
				case(1):	reportJoystick.rot2a = 1; break;
311
			}
62 pfowler 312
	        } else if (rotData[rot].stat == 0x02 && rotData[rot].sent == 0) {
313
                	rotData[rot].sent = 1;
33 pfowler 314
                        switch (rot) {
315
                                case(0):      reportJoystick.rot1b = 1; break;
316
                                case(1):      reportJoystick.rot2b = 1; break;
317
                        }
318
	        } else {
62 pfowler 319
        	        rotData[rot].sent = 0;
33 pfowler 320
	        }
62 pfowler 321
		rotData[rot].stat = 0;
33 pfowler 322
 
323
	        if (rbi(PINB, PB4))
62 pfowler 324
        	        rotData[rot].sent = 0;
33 pfowler 325
	}
326
 
327
      /* called after every poll of the interrupt endpoint */
39 pfowler 328
      //usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
329
      //usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
330
 
331
	usbSendHidReport(&reportKeyboard, sizeof(reportKeyboard));
332
	usbSendHidReport(&reportJoystick, sizeof(reportJoystick));
333
 
33 pfowler 334
    }
63 pfowler 335
 
336
	if (lcdupdate) {
64 pfowler 337
		usbPoll();
63 pfowler 338
		lcdupdate = 0;
339
 
340
                char syschar[10];
341
                ultoa(systime, syschar, 10);
342
                lcd_overprint_right(syschar, 10, 5, 1);
343
 
344
                uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
345
                if (potVal != oldpotVal) {
346
                        lcd_percent_graph(potVal, 0, 0);
347
                        oldpotVal = potVal;
348
 
349
                        char pot[3];
350
                        utoa(potVal, pot, 10);
351
                        lcd_overprint_right(pot, 3, 11, 0);
352
 
353
                        // Set percentage
354
                        lcd_setCursor(15, 0);
355
                        lcd_char(0x25);
356
                }
357
 
358
	}
359
 
33 pfowler 360
  }
361
}
362
 
363
uint8_t getKey() {
364
	uint8_t col, row = 0;
365
	uint8_t key = 0;
366
	uint8_t n = 1;
367
 
368
	for (row=0; row<=3; row++) {
369
		cbi(PORTB, row);
370
		_delay_us(10);				// Wait for the port to change
371
 
372
		for (col=5; col<=7; col++) {
373
			if (rbi(PIND, col) == 0)
374
				key = n;
375
			n++;
376
		}	
377
 
378
		sbi(PORTB, row);
379
	}
380
	return key;
381
}
382
 
383
/*
384
 *
385
 * Process the Pin Change Interrupt.
386
 * pcint provides what bank caused the interrupt
387
 *
388
 */
62 pfowler 389
void pcInterrupt(uint8_t pcint) {
33 pfowler 390
 
62 pfowler 391
	switch (pcint) {
392
		case 0:	pcInt[pcint].current = PINB; break;
393
		case 1:	pcInt[pcint].current = PIND; break;
394
		case 2:	pcInt[pcint].current = PINC; break;
395
	}
396
	pcInt[pcint].current = PIND;
397
	pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
398
	pcInt[pcint].last = pcInt[pcint].current;
399
 
33 pfowler 400
	// Select what rotary we are dealing with
401
	//   based on the pc interrupt that fired.
402
	uint8_t rot = 0;
403
	if (pcint == 1) 
404
		rot = 1;
405
 
406
	// If rot stat is not 0, we havn't sent
407
	//  our last results yet. Skip this click.
62 pfowler 408
	if (rotData[rot].stat != 0) {
409
		pcInt[pcint].mask = 0;
33 pfowler 410
		return;
411
	}
412
	// Check which pin caused the interrupt. If they both
413
	//  equal 0, the pin that interrupted is the direction
62 pfowler 414
  	if (rbi(pcInt[pcint].current, PCINT17) == 0 
415
		&& rbi(pcInt[pcint].current, PCINT17) == 0 
416
		&& rbi(pcInt[pcint].mask, PCINT16) ) {
417
			rotData[rot].stat = 1;
418
  	} else if (rbi(pcInt[pcint].current, PCINT16) == 0 
419
		&& rbi(pcInt[pcint].current, PCINT17) == 0 
420
		&& rbi(pcInt[pcint].mask, PCINT17) ) {
421
			rotData[rot].stat = 2;
33 pfowler 422
  	}
423
 
424
	// Clear the mask so we know we've delth with it
62 pfowler 425
	pcInt[pcint].mask = 0;
33 pfowler 426
}
427
 
428
ISR(TIMER0_OVF_vect) {
63 pfowler 429
	tmr0_ovf++;
430
	if (tmr0_ovf>=46) {
431
		systime++;
432
		tmr0_ovf = 0;
433
	}
39 pfowler 434
	if (switches[0][DETECTED] && switches[0][TIMER])
435
		switches[0][TIMER]--;
33 pfowler 436
}
437
 
63 pfowler 438
ISR(TIMER2_OVF_vect) {
439
        tmr2_ovf++;
440
        if (tmr2_ovf>=58) {
441
                lcdupdate=1;
442
                tmr2_ovf = 0;
64 pfowler 443
	//xbi(PORTD, PD4);
63 pfowler 444
        }
445
}
446
 
447
 
62 pfowler 448
ISR(PCINT0_vect) {
449
	pcInterrupt(0);
33 pfowler 450
}
451
 
62 pfowler 452
ISR(PCINT1_vect) {
453
	pcInterrupt(1);
33 pfowler 454
}
455
 
62 pfowler 456
ISR(PCINT2_vect) {
457
	pcInterrupt(2);
458
}
459