Subversion Repositories group.electronics

Rev

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

Rev 85 Rev 86
Line 13... Line 13...
13
#include <avr/interrupt.h>
13
#include <avr/interrupt.h>
14
#include <avr/pgmspace.h>
14
#include <avr/pgmspace.h>
15
#include <util/delay.h>
15
#include <util/delay.h>
16
 
16
 
17
#include "usbdrv.h"
17
#include "usbdrv.h"
18
//#include "oddebug.h"
-
 
19
#include "config.h"
18
#include "config.h"
20
 
19
 
21
#ifndef NULL
20
#ifndef NULL
22
#define NULL    ((void *)0)
21
#define NULL    ((void *)0)
23
#endif
22
#endif
24
 
23
 
25
/* ------------------------------------------------------------------------- */
24
/* ------------------------------------------------------------------------- */
26
 
25
 
27
uint8_t getKey(void);
26
uint8_t getKey(void);
28
void doButtons(void);
27
void doButtons(uint8_t);
29
inline void setLeds(uint8_t);
28
inline void setLeds(uint8_t);
30
 
29
 
31
struct {
30
struct {
32
	union {
-
 
33
		int16_t axis[2];
31
	int16_t axis[2];
34
		struct {
-
 
35
			int16_t axis0:16;
-
 
36
			int16_t axis1:16;
-
 
37
		};
-
 
38
		uint8_t buttons;
32
	uint8_t buttons;
39
		struct {
-
 
40
			int8_t b1:1;
-
 
41
			int8_t b2:2;
-
 
42
			int8_t b3:3;
-
 
43
			int8_t b4:4;
-
 
44
			int8_t b5:5;
-
 
45
			int8_t b6:6;
-
 
46
			int8_t b7:7;
-
 
47
			int8_t b8:8;
-
 
48
		};
-
 
49
	};
-
 
50
} reportBuffer;
33
} reportBuffer;
51
 
34
 
52
 
35
 
53
volatile struct {
36
volatile struct {
54
	uint8_t buttons;
37
	uint8_t buttons;
55
	uint8_t waitup;
38
	uint8_t waitup;
56
	uint8_t timer;
39
	uint8_t timer;
57
} debounce;
40
} debounce;
58
 
41
 
59
uint8_t currleds = 128;
42
uint8_t currleds = 1;
60
static uchar    idleRate;           /* in 4 ms units */
43
static uchar    idleRate;           /* in 4 ms units */
61
volatile uint8_t tmr0_ovf = 0;
44
volatile uint8_t tmr0_ovf = 0;
62
volatile uint32_t systime = 0;
45
volatile uint32_t systime = 0;
63
 
46
 
64
/* ------------------------------------------------------------------------- */
47
/* ------------------------------------------------------------------------- */
Line 88... Line 71...
88
 
71
 
89
    0xc0,                          //   END_COLLECTION
72
    0xc0,                          //   END_COLLECTION
90
    0xc0                           // END_COLLECTION
73
    0xc0                           // END_COLLECTION
91
};
74
};
92
 
75
 
93
void usbEventResetReady(void) {
76
void hadUsbReset(void) {
94
 
-
 
95
}
77
}
96
 
78
 
97
 
79
 
98
uchar	usbFunctionSetup(uchar data[8])
80
uchar	usbFunctionSetup(uchar data[8])
99
{
81
{
Line 101... Line 83...
101
 
83
 
102
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
84
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
103
        if(rq->bRequest == USBRQ_HID_GET_REPORT){ 
85
        if(rq->bRequest == USBRQ_HID_GET_REPORT){ 
104
            return sizeof(reportBuffer);
86
            return sizeof(reportBuffer);
105
        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
87
        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
106
            usbMsgPtr = &idleRate;
88
            //usbMsgPtr = &idleRate;
107
            return 1;
89
            return 1;
108
        }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
90
        }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
109
            idleRate = rq->wValue.bytes[1];
91
            idleRate = rq->wValue.bytes[1];
110
        }
92
        }
