Subversion Repositories group.electronics

Rev

Rev 88 | Rev 90 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
79 pfowler 1
#include <avr/io.h>
2
#include <avr/wdt.h>
3
#include <avr/interrupt.h>
4
#include <avr/pgmspace.h>
5
#include <util/delay.h>
6
 
7
#include "usbdrv.h"
8
#include "config.h"
9
 
10
#ifndef NULL
11
#define NULL    ((void *)0)
12
#endif
13
 
88 pfowler 14
#define USB_GET_LED_STATE  100
15
#define USB_SET_LED_STATE  101
16
 
79 pfowler 17
/* ------------------------------------------------------------------------- */
18
 
84 pfowler 19
uint8_t getKey(void);
86 pfowler 20
void doButtons(uint8_t);
85 pfowler 21
inline void setLeds(uint8_t);
84 pfowler 22
 
79 pfowler 23
struct {
86 pfowler 24
	int16_t axis[2];
25
	uint8_t buttons;
79 pfowler 26
} reportBuffer;
27
 
28
 
29
volatile struct {
84 pfowler 30
	uint8_t buttons;
31
	uint8_t waitup;
79 pfowler 32
	uint8_t timer;
84 pfowler 33
} debounce;
79 pfowler 34
 
86 pfowler 35
uint8_t currleds = 1;
79 pfowler 36
static uchar    idleRate;           /* in 4 ms units */
37
volatile uint8_t tmr0_ovf = 0;
38
volatile uint32_t systime = 0;
39
 
40
/* ------------------------------------------------------------------------- */
41
 
42
const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
43
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
44
    0x09, 0x05,                    // USAGE (Game Pad)
45
    0xa1, 0x01,                    // COLLECTION (Application)
46
    0x09, 0x01,                    //   USAGE (Pointer)
47
    0xa1, 0x00,                    //   COLLECTION (Physical)
48
    0x09, 0x30,                    //     USAGE (X)
49
    0x09, 0x31,                    //     USAGE (Y)
50
    0x16, 0x00, 0x80,		   //	  Log Min -32768
51
    0x26, 0xff, 0x7f,		   //	  Log max 32768
52
    0x75, 0x10,                    //     REPORT_SIZE (16)
53
    0x95, 0x02,                    //     REPORT_COUNT (2)
54
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
55
 
56
    0x05, 0x09,                    //     USAGE_PAGE (Button)
57
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
84 pfowler 58
    0x29, 0x08,                    //     USAGE_MAXIMUM (Button 8)
79 pfowler 59
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
60
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
61
    0x75, 0x01,                    //     REPORT_SIZE (1)
62
    0x95, 0x08,                    //     REPORT_COUNT (2)
63
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
64
 
65
    0xc0,                          //   END_COLLECTION
66
    0xc0                           // END_COLLECTION
67
};
68
 
86 pfowler 69
void hadUsbReset(void) {
85 pfowler 70
}
71
 
72
 
79 pfowler 73
uchar	usbFunctionSetup(uchar data[8])
74
{
75
    usbRequest_t    *rq = (void *)data;
76
 
88 pfowler 77
    //if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
89 pfowler 78
 
79
	sbi (PORTD, 4);	
88 pfowler 80
 
81
        switch (rq->bRequest ) {
89 pfowler 82
		case USBRQ_HID_GET_REPORT:
83
			return sizeof(reportBuffer);
84
		case USBRQ_HID_GET_IDLE:
85
			usbMsgPtr = &idleRate;
86
			return 1;
87
		case USBRQ_HID_SET_IDLE:
88
			idleRate = rq->wValue.bytes[1];
89
			return 0;
90
		case USB_GET_LED_STATE: // send data to PC
91
			usbMsgPtr = currleds;
92
			return sizeof(currleds);
93
		case USB_SET_LED_STATE: // modify reply buffer
94
			//currleds = rq->wValue.bytes[0];
95
			//currleds = rq->wIndex.bytes[0];
96
			currleds = 0xaa;
97
			return 0;
79 pfowler 98
        }
88 pfowler 99
    //}else{
79 pfowler 100
 
88 pfowler 101
    //}
79 pfowler 102
	return 0;
103
}
104
 
