Subversion Repositories group.electronics

Rev

Rev 90 | Rev 92 | 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){ 
78
        switch (rq->bRequest ) {
89 pfowler 79
		case USBRQ_HID_GET_REPORT:
80
			return sizeof(reportBuffer);
81
		case USBRQ_HID_GET_IDLE:
82
			usbMsgPtr = &idleRate;
83
			return 1;
84
		case USBRQ_HID_SET_IDLE:
85
			idleRate = rq->wValue.bytes[1];
86
			return 0;
87
		case USB_GET_LED_STATE: // send data to PC
88
			usbMsgPtr = currleds;
91 pfowler 89
			//currleds = 1;
89 pfowler 90
			return sizeof(currleds);
91
		case USB_SET_LED_STATE: // modify reply buffer
91 pfowler 92
			currleds = rq->wValue.bytes[0];
89 pfowler 93
			return 0;
79 pfowler 94
        }
91 pfowler 95
    //}
79 pfowler 96
 
97
	return 0;
98
}
99
 
100
 
101
void usbSendHidReport(uchar * data, uchar len) {
102
	usbSetInterrupt(data, len);
103
}
104
 
105
int main(void) {
106
 
86 pfowler 107
	DIDR0 = 0x00;
108
 
80 pfowler 109
  /*
110
  DDR : 1 = Output, 0 = Input
111
  PORT: 1 = Pullup for Input, otherwise set output
112
  PIN : Read input pin
113
  */
114
 
115
  /*
85 pfowler 116
        PB0     - Output                - LED 3
117
        PB1     - Output                - LED 4
118
        PB2     - Output                - LED 5
119
        PB3     - Output                - LED 6
120
        PB4     - Output                - LED 7
121
        PB5     - 
122
        PB6     - 
123
        PB7     - 
80 pfowler 124
  */
85 pfowler 125
  DDRB          = 0B00011111;
80 pfowler 126
  PORTB         = 0B00000000;
85 pfowler 127
 
80 pfowler 128
  /*
85 pfowler 129
        PC0     - Output		- ButtonPad Gnd0
130
        PC1     - Output		- ButtonPad Gnd1
131
        PC2     - Input, Pullup		- ButtonPad 0
132
        PC3     - Input, Pullup		- ButtonPad 1
133
        PC4     - Input, Pullup		- ButtonPad 2
134
        PC5     - Input, Pullup		- ButtonPad 3
80 pfowler 135
  */
86 pfowler 136
  DDRC          = 0B00000011;
137
  PORTC         = 0B00111111;
80 pfowler 138
 
85 pfowler 139
  /*
140
        PD0     - 
141
        PD1     - 
142
        PD2     - 
143
        PD3     - 
144
        PD4     - 
145
        PD5     - Output		- LED 0
146
        PD6     - Output		- LED 1
147
        PD7     - Output		- LED 2
148
  */
149
  DDRD          = 0B11100000;
150
  PORTD         = 0B00000000;
151
 
86 pfowler 152
	setLeds(0xff);
80 pfowler 153
 
79 pfowler 154
    usbDeviceDisconnect();
86 pfowler 155
    _delay_ms(500);
79 pfowler 156
    usbDeviceConnect();
157
 
158
 
85 pfowler 159
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
79 pfowler 160
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
85 pfowler 161
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
79 pfowler 162
 
86 pfowler 163
    wdt_enable(WDTO_1S);
79 pfowler 164
    usbInit();
165
    sei();
166
 
86 pfowler 167
    reportBuffer.axis[0] = 0xfffd;
168
    reportBuffer.axis[1] = 0xfffd;
169
    reportBuffer.buttons = 0x00;
79 pfowler 170
 
86 pfowler 171
	setLeds(currleds);
80 pfowler 172
 
86 pfowler 173
    for(;;){
79 pfowler 174
        wdt_reset();
175
        usbPoll();
176
 
86 pfowler 177
	uint8_t pressed = getKey();
90 pfowler 178
	//doButtons(pressed);	
179
	setLeds(currleds);
80 pfowler 180
 
79 pfowler 181
        if(usbInterruptIsReady()){ 
86 pfowler 182
                reportBuffer.buttons = pressed;
79 pfowler 183
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
184
        }
84 pfowler 185
 
79 pfowler 186
    }
86 pfowler 187
 
79 pfowler 188
    return 0;
189
}
190
 
