Subversion Repositories group.electronics

Rev

Rev 109 | Go to most recent revision | 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
		PB2	- Input, Pullup, PCINT16	- Rotary 2a
112
		PB3	- Input, Pullup, PCINT16	- Rotary 2b
113
	*/
114
	DDRC	= 0B00000000;
115
	PORTC 	= 0B00000011;	
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
 
109 pfowler 328
        //  Dont continue if we already have a button press going out
110 pfowler 329
        //if (rotary.direction != 0 && rotary.timer == 0) {
330
        //        pcInt[pcint].mask = 0;
331
        //        return;
332
        //}
333
 
334
		// For Each rotary:
335
        // 		Check which pin caused the interrupt. If they both
336
        //  		equal 0, the pin that interrupted is the direction
337
 
338
		// Rotary 0
339
        if (rbi(pcInt[pcint].current, PCINT10) == 0 
340
                && rbi(pcInt[pcint].current, PCINT11) == 0 
341
                && rbi(pcInt[pcint].mask, PCINT10) ) {
342
 
343
					if (rotary[0].timer == 0 && rotary[0].direction == 0) {
344
                        rotary[0].direction = 1;
345
						rotary[0].timer = ROTARY_ON_TIME;
346
					}
347
        } else if (rbi(pcInt[pcint].current, PCINT10) == 0 
348
                && rbi(pcInt[pcint].current, PCINT11) == 0 
349
                && rbi(pcInt[pcint].mask, PCINT11) ) {
350
 
351
					if (rotary[0].timer == 0 && rotary[0].direction == 0) {
352
                        rotary[0].direction = 2;
353
						rotary[0].timer = ROTARY_ON_TIME;
354
					}
102 pfowler 355
        }
110 pfowler 356
 
357
		// Rotary 1
106 pfowler 358
        if (rbi(pcInt[pcint].current, PCINT16) == 0 
102 pfowler 359
                && rbi(pcInt[pcint].current, PCINT17) == 0 
360
                && rbi(pcInt[pcint].mask, PCINT16) ) {
105 pfowler 361
 
110 pfowler 362
					if (rotary[0].timer == 0 && rotary[0].direction == 0) {
363
                        rotary[0].direction = 1;
364
						rotary[0].timer = ROTARY_ON_TIME;
105 pfowler 365
					}
102 pfowler 366
        } else if (rbi(pcInt[pcint].current, PCINT16) == 0 
367
                && rbi(pcInt[pcint].current, PCINT17) == 0 
368
                && rbi(pcInt[pcint].mask, PCINT17) ) {
105 pfowler 369
 
110 pfowler 370
					if (rotary[0].timer == 0 && rotary[0].direction == 0) {
371
                        rotary[0].direction = 2;
372
						rotary[0].timer = ROTARY_ON_TIME;
105 pfowler 373
					}
102 pfowler 374
        }
375
 
376
        // Clear the mask so we know we've delth with it
377
        pcInt[pcint].mask = 0;
378
}
379
 
33 pfowler 380
ISR(TIMER0_OVF_vect) {
63 pfowler 381
	tmr0_ovf++;
99 pfowler 382
	if (tmr0_ovf >= sys_ovf_tick) {
63 pfowler 383
		systime++;
384
		tmr0_ovf = 0;
99 pfowler 385
		//millis_tick();	// Not working, taking too long to call?
386
 
110 pfowler 387
		// Decrease our various millisecond timers
388
 
101 pfowler 389
		if (buttons[0].timer)
99 pfowler 390
			buttons[0].timer--;
100 pfowler 391
 
101 pfowler 392
		if (lcdTimer)
393
			lcdTimer--;
110 pfowler 394
 
395
		uint8_t i = 0;
396
		for (i=0; i<=1; i++) {
397
			if (rotary[i].timer)
398
				rotary[i].timer--;
399
		}
101 pfowler 400
 
63 pfowler 401
	}
33 pfowler 402
}
102 pfowler 403
 
404
ISR(PCINT0_vect) {
405
        pcInterrupt(0);
406
}
407
 
408
ISR(PCINT1_vect) {
409
        pcInterrupt(1);
410
}
411
 
412
ISR(PCINT2_vect) {
413
        pcInterrupt(2);
414
}
415
 
416
 
98 pfowler 417
void usbSendHidReport(uchar * data, uchar len) {
418
	while(1)
419
	{
420
		usbPoll();
421
		if (usbInterruptIsReady())
422
		{
423
			usbSetInterrupt(data, len);
424
			break;
425
		}
426
	}
33 pfowler 427
}
428
 
98 pfowler 429
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
430
	usbRequest_t *rq = (void *)data;
33 pfowler 431
 
98 pfowler 432
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
433
		switch (rq->bRequest) {
434
			case USBRQ_HID_GET_REPORT:
100 pfowler 435
				if (rq->wValue.bytes[0] == 1)
436
					return sizeof(reportKeyboard);
437
				else if (rq->wValue.bytes[0] == 2)
438
					return sizeof(reportJoystick);
439
				else
440
					return 0;
98 pfowler 441
			case USBRQ_HID_GET_IDLE:
100 pfowler 442
				usbMsgPtr = &idleRate;
101 pfowler 443
				return 1;
98 pfowler 444
 
445
 
446
 
447
			default:
101 pfowler 448
				return 0;
98 pfowler 449
		}
450
	}
451
	return 0;
62 pfowler 452
}
453
 
98 pfowler 454
void hadUsbReset(void) {
99 pfowler 455
}