105
 
106
void usbSendHidReport(uchar * data, uchar len) {
107
	usbSetInterrupt(data, len);
108
}
109
 
86 pfowler 110
/*
111
void usbSendHidReport(uchar * data, uchar len) {
112
        while(1)
113
        {
114
                usbPoll();
115
                if (usbInterruptIsReady())
116
                {
117
                        usbSetInterrupt(data, len);
118
                        break;
119
                }
120
        }
121
}
122
*/
79 pfowler 123
 
86 pfowler 124
 
79 pfowler 125
int main(void) {
126
 
86 pfowler 127
	DIDR0 = 0x00;
128
 
80 pfowler 129
  /*
130
  DDR : 1 = Output, 0 = Input
131
  PORT: 1 = Pullup for Input, otherwise set output
132
  PIN : Read input pin
133
  */
134
 
135
  /*
85 pfowler 136
        PB0     - Output                - LED 3
137
        PB1     - Output                - LED 4
138
        PB2     - Output                - LED 5
139
        PB3     - Output                - LED 6
140
        PB4     - Output                - LED 7
141
        PB5     - 
142
        PB6     - 
143
        PB7     - 
80 pfowler 144
  */
85 pfowler 145
  DDRB          = 0B00011111;
80 pfowler 146
  PORTB         = 0B00000000;
85 pfowler 147
 
80 pfowler 148
  /*
85 pfowler 149
        PC0     - Output		- ButtonPad Gnd0
150
        PC1     - Output		- ButtonPad Gnd1
151
        PC2     - Input, Pullup		- ButtonPad 0
152
        PC3     - Input, Pullup		- ButtonPad 1
153
        PC4     - Input, Pullup		- ButtonPad 2
154
        PC5     - Input, Pullup		- ButtonPad 3
80 pfowler 155
  */
86 pfowler 156
  DDRC          = 0B00000011;
157
  PORTC         = 0B00111111;
80 pfowler 158
 
85 pfowler 159
  /*
160
        PD0     - 
161
        PD1     - 
162
        PD2     - 
163
        PD3     - 
164
        PD4     - 
165
        PD5     - Output		- LED 0
166
        PD6     - Output		- LED 1
167
        PD7     - Output		- LED 2
168
  */
169
  DDRD          = 0B11100000;
170
  PORTD         = 0B00000000;
171
 
86 pfowler 172
	setLeds(0xff);
80 pfowler 173
 
79 pfowler 174
    usbDeviceDisconnect();
86 pfowler 175
    _delay_ms(500);
79 pfowler 176
    usbDeviceConnect();
177
 
178
 
85 pfowler 179
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
79 pfowler 180
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
85 pfowler 181
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
79 pfowler 182
 
86 pfowler 183
    wdt_enable(WDTO_1S);
79 pfowler 184
    usbInit();
185
    sei();
186
 
86 pfowler 187
    reportBuffer.axis[0] = 0xfffd;
188
    reportBuffer.axis[1] = 0xfffd;
189
    reportBuffer.buttons = 0x00;
79 pfowler 190
 
86 pfowler 191
	setLeds(currleds);
80 pfowler 192
 
86 pfowler 193
    for(;;){
79 pfowler 194
        wdt_reset();
195
        usbPoll();
196
 
86 pfowler 197
	uint8_t pressed = getKey();
198
	doButtons(pressed);	
80 pfowler 199
 
79 pfowler 200
        if(usbInterruptIsReady()){ 
86 pfowler 201
                reportBuffer.buttons = pressed;
79 pfowler 202
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
203
        }
84 pfowler 204
 
79 pfowler 205
    }
86 pfowler 206
 
79 pfowler 207
    return 0;
208
}
209
 
85 pfowler 210
inline void setLeds(uint8_t leds) {
86 pfowler 211
	PORTB &= 0xE0;
85 pfowler 212
        PORTB |= (0x1F & (leds >> 3));
86 pfowler 213
	PORTD &= 0x1F;
85 pfowler 214
        PORTD |= (0xE0 & (leds << 5));
215
}
216
 
