Subversion Repositories group.electronics

Rev

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

Rev 81 Rev 84
Line 23... Line 23...
23
#define NULL    ((void *)0)
23
#define NULL    ((void *)0)
24
#endif
24
#endif
25
 
25
 
26
/* ------------------------------------------------------------------------- */
26
/* ------------------------------------------------------------------------- */
27
 
27
 
-
 
28
uint8_t getKey(void);
-
 
29
void doButtons(void);
-
 
30
 
28
struct {
31
struct {
29
	union {
32
	union {
30
		int16_t axis[1];
33
		int16_t axis[2];
31
		struct {
34
		struct {
32
			int16_t axis0:16;
35
			int16_t axis0:16;
33
			//int16_t axis1:16;
36
			int16_t axis1:16;
34
		};
37
		};
35
		uint8_t buttons;
38
		uint8_t buttons;
36
		struct {
39
		struct {
37
			int8_t b1:1;
40
			int8_t b1:1;
38
			int8_t b2:2;
41
			int8_t b2:2;
Line 46... Line 49...
46
	};
49
	};
47
} reportBuffer;
50
} reportBuffer;
48
 
51
 
49
 
52
 
50
volatile struct {
53
volatile struct {
51
        uint8_t current;
54
	uint8_t buttons;
52
        uint8_t last;
-
 
53
        uint8_t mask;
-
 
54
} pcInt[1];
-
 
55
 
-
 
56
volatile struct {
-
 
57
	uint8_t button;
55
	uint8_t waitup;
58
	uint8_t timer;
56
	uint8_t timer;
59
} buttons;
57
} debounce;
60
 
-
 
61
 
58
 
-
 
59
uint8_t currleds = 128;
62
static uchar    idleRate;           /* in 4 ms units */
60
static uchar    idleRate;           /* in 4 ms units */
63
volatile uint8_t tmr0_ovf = 0;
61
volatile uint8_t tmr0_ovf = 0;
64
volatile uint32_t systime = 0;
62
volatile uint32_t systime = 0;
65
 
63
 
66
/* ------------------------------------------------------------------------- */
64
/* ------------------------------------------------------------------------- */
Line 79... Line 77...
79
    0x95, 0x02,                    //     REPORT_COUNT (2)
77
    0x95, 0x02,                    //     REPORT_COUNT (2)
80
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
78
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
81
 
79
 
82
    0x05, 0x09,                    //     USAGE_PAGE (Button)
80
    0x05, 0x09,                    //     USAGE_PAGE (Button)
83
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
81
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
84
    0x29, 0x08,                    //     USAGE_MAXIMUM (Button 2)
82
    0x29, 0x08,                    //     USAGE_MAXIMUM (Button 8)
85
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
83
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
86
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
84
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
87
    0x75, 0x01,                    //     REPORT_SIZE (1)
85
    0x75, 0x01,                    //     REPORT_SIZE (1)
88
    0x95, 0x08,                    //     REPORT_COUNT (2)
86
    0x95, 0x08,                    //     REPORT_COUNT (2)
89
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
87
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
Line 171... Line 169...
171
  PORT: 1 = Pullup for Input, otherwise set output
169
  PORT: 1 = Pullup for Input, otherwise set output
172
  PIN : Read input pin
170
  PIN : Read input pin
173
  */
171
  */
174
  /*
172
  /*
175
 
173
 
176
        PA0     - Output        - ButtonPad 1
174
        PA0     - Input, Pullup - ButtonPad 1
177
        PA1     - Output        - ButtonPad 0
175
        PA1     - Input, Pullup - ButtonPad 0
178
        PA2     - Output        - Reset
176
        PA2     - Output        - Reset
179
  */
177
  */
180
  DDRD          = 0B00000000;
178
  DDRD          = 0B00000000;
181
  PORTD         = 0B00000000;
179
  PORTD         = 0B00000011;
182
  /*
180
  /*
183
        PB0     - Output                - LED 0
181
        PB0     - Output                - LED 0
184
        PB1     - Output                - LED 1
182
        PB1     - Output                - LED 1
185
        PB2     - Output                - LED 2
183
        PB2     - Output                - LED 2
186
        PB3     - Output                - LED 3
184
        PB3     - Output                - LED 3
187
        PB4     - Output                - LED 4
185
        PB4     - Output                - LED 4
188
        PB5     - Output                - LED 5
186
        PB5     - Output                - LED 5
189
        PB6     - Output                - LED 6
187
        PB6     - Output                - LED 6
190
        PB7     - Output                - LED 7
188
        PB7     - Output                - LED 7
191
  */
189
  */
192
  DDRB          = 0B00000000;
190
  DDRB          = 0B11111111;
193
  PORTB         = 0B00000000;
191
  PORTB         = 0B00000000;
194
  /*
192
  /*
195
 
193
 
196
        PD0     - Output        - ButtonPad Gnd1
194
        PD0     - Output        - ButtonPad Gnd1
197
        PD1     - Output        - ButtonPad Gnd2
195
        PD1     - Output        - ButtonPad Gnd2
198
        PD2     - Output        - USB D+
196
        PD2     - Output        - USB D+
199
        PD3     - Output        - USB D-
197
        PD3     - Output        - USB D-
200
        PD4     - Output        - ButtonPad 2
198
        PD4     - Input, Pullup - ButtonPad 2
201
        PD5     - Output        - ButtonPad 3
199
        PD5     - Input, Pullup - ButtonPad 3
202
        PD6     - Output        - Select Switch
200
        PD6     - Input, Pullup - Select Switch
203
  */
201
  */
204
  DDRD          = 0B01000000;
202
  DDRD          = 0B00000011;
205
  PORTD         = 0B00000000;
203
  PORTD         = 0B01110011;
206
 
204
 
-
 
205
	PORTB = 255;
-
 
206
	_delay_ms(20);
-
 
207
	PORTB = currleds;
207
 
208
 
208
    //odDebugInit();
209
    //odDebugInit();
209
    usbDeviceDisconnect();
210
    usbDeviceDisconnect();
210
    for(i=0;i<20;i++){  /* 300 ms disconnect */
211
    for(i=0;i<20;i++){  /* 300 ms disconnect */
211
        _delay_ms(15);
212
        _delay_ms(15);
Line 221... Line 222...
221
 
222
 
222
    usbInit();
223
    usbInit();
223
    sei();
224
    sei();
224
 
225
 
225
    reportBuffer.axis0 = 0;
226
    reportBuffer.axis0 = 0;
226
    //reportBuffer.axis1 = 0;
227
    reportBuffer.axis1 = 0;
227
    reportBuffer.buttons = 0;
228
    reportBuffer.buttons = 0;
228
 
229
 
229
	PORTB = 0xaa;
-
 
230
 
230
 
231
    for(;;){    /* main event loop */
231
    for(;;){    /* main event loop */
232
        wdt_reset();
232
        wdt_reset();
233
        usbPoll();
233
        usbPoll();
234
 
234
 
235
	
235
	doButtons();	
236
 
236
 
237
        if(usbInterruptIsReady()){ 
237
        if(usbInterruptIsReady()){ 
238
 
238
 
239
		// ReportBuffer logic here
-
 
240
 
-
 
241
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
239
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
242
        }
240
        }
-
 
241
 
-
 
242
	_delay_ms(1);
243
    }
243
    }
244
    return 0;
244
    return 0;
245
}
245
}
246
 
246
 
247
void pcInterrupt(uint8_t pcint) {
247
void doButtons() {
-
 
248
        uint8_t pressed = getKey();
248
 
249
 
249
        switch (pcint) {
250
        // Deboucing
-
 
251
        // When i key first goes down, wait 5 ms, check it again to see if its still down
-
 
252
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
250
                case 0: pcInt[pcint].current = PINB; break;
253
                debounce.buttons = pressed;
-
 
254
                debounce.timer = 5;
251
        }
255
        }
252
        pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
-
 
253
        pcInt[pcint].last = pcInt[pcint].current;
-
 
254
 
-
 
255
        if (pcInt[pcint].mask == 0)
-
 
256
		return;
-
 
257
 
256
 
258
	// PCINT logic here
257
        // The key has come up
-
 
258
        if (pressed != debounce.buttons) {
-
 
259
                debounce.buttons = 0;
-
 
260
                debounce.timer = 0;
-
 
261
                debounce.waitup = 0;
-
 
262
        }
259
 
263
 
260
        // Clear the mask so we know we've delth with it
264
        // Debounce timer is up, process our button
-
 
265
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
261
        pcInt[pcint].mask = 0;
266
                uint8_t i = 0;
-
 
267
                for (i=0; i<=7; i++) {
-
 
268
                        // Button pressed and the led is currently on
-
 
269
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
-
 
270
                                if ( i == 6 && rbi(currleds, 7) != 1)  //Dont turn off com1 if no comm2
-
 
271
                                        break;
-
 
272
 
-
 
273
                                if ( i == 7 && rbi(currleds, 6) != 1)  //Dont turn off com2 if no comm1
-
 
274
                                        break;
-
 
275
 
-
 
276
                                cbi(currleds, i);
-
 
277
                        // Button is pressed and led is currently off
-
 
278
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
-
 
279
                                if ( i == 6 && rbi(currleds, 7) == 1)  //Turn on comm2, turn off comm1
-
 
280
                                        cbi(currleds,7);
-
 
281
 
-
 
282
                                if ( i == 7 && rbi(currleds, 6) == 1)  //Turn on comm1, turn off comm2
-
 
283
                                        cbi(currleds,6);
-
 
284
 
-
 
285
                                sbi(currleds, i);
-
 
286
                        }
-
 
287
                }
-
 
288
                PORTB = currleds;
-
 
289
                reportBuffer.buttons = debounce.buttons;
-
 
290
                // Set debounce to wait to button up
-
 
291
                debounce.waitup = 1;
-
 
292
        }
262
}
293
}
263
 
