Subversion Repositories group.electronics

Rev

Rev 110 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
98 pfowler 1
/*
2
 * atcpad.c
3
 *
4
 * Created: 7/06/2013 10:15:34 PM
5
 *  Author: pfowler
6
 */ 
7
 
8
 
33 pfowler 9
#include <avr/io.h>
10
#include <avr/pgmspace.h>
11
#include <avr/interrupt.h>
12
 
13
#include <util/delay.h>
14
#include <avr/wdt.h>
100 pfowler 15
#include <stdlib.h>
16
#include <string.h>
17
 
98 pfowler 18
#include "usbdrv.h"
19
#include "atcpad.h"
20
#include "avrutil.h"
63 pfowler 21
#include "lcd.h"
40 pfowler 22
#include "wire.h"
33 pfowler 23
#include "hiddesc.h"
24
 
101 pfowler 25
#define BUTTONS	 		1
39 pfowler 26
 
101 pfowler 27
#define LCD_UPDATE		11
100 pfowler 28
 
108 pfowler 29
#define ROTARY_ON_TIME		30
30
#define ROTARY_OFF_TIME		15
31
 
102 pfowler 32
void pcInterrupt(uint8_t);
100 pfowler 33
uint8_t getKey(void);
34
void updateLcd();
101 pfowler 35
void checkRotarys();
36
void checkButtons(void);
100 pfowler 37
 
101 pfowler 38
 
105 pfowler 39
// * = 0x25, #=0x20, 0 = 0x27
40
// F9 = 0x42, F12 = 0x45, ` = 0x35
41
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,	// 1, 2, 3
42
						0x21, 0x22, 0x23,	// 4, 5, 6
43
						0x24, 0x25, 0x26,	// 7, 8, 9
44
						0x42, 0x35, 0x45 };	// F9, `, F12
100 pfowler 45
 
98 pfowler 46
uint8_t lcdRectangle[] = {   
47
	0B00011111,
48
	0B00010001,
49
	0B00010001,
50
	0B00010001,
51
	0B00010001,
52
	0B00010001,
53
	0B00010001,
54
	0B00011111 };
97 pfowler 55
 
56
 
100 pfowler 57
uint8_t oldpotVal = 0;
58
uint8_t keySelect = 1;
59
 
101 pfowler 60
// Start these of at different times, so they
61
//  don't kick of it the same loop run
62
volatile uint8_t lcdTimer = 8;
63
 
98 pfowler 64
volatile struct {
102 pfowler 65
        uint8_t current;
66
        uint8_t last;
67
        uint8_t mask;
68
} pcInt[3];
69
 
70
volatile struct {
98 pfowler 71
	uint8_t detected;
72
	uint8_t timer;
99 pfowler 73
	uint8_t waitup;
98 pfowler 74
} buttons[BUTTONS];
97 pfowler 75
 
101 pfowler 76
volatile struct {
105 pfowler 77
	uint8_t direction;
78
	uint8_t timer;
110 pfowler 79
} rotary[2];
101 pfowler 80
 
104 pfowler 81
int main(void) {
98 pfowler 82
 
83
	setup();
84
 
85
    while(1)
86
    {
100 pfowler 87
		wdt_reset();
98 pfowler 88
        loop(); 
89
    }
39 pfowler 90
}
91
 
