Subversion Repositories group.electronics

Rev

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