Subversion Repositories group.electronics

Rev

Rev 101 | Rev 103 | 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
26
#define ROTARYS			1
39 pfowler 27
 
101 pfowler 28
#define LCD_UPDATE		11
29
#define	ROTARY_UPDATE	5
100 pfowler 30
 
102 pfowler 31
void pcInterrupt(uint8_t);
100 pfowler 32
uint8_t getKey(void);
33
void updateLcd();
101 pfowler 34
void checkRotarys();
35
void checkButtons(void);
100 pfowler 36
 
101 pfowler 37
 
97 pfowler 38
// * = 0x25, #=0x20
39
// F9 = 0x42, F12 = 0x45
40
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
41
						0x21, 0x22, 0x23,
42
						0x24, 0x25, 0x26,
43
						0x42, 0x27, 0x45 };
100 pfowler 44
 
98 pfowler 45
uint8_t lcdRectangle[] = {   
46
	0B00011111,
47
	0B00010001,
48
	0B00010001,
49
	0B00010001,
50
	0B00010001,
51
	0B00010001,
52
	0B00010001,
53
	0B00011111 };
97 pfowler 54
 
55
 
100 pfowler 56
uint8_t oldpotVal = 0;
57
uint8_t keySelect = 1;
58
 
101 pfowler 59
// Start these of at different times, so they
60
//  don't kick of it the same loop run
61
volatile uint8_t lcdTimer = 8;
62
volatile uint8_t rotaryTimer = 5;
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 {
77
 
78
	union {
79
		uint8_t data;
80
		struct {
81
			uint8_t stat:4;
82
			uint8_t sent:4;
83
		};
84
	};
85
} rotary[ROTARYS];
86
 
98 pfowler 87
int main(void)
88
{
89
 
90
	setup();
91
 
92
    while(1)
93
    {
100 pfowler 94
		wdt_reset();
98 pfowler 95
        loop(); 
96
    }
39 pfowler 97
}
98
 
98 pfowler 99
void setup() {
97 pfowler 100
	/*
101
		DDR : 1 = Output, 0 = Input
102
		PORT: 1 = Pullup for Input, otherwise set output
103
		PIN : Read input pin
104
	*/
33 pfowler 105
 
97 pfowler 106
	/*
107
		PB0	- Output 		- Keypad 2
108
		PB1	- Output 		- Keypad 7
109
		PB2	- Output 		- Keypad 6
110
		PB3	- Output 		- Keypad 4
111
		PB4	- Input, Pullup		- Function select
112
		PB5	- Input, Pullup		- Function select
113
	*/
101 pfowler 114
	DDRB	= 0B00001111;
97 pfowler 115
	PORTB 	= 0B00111111;
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;
33 pfowler 129
 
97 pfowler 130
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
98 pfowler 131
	PCICR |= ( 1 << PCIE2 ); //enable pin change interrupts
132
 
133
	analogInit();
134
	sysclockInit();
135
 
136
	reportKeyboard.report_id = 1;
137
	reportJoystick.report_id = 2;	
138
 
97 pfowler 139
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
140
	_delay_ms(500);
141
	usbDeviceConnect();
33 pfowler 142
 
99 pfowler 143
	sei();
97 pfowler 144
	wdt_enable(WDTO_1S);
145
	usbInit();
63 pfowler 146
 
40 pfowler 147
	i2c_master();
63 pfowler 148
	lcd_init();
98 pfowler 149
	lcd_createChar(0x00, lcdRectangle);
40 pfowler 150
 
63 pfowler 151
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
97 pfowler 152
	lcd_setCursor(0, 1);
98 pfowler 153
	lcd_print(strTime);
99 pfowler 154
 
155
	buttons[0].detected = 0;
156
	buttons[0].timer = 0;
157
	buttons[0].waitup = 0;
158
 
159
	cbi(PORTD, 4);
98 pfowler 160
 
161
}
33 pfowler 162
 
98 pfowler 163
void loop() {
99 pfowler 164
 
100 pfowler 165
   	usbPoll();
166
 
101 pfowler 167
	if (lcdTimer==0) {
100 pfowler 168
		updateLcd();
101 pfowler 169
		lcdTimer = LCD_UPDATE;
100 pfowler 170
	}
101 pfowler 171
 
172
 
173
	if (rotaryTimer==0) {
174
		checkRotarys();
175
		rotaryTimer = ROTARY_UPDATE;
176
	}
177
 
102 pfowler 178
 
101 pfowler 179
	checkButtons();
100 pfowler 180
 
181
 
98 pfowler 182
	if(usbInterruptIsReady()){
183
	    usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
184
	    usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
100 pfowler 185
    }	
33 pfowler 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)
98 pfowler 199
			key = n;
33 pfowler 200
			n++;
98 pfowler 201
		}
33 pfowler 202
 
203
		sbi(PORTB, row);
204
	}
205
	return key;
206
}
207
 
