Subversion Repositories group.electronics

Rev

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

Rev 96 Rev 97
Line 13... Line 13...
13
#include "util.h"
13
#include "util.h"
14
#include "wire.h"
14
#include "wire.h"
15
#include "config.h"
15
#include "config.h"
16
#include "hiddesc.h"
16
#include "hiddesc.h"
17
 
17
 
-
 
18
 
18
#define ROTS_ATTACHED	1
19
#define ROTS_ATTACHED	1
19
#define STAT		0
20
#define STAT		0
20
#define SENT		1
21
#define SENT		1
21
 
22
 
22
#define SWITCHES	1
23
#define BUTTONS	1
23
#define ON		1
24
#define ON		1
24
#define OFF		0
25
#define OFF		0
25
 
26
 
26
#define DETECTED	0
27
#define DETECTED	0
27
#define TIMER		1
28
#define TIMER		1
Line 37... Line 38...
37
#define MOD_CONTROL_RIGHT   (1<<4)
38
#define MOD_CONTROL_RIGHT   (1<<4)
38
#define MOD_SHIFT_RIGHT     (1<<5)
39
#define MOD_SHIFT_RIGHT     (1<<5)
39
#define MOD_ALT_RIGHT       (1<<6)
40
#define MOD_ALT_RIGHT       (1<<6)
40
#define MOD_GUI_RIGHT       (1<<7)
41
#define MOD_GUI_RIGHT       (1<<7)
41
 
42
 
42
 
43
// CLOCK STUFF
43
void doInt(uint8_t pcint);
44
// Clk/1 TCCR0B = (1<< CS00);
44
uint8_t getKey(void);
45
//20.0Mhz, 1ms = 78ovf
45
 
-
 
-
 
46
//16.5Mhz, 1ms = 64ovf
-
 
47
//12.0Mhz, 1ms = 46ovf
46
uint8_t oldpotVal = 0;
48
const uint8_t sys_ovf_tick = 78;
47
 
-
 
48
volatile uint8_t tmr0_ovf = 0;
49
volatile uint8_t tmr0_ovf = 0;
49
volatile uint8_t tmr2_ovf = 0;
-
 
50
volatile uint32_t systime = 0;
50
volatile uint32_t systime = 0;
51
volatile uint8_t lcdupdate = 1;
51
void millis_tick(void);
52
 
-
 
53
uint8_t emblock[] = {   0B00011111,
-
 
54
                        0B00010001,
-
 
55
                        0B00010001,
-
 
56
                        0B00010001,
-
 
57
                        0B00010001,
-
 
58
                        0B00010001,
-
 
59
                        0B00010001,
-
 
60
                        0B00011111 };
-
 
61
 
52
 
62
 
53
 
63
volatile struct {
54
volatile struct {
64
	uint8_t current;
55
	uint8_t current;
65
	uint8_t last;
56
	uint8_t last;
66
	uint8_t mask;
57
	uint8_t mask;
67
} pcInt[3];
58
} pcInt[3];
68
 
59
 
-
 
60
volatile struct {
69
// switches = [detected][timer][debounced]
61
	uint8_t detected;
70
volatile uint8_t switches[1][3] = { {0,0,0} };
62
	uint8_t timer;
71
 
-
 
72
// rotData = [rot#][(stat|sent)]
63
	uint8_t debounced;
73
//volatile uint8_t rotData[2][2] = { {0,0}, {0,0} };
64
} buttons[BUTTONS];
74
 
65
 
75
volatile struct {
66
volatile struct {
76
	union {
67
	union {
77
		uint8_t data;
68
		uint8_t data;
78
		struct {
69
		struct {
79
			uint8_t stat:4;
70
			uint8_t stat:4;
80
			uint8_t sent:4;
71
			uint8_t sent:4;
81
		};
72
		};
82
	};
73
	};
83
} rotData[2];
74
} rotData[ROTS_ATTACHED];
84
 
-
 
85
// * = 0x25, #=0x20
-
 
86
// F9 = 0x42, F12 = 0x45
-
 
87
 
-
 
88
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
-
 
89
			0x21, 0x22, 0x23,
-
 
90
			0x24, 0x25, 0x26,
-
 
91
			0x42, 0x27, 0x45 };
-
 
92
 
-
 
93
uint8_t keySelect = 1;
-
 
94
 
75
 
