Subversion Repositories group.electronics

Rev

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