Subversion Repositories group.electronics

Rev

Rev 98 | Rev 100 | 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>
98 pfowler 15
#include "usbdrv.h"
33 pfowler 16
 
98 pfowler 17
#include "atcpad.h"
18
#include "avrutil.h"
63 pfowler 19
#include "lcd.h"
40 pfowler 20
#include "wire.h"
33 pfowler 21
#include "hiddesc.h"
22
 
99 pfowler 23
#define BUTTONS	 	1
39 pfowler 24
#define ON		1
25
#define OFF		0
26
 
97 pfowler 27
// * = 0x25, #=0x20
28
// F9 = 0x42, F12 = 0x45
29
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
30
						0x21, 0x22, 0x23,
31
						0x24, 0x25, 0x26,
32
						0x42, 0x27, 0x45 };
33
uint8_t keySelect = 1;
98 pfowler 34
uint8_t lcdRectangle[] = {   
35
	0B00011111,
36
	0B00010001,
37
	0B00010001,
38
	0B00010001,
39
	0B00010001,
40
	0B00010001,
41
	0B00010001,
42
	0B00011111 };
97 pfowler 43
 
44
uint8_t getKey(void);
45
 
98 pfowler 46
volatile struct {
47
	uint8_t detected;
48
	uint8_t timer;
99 pfowler 49
	uint8_t waitup;
98 pfowler 50
} buttons[BUTTONS];
97 pfowler 51
 
98 pfowler 52
int main(void)
53
{
54
 
55
	setup();
56
 
57
    while(1)
58
    {
99 pfowler 59
	wdt_reset();
98 pfowler 60
        loop(); 
61
    }
39 pfowler 62
}
63
 
98 pfowler 64
void setup() {
97 pfowler 65
	/*
66
		DDR : 1 = Output, 0 = Input
67
		PORT: 1 = Pullup for Input, otherwise set output
68
		PIN : Read input pin
69
	*/
33 pfowler 70
 
97 pfowler 71
	/*
72
		PB0	- Output 		- Keypad 2
73
		PB1	- Output 		- Keypad 7
74
		PB2	- Output 		- Keypad 6
75
		PB3	- Output 		- Keypad 4
76
		PB4	- Input, Pullup		- Function select
77
		PB5	- Input, Pullup		- Function select
78
	*/
79
	DDRB		= 0B00001111;
80
	PORTB 	= 0B00111111;
33 pfowler 81
 
97 pfowler 82
	/*
83
		PD0	- Input, Pullup, PCINT16	- Rotary 1a
84
		PD1	- Input, Pullup, PCINT17	- Rotary 1b
33 pfowler 85
 
86
 
97 pfowler 87
		PD4	- Output		- Keypad select status led
88
		PD5	- Input, Pullup		- Keypad 3
89
		PD6	- Input, Pullup		- Keypad 1
90
		PD7	- Input, Pullup		- Keypad 5
91
	*/
92
	DDRD		= 0B00010000;
93
	PORTD		= 0B11110011;
33 pfowler 94
 
97 pfowler 95
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
98 pfowler 96
	PCICR |= ( 1 << PCIE2 ); //enable pin change interrupts
97
 
98
	analogInit();
99
	sysclockInit();
100
 
101
	reportKeyboard.report_id = 1;
102
	reportJoystick.report_id = 2;	
103
 
97 pfowler 104
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
105
	_delay_ms(500);
106
	usbDeviceConnect();
33 pfowler 107
 
99 pfowler 108
	sei();
97 pfowler 109
	wdt_enable(WDTO_1S);
110
	usbInit();
63 pfowler 111
 
40 pfowler 112
	i2c_master();
63 pfowler 113
	lcd_init();
98 pfowler 114
	lcd_createChar(0x00, lcdRectangle);
40 pfowler 115
 
63 pfowler 116
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
97 pfowler 117
	lcd_setCursor(0, 1);
98 pfowler 118
	lcd_print(strTime);
99 pfowler 119
 
120
	buttons[0].detected = 0;
121
	buttons[0].timer = 0;
122
	buttons[0].waitup = 0;
123
 
124
	cbi(PORTD, 4);
98 pfowler 125
 
126
}
33 pfowler 127
 
