Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

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