111
    }else{
93
    }else{
Line 117... Line 99...
117
 
99
 
118
void usbSendHidReport(uchar * data, uchar len) {
100
void usbSendHidReport(uchar * data, uchar len) {
119
	usbSetInterrupt(data, len);
101
	usbSetInterrupt(data, len);
120
}
102
}
121
 
103
 
-
 
104
/*
-
 
105
void usbSendHidReport(uchar * data, uchar len) {
-
 
106
        while(1)
-
 
107
        {
-
 
108
                usbPoll();
-
 
109
                if (usbInterruptIsReady())
-
 
110
                {
-
 
111
                        usbSetInterrupt(data, len);
-
 
112
                        break;
-
 
113
                }
-
 
114
        }
-
 
115
}
-
 
116
*/
-
 
117
 
122
 
118
 
123
int main(void) {
119
int main(void) {
124
 
120
 
-
 
121
	DIDR0 = 0x00;
-
 
122
 
125
  /*
123
  /*
126
  DDR : 1 = Output, 0 = Input
124
  DDR : 1 = Output, 0 = Input
127
  PORT: 1 = Pullup for Input, otherwise set output
125
  PORT: 1 = Pullup for Input, otherwise set output
128
  PIN : Read input pin
126
  PIN : Read input pin
129
  */
127
  */
Line 147... Line 145...
147
        PC2     - Input, Pullup		- ButtonPad 0
145
        PC2     - Input, Pullup		- ButtonPad 0
148
        PC3     - Input, Pullup		- ButtonPad 1
146
        PC3     - Input, Pullup		- ButtonPad 1
149
        PC4     - Input, Pullup		- ButtonPad 2
147
        PC4     - Input, Pullup		- ButtonPad 2
150
        PC5     - Input, Pullup		- ButtonPad 3
148
        PC5     - Input, Pullup		- ButtonPad 3
151
  */
149
  */
152
  DDRD          = 0B00000011;
150
  DDRC          = 0B00000011;
153
  PORTD         = 0B00111111;
151
  PORTC         = 0B00111111;
154
 
152
 
155
  /*
153
  /*
156
        PD0     - 
154
        PD0     - 
157
        PD1     - 
155
        PD1     - 
158
        PD2     - 
156
        PD2     - 
Line 163... Line 161...
163
        PD7     - Output		- LED 2
161
        PD7     - Output		- LED 2
164
  */
162
  */
165
  DDRD          = 0B11100000;
163
  DDRD          = 0B11100000;
166
  PORTD         = 0B00000000;
164
  PORTD         = 0B00000000;
167
 
165
 
168
	setLeds(255);
166
	setLeds(0xff);
169
	_delay_ms(20);
-
 
170
	setLeds(currleds);
-
 
171
 
167
 
172
    usbDeviceDisconnect();
168
    usbDeviceDisconnect();
173
        _delay_ms(500);
169
    _delay_ms(500);
174
 
-
 
175
    usbDeviceConnect();
170
    usbDeviceConnect();
176
    wdt_enable(WDTO_1S);
-
 
177
 
171
 
178
 
172
 
179
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
173
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
180
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
174
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
181
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
175
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
182
 
176
 
-
 
177
    wdt_enable(WDTO_1S);
183
    usbInit();
178
    usbInit();
184
    sei();
179
    sei();
185
 
180
 
186
    reportBuffer.axis0 = 0;
181
    reportBuffer.axis[0] = 0xfffd;
187
    reportBuffer.axis1 = 0;
182
    reportBuffer.axis[1] = 0xfffd;
188
    reportBuffer.buttons = 0;
183
    reportBuffer.buttons = 0x00;
189
 
184
 
-
 
185
	setLeds(currleds);
190
 
186
 
191
    for(;;){    /* main event loop */
187
    for(;;){
192
        wdt_reset();
188
        wdt_reset();
193
        usbPoll();
189
        usbPoll();
194
 
190
 
-
 
191
	uint8_t pressed = getKey();
195
	doButtons();	
192
	doButtons(pressed);	
196
 
193
 
197
        if(usbInterruptIsReady()){ 
194
        if(usbInterruptIsReady()){ 
198
 
-
 
-
 
195
                reportBuffer.buttons = pressed;
199
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
196
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
200
        }
197
        }
201
 
198
 
202
	_delay_ms(1);
-
 
203
    }
199
    }
-
 
200
 
204
    return 0;
201
    return 0;
205
}
202
}
206
 
203
 
207
inline void setLeds(uint8_t leds) {
204
inline void setLeds(uint8_t leds) {
-
 
205
	PORTB &= 0xE0;
208
        PORTB |= (0x1F & (leds >> 3));
206
        PORTB |= (0x1F & (leds >> 3));
-
 
207
	PORTD &= 0x1F;
209
        PORTD |= (0xE0 & (leds << 5));
208
        PORTD |= (0xE0 & (leds << 5));
210
}
209
}
211
 
210
 
212
void doButtons() {
211
void doButtons(uint8_t pressed) {
213
        uint8_t pressed = getKey();
-
 
214
 
212
 
215
        // Deboucing
213
        // Deboucing
216
        // When i key first goes down, wait 5 ms, check it again to see if its still down
214
        // When i key first goes down, wait 5 ms, check it again to see if its still down
217
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
215
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
218
                debounce.buttons = pressed;
216
                debounce.buttons = pressed;
Line 230... Line 228...
230
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
228
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
231
                uint8_t i = 0;
229
                uint8_t i = 0;
232
                for (i=0; i<=7; i++) {
230
                for (i=0; i<=7; i++) {
233
                        // Button pressed and the led is currently on
231
                        // Button pressed and the led is currently on
234
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
232
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
235
                                if ( i == 6 && rbi(currleds, 7) != 1)  //Dont turn off com1 if no comm2
233
                                if ( i == 0 && rbi(currleds, 1) != 1)  //Dont turn off com1 if no comm2
236
                                        break;
234
                                        break;
237
 
235
 
238
                                if ( i == 7 && rbi(currleds, 6) != 1)  //Dont turn off com2 if no comm1
236
                                if ( i == 1 && rbi(currleds, 0) != 1)  //Dont turn off com2 if no comm1
239
                                        break;
237
                                        break;
240
 
238
 
241
                                cbi(currleds, i);
239
                                cbi(currleds, i);
242
                        // Button is pressed and led is currently off
240
                        // Button is pressed and led is currently off
243
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
241
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
244
                                if ( i == 6 && rbi(currleds, 7) == 1)  //Turn on comm2, turn off comm1
242
                                if ( i == 0 && rbi(currleds, 1) == 1)  //Turn on comm2, turn off comm1
245
                                        cbi(currleds,7);
243
                                        cbi(currleds,1);
246
 
244
 
247
                                if ( i == 7 && rbi(currleds, 6) == 1)  //Turn on comm1, turn off comm2
245
                                if ( i == 1 && rbi(currleds, 0) == 1)  //Turn on comm1, turn off comm2
248
                                        cbi(currleds,6);
246
                                        cbi(currleds,0);
249
 
247
 
250
                                sbi(currleds, i);
248
                                sbi(currleds, i);
251
                        }
249
                        }
252
                }
250
                }
253
                setLeds(currleds);
251
                setLeds(currleds);
254
                reportBuffer.buttons = debounce.buttons;
-
 
255
                // Set debounce to wait to button up
-
 
256
                debounce.waitup = 1;
252
                debounce.waitup = 1;
257
        }