98 pfowler 92
void setup() {
97 pfowler 93
	/*
94
		DDR : 1 = Output, 0 = Input
95
		PORT: 1 = Pullup for Input, otherwise set output
96
		PIN : Read input pin
97
	*/
33 pfowler 98
 
97 pfowler 99
	/*
100
		PB0	- Output 		- Keypad 2
101
		PB1	- Output 		- Keypad 7
102
		PB2	- Output 		- Keypad 6
103
		PB3	- Output 		- Keypad 4
104
		PB4	- Input, Pullup		- Function select
105
		PB5	- Input, Pullup		- Function select
106
	*/
101 pfowler 107
	DDRB	= 0B00001111;
97 pfowler 108
	PORTB 	= 0B00111111;
110 pfowler 109
 
110
	/*
111 pfowler 111
		PC2	- Input, Pullup, PCINT16	- Rotary 2a
112
		PC3	- Input, Pullup, PCINT16	- Rotary 2b
110 pfowler 113
	*/
114
	DDRC	= 0B00000000;
111 pfowler 115
	PORTC 	= 0B00001100;	
33 pfowler 116
 
97 pfowler 117
	/*
118
		PD0	- Input, Pullup, PCINT16	- Rotary 1a
119
		PD1	- Input, Pullup, PCINT17	- Rotary 1b
33 pfowler 120
 
121
 
97 pfowler 122
		PD4	- Output		- Keypad select status led
123
		PD5	- Input, Pullup		- Keypad 3
124
		PD6	- Input, Pullup		- Keypad 1
125
		PD7	- Input, Pullup		- Keypad 5
126
	*/
101 pfowler 127
	DDRD	= 0B00010000;
128
	PORTD	= 0B11110011;
98 pfowler 129
 
110 pfowler 130
	// Pin Change Interrupts
131
	PCMSK1 |= (( 1 << PCINT10 ) | ( 1 << PCINT11 )); //Rotary Encoder 2
132
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //Rotary Encoder 1
133
	PCICR |= ((1 << PCIE1 ) | (1 << PCIE2)); 
134
 
98 pfowler 135
	analogInit();
136
	sysclockInit();
137
 
138
	reportKeyboard.report_id = 1;
139
	reportJoystick.report_id = 2;	
140
 
97 pfowler 141
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
142
	_delay_ms(500);
143
	usbDeviceConnect();
33 pfowler 144
 
99 pfowler 145
	sei();
97 pfowler 146
	wdt_enable(WDTO_1S);
147
	usbInit();
63 pfowler 148
 
40 pfowler 149
	i2c_master();
63 pfowler 150
	lcd_init();
98 pfowler 151
	lcd_createChar(0x00, lcdRectangle);
40 pfowler 152
 
63 pfowler 153
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
97 pfowler 154
	lcd_setCursor(0, 1);
98 pfowler 155
	lcd_print(strTime);
99 pfowler 156
 
157
	buttons[0].detected = 0;
158
	buttons[0].timer = 0;
159
	buttons[0].waitup = 0;
160
 
161
	cbi(PORTD, 4);
98 pfowler 162
 
163
}
33 pfowler 164
 
98 pfowler 165
void loop() {
99 pfowler 166
 
100 pfowler 167
   	usbPoll();
168
 
101 pfowler 169
	if (lcdTimer==0) {
100 pfowler 170
		updateLcd();
101 pfowler 171
		lcdTimer = LCD_UPDATE;
100 pfowler 172
	}
101 pfowler 173
 
103 pfowler 174
	reportJoystick.data1[0] = (-128 + analogRead(0));
175
	reportJoystick.data1[1] = (-128 + analogRead(1));
176
	reportJoystick.data2 = 0x0000;		// Clear all the buttons
177
 
178
	reportKeyboard.modifier = 0x00;
179
	reportKeyboard.keycode = 0x00;	
101 pfowler 180
 
181
 
103 pfowler 182
	checkButtons();
183
	checkRotarys();
102 pfowler 184
 
100 pfowler 185
 
98 pfowler 186
	if(usbInterruptIsReady()){
187
	    usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
188
	    usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
100 pfowler 189
    }	
33 pfowler 190
}
191
 
192
uint8_t getKey() {
193
	uint8_t col, row = 0;
194
	uint8_t key = 0;
195
	uint8_t n = 1;
196
 
197
	for (row=0; row<=3; row++) {
198
		cbi(PORTB, row);
199
		_delay_us(10);				// Wait for the port to change
200
 
201
		for (col=5; col<=7; col++) {
202
			if (rbi(PIND, col) == 0)
98 pfowler 203
			key = n;
33 pfowler 204
			n++;
98 pfowler 205
		}
33 pfowler 206
 
207
		sbi(PORTB, row);
208
	}
209
	return key;
210
}
211
 
