Subversion Repositories group.electronics

Rev

Rev 24 | Rev 27 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 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
 
17
void doInt(void);
24 pfowler 18
uint8_t getKey(void);
21 pfowler 19
 
20
volatile uint8_t pcIntCurr = 0;
21
volatile uint8_t pcIntLast = 0;
22
volatile uint8_t pcIntMask = 0;
23
 
22 pfowler 24
volatile uint8_t rot_stat = 0;
23 pfowler 25
volatile uint8_t rot_sent = 0;
21 pfowler 26
 
27
struct{
28
  union {
26 pfowler 29
    uint8_t data[5];	// Rotaries
21 pfowler 30
    struct {
26 pfowler 31
	uint8_t rx:8;
32
	uint8_t ry:8;
33
	uint8_t b00:1;
34
	uint8_t b01:1;
35
	uint8_t b02:1;
36
	uint8_t b03:1;
37
	uint8_t b04:1;
38
	uint8_t b05:1;
39
	uint8_t b06:1;
40
	uint8_t b07:1;
41
	uint8_t b08:1;
42
	uint8_t b09:1;
43
	uint8_t b10:1;
44
	uint8_t b11:1;
45
	uint8_t b12:1;
46
	uint8_t b13:1;
47
	uint8_t b14:1;
48
	uint8_t b15:1;
21 pfowler 49
    };
50
  };
51
} report;
52
 
53
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
54
  usbRequest_t *rq = (void *)data;
55
 
56
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
57
        if(rq->bRequest == USBRQ_HID_GET_REPORT) {  
58
            return sizeof(report);
59
        } else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
60
            return 1;
61
        } 
62
    }
63
 
64
  return 0;
65
}
66
 
67
void hadUsbReset(void) {
68
}
69
 
70
int main(void) {
71
 
72
  ACSR |= (1<<ACD); // Disable analog comparator
73
 
23 pfowler 74
  /*
75
  DDR : 1 = Output, 0 = Input
76
  PORT: 1 = Pullup for Input, otherwise set output
77
  PIN : Read input pin
78
  */
79
 
80
  /*
24 pfowler 81
	PB0	- Input	 		- Keypad 5
82
	PB1	- Input	 		- Keypad 6
83
	PB2	- Input	 		- Keypad 7
84
	PB3	- Input	 		- Keypad 8
85
	PB4	- Input, Pullup		- Function select
23 pfowler 86
  */
24 pfowler 87
  DDRB		= 0B00000000;
88
  PORTB 	= 0B00010000;
21 pfowler 89
 
23 pfowler 90
  /*
24 pfowler 91
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
92
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
93
 
94
 
95
	PD4	- Output			- Keypad 1
96
	PD5	- Output			- Keypad 2
97
	PD6	- Output			- Keypad 3
98
	PD7	- Output			- Keypad 4
23 pfowler 99
  */
24 pfowler 100
  DDRD		= 0B11110000;
101
  PORTD		= 0B00000011;
21 pfowler 102
 
24 pfowler 103
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
104
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
21 pfowler 105
 
23 pfowler 106
  // Timers not used for the moment
107
  // Setup timer0 - Enable overflow, 8 times prescaler
108
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
109
  //TCNT0 = 0x00;				// Set Timer0 to 0
110
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
21 pfowler 111
 
112
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
113
  _delay_ms(500);
114
  usbDeviceConnect();
115
 
116
  wdt_enable(WDTO_1S);
117
  usbInit();
118
  sei();
119
 
120
 
121
  for(;;) {
122
    wdt_reset();
123
    usbPoll();
124
 
125
    if(usbInterruptIsReady()){
126
 
26 pfowler 127
	report.data[2] |= (1 << getKey());
21 pfowler 128
 
23 pfowler 129
	// Now work out what rotary to send, if any
130
	// Also record if we sent a positive response, 
131
	//  so we can send a '0' next time (if selected on PD4)
26 pfowler 132
	/*
23 pfowler 133
	if (rot_stat == 0x01 && rot_sent == 0) {
24 pfowler 134
		report.rot1a = 1;
23 pfowler 135
		rot_sent = 1;
136
	} else if (rot_stat == 0x02 && rot_sent == 0) {
24 pfowler 137
		report.rot1b = 1;
23 pfowler 138
		rot_sent = 1;
22 pfowler 139
	} else {
23 pfowler 140
		rot_sent = 0;
26 pfowler 141
	}*/
22 pfowler 142
 
23 pfowler 143
	// Reset our stat so ready for next turn
22 pfowler 144
	rot_stat = 0;
145
 
23 pfowler 146
	// If our function select is set, dont bother
147
	//  sending a 'o' between consequtive 1's.
24 pfowler 148
	if (rbi(PINB, PB4))
23 pfowler 149
		rot_sent = 0;
150
 
21 pfowler 151
      /* called after every poll of the interrupt endpoint */
152
      usbSetInterrupt(&report, sizeof(report));
153
    }
154
  }
155
}
156
 
24 pfowler 157
uint8_t getKey() {
158
	uint8_t col, row = 0;
159
	uint8_t key = 0;
160
	uint8_t n = 0;
161
 
162
	for (col=4; col<=7; col++) {
163
		sbi(PORTD, col);
164
		for (row=0; row<=3; row++); {
165
			if (rbi(PINB, row))
166
				key = n;
167
			n++;
168
		}
169
		cbi(PORTD, col);
170
	}
171
	return key;
172
}
173
 
21 pfowler 174
void doInt() {
23 pfowler 175
	// If rot_stat is not 0, we havn't sent
176
	//  our last results yet. Skip this click.
22 pfowler 177
	if (rot_stat != 0) {
178
		pcIntMask = 0;
179
		return;
180
	}
23 pfowler 181
 
182
	// Check which pin caused the interrupt. If they both
183
	//  equal 0, the pin that interrupted is the direction
24 pfowler 184
  	if (rbi(pcIntCurr, PCINT17) == 0 
185
		&& rbi(pcIntCurr, PCINT17) == 0 
186
		&& rbi(pcIntMask, PCINT16) ) {
23 pfowler 187
			rot_stat = 1;
24 pfowler 188
  	} else if (rbi(pcIntCurr, PCINT16) == 0 
189
		&& rbi(pcIntCurr, PCINT17) == 0 
190
		&& rbi(pcIntMask, PCINT17) ) {
23 pfowler 191
			rot_stat = 2;
192
  	}
193
 
194
	// Clear the mask so we know we've delth with it
195
	pcIntMask = 0;
21 pfowler 196
}
197
 
23 pfowler 198
/* Not used for the moment
21 pfowler 199
ISR(TIMER0_OVF_vect) {
200
	timer0_ovf++;
201
}
23 pfowler 202
*/
21 pfowler 203
 
24 pfowler 204
ISR(PCINT2_vect)
21 pfowler 205
{
23 pfowler 206
	// Save the state and work out which pin caused
207
	//  the interrupt to occur
24 pfowler 208
	pcIntCurr = PIND;
23 pfowler 209
	pcIntMask = pcIntCurr ^ pcIntLast;
210
	pcIntLast = pcIntCurr;
211
	doInt();
21 pfowler 212
}
213