Subversion Repositories group.electronics

Rev

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