98 pfowler 128
void loop() {
99 pfowler 129
 
130
   	usbPoll(); 
98 pfowler 131
	if(usbInterruptIsReady()){
132
	    reportJoystick.data1[0] = (-128 + analogRead(0));
133
	    reportJoystick.data1[1] = (-128 + analogRead(1));
134
	    reportJoystick.data2 = 0x0000;				// Clear all the buttons
33 pfowler 135
 
98 pfowler 136
	    reportKeyboard.modifier = 0x00;
137
	    reportKeyboard.keycode = 0x00;
39 pfowler 138
 
98 pfowler 139
	    uint8_t key = getKey();
140
	    if (rbi(keySelect, 0)) {
141
		    // Keypad is joystick
142
		    if (key > 0)
143
				reportJoystick.data2 |= (1 << (--key));
144
		    } else {
145
		    // Keypad is keyboard
146
		    if (key > 0) {
147
			    //if (key==10 || key==12) // Left shift, for *, #
148
			    //	reportKeyboard.modifier |= (1<<1);
149
			    reportKeyboard.keycode = keyMap[--key];
150
		    }
151
	    }
152
	    usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
153
	    usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
99 pfowler 154
    	}	
33 pfowler 155
}
156
 
157
uint8_t getKey() {
158
	uint8_t col, row = 0;
159
	uint8_t key = 0;
160
	uint8_t n = 1;
161
 
162
	for (row=0; row<=3; row++) {
163
		cbi(PORTB, row);
164
		_delay_us(10);				// Wait for the port to change
165
 
166
		for (col=5; col<=7; col++) {
167
			if (rbi(PIND, col) == 0)
98 pfowler 168
			key = n;
33 pfowler 169
			n++;
98 pfowler 170
		}
33 pfowler 171
 
172
		sbi(PORTB, row);
173
	}
174
	return key;
175
}
176
 
97 pfowler 177
void millis_tick() {
178
	if (buttons[0].detected && buttons[0].timer)
179
		buttons[0].timer--;
98 pfowler 180
 
97 pfowler 181
}
182
 
33 pfowler 183
ISR(TIMER0_OVF_vect) {
63 pfowler 184
	tmr0_ovf++;
99 pfowler 185
	if (tmr0_ovf >= sys_ovf_tick) {
63 pfowler 186
		systime++;
187
		tmr0_ovf = 0;
99 pfowler 188
		//millis_tick();	// Not working, taking too long to call?
189
 
190
		if (buttons[0].detected && buttons[0].timer)
191
			buttons[0].timer--;
63 pfowler 192
	}
33 pfowler 193
}
98 pfowler 194
void usbSendHidReport(uchar * data, uchar len) {
195
	while(1)
196
	{
197
		usbPoll();
198
		if (usbInterruptIsReady())
199
		{
200
			usbSetInterrupt(data, len);
201
			break;
202
		}
203
	}
33 pfowler 204
}
205
 
98 pfowler 206
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
207
	usbRequest_t *rq = (void *)data;
33 pfowler 208
 
98 pfowler 209
	if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
210
		switch (rq->bRequest) {
211
			case USBRQ_HID_GET_REPORT:
212
			if (rq->wValue.bytes[0] == 1)
213
			return sizeof(reportKeyboard);
214
			else if (rq->wValue.bytes[0] == 2)
215
			return sizeof(reportJoystick);
216
			else
217
			return 0;
218
			case USBRQ_HID_GET_IDLE:
219
			usbMsgPtr = &idleRate;
220
			return 1;
221
 
222
 
223
 
224
			default:
225
			return 0;
226
		}
227
	}
228
	return 0;
62 pfowler 229
}
230
 
98 pfowler 231
void hadUsbReset(void) {
99 pfowler 232
}