Subversion Repositories group.electronics

Rev

Rev 100 | Rev 102 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 100 Rev 101
Line 20... Line 20...
20
#include "avrutil.h"
20
#include "avrutil.h"
21
#include "lcd.h"
21
#include "lcd.h"
22
#include "wire.h"
22
#include "wire.h"
23
#include "hiddesc.h"
23
#include "hiddesc.h"
24
 
24
 
25
#define BUTTONS	 	1
25
#define BUTTONS	 		1
26
#define ON		1
26
#define ROTARYS			1
-
 
27
 
27
#define OFF		0
28
#define LCD_UPDATE		11
-
 
29
#define	ROTARY_UPDATE	5
28
 
30
 
29
 
31
 
30
uint8_t getKey(void);
32
uint8_t getKey(void);
31
void updateLcd();
33
void updateLcd();
-
 
34
void checkRotarys();
-
 
35
void checkButtons(void);
-
 
36
 
32
 
37
 
33
// * = 0x25, #=0x20
38
// * = 0x25, #=0x20
34
// F9 = 0x42, F12 = 0x45
39
// F9 = 0x42, F12 = 0x45
35
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
40
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
36
						0x21, 0x22, 0x23,
41
						0x21, 0x22, 0x23,
Line 47... Line 52...
47
	0B00010001,
52
	0B00010001,
48
	0B00011111 };
53
	0B00011111 };
49
 
54
 
50
 
55
 
51
uint8_t oldpotVal = 0;
56
uint8_t oldpotVal = 0;
52
volatile uint8_t lcdtimer = 0;
-
 
53
uint8_t keySelect = 1;
57
uint8_t keySelect = 1;
54
 
58
 
-
 
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
 
55
volatile struct {
64
volatile struct {
56
	uint8_t detected;
65
	uint8_t detected;
57
	uint8_t timer;
66
	uint8_t timer;
58
	uint8_t waitup;
67
	uint8_t waitup;
59
} buttons[BUTTONS];
68
} buttons[BUTTONS];
60
 
69
 
-
 
70
volatile struct {
-
 
71
	
-
 
72
	union {
-
 
73
		uint8_t data;
-
 
74
		struct {
-
 
75
			uint8_t stat:4;
-
 
76
			uint8_t sent:4;
-
 
77
		};
-
 
78
	};
-
 
79
} rotary[ROTARYS];
-
 
80
 
