Subversion Repositories group.electronics

Rev

Rev 18 | 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
19 pfowler 65
  DDRB		= 0B00000001;
66
  PORTB 	= 0B00000110;
16 pfowler 67
 
19 pfowler 68
  DDRD		= 0B00000000;
69
  PORTD		= 0B01100000;
16 pfowler 70
 
19 pfowler 71
  //PORTB |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //turn on pullups
72
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
73
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
18 pfowler 74
 
16 pfowler 75
  // Setup timer0
19 pfowler 76
  TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
16 pfowler 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;
19 pfowler 99
		cbi(PORTB, PB0);
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++;
19 pfowler 114
*/
115
	if (bit_is_clear(PIND, PD5))
116
		report.A = 1;
7 pfowler 117
 
19 pfowler 118
	if (bit_is_clear(PIND, PD6))
16 pfowler 119
		report.B = 1;
7 pfowler 120
 
121
      /* called after every poll of the interrupt endpoint */
16 pfowler 122
      usbSetInterrupt(&report, sizeof(report));
7 pfowler 123
    }
16 pfowler 124
  }
125
}
7 pfowler 126
 
16 pfowler 127
void doInt() {
19 pfowler 128
  if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT1) ) {
18 pfowler 129
	time_rot = 60;
16 pfowler 130
	report.rot1 = 1;
131
	report.rot2 = 0;
19 pfowler 132
	sbi(PORTB, PB0);
133
  } else if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT2) ) {
18 pfowler 134
	time_rot = 60;
16 pfowler 135
	report.rot1 = 0;
136
	report.rot2 = 1;
19 pfowler 137
	sbi(PORTB, PB0);
7 pfowler 138
  }
139
 
16 pfowler 140
  pcIntMask = 0;
7 pfowler 141
}
16 pfowler 142
 
143
ISR(TIMER0_OVF_vect) {
144
	timer0_ovf++;
145
 
146
	if (time_rot) {
147
		time_rot--;
148
	}
149
}
150
 
19 pfowler 151
ISR(PCINT0_vect)
16 pfowler 152
{
153
  pcIntCurr = PINB;
154
  pcIntMask = pcIntCurr ^ pcIntLast;
155
  pcIntLast = pcIntCurr;
18 pfowler 156
  doInt();
16 pfowler 157
}
158