95
struct {
76
struct {
96
	uint8_t report_id;
77
	uint8_t report_id;
97
	uint8_t modifier;
78
	uint8_t modifier;
98
	uint8_t keycode;
79
	uint8_t keycode;
99
} reportKeyboard;
80
} reportKeyboard;
100
 
81
 
101
struct{
82
struct {
102
	uint8_t report_id;
83
	uint8_t report_id;
103
  union {
84
	union {
104
    uint8_t data1[2];
85
		uint8_t data1[2];
105
    struct {
86
		struct {
106
	uint8_t rx:8;
87
		uint8_t rx:8;
107
	uint8_t ry:8;
88
		uint8_t ry:8;
108
    };
89
		};
109
  };
90
	};
110
 
91
 
111
  union {
92
	union {
112
    uint16_t data2;
93
		uint16_t data2;
113
    struct {
94
		struct {
114
	uint16_t buttons:12;
95
			uint16_t buttons:12;
115
	uint16_t rot2a:1;
96
			uint16_t rot2a:1;
116
	uint16_t rot2b:1;
97
			uint16_t rot2b:1;
117
	uint16_t rot1a:1;
98
			uint16_t rot1a:1;
118
	uint16_t rot1b:1;
99
			uint16_t rot1b:1;
119
    };
100
		};
120
  };
101
	};
121
} reportJoystick;
102
} reportJoystick;
122
 
103
 
-
 
104
// * = 0x25, #=0x20
-
 
105
// F9 = 0x42, F12 = 0x45
-
 
106
 
-
 
107
uint8_t keyMap[] = { 	0x1E, 0x1F, 0x20,
-
 
108
						0x21, 0x22, 0x23,
-
 
109
						0x24, 0x25, 0x26,
-
 
110
						0x42, 0x27, 0x45 };
-
 
111
 
-
 
112
uint8_t keySelect = 1;
-
 
113
 
-
 
114
void doInt(uint8_t pcint);
-
 
115
uint8_t getKey(void);
-
 
116
 
-
 
117
uint8_t oldpotVal = 0;
-
 
118
volatile uint8_t lcdupdate = 1;
-
 
119
static uchar    idleRate;
-
 
120
 
-
 
121
 
-
 
122
uint8_t emblock[] = {   0B00011111,
-
 
123
                        0B00010001,
-
 
124
                        0B00010001,
-
 
125
                        0B00010001,
-
 
126
                        0B00010001,
-
 
127
                        0B00010001,
-
 
128
                        0B00010001,
-
 
129
                        0B00011111 };
-
 
130
 
123
void usbSendHidReport(uchar * data, uchar len) {
131
void usbSendHidReport(uchar * data, uchar len) {
124
	while(1)
132
	while(1)
125
	{
133
	{
126
		usbPoll();
134
		usbPoll();
127
		if (usbInterruptIsReady())
135
		if (usbInterruptIsReady())
Line 143... Line 151...
143
				else if (rq->wValue.bytes[0] == 2)
151
				else if (rq->wValue.bytes[0] == 2)
144
					return sizeof(reportJoystick);
152
					return sizeof(reportJoystick);
145
				else
153
				else
146
					return 0;
154
					return 0;
147
			case USBRQ_HID_GET_IDLE:
155
			case USBRQ_HID_GET_IDLE:
-
 
156
				usbMsgPtr = &idleRate;
148
				return 1;
157
				return 1;
-
 
158
				
-
 
159
			
-
 
160
				
149
			default:
161
			default:
150
				return 0;
162
				return 0;
151
		}
163
		}
152
	}
164
	}
153
	return 0;
165
	return 0;
Line 156... Line 168...
156
void hadUsbReset(void) {
168
void hadUsbReset(void) {
157
}
169
}
158
 
170
 