61
int main(void)
81
int main(void)
62
{
82
{
63
	
83
	
64
	setup();
84
	setup();
65
	
85
	
Line 83... Line 103...
83
		PB2	- Output 		- Keypad 6
103
		PB2	- Output 		- Keypad 6
84
		PB3	- Output 		- Keypad 4
104
		PB3	- Output 		- Keypad 4
85
		PB4	- Input, Pullup		- Function select
105
		PB4	- Input, Pullup		- Function select
86
		PB5	- Input, Pullup		- Function select
106
		PB5	- Input, Pullup		- Function select
87
	*/
107
	*/
88
	DDRB		= 0B00001111;
108
	DDRB	= 0B00001111;
89
	PORTB 	= 0B00111111;
109
	PORTB 	= 0B00111111;
90
 
110
 
91
	/*
111
	/*
92
		PD0	- Input, Pullup, PCINT16	- Rotary 1a
112
		PD0	- Input, Pullup, PCINT16	- Rotary 1a
93
		PD1	- Input, Pullup, PCINT17	- Rotary 1b
113
		PD1	- Input, Pullup, PCINT17	- Rotary 1b
Line 96... Line 116...
96
		PD4	- Output		- Keypad select status led
116
		PD4	- Output		- Keypad select status led
97
		PD5	- Input, Pullup		- Keypad 3
117
		PD5	- Input, Pullup		- Keypad 3
98
		PD6	- Input, Pullup		- Keypad 1
118
		PD6	- Input, Pullup		- Keypad 1
99
		PD7	- Input, Pullup		- Keypad 5
119
		PD7	- Input, Pullup		- Keypad 5
100
	*/
120
	*/
101
	DDRD		= 0B00010000;
121
	DDRD	= 0B00010000;
102
	PORTD		= 0B11110011;
122
	PORTD	= 0B11110011;
103
 
123
 
104
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
124
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
105
	PCICR |= ( 1 << PCIE2 ); //enable pin change interrupts
125
	PCICR |= ( 1 << PCIE2 ); //enable pin change interrupts
106
	
126
	
107
	analogInit();
127
	analogInit();
Line 136... Line 156...
136
 
156
 
137
void loop() {
157
void loop() {
138
 
158
 
139
   	usbPoll();
159
   	usbPoll();
140
 
160
 
141
	if (lcdtimer==0) {
161
	if (lcdTimer==0) {
142
		lcdtimer = 100;
-
 
143
		updateLcd();
162
		updateLcd();
-
 
163
		lcdTimer = LCD_UPDATE;
-
 
164
	}
-
 
165
	
-
 
166
	
-
 
167
	if (rotaryTimer==0) {
-
 
168
		checkRotarys();
-
 
169
		rotaryTimer = ROTARY_UPDATE;
144
	}
170
	}
-
 
171
	
-
 
172
	checkButtons();
145
 
173
 
146
 
174
 
147
	if(usbInterruptIsReady()){
175
	if(usbInterruptIsReady()){
148
	    reportJoystick.data1[0] = (-128 + analogRead(0));
-
 
149
	    reportJoystick.data1[1] = (-128 + analogRead(1));
-
 
150
	    reportJoystick.data2 = 0x0000;				// Clear all the buttons
-
 
151
 
-
 
152
	    reportKeyboard.modifier = 0x00;
-
 
153
	    reportKeyboard.keycode = 0x00;
-
 
154
 
-
 
155
	    uint8_t key = getKey();
-
 
156
	    if (rbi(keySelect, 0)) {
-
 
157
		    // Keypad is joystick
-
 
158
		    if (key > 0)
-
 
159
				reportJoystick.data2 |= (1 << (--key));
-
 
160
		    } else {
-
 
161
		    // Keypad is keyboard
-
 
162
		    if (key > 0) {
-
 
163
			    //if (key==10 || key==12) // Left shift, for *, #
-
 
164
			    //	reportKeyboard.modifier |= (1<<1);
-
 
165
			    reportKeyboard.keycode = keyMap[--key];
-
 
166
		    }
-
 
167
	    }
-
 
168
	    usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
176
	    usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
169
	    usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
177
	    usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
170
    }	
178
    }	
171
}
179
}
172
 
180
 
Line 188... Line 196...
188
		sbi(PORTB, row);
196
		sbi(PORTB, row);
189
	}
197
	}
190
	return key;
198
	return key;
191
}
199
}
192
 
200
 
-
 
201
void checkRotarys() {
-
 
202
	uint8_t rot = 0;
-
 
203
	for (rot=0; rot<=(ROTARYS - 1); rot++) {
-
 
204
		if (rotary[rot].stat == 0x01 && rotary[rot].sent == 0) {
-
 
205
			rotary[rot].sent = 1;
-
 
206
			switch (rot) {
-
 
207
				case(0):        reportJoystick.rot1a = 1; break;
-
 
208
				case(1):        reportJoystick.rot2a = 1; break;
-
 
209
			}
-
 
210
		} else if (rotary[rot].stat == 0x02 && rotary[rot].sent == 0) {
-
 
211
			rotary[rot].sent = 1;
-
 
212
			switch (rot) {
-
 
213
				case(0):      reportJoystick.rot1b = 1; break;
-
 
214
				case(1):      reportJoystick.rot2b = 1; break;
-
 
215
			}
-
 
216
		} else {
-
 
217
			rotary[rot].sent = 0;
-
 
218
		}
-
 
219
		rotary[rot].stat = 0;
-
 
220
 
-
 
221
		if (rbi(PINB, PB4))
-
 
222
			rotary[rot].sent = 0;
-
 
223
	}
-
 
224
}
-
 
225
 
-
 
