Subversion Repositories group.electronics

Rev

Rev 7 | Rev 18 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7 Rev 16
Line 12... Line 12...
12
#include <string.h>
12
#include <string.h>
13
 
13
 
14
#include "config.h"
14
#include "config.h"
15
#include "hiddesc.h"
15
#include "hiddesc.h"
16
 
16
 
-
 
17
#define	DEPRESS_TIME	1
-
 
18
 
-
 
19
void doInt(void);
-
 
20
 
-
 
21
volatile uint8_t pcIntCurr = 0;
-
 
22
volatile uint8_t pcIntLast = 0;
-
 
23
volatile uint8_t pcIntMask = 0;
-
 
24
 
-
 
25
volatile uint8_t timer0_ovf = 0;
-
 
26
 
-
 
27
uint8_t time_rot = 0;
-
 
28
 
17
struct{
29
struct{
18
  union {
30
  union {
19
    uint8_t data;
31
    uint8_t data;
20
    struct {
32
    struct {
21
      uint8_t X:2;
33
      uint8_t X:2;
22
      uint8_t Y:2;
34
      uint8_t Y:2;
23
      uint8_t B:1;
35
      uint8_t B:1;
24
      uint8_t A:1;
36
      uint8_t A:1;
25
      uint8_t ROT1:1;
37
      uint8_t rot1:1;
26
      uint8_t ROT2:1;
38
      uint8_t rot2:1;
27
    };
39
    };
28
  };
40
  };
29
} reportBuffer;
41
} report;
30
 
42
 
31
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
43
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
32
  usbRequest_t *rq = (void *)data;
44
  usbRequest_t *rq = (void *)data;
33
 
45
 
34
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
46
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
35
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
47
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
36
            return sizeof(reportBuffer);
48
            return sizeof(report);
37
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
49
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
38
            return 1;
50
            return 1;
39
        } 
51
        } 
40
    }
52
    }
41
 
53
 
42
  return 0;
54
  return 0;
43
}
55
}
44
 
56
 
45
void hadUsbReset(void) {
57
void hadUsbReset(void) {
46
 
-
 
47
}
58
}
48
 
59
 
49
int main(void) {
60
int main(void) {
50
 
61
 
51
  ACSR |= (1<<ACD); // Disable analog comparator
62
  ACSR |= (1<<ACD); // Disable analog comparator
52
 
63
 
53
        sbi(DDRD, PD6);
64
  // DDR : 1 = Output, 0 = Input
-
 
65
  // PORT: 1 = Pullup for Input, otherwise set output
54
        sbi(PORTD, PD6);
66
  // PIN : Read input pin
-
 
67
  DDRB		= 0B00011111;
-
 
68
  PORTB 	= 0B00011111;
-
 
69
 
-
 
70
  DDRD		= 0B00110000;
-
 
71
  PORTD		= 0B00110000;
-
 
72
 
-
 
73
  // Setup timer0
-
 
74
  TIMSK = (1<<TOIE0);			// Eable timer overflow for Timer0
-
 
75
  TCNT0 = 0x00;				// Set Timer0 to 0
-
 
76
  TCCR0B = (1<< CS02) | (1<<CS00);	// /1024 prescaler
55
 
77
 
56
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
78
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
57
  _delay_ms(500);
79
  _delay_ms(500);
58
  usbDeviceConnect();
80
  usbDeviceConnect();
59
 
81
 
60
  wdt_enable(WDTO_1S);
82
  wdt_enable(WDTO_1S);
61
  usbInit();
83
  usbInit();
62
  sei();
84
  sei();
63
 
85
 
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
 
86
 
84
  for(;;) {
87
  for(;;) {
85
    wdt_reset();
88
    wdt_reset();
86
    usbPoll();
89
    usbPoll();
87
 
90
 
88
        cbi (PORTD, PD6);
91
	if (pcIntMask);
-
 
92
		doInt();
-
 
93
 
89
        if (bit_is_clear(PIND, PD5))
94
	if (!time_rot) {
-
 
95
		report.rot1 = 0;
90
                sbi (PORTD, PD6);
96
		report.rot2 = 0;
-
 
97
	}	
91
 
98
 
92
    if(usbInterruptIsReady()){
99
    if(usbInterruptIsReady()){
93
      
-
 
94
      reportBuffer.data = 0x05; // Center pad, little endian
100
      report.data = 0x05; // Center pad, little endian
95
 
101
 
96
	if (bit_is_clear(PINB, PB0)) 
102
	if (bit_is_clear(PINB, PB0)) 
97
		reportBuffer.X++;
103
		report.X++;
98
	if (bit_is_clear(PINB, PB1))
104
	if (bit_is_clear(PINB, PB1))
99
		reportBuffer.Y--;
105
		report.Y--;
100
	if (bit_is_clear(PINB, PB2))
106
	if (bit_is_clear(PINB, PB2))
101
		reportBuffer.X--;
107
		report.X--;
102
	if (bit_is_clear(PINB, PB3))
108
	if (bit_is_clear(PINB, PB3))
103
		reportBuffer.Y++;
109
		report.Y++;
104
 
110
 
105
	if (bit_is_clear(PIND, PD4))
111
	if (bit_is_clear(PIND, PD4))
106
		reportBuffer.A = 1;
112
		report.A = 1;
107
	if (bit_is_clear(PIND, PD5))
113
	if (bit_is_clear(PIND, PD5))
108
		reportBuffer.B = 1;
114
		report.B = 1;
109
 
115
 
110
      /* called after every poll of the interrupt endpoint */
116
      /* called after every poll of the interrupt endpoint */
111
      usbSetInterrupt(&reportBuffer, sizeof(reportBuffer));
117
      usbSetInterrupt(&report, sizeof(report));
112
 
-
 
113
    }
118
    }
-
 
119
  }
-
 
120
}
114
 
121
 
-
 
122
void doInt() {
-
 
123
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
-
 
124
	time_rot = 5;
-
 
125
	report.rot1 = 1;
-
 
126
	report.rot2 = 0;
-
 
127
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
-
 
128
	time_rot = 5;
-
 
129
	report.rot1 = 0;
-
 
130
	report.rot2 = 1;
115
  }
131
  }
116
 
132
 
-
 
133
  pcIntMask = 0;
-
 
134
}
-
 
135
 
-
 
136
ISR(TIMER0_OVF_vect) {
-
 
137
	timer0_ovf++;
-
 
138
 
-
 
139
	if (time_rot) {
-
 
140
		time_rot--;
-
 
141
	}
117
}
142
}
-
 
143
 
-
 
144
ISR(PCINT_vect)
-
 
145
{
-
 
146
  pcIntCurr = PINB;
-
 
147
  pcIntMask = pcIntCurr ^ pcIntLast;
-
 
148
  pcIntLast = pcIntCurr;
-
 
149
}
-
 
150