159
int main(void) {
171
int main(void) {
160
	analogInit();
172
	analogInit();
-
 
173
	sysclockInit();
161
 
174
 
162
  /*
175
	/*
163
  DDR : 1 = Output, 0 = Input
176
		DDR : 1 = Output, 0 = Input
164
  PORT: 1 = Pullup for Input, otherwise set output
177
		PORT: 1 = Pullup for Input, otherwise set output
165
  PIN : Read input pin
178
		PIN : Read input pin
166
  */
-
 
167
 
-
 
168
  /*
-
 
169
	PB0	- Output 		- Keypad 2
-
 
170
	PB1	- Output 		- Keypad 7
-
 
171
	PB2	- Output 		- Keypad 6
-
 
172
	PB3	- Output 		- Keypad 4
-
 
173
	PB4	- Input, Pullup		- Function select
-
 
174
	PB5	- Input, Pullup		- Function select
-
 
175
  */
-
 
176
  DDRB		= 0B00001111;
-
 
177
  PORTB 	= 0B00111111;
-
 
178
 
-
 
179
  /*
-
 
180
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
-
 
181
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
-
 
182
 
-
 
183
 
-
 
184
	PD4	- Output		- Keypad select status led
-
 
185
	PD5	- Input, Pullup		- Keypad 3
-
 
186
	PD6	- Input, Pullup		- Keypad 1
-
 
187
	PD7	- Input, Pullup		- Keypad 5
-
 
188
  */
179
	*/
189
  DDRD		= 0B00010000;
-
 
190
  PORTD		= 0B11110011;
-
 
191
 
-
 
192
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
-
 
193
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
-
 
194
 
-
 
195
  // Timers not used for the moment
-
 
196
  // Setup timer0 - Enable overflow, 8 times prescaler
-
 
197
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
-
 
198
	TCNT0 = 0x00;				// Set Timer0 to 0
-
 
199
	TCCR0B = (1<< CS00) ;			// /1 prescaler
-
 
200
 
-
 
201
	TIMSK2 = (1<<TOIE2);
-
 
202
	TCNT2 = 0x00;
-
 
203
	TCCR2B = (1<<CS21 | 1<<CS20);
-
 
204
 
180
 
205
	sei();
181
	/*
-
 
182
		PB0	- Output 		- Keypad 2
-
 
183
		PB1	- Output 		- Keypad 7
206
	i2c_master();
184
		PB2	- Output 		- Keypad 6
-
 
185
		PB3	- Output 		- Keypad 4
-
 
186
		PB4	- Input, Pullup		- Function select
-
 
187
		PB5	- Input, Pullup		- Function select
-
 
188
	*/
207
	lcd_init();
189
	DDRB		= 0B00001111;
208
	lcd_createChar(0x00, emblock);
190
	PORTB 	= 0B00111111;
209
 
191
 
-
 
192
	/*
-
 
193
		PD0	- Input, Pullup, PCINT16	- Rotary 1a
210
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
194
		PD1	- Input, Pullup, PCINT17	- Rotary 1b
-
 
195
 
-
 
196
 
-
 
197
		PD4	- Output		- Keypad select status led
-
 
198
		PD5	- Input, Pullup		- Keypad 3
211
        lcd_setCursor(0, 1);
199
		PD6	- Input, Pullup		- Keypad 1
212
        lcd_print(strTime);
200
		PD7	- Input, Pullup		- Keypad 5
-
 
201
	*/
-
 
202
	DDRD		= 0B00010000;
-
 
203
	PORTD		= 0B11110011;
-
 
204
 
-
 
205
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
-
 
206
	PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
-
 
207
 
213
 
208
 
214
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
209
	usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
215
	_delay_ms(500);
210
	_delay_ms(500);
216
	usbDeviceConnect();
211
	usbDeviceConnect();
217
 
212
 
218
	wdt_enable(WDTO_1S);
213
	wdt_enable(WDTO_1S);
219
	usbInit();
214
	usbInit();
-
 
215
	usbPoll();
-
 
216
 
220
	//sei();
217
	sei();
-
 
218
	i2c_master();
-
 
219
	lcd_init();
-
 
220
	lcd_createChar(0x00, emblock);
-
 
221
	usbPoll();
-
 
222
 
-
 
223
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
-
 
224
	lcd_setCursor(0, 1);
-
 
225
	lcd_print(strTime);	
221
 
226
 
222
	reportKeyboard.report_id = 1;
227
	reportKeyboard.report_id = 1;
223
	reportJoystick.report_id = 2;
228
	reportJoystick.report_id = 2;
224
 
229
 
225
	cbi(PORTD, PD4);
230
	cbi(PORTD, PD4);
Line 230... Line 235...
230
	/*
235
	/*
231
	 * This is some really bad deboucing code
236
	 * This is some really bad deboucing code
232
	 */
237
	 */
233
 
238
 
234
	// Detect the button press, wait 100 timer cycles (1.7ms / cycle)
239
	// Detect the button press, wait 100 timer cycles (1.7ms / cycle)
235
	if (rbi(PINB, PB5) == 0 && switches[0][DETECTED]==0) {
240
	if (rbi(PINB, PB5) == 0 && buttons[0].detected==0) {
236
		switches[0][DETECTED] = 1;
241
		buttons[0].detected = 1;
237
		switches[0][TIMER] = 100;
242
		buttons[0].timer = 100;
238
	}
243
	}
239
 
244
 
240
	// After timer is zero, check switch again.
245
	// After timer is zero, check switch again.
241
	//  If switch still pressed, its debounced
246
	//  If switch still pressed, its debounced
242
	//  Otherwise, reset the debounce
247
	//  Otherwise, reset the debounce
243
	if (switches[0][DETECTED] == 1 && switches[0][TIMER] == 0 && switches[0][DEBOUNCED]==0) {
248
	if (buttons[0].detected == 1 && buttons[0].timer == 0 && buttons[0].debounced==0) {
244
		if (rbi(PINB, PB5) == 0)	
249
		if (rbi(PINB, PB5) == 0)	
245
			switches[0][DEBOUNCED] = 1;
250
			buttons[0].debounced = 1;
246
		else {
251
		else {
247
			switches[0][DETECTED] = 0;
252
			buttons[0].detected = 0;
248
			switches[0][TIMER] = 0;
253
			buttons[0].timer = 0;
249
		}
254
		}
250
	}
255
	}
251
 
256
 
252
	// If the switch has come up, do another debounce
257
	// If the switch has come up, do another debounce
253
	if (rbi(PINB, PB5) && switches[0][DETECTED]==2) {
258
	if (rbi(PINB, PB5) && buttons[0].detected==2) {
254
		switches[0][TIMER] = 100;
259
		buttons[0].timer = 100;
255
		switches[0][DETECTED] = 3;
260
		buttons[0].detected = 3;
256
	}
261
	}
257
 
262
 
258
	// After the up switch timer is zero, do the debounce check
263
	// After the up switch timer is zero, do the debounce check
259
	// Otherwise, assume switch is still down
264
	// Otherwise, assume switch is still down
260
	if (switches[0][DETECTED] == 3 && switches[0][TIMER] == 0) {
265
	if (buttons[0].detected == 3 && buttons[0].timer == 0) {
261
		if (rbi(PINB, PB5))
266
		if (rbi(PINB, PB5))
262
			switches[0][DETECTED] = 0;
267
			buttons[0].detected = 0;
263
	}
268
	}
264
 
269
 
265
	// Process the switch
270
	// Process the switch
266
	if (switches[0][DEBOUNCED] == 1) {
271
	if (buttons[0].debounced == 1) {
267
		xbi(keySelect, 0);
272
		xbi(keySelect, 0);
268
 
273
 
269
		if (keySelect == 0)
274
		if (keySelect == 0)
270
			sbi(PORTD, PD4);
275
			sbi(PORTD, PD4);
271
		else
276
		else
272
			cbi(PORTD, PD4);
277
			cbi(PORTD, PD4);
273
 
278
 
274
		switches[0][DETECTED] = 2;
279
		buttons[0].detected = 2;
275
		switches[0][DEBOUNCED] = 0;
280
		buttons[0].debounced = 0;
276
	}
281
	}
277
	
282
	
278
	usbPoll();	
283
	usbPoll();	
279
 
284
 
280
    if(usbInterruptIsReady()){
285
    if(usbInterruptIsReady()){
Line 328... Line 333...
328
 
333
 
329
      /* called after every poll of the interrupt endpoint */
334
      /* called after every poll of the interrupt endpoint */
330
      //usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
335
      //usbSetInterrupt(&reportKeyboard, sizeof(reportKeyboard));
331
      //usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
336
      //usbSetInterrupt(&reportJoystick, sizeof(reportJoystick));
332
 
337
 
333
	usbSendHidReport(&reportKeyboard, sizeof(reportKeyboard));
338
	usbSendHidReport((uchar*)&reportKeyboard, sizeof(reportKeyboard));
334
	usbSendHidReport(&reportJoystick, sizeof(reportJoystick));
339
	usbSendHidReport((uchar*)&reportJoystick, sizeof(reportJoystick));
335
 
340
 
336
    }
341
    }
337
 
342
 
338
	if (lcdupdate) {
343
	if (lcdupdate) {
339
		usbPoll();
344
		usbPoll();
340
		lcdupdate = 0;
345
		lcdupdate = 0;
341
 
346
 
342
                char syschar[10];
347
		char syschar[10];
343
                ultoa(systime, syschar, 10);
348
		ultoa(systime, syschar, 10);
344
                lcd_overprint_right(syschar, 10, 5, 1);
349
		lcd_overprint_right(syschar, 10, 5, 1);
345
 
350
 
346
                uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
351
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
347
                if (potVal != oldpotVal) {
352
		if (potVal != oldpotVal) {
348
                        lcd_percent_graph(potVal, 0, 0);
353
				lcd_percent_graph(potVal, 0, 0);
349
                        oldpotVal = potVal;
354
				oldpotVal = potVal;
350
 
355
 
351
                        char pot[3];
356
				char pot[3];
352
                        utoa(potVal, pot, 10);
357
				utoa(potVal, pot, 10);
353
                        lcd_overprint_right(pot, 3, 11, 0);
358
				lcd_overprint_right(pot, 3, 11, 0);
354
 
359
 
355
                        // Set percentage
360
				// Set percentage
356
                        lcd_setCursor(15, 0);
361
				lcd_setCursor(15, 0);
357
                        lcd_char(0x25);
362
				lcd_char(0x25);
358
                }
363
		}
359
 
364
 
360
	}
365
	}
361
 
366
 
362
  }
367
  }
