Subversion Repositories group.electronics

Rev

Rev 93 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 112
Line 9... Line 9...
9
 
9
 
10
#ifndef NULL
10
#ifndef NULL
11
#define NULL    ((void *)0)
11
#define NULL    ((void *)0)
12
#endif
12
#endif
13
 
13
 
14
#define USB_GET_LED_STATE  100
14
#define USB_GET_STATE  100
15
#define USB_SET_LED_STATE  101
15
#define USB_LEDS_SET  101
16
 
16
 
17
/* ------------------------------------------------------------------------- */
17
/* ------------------------------------------------------------------------- */
18
 
18
 
19
uint8_t getKey(void);
19
uint8_t getKey(void);
20
void doButtons(uint8_t);
20
void doButtons(uint8_t);
Line 39... Line 39...
39
		uint8_t other:1;
39
		uint8_t other:1;
40
		uint8_t reserved:6;
40
		uint8_t reserved:6;
41
	};
41
	};
42
} config;
42
} config;
43
 
43
 
-
 
44
static uchar usbReplyBuf[16];
-
 
45
 
44
uint8_t currleds = 1;
46
volatile uint8_t currLeds = 0;
45
static uchar    idleRate;           /* in 4 ms units */
47
volatile uint8_t currKeys = 0;
46
volatile uint8_t tmr0_ovf = 0;
48
volatile uint8_t tmr0_ovf = 0;
47
volatile uint32_t systime = 0;
49
volatile uint32_t systime = 0;
48
 
50
 
49
/* ------------------------------------------------------------------------- */
51
/* ------------------------------------------------------------------------- */
50
 
52
 
51
const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
-
 
52
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
-
 
53
    0x09, 0x05,                    // USAGE (Game Pad)
-
 
54
    0xa1, 0x01,                    // COLLECTION (Application)
-
 
55
    0x09, 0x01,                    //   USAGE (Pointer)
-
 
56
    0xa1, 0x00,                    //   COLLECTION (Physical)
-
 
57
    0x09, 0x30,                    //     USAGE (X)
-
 
58
    0x09, 0x31,                    //     USAGE (Y)
-
 
59
    0x16, 0x00, 0x80,		   //	  Log Min -32768
-
 
60
    0x26, 0xff, 0x7f,		   //	  Log max 32768
-
 
61
    0x75, 0x10,                    //     REPORT_SIZE (16)
-
 
62
    0x95, 0x02,                    //     REPORT_COUNT (2)
-
 
63
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
-
 
64
 
-
 
65
    0x05, 0x09,                    //     USAGE_PAGE (Button)
-
 
66
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
-
 
67
    0x29, 0x08,                    //     USAGE_MAXIMUM (Button 8)
-
 
68
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
-
 
69
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
-
 
70
    0x75, 0x01,                    //     REPORT_SIZE (1)
-
 
71
    0x95, 0x08,                    //     REPORT_COUNT (2)
-
 
72
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
-
 
73
 
-
 
74
    0xc0,                          //   END_COLLECTION
-
 
75
    0xc0                           // END_COLLECTION
-
 
76
};
-
 
77
 
-
 
78
void hadUsbReset(void) {
53
void hadUsbReset(void) {
79
}
54
}
80
 
55
 
81
 
56
 
82
uchar	usbFunctionSetup(uchar data[8])
57
uchar	usbFunctionSetup(uchar data[8])
83
{
58
{
84
    usbRequest_t    *rq = (void *)data;
59
    usbRequest_t    *rq = (void *)data;
85
 
60
 
86
    //if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
-
 
87
        switch (rq->bRequest ) {
61
        switch (rq->bRequest ) {
88
		case USBRQ_HID_GET_REPORT:
-
 
89
			return sizeof(reportBuffer);
-
 
90
		case USBRQ_HID_GET_IDLE:
62
		case USB_GET_STATE:
91
			usbMsgPtr = &idleRate;
63
			usbReplyBuf[0] = currLeds;
92
			return 1;
-
 
93
		case USBRQ_HID_SET_IDLE:
-
 
94
			idleRate = rq->wValue.bytes[1];
64
			usbReplyBuf[1] = currKeys;
95
			return 0;
-
 
96
		case USB_GET_LED_STATE: // send data to PC
-
 
97
			usbMsgPtr = currleds;
65
			usbMsgPtr = usbReplyBuf;
98
			return sizeof(currleds);
66
			return sizeof(usbReplyBuf);
99
		case USB_SET_LED_STATE: // modify reply buffer
67
		case USB_LEDS_SET:
100
			currleds = rq->wValue.bytes[0];
68
			currLeds = rq->wValue.bytes[0];
101
			return 0;
69
			return 0;
-
 
70
			
102
        }
71
        }
103
    //}
-
 
104
 
72
 
105
	return 0;
73
	return 0;
106
}
74
}
107
 