253
        }
258
}
254
}
259
 
255
 
260
// Gnd = PC0, PC1
256
// Gnd = PC0, PC1
261
// Btn = PC2, PC3, PC4, PC5
257
// Btn = PC2, PC3, PC4, PC5
262
uint8_t getKey() {
258
uint8_t getKey() {
263
        uint8_t key = 0;
259
        uint8_t key = 0;
264
 
260
 
265
        cbi(PORTC, 0);
261
        cbi(PORTC, 1);
266
        _delay_us(10);        // Wait for the port change
262
        _delay_us(10);        // Wait for the port change
267
        if (rbi(PINC, 2) == 0) key = 1;
263
        if (rbi(PINC, 2) == 0) key = 1;
268
        if (rbi(PINC, 3) == 0) key = 2;
264
        if (rbi(PINC, 3) == 0) key = 2;
269
        if (rbi(PINC, 4) == 0) key = 4;
265
        if (rbi(PINC, 4) == 0) key = 4;
270
        if (rbi(PINC, 5) == 0) key = 8;
266
        if (rbi(PINC, 5) == 0) key = 8;
271
        sbi(PORTC, 0);
267
        sbi(PORTC, 1);
272
 
268
 
273
        cbi(PORTC, 1);
269
        cbi(PORTC, 0);
274
        _delay_us(10);
270
        _delay_us(10);
275
        if (rbi(PINC, 2) == 0) key = 16;
271
        if (rbi(PINC, 2) == 0) key = 16;
276
        if (rbi(PINC, 3) == 0) key = 32;
272
        if (rbi(PINC, 3) == 0) key = 32;
277
        if (rbi(PINC, 4) == 0) key = 64;
273
        if (rbi(PINC, 4) == 0) key = 64;
278
        if (rbi(PINC, 5) == 0) key = 128;
274
        if (rbi(PINC, 5) == 0) key = 128;
279
        sbi(PORTC, 1);
275
        sbi(PORTC, 0);
280
 
276
 
281
        return key;
277
        return key;
282
}
278
}
283
 
279
 
284
 
280
 
Line 286... Line 282...
286
        tmr0_ovf++;
282
        tmr0_ovf++;
287
 
283
 
288
	//16.5Mhz, 1ms = 50ovf
284
	//16.5Mhz, 1ms = 50ovf
289
	//12.0Mhz, 1ms = 46ovf
285
	//12.0Mhz, 1ms = 46ovf
290
 
286
 
291
        if (tmr0_ovf>=46) {
287
        if (tmr0_ovf>=65) {
292
                systime++;
288
                systime++;
293
                tmr0_ovf = 0;
289
                tmr0_ovf = 0;
294
 
290
 
295
		if (debounce.timer != 0)
291
		if (debounce.timer != 0)
296
			debounce.timer--;
292
			debounce.timer--;