101 pfowler 208
void checkRotarys() {
209
	uint8_t rot = 0;
210
	for (rot=0; rot<=(ROTARYS - 1); rot++) {
211
		if (rotary[rot].stat == 0x01 && rotary[rot].sent == 0) {
212
			rotary[rot].sent = 1;
213
			switch (rot) {
214
				case(0):        reportJoystick.rot1a = 1; break;
215
				case(1):        reportJoystick.rot2a = 1; break;
216
			}
217
		} else if (rotary[rot].stat == 0x02 && rotary[rot].sent == 0) {
218
			rotary[rot].sent = 1;
219
			switch (rot) {
220
				case(0):      reportJoystick.rot1b = 1; break;
221
				case(1):      reportJoystick.rot2b = 1; break;
222
			}
223
		} else {
224
			rotary[rot].sent = 0;
225
		}
226
		rotary[rot].stat = 0;
227
 
102 pfowler 228
		// tha fuck is this?
229
		//if (rbi(PINB, PB4))
230
		//	rotary[rot].sent = 0;
101 pfowler 231
	}
232
}
233
 
234
void checkButtons() {
235
 
236
	reportJoystick.data1[0] = (-128 + analogRead(0));
237
	reportJoystick.data1[1] = (-128 + analogRead(1));
238
	reportJoystick.data2 = 0x0000;				// Clear all the buttons
239
 
240
	reportKeyboard.modifier = 0x00;
241
	reportKeyboard.keycode = 0x00;
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) {
317
 
318
        switch (pcint) {
319
                case 0: pcInt[pcint].current = PINB; break;
320
                case 1: pcInt[pcint].current = PIND; break;
321
                case 2: pcInt[pcint].current = PINC; break;
322
        }
323
        //pcInt[pcint].current = PIND;
324
        pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
325
        pcInt[pcint].last = pcInt[pcint].current;
326
 
327
        // Select what rotary we are dealing with
328
        //   based on the pc interrupt that fired.
329
        uint8_t rot = 0;
330
        if (pcint == 1) 
331
                rot = 1;
332
 
333
        // If rot stat is not 0, we havn't sent
334
        //  our last results yet. Skip this click.
335
        if (rotary[rot].stat != 0) {
336
                pcInt[pcint].mask = 0;
337
                return;
338
        }
339
        // Check which pin caused the interrupt. If they both
340
        //  equal 0, the pin that interrupted is the direction
341
        if (rbi(pcInt[pcint].current, PCINT17) == 0 
342
                && rbi(pcInt[pcint].current, PCINT17) == 0 
343
                && rbi(pcInt[pcint].mask, PCINT16) ) {
344
                        rotary[rot].stat = 1;
345
        } else if (rbi(pcInt[pcint].current, PCINT16) == 0 
346
                && rbi(pcInt[pcint].current, PCINT17) == 0 
347
                && rbi(pcInt[pcint].mask, PCINT17) ) {
348
                        rotary[rot].stat = 2;
349
        }
350
 
351
        // Clear the mask so we know we've delth with it
352
        pcInt[pcint].mask = 0;
353
}
354
 
33 pfowler 355
ISR(TIMER0_OVF_vect) {
63 pfowler 356
	tmr0_ovf++;
99 pfowler 357
	if (tmr0_ovf >= sys_ovf_tick) {
63 pfowler 358
		systime++;
359
		tmr0_ovf = 0;
99 pfowler 360
		//millis_tick();	// Not working, taking too long to call?
361
 
101 pfowler 362
		if (buttons[0].timer)
99 pfowler 363
			buttons[0].timer--;
100 pfowler 364
 
101 pfowler 365
		if (lcdTimer)
366
			lcdTimer--;
367
 
368
		if (rotaryTimer)
369
			rotaryTimer--;
370
 
63 pfowler 371
	}
33 pfowler 372
}
102 pfowler 373
 
374
ISR(PCINT0_vect) {
375
        pcInterrupt(0);
376
}
377
 
378
ISR(PCINT1_vect) {
379
        pcInterrupt(1);
380
}
381
 
382
ISR(PCINT2_vect) {
383
        pcInterrupt(2);
384
}
385
 
386
 
98 pfowler 387
void usbSendHidReport(uchar * data, uchar len) {
388
	while(1)
389
	{
390
		usbPoll();
391
		if (usbInterruptIsReady())
392
		{
393
			usbSetInterrupt(data, len);
394
			break;
395
		}
396
	}
33 pfowler 397
}
398
 
98 pfowler 399
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
400
	usbRequest_t *rq = (void *)data;
33 pfowler 401
 
98 pfowler 402
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
403
		switch (rq->bRequest) {
404
			case USBRQ_HID_GET_REPORT:
100 pfowler 405
				if (rq->wValue.bytes[0] == 1)
406
					return sizeof(reportKeyboard);
407
				else if (rq->wValue.bytes[0] == 2)
408
					return sizeof(reportJoystick);
409
				else
410
					return 0;
98 pfowler 411
			case USBRQ_HID_GET_IDLE:
100 pfowler 412
				usbMsgPtr = &idleRate;
101 pfowler 413
				return 1;
98 pfowler 414
 
415
 
416
 
417
			default:
101 pfowler 418
				return 0;
98 pfowler 419
		}
420
	}
421
	return 0;
62 pfowler 422
}
423
 
98 pfowler 424
void hadUsbReset(void) {
99 pfowler 425
}