101 pfowler 212
void checkRotarys() {
110 pfowler 213
	uint8_t i = 0;
214
	for (i=0; i<=1; i++) {
215
		// If our rotary timer has run out, turn direction
216
		//   off and set the cool down period
217
		if (rotary[i].direction && rotary[i].timer == 0) {
218
			rotary[i].direction = 0x00;
219
			rotary[i].timer = ROTARY_OFF_TIME;
108 pfowler 220
		}
110 pfowler 221
 
222
		// If timer is still going, set the correct button
223
		//   of the joystick to press
224
		if (rotary[i].timer != 0) {
225
			if (rotary[i].direction == 1) {
226
				switch (i) {
227
					case 0: reportJoystick.rot1a = 1; break;
228
					case 1: reportJoystick.rot2a = 1; break;
229
				}
230
 
231
			} else if (rotary[i].direction == 2) {
232
				switch (i) {
233
					case 0: reportJoystick.rot1b = 1; break;
234
					case 1: reportJoystick.rot2b = 1; break;
235
				} 
236
			}
237
		}
108 pfowler 238
	}
101 pfowler 239
}
240
 
241
void checkButtons() {
242
 
243
	uint8_t key = getKey();
244
	if (rbi(keySelect, 0)) {
245
		// Keypad is joystick
246
		if (key > 0)
247
			reportJoystick.data2 |= (1 << (--key));
248
		} else {
249
		// Keypad is keyboard
250
		if (key > 0) {
251
			//if (key==10 || key==12) // Left shift, for *, #
252
			//	reportKeyboard.modifier |= (1<<1);
253
			reportKeyboard.keycode = keyMap[--key];
254
		}
255
	}
256
 
257
	// Only one button for now
258
	if (rbi(PINB, 5) == 0 && buttons[0].detected == 0 && buttons[0].timer == 0) {
259
		buttons[0].detected = 1;
260
		buttons[0].timer = 5;
102 pfowler 261
		buttons[0].waitup = 0;
101 pfowler 262
	}
263
 
264
	if (rbi(PINB, 5) == 1 && buttons[0].waitup == 1) {
265
		buttons[0].detected = 0;
266
		buttons[0].timer = 0;
267
		buttons[0].waitup = 0;
268
	}
269
 
270
	if (buttons[0].detected == 1 && buttons[0].timer == 0 && buttons[0].waitup == 0 ) {
271
 
272
		xbi(keySelect, 0);
273
 
274
		if (keySelect == 0)
275
			sbi(PORTD, PD4);
276
		else
277
			cbi(PORTD, PD4);	
278
 
279
		buttons[0].waitup = 1;
280
	}
281
}
282
 
100 pfowler 283
void updateLcd() {
284
	usbPoll();
285
 
286
 
287
	char syschar[10];
288
	ultoa(systime, syschar, 10);
289
	lcd_overprint_right(syschar, 10, 5, 1);
290
 
291
	uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
292
	if (potVal != oldpotVal) {
293
		lcd_percent_graph(potVal, 0, 0);
294
		oldpotVal = potVal;
295
 
296
		char pot[3];
297
		utoa(potVal, pot, 10);
298
		lcd_overprint_right(pot, 3, 11, 0);
299
 
300
		// Set percentage
301
		lcd_setCursor(15, 0);
302
		lcd_char(0x25);
303
	}
304
}
305
 
97 pfowler 306
void millis_tick() {
98 pfowler 307
 
97 pfowler 308
}
309
 
102 pfowler 310
/*
311
 *
312
 * Process the Pin Change Interrupt.
313
 * pcint provides what bank caused the interrupt
314
 *
315
 */