363
}
368
}
Line 393... Line 398...
393
	switch (pcint) {
398
	switch (pcint) {
394
		case 0:	pcInt[pcint].current = PINB; break;
399
		case 0:	pcInt[pcint].current = PINB; break;
395
		case 1:	pcInt[pcint].current = PIND; break;
400
		case 1:	pcInt[pcint].current = PIND; break;
396
		case 2:	pcInt[pcint].current = PINC; break;
401
		case 2:	pcInt[pcint].current = PINC; break;
397
	}
402
	}
398
	pcInt[pcint].current = PIND;
403
	//pcInt[pcint].current = PIND;
399
	pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
404
	pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
400
	pcInt[pcint].last = pcInt[pcint].current;
405
	pcInt[pcint].last = pcInt[pcint].current;
401
 
406
 
402
	// Select what rotary we are dealing with
407
	// Select what rotary we are dealing with
403
	//   based on the pc interrupt that fired.
408
	//   based on the pc interrupt that fired.
Line 425... Line 430...
425
 
430
 
426
	// Clear the mask so we know we've delth with it
431
	// Clear the mask so we know we've delth with it
427
	pcInt[pcint].mask = 0;
432
	pcInt[pcint].mask = 0;
428
}
433
}
429
 
434
 
430
ISR(TIMER0_OVF_vect) {
435
void millis_tick() {
431
 
-
 
432
	// Clk/1 TCCR0B = (1<< CS00);
436
	if (buttons[0].detected && buttons[0].timer)
433
	//20.0Mhz, 1ms = 78ovf
437
		buttons[0].timer--;
434
	//16.5Mhz, 1ms = 64ovf
438
		
435
	//12.0Mhz, 1ms = 46ovf
439
	lcdupdate = 1;
-
 
440
}
436
 
441
 
-
 
442
ISR(TIMER0_OVF_vect) {
437
	tmr0_ovf++;
443
	tmr0_ovf++;
438
	if (tmr0_ovf>=78) {
444
	if (tmr0_ovf>=sys_ovf_tick) {
439
		systime++;
445
		systime++;
440
		tmr0_ovf = 0;
446
		tmr0_ovf = 0;
-
 
447
		millis_tick();
441
	}
448
	}
442
 
-
 
443
	if (switches[0][DETECTED] && switches[0][TIMER])
-
 
444
		switches[0][TIMER]--;
-
 
445
}
449
}
446
 
450
/*
447
ISR(TIMER2_OVF_vect) {
451
ISR(TIMER2_OVF_vect) {
448
        tmr2_ovf++;
452
	tmr2_ovf++;
449
        if (tmr2_ovf>=78) {
453
	if (tmr2_ovf>=78) {}
450
                lcdupdate=1;
-
 
451
                tmr2_ovf = 0;
-
 
452
        }
-
 
453
}
454
}
454
 
455
*/
455
 
456
 
456
ISR(PCINT0_vect) {
457
ISR(PCINT0_vect) {
457
	pcInterrupt(0);
458
	pcInterrupt(0);
458
}
459
}
459
 
460