75
 
108
 
-
 
109
void usbSendHidReport(uchar * data, uchar len) {
-
 
110
	usbSetInterrupt(data, len);
-
 
111
}
-
 
112
 
-
 
113
int main(void) {
76
int main(void) {
114
 
77
 
115
	DIDR0 = 0x00;
78
	DIDR0 = 0x00;
116
 
79
 
117
  /*
80
  /*
Line 170... Line 133...
170
 
133
 
171
    wdt_enable(WDTO_1S);
134
    wdt_enable(WDTO_1S);
172
    usbInit();
135
    usbInit();
173
    sei();
136
    sei();
174
 
137
 
175
    reportBuffer.axis[0] = 0xfffd;
-
 
176
    reportBuffer.axis[1] = 0xfffd;
-
 
177
    reportBuffer.buttons = 0x00;
-
 
178
 
-
 
179
	config.led_method = 1;
138
	config.led_method = 1;
180
	setLeds(currleds);
139
	setLeds(currLeds);
181
 
140
 
182
 
141
 
183
    for(;;){
142
    for(;;){
184
        wdt_reset();
143
        wdt_reset();
185
        usbPoll();
144
        usbPoll();
186
 
145
 
187
	uint8_t pressed = getKey();
146
	currKeys = getKey();
188
 
147
 
189
	if (config.led_method != rbi(PORTB, 5)) {
148
	if (config.led_method != rbi(PORTB, 5)) {
190
		// Reset the leds when switcing	to buttons
149
		// Reset the leds when switcing	to buttons
191
		if (config.led_method == 0) currleds = 1;
150
		if (config.led_method == 0) currLeds = 1;
192
		config.led_method = rbi(PORTB, 5);
151
		config.led_method = rbi(PORTB, 5);
193
	}
152
	}
194
 
153
 
195
	if (config.led_method == 1)
154
	if (config.led_method == 1) {
-
 
155
		uint8_t pressed = getKey();
196
		doButtons(pressed);
156
		doButtons(pressed);
-
 
157
	}
197
 
158
 
198
	setLeds(currleds);
159
	setLeds(currLeds);
199
 
160
 
200
        if(usbInterruptIsReady()){ 
-
 
201
                reportBuffer.buttons = pressed;
-
 
202
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
-
 
203
        }
-
 
204
 
161
 
205
    }
162
    }
206
 
163
 
207
    return 0;
164
    return 0;
208
}
165
}
Line 233... Line 190...
233
        // Debounce timer is up, process our button
190
        // Debounce timer is up, process our button
234
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
191
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
235
                uint8_t i = 0;
192
                uint8_t i = 0;
236
                for (i=0; i<=7; i++) {
193
                for (i=0; i<=7; i++) {
237
                        // Button pressed and the led is currently on
194
                        // Button pressed and the led is currently on
238
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
195
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currLeds, i) == 1 ) {
239
                                if ( i == 0 && rbi(currleds, 1) != 1)  //Dont turn off com1 if no comm2
196
                                if ( i == 0 && rbi(currLeds, 1) != 1)  //Dont turn off com1 if no comm2
240
                                        break;
197
                                        break;
241
 
198
 
242
                                if ( i == 1 && rbi(currleds, 0) != 1)  //Dont turn off com2 if no comm1
199
                                if ( i == 1 && rbi(currLeds, 0) != 1)  //Dont turn off com2 if no comm1
243
                                        break;
200
                                        break;
244
 
201
 
245
                                cbi(currleds, i);
202
                                cbi(currLeds, i);
246
                        // Button is pressed and led is currently off
203
                        // Button is pressed and led is currently off
247
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
204
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currLeds, i) == 0 ) {
248
                                if ( i == 0 && rbi(currleds, 1) == 1)  //Turn on comm2, turn off comm1
205
                                if ( i == 0 && rbi(currLeds, 1) == 1)  //Turn on comm2, turn off comm1
249
                                        cbi(currleds,1);
206
                                        cbi(currLeds,1);
250
 
207
 
251
                                if ( i == 1 && rbi(currleds, 0) == 1)  //Turn on comm1, turn off comm2
208
                                if ( i == 1 && rbi(currLeds, 0) == 1)  //Turn on comm1, turn off comm2
252
                                        cbi(currleds,0);
209
                                        cbi(currLeds,0);
253
 
210
 
254
                                sbi(currleds, i);
211
                                sbi(currLeds, i);
255
                        }
212
                        }
256
                }
213
                }
257
                //setLeds(currleds);
-
 
258
                debounce.waitup = 1;
214
                debounce.waitup = 1;
259
        }
215
        }
260
}
216
}
261
 
217
 
262
// Gnd = PC0, PC1
218
// Gnd = PC0, PC1