Subversion Repositories group.electronics

Rev

Rev 16 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 pfowler 1
#include <avr/io.h>
2
#include <avr/pgmspace.h>
3
#include <avr/interrupt.h>
4
 
5
#define F_CPU 12000000
6
#include <util/delay.h>
7
#include <avr/wdt.h>
8
#include <avr/eeprom.h>
9
#include <usbdrv.h>
10
 
11
#include <stdlib.h>
12
#include <string.h>
13
 
14
#include "config.h"
15
#include "hiddesc.h"
16
 
17
struct{
18
  union {
19
    uint8_t data;
20
    struct {
21
      uint8_t X:2;
22
      uint8_t Y:2;
23
      uint8_t B:1;
24
      uint8_t A:1;
25
      uint8_t ROT1:1;
26
      uint8_t ROT2:1;
27
    };
28
  };
29
} reportBuffer;
30
 
31
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
32
  usbRequest_t *rq = (void *)data;
33
 
34
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
35
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
36
            return sizeof(reportBuffer);
37
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
38
            return 1;
39
        } 
40
    }
41
 
42
  return 0;
43
}
44
 
45
void hadUsbReset(void) {
46
 
47
}
48
 
49
int main(void) {
50
 
51
  ACSR |= (1<<ACD); // Disable analog comparator
52
 
53
        sbi(DDRD, PD6);
54
        sbi(PORTD, PD6);
55
 
56
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
57
  _delay_ms(500);
58
  usbDeviceConnect();
59
 
60
  wdt_enable(WDTO_1S);
61
  usbInit();
62
  sei();
63
 
64
	// Setup the joystick ports 
65
        cbi (DDRB, PB0);
66
        sbi (PORTB, PB0);
67
        cbi (DDRB, PB1);
68
        sbi (PORTB, PB1);
69
        cbi (DDRB, PB2);
70
        sbi (PORTB, PB2);
71
        cbi (DDRB, PB3);
72
        sbi (PORTB, PB3);
73
        cbi (DDRB, PB4);
74
        sbi (PORTB, PB4);
75
 
76
        // Setup the buttons
77
        cbi (DDRD, PD4);
78
        sbi (PORTD, PD4);
79
        cbi (DDRD, PD5);
80
        sbi (PORTD, PD5);
81
 
82
	cbi(PORTD, PD6);
83
 
84
  for(;;) {
85
    wdt_reset();
86
    usbPoll();
87
 
88
        cbi (PORTD, PD6);
89
        if (bit_is_clear(PIND, PD5))
90
                sbi (PORTD, PD6);
91
 
92
    if(usbInterruptIsReady()){
93
 
94
      reportBuffer.data = 0x05; // Center pad, little endian
95
 
96
	if (bit_is_clear(PINB, PB0)) 
97
		reportBuffer.X++;
98
	if (bit_is_clear(PINB, PB1))
99
		reportBuffer.Y--;
100
	if (bit_is_clear(PINB, PB2))
101
		reportBuffer.X--;
102
	if (bit_is_clear(PINB, PB3))
103
		reportBuffer.Y++;
104
 
105
	if (bit_is_clear(PIND, PD4))
106
		reportBuffer.A = 1;
107
	if (bit_is_clear(PIND, PD5))
108
		reportBuffer.B = 1;
109
 
110
      /* called after every poll of the interrupt endpoint */
111
      usbSetInterrupt(&reportBuffer, sizeof(reportBuffer));
112
 
113
    }
114
 
115
  }
116
 
117
}