86 pfowler 217
void doButtons(uint8_t pressed) {
79 pfowler 218
 
84 pfowler 219
        // Deboucing
220
        // When i key first goes down, wait 5 ms, check it again to see if its still down
221
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
222
                debounce.buttons = pressed;
223
                debounce.timer = 5;
79 pfowler 224
        }
225
 
84 pfowler 226
        // The key has come up
227
        if (pressed != debounce.buttons) {
228
                debounce.buttons = 0;
229
                debounce.timer = 0;
230
                debounce.waitup = 0;
231
        }
79 pfowler 232
 
84 pfowler 233
        // Debounce timer is up, process our button
234
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
235
                uint8_t i = 0;
236
                for (i=0; i<=7; i++) {
237
                        // Button pressed and the led is currently on
238
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
86 pfowler 239
                                if ( i == 0 && rbi(currleds, 1) != 1)  //Dont turn off com1 if no comm2
84 pfowler 240
                                        break;
79 pfowler 241
 
86 pfowler 242
                                if ( i == 1 && rbi(currleds, 0) != 1)  //Dont turn off com2 if no comm1
84 pfowler 243
                                        break;
244
 
245
                                cbi(currleds, i);
246
                        // Button is pressed and led is currently off
247
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
86 pfowler 248
                                if ( i == 0 && rbi(currleds, 1) == 1)  //Turn on comm2, turn off comm1
249
                                        cbi(currleds,1);
84 pfowler 250
 
86 pfowler 251
                                if ( i == 1 && rbi(currleds, 0) == 1)  //Turn on comm1, turn off comm2
252
                                        cbi(currleds,0);
84 pfowler 253
 
254
                                sbi(currleds, i);
255
                        }
256
                }
85 pfowler 257
                setLeds(currleds);
84 pfowler 258
                debounce.waitup = 1;
259
        }
79 pfowler 260
}
261
 
85 pfowler 262
// Gnd = PC0, PC1
263
// Btn = PC2, PC3, PC4, PC5
84 pfowler 264
uint8_t getKey() {
265
        uint8_t key = 0;
266
 
86 pfowler 267
        cbi(PORTC, 1);
84 pfowler 268
        _delay_us(10);        // Wait for the port change
85 pfowler 269
        if (rbi(PINC, 2) == 0) key = 1;
270
        if (rbi(PINC, 3) == 0) key = 2;
271
        if (rbi(PINC, 4) == 0) key = 4;
272
        if (rbi(PINC, 5) == 0) key = 8;
86 pfowler 273
        sbi(PORTC, 1);
84 pfowler 274
 
86 pfowler 275
        cbi(PORTC, 0);
84 pfowler 276
        _delay_us(10);
85 pfowler 277
        if (rbi(PINC, 2) == 0) key = 16;
278
        if (rbi(PINC, 3) == 0) key = 32;
279
        if (rbi(PINC, 4) == 0) key = 64;
280
        if (rbi(PINC, 5) == 0) key = 128;
86 pfowler 281
        sbi(PORTC, 0);
84 pfowler 282
 
283
        return key;
79 pfowler 284
}
285
 
84 pfowler 286
 
79 pfowler 287
ISR(TIMER0_OVF_vect) {
288
        tmr0_ovf++;
84 pfowler 289
 
87 pfowler 290
	// Clk/1 TCCR0B = (1<< CS00);
291
	//20.0Mhz, 1ms = 78ovf
292
	//16.5Mhz, 1ms = 64ovf
85 pfowler 293
	//12.0Mhz, 1ms = 46ovf
84 pfowler 294
 
87 pfowler 295
        if (tmr0_ovf>=78) {
79 pfowler 296
                systime++;
297
                tmr0_ovf = 0;
84 pfowler 298
 
299
		if (debounce.timer != 0)
300
			debounce.timer--;
79 pfowler 301
        }
302
}