316
void pcInterrupt(uint8_t pcint) {
108 pfowler 317
 
318
        switch (pcint) {
102 pfowler 319
                case 0: pcInt[pcint].current = PINB; break;
109 pfowler 320
                case 1: pcInt[pcint].current = PINC; break;
321
                case 2: pcInt[pcint].current = PIND; break;
108 pfowler 322
        }
323
 
102 pfowler 324
        pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
325
        pcInt[pcint].last = pcInt[pcint].current;
326
 
327
 
110 pfowler 328
		// For Each rotary:
329
        // 		Check which pin caused the interrupt. If they both
330
        //  		equal 0, the pin that interrupted is the direction
331
 
332
		// Rotary 0
333
        if (rbi(pcInt[pcint].current, PCINT10) == 0 
334
                && rbi(pcInt[pcint].current, PCINT11) == 0 
111 pfowler 335
                && rbi(pcInt[pcint].mask, PCINT11) ) {
110 pfowler 336
 
337
					if (rotary[0].timer == 0 && rotary[0].direction == 0) {
338
                        rotary[0].direction = 1;
339
						rotary[0].timer = ROTARY_ON_TIME;
340
					}
341
        } else if (rbi(pcInt[pcint].current, PCINT10) == 0 
342
                && rbi(pcInt[pcint].current, PCINT11) == 0 
111 pfowler 343
                && rbi(pcInt[pcint].mask, PCINT10) ) {
110 pfowler 344
 
345
					if (rotary[0].timer == 0 && rotary[0].direction == 0) {
346
                        rotary[0].direction = 2;
347
						rotary[0].timer = ROTARY_ON_TIME;
348
					}
102 pfowler 349
        }
110 pfowler 350
 
351
		// Rotary 1
106 pfowler 352
        if (rbi(pcInt[pcint].current, PCINT16) == 0 
102 pfowler 353
                && rbi(pcInt[pcint].current, PCINT17) == 0 
354
                && rbi(pcInt[pcint].mask, PCINT16) ) {
105 pfowler 355
 
111 pfowler 356
					if (rotary[1].timer == 0 && rotary[1].direction == 0) {
357
                        rotary[1].direction = 1;
358
						rotary[1].timer = ROTARY_ON_TIME;
105 pfowler 359
					}
102 pfowler 360
        } else if (rbi(pcInt[pcint].current, PCINT16) == 0 
361
                && rbi(pcInt[pcint].current, PCINT17) == 0 
362
                && rbi(pcInt[pcint].mask, PCINT17) ) {
105 pfowler 363
 
111 pfowler 364
					if (rotary[1].timer == 0 && rotary[1].direction == 0) {
365
                        rotary[1].direction = 2;
366
						rotary[1].timer = ROTARY_ON_TIME;
105 pfowler 367
					}
102 pfowler 368
        }
369
 
370
        // Clear the mask so we know we've delth with it
371
        pcInt[pcint].mask = 0;
372
}
373
 
33 pfowler 374
ISR(TIMER0_OVF_vect) {
63 pfowler 375
	tmr0_ovf++;
99 pfowler 376
	if (tmr0_ovf >= sys_ovf_tick) {
63 pfowler 377
		systime++;
378
		tmr0_ovf = 0;
99 pfowler 379
		//millis_tick();	// Not working, taking too long to call?
380
 
110 pfowler 381
		// Decrease our various millisecond timers
382
 
101 pfowler 383
		if (buttons[0].timer)
99 pfowler 384
			buttons[0].timer--;
100 pfowler 385
 
101 pfowler 386
		if (lcdTimer)
387
			lcdTimer--;
110 pfowler 388
 
389
		uint8_t i = 0;
390
		for (i=0; i<=1; i++) {
391
			if (rotary[i].timer)
392
				rotary[i].timer--;
393
		}
101 pfowler 394
 
63 pfowler 395
	}
33 pfowler 396
}
102 pfowler 397
 
398
ISR(PCINT0_vect) {
399
        pcInterrupt(0);
400
}
401
 
402
ISR(PCINT1_vect) {
403
        pcInterrupt(1);
404
}
405
 
406
ISR(PCINT2_vect) {
407
        pcInterrupt(2);
408
}
409
 
410
 
98 pfowler 411
void usbSendHidReport(uchar * data, uchar len) {
412
	while(1)
413
	{
414
		usbPoll();
415
		if (usbInterruptIsReady())
416
		{
417
			usbSetInterrupt(data, len);
418
			break;
419
		}
420
	}
33 pfowler 421
}
422
 
98 pfowler 423
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
424
	usbRequest_t *rq = (void *)data;
33 pfowler 425
 
98 pfowler 426
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
427
		switch (rq->bRequest) {
428
			case USBRQ_HID_GET_REPORT:
100 pfowler 429
				if (rq->wValue.bytes[0] == 1)
430
					return sizeof(reportKeyboard);
431
				else if (rq->wValue.bytes[0] == 2)
432
					return sizeof(reportJoystick);
433
				else
434
					return 0;
98 pfowler 435
			case USBRQ_HID_GET_IDLE:
100 pfowler 436
				usbMsgPtr = &idleRate;
101 pfowler 437
				return 1;
98 pfowler 438
 
439
 
440
 
441
			default:
101 pfowler 442
				return 0;
98 pfowler 443
		}
444
	}
445
	return 0;
62 pfowler 446
}
447
 
98 pfowler 448
void hadUsbReset(void) {
99 pfowler 449
}