294
 
-
 
295
// Gnd = PD0, PD1
-
 
296
// Btn = PA1, PA0, PD4, PD5
264
ISR(PCINT_vect) {
297
uint8_t getKey() {
-
 
298
        uint8_t key = 0;
-
 
299
 
-
 
300
        cbi(PORTD, 0);
-
 
301
        _delay_us(10);        // Wait for the port change
-
 
302
        if (rbi(PIND, 5) == 0) key = 1;
-
 
303
        if (rbi(PIND, 4) == 0) key = 2;
-
 
304
        if (rbi(PINA, 0) == 0) key = 4;
-
 
305
        if (rbi(PINA, 1) == 0) key = 8;
-
 
306
        sbi(PORTD, 0);
-
 
307
 
-
 
308
        cbi(PORTD, 1);
265
        pcInterrupt(0);
309
        _delay_us(10);
-
 
310
        if (rbi(PIND, 5) == 0) key = 16;
-
 
311
        if (rbi(PIND, 4) == 0) key = 32;
-
 
312
        if (rbi(PINA, 0) == 0) key = 64;
-
 
313
        if (rbi(PINA, 1) == 0) key = 128;
-
 
314
        sbi(PORTD, 1);
-
 
315
 
-
 
316
        return key;
266
}
317
}
267
 
318
 
-
 
319
 
268
ISR(TIMER0_OVF_vect) {
320
ISR(TIMER0_OVF_vect) {
269
        tmr0_ovf++;
321
        tmr0_ovf++;
-
 
322
 
-
 
323
	//16.5Mhz, 1ms = 50ovf
-
 
324
 
270
        if (tmr0_ovf>=50) {
325
        if (tmr0_ovf>=50) {
271
                systime++;
326
                systime++;
272
                tmr0_ovf = 0;
327
                tmr0_ovf = 0;
-
 
328
 
-
 
329
		if (debounce.timer != 0)
-
 
330
			debounce.timer--;
273
        }
331
        }
274
}
332
}