85 pfowler 191
inline void setLeds(uint8_t leds) {
86 pfowler 192
	PORTB &= 0xE0;
85 pfowler 193
        PORTB |= (0x1F & (leds >> 3));
86 pfowler 194
	PORTD &= 0x1F;
85 pfowler 195
        PORTD |= (0xE0 & (leds << 5));
196
}
197
 
86 pfowler 198
void doButtons(uint8_t pressed) {
79 pfowler 199
 
84 pfowler 200
        // Deboucing
201
        // When i key first goes down, wait 5 ms, check it again to see if its still down
202
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
203
                debounce.buttons = pressed;
204
                debounce.timer = 5;
79 pfowler 205
        }
206
 
84 pfowler 207
        // The key has come up
208
        if (pressed != debounce.buttons) {
209
                debounce.buttons = 0;
210
                debounce.timer = 0;
211
                debounce.waitup = 0;
212
        }
79 pfowler 213
 
84 pfowler 214
        // Debounce timer is up, process our button
215
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
216
                uint8_t i = 0;
217
                for (i=0; i<=7; i++) {
218
                        // Button pressed and the led is currently on
219
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
86 pfowler 220
                                if ( i == 0 && rbi(currleds, 1) != 1)  //Dont turn off com1 if no comm2
84 pfowler 221
                                        break;
79 pfowler 222
 
86 pfowler 223
                                if ( i == 1 && rbi(currleds, 0) != 1)  //Dont turn off com2 if no comm1
84 pfowler 224
                                        break;
225
 
226
                                cbi(currleds, i);
227
                        // Button is pressed and led is currently off
228
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
86 pfowler 229
                                if ( i == 0 && rbi(currleds, 1) == 1)  //Turn on comm2, turn off comm1
230
                                        cbi(currleds,1);
84 pfowler 231
 
86 pfowler 232
                                if ( i == 1 && rbi(currleds, 0) == 1)  //Turn on comm1, turn off comm2
233
                                        cbi(currleds,0);
84 pfowler 234
 
235
                                sbi(currleds, i);
236
                        }
237
                }
85 pfowler 238
                setLeds(currleds);
84 pfowler 239
                debounce.waitup = 1;
240
        }
79 pfowler 241
}
242
 
85 pfowler 243
// Gnd = PC0, PC1
244
// Btn = PC2, PC3, PC4, PC5
84 pfowler 245
uint8_t getKey() {
246
        uint8_t key = 0;
247
 
86 pfowler 248
        cbi(PORTC, 1);
84 pfowler 249
        _delay_us(10);        // Wait for the port change
85 pfowler 250
        if (rbi(PINC, 2) == 0) key = 1;
251
        if (rbi(PINC, 3) == 0) key = 2;
252
        if (rbi(PINC, 4) == 0) key = 4;
253
        if (rbi(PINC, 5) == 0) key = 8;
86 pfowler 254
        sbi(PORTC, 1);
84 pfowler 255
 
86 pfowler 256
        cbi(PORTC, 0);
84 pfowler 257
        _delay_us(10);
85 pfowler 258
        if (rbi(PINC, 2) == 0) key = 16;
259
        if (rbi(PINC, 3) == 0) key = 32;
260
        if (rbi(PINC, 4) == 0) key = 64;
261
        if (rbi(PINC, 5) == 0) key = 128;
86 pfowler 262
        sbi(PORTC, 0);
84 pfowler 263
 
264
        return key;
79 pfowler 265
}
266
 
84 pfowler 267
 
79 pfowler 268
ISR(TIMER0_OVF_vect) {
269
        tmr0_ovf++;
84 pfowler 270
 
87 pfowler 271
	// Clk/1 TCCR0B = (1<< CS00);
272
	//20.0Mhz, 1ms = 78ovf
273
	//16.5Mhz, 1ms = 64ovf
85 pfowler 274
	//12.0Mhz, 1ms = 46ovf
84 pfowler 275
 
87 pfowler 276
        if (tmr0_ovf>=78) {
79 pfowler 277
                systime++;
278
                tmr0_ovf = 0;
84 pfowler 279
 
280
		if (debounce.timer != 0)
281
			debounce.timer--;
79 pfowler 282
        }
283
}