226
void checkButtons() {
-
 
227
 
-
 
228
	reportJoystick.data1[0] = (-128 + analogRead(0));
-
 
229
	reportJoystick.data1[1] = (-128 + analogRead(1));
-
 
230
	reportJoystick.data2 = 0x0000;				// Clear all the buttons
-
 
231
 
-
 
232
	reportKeyboard.modifier = 0x00;
-
 
233
	reportKeyboard.keycode = 0x00;
-
 
234
 
-
 
235
	uint8_t key = getKey();
-
 
236
	if (rbi(keySelect, 0)) {
-
 
237
		// Keypad is joystick
-
 
238
		if (key > 0)
-
 
239
			reportJoystick.data2 |= (1 << (--key));
-
 
240
		} else {
-
 
241
		// Keypad is keyboard
-
 
242
		if (key > 0) {
-
 
243
			//if (key==10 || key==12) // Left shift, for *, #
-
 
244
			//	reportKeyboard.modifier |= (1<<1);
-
 
245
			reportKeyboard.keycode = keyMap[--key];
-
 
246
		}
-
 
247
	}
-
 
248
 
-
 
249
	// Only one button for now
-
 
250
	if (rbi(PINB, 5) == 0 && buttons[0].detected == 0 && buttons[0].timer == 0) {
-
 
251
		buttons[0].detected = 1;
-
 
252
		buttons[0].timer = 5;
-
 
253
		
-
 
254
	}
-
 
255
	
-
 
256
	if (rbi(PINB, 5) == 1 && buttons[0].waitup == 1) {
-
 
257
		buttons[0].detected = 0;
-
 
258
		buttons[0].timer = 0;
-
 
259
		buttons[0].waitup = 0;
-
 
260
	}
-
 
261
	
-
 
262
	if (buttons[0].detected == 1 && buttons[0].timer == 0 && buttons[0].waitup == 0 ) {
-
 
263
		
-
 
264
		xbi(keySelect, 0);
-
 
265
 
-
 
266
		if (keySelect == 0)
-
 
267
			sbi(PORTD, PD4);
-
 
268
		else
-
 
269
			cbi(PORTD, PD4);	
-
 
270
		
-
 
271
		buttons[0].waitup = 1;
-
 
272
	}
-
 
273
}
-
 
274
 
193
void updateLcd() {
275
void updateLcd() {
194
	usbPoll();
276
	usbPoll();
195
	
277
	
196
 
278
 
197
	char syschar[10];
279
	char syschar[10];
Line 209... Line 291...
209
 
291
 
210
		// Set percentage
292
		// Set percentage
211
		lcd_setCursor(15, 0);
293
		lcd_setCursor(15, 0);
212
		lcd_char(0x25);
294
		lcd_char(0x25);
213
	}
295
	}
214
	
-
 
215
	lcdtimer = 100;
-
 
216
}
296
}
217
 
297
 
218
void millis_tick() {
298
void millis_tick() {
219
	if (buttons[0].detected && buttons[0].timer)
-
 
220
		buttons[0].timer--;
-
 
221
	
299
	
222
}
300
}
223
 
301
 
224
ISR(TIMER0_OVF_vect) {
302
ISR(TIMER0_OVF_vect) {
225
	tmr0_ovf++;
303
	tmr0_ovf++;
226
	if (tmr0_ovf >= sys_ovf_tick) {
304
	if (tmr0_ovf >= sys_ovf_tick) {
227
		systime++;
305
		systime++;
228
		tmr0_ovf = 0;
306
		tmr0_ovf = 0;
229
		//millis_tick();	// Not working, taking too long to call?
307
		//millis_tick();	// Not working, taking too long to call?
230
 
308
 
231
		if (buttons[0].detected && buttons[0].timer)
309
		if (buttons[0].timer)
232
			buttons[0].timer--;
310
			buttons[0].timer--;
233
			
311
			
234
		if (lcdtimer)
312
		if (lcdTimer)
235
			lcdtimer--;
313
			lcdTimer--;
-
 
314
			
-
 
315
		if (rotaryTimer)
-
 
316
			rotaryTimer--;
-
 
317
 
236
	}
318
	}
237
}
319
}
238
void usbSendHidReport(uchar * data, uchar len) {
320
void usbSendHidReport(uchar * data, uchar len) {
239
	while(1)
321
	while(1)
240
	{
322
	{
Line 259... Line 341...
259
					return sizeof(reportJoystick);
341
					return sizeof(reportJoystick);
260
				else
342
				else
261
					return 0;
343
					return 0;
262
			case USBRQ_HID_GET_IDLE:
344
			case USBRQ_HID_GET_IDLE:
263
				usbMsgPtr = &idleRate;
345
				usbMsgPtr = &idleRate;
264
			return 1;
346
				return 1;
265
			
347
			
266
			
348
			
267
			
349
			
268
			default:
350
			default:
269
			return 0;
351
				return 0;
270
		}
352
		}
271
	}
353
	}
272
	return 0;
354
	return 0;
273
}
355
}
274
 
356