Subversion Repositories group.electronics

Rev

Rev 30 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30 Rev 32
Line 11... Line 11...
11
#include <string.h>
11
#include <string.h>
12
 
12
 
13
#include "config.h"
13
#include "config.h"
14
#include "hiddesc.h"
14
#include "hiddesc.h"
15
 
15
 
-
 
16
#define ROTS_ATTACHED	2
-
 
17
#define STAT		0
-
 
18
#define SENT		1
-
 
19
 
16
 
20
 
17
void doInt(uint8_t pcint);
21
void doInt(uint8_t pcint);
18
uint8_t getKey(void);
22
uint8_t getKey(void);
19
uint8_t analogRead(uint8_t pin);
23
uint8_t analogRead(uint8_t pin);
20
 
24
 
21
volatile uint8_t pcIntCurr[3] = {0,0,0};
25
volatile uint8_t pcIntCurr[3] = {0,0,0};
22
volatile uint8_t pcIntLast[3] = {0,0,0};
26
volatile uint8_t pcIntLast[3] = {0,0,0};
23
volatile uint8_t pcIntMask[3] = {0,0,0};
27
volatile uint8_t pcIntMask[3] = {0,0,0};
24
 
28
 
25
volatile uint8_t rot_stat[2] = {0,0};
29
//volatile uint8_t rot_stat[2] = {0,0};
26
volatile uint8_t rot_sent[2] = {0,0};
30
//volatile uint8_t rot_sent[2] = {0,0};
-
 
31
// rotdata = [rot#][(stat|sent)]
-
 
32
volatile uint8_t rotdata[2][2] = { {0,0}, {0,0} };
27
 
33
 
28
struct{
34
struct{
29
  union {
35
  union {
30
    uint8_t data1[2];	// Rotaries
36
    uint8_t data1[2];	// Rotaries
31
    struct {
37
    struct {
Line 48... Line 54...
48
	uint8_t b07:1;
54
	uint8_t b07:1;
49
	uint8_t b08:1;
55
	uint8_t b08:1;
50
	uint8_t b09:1;
56
	uint8_t b09:1;
51
	uint8_t b10:1;
57
	uint8_t b10:1;
52
	uint8_t b11:1;
58
	uint8_t b11:1;
53
	uint8_t b12:1;
59
	uint8_t rot2a:1;
54
	uint8_t b13:1;
60
	uint8_t rot2b:1;
55
	uint8_t rot1a:1;
61
	uint8_t rot1a:1;
56
	uint8_t rot1b:1;
62
	uint8_t rot1b:1;
57
    };
63
    };
58
  };
64
  };
59
} report;
65
} report;
Line 144... Line 150...
144
		report.data2 |= (1 << (key -1));
150
		report.data2 |= (1 << (key -1));
145
 
151
 
146
	// Now work out what rotary to send, if any
152
	// Now work out what rotary to send, if any
147
	// Also record if we sent a positive response, 
153
	// Also record if we sent a positive response, 
148
	//  so we can send a '0' next time (if selected on PD4)
154
	//  so we can send a '0' next time (if selected on PD4)
-
 
155
	// rotdata = [rot#][(stat|sent)]
-
 
156
	uint8_t rot = 0;
-
 
157
	for (rot=0; rot<=(ROTS_ATTACHED - 1); rot++) {
-
 
158
	        if (rotdata[rot][STAT] == 0x01 && rotdata[rot][SENT] == 0) {
-
 
159
                	rotdata[rot][SENT] = 1;
-
 
160
			switch (rot) {
-
 
161
				case(0):	report.rot1a = 1; break;
-
 
162
				case(1):	report.rot2a = 1; break;
-
 
163
			}
-
 
164
	        } else if (rotdata[rot][STAT] == 0x02 && rotdata[rot][SENT] == 0) {
-
 
165
                	rotdata[rot][SENT] = 1;
-
 
166
                        switch (rot) {
-
 
167
                                case(0):      report.rot1b = 1; break;
-
 
168
                                case(1):      report.rot2b = 1; break;
-
 
169
                        }
-
 
170
	        } else {
-
 
171
        	        rotdata[rot][SENT] = 0;
-
 
172
	        }
-
 
173
		rotdata[rot][STAT] = 0;
-
 
174
 
-
 
175
	        if (rbi(PINB, PB4))
-
 
176
        	        rotdata[rot][SENT] = 0;
149
	
177
	}
-
 
178
 
-
 
179
/*	
150
	if (rot_stat[0] == 0x01 && rot_sent[0] == 0) {
180
	if (rot_stat[0] == 0x01 && rot_sent[0] == 0) {
151
		report.rot1a = 1;
181
		report.rot1a = 1;
152
		rot_sent[0] = 1;
182
		rot_sent[0] = 1;
153
	} else if (rot_stat[0] == 0x02 && rot_sent[0] == 0) {
183
	} else if (rot_stat[0] == 0x02 && rot_sent[0] == 0) {
154
		report.rot1b = 1;
184
		report.rot1b = 1;
Line 162... Line 192...
162
 
192
 
163
	// If our function select is set, dont bother
193
	// If our function select is set, dont bother
164
	//  sending a 'o' between consequtive 1's.
194
	//  sending a 'o' between consequtive 1's.
165
	if (rbi(PINB, PB4))
195
	if (rbi(PINB, PB4))
166
		rot_sent[0] = 0;
196
		rot_sent[0] = 0;
167
 
197
*/
168
	if (rbi(PINB, PB5))
-
 
169
		rot_sent[1] = 0;
-
 
170
 
198
 
171
      /* called after every poll of the interrupt endpoint */
199
      /* called after every poll of the interrupt endpoint */
172
      usbSetInterrupt(&report, sizeof(report));
200
      usbSetInterrupt(&report, sizeof(report));
173
    }
201
    }
174
  }
202
  }
175
}
203
}
176
 
204
 
177
uint8_t analogRead(uint8_t pin) {
205
uint8_t analogRead(uint8_t pin) {
178
	//ADMUX |= (1<<ADLAR) | (0<<REFS0) |(1<<0);
-
 
179
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
206
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
180
	ADCSRA |= (1<<ADSC);		// Start converting
207
	ADCSRA |= (1<<ADSC);		// Start converting
181
 
208
 
182
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
209
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
183
	uint8_t result = ADCH;
210
	uint8_t result = ADCH;
184
	//ADCSRA |= (0<<ADSC);		// Stop converting
211
	//ADCSRA |= (0<<ADSC);		// Stop converting
185
 
212
 
186
	// result = result * 5/255;		// wtf?
-
 
187
	return result;
213
	return result;
188
}
214
}
189
 
215
 
190
uint8_t getKey() {
216
uint8_t getKey() {
191
	uint8_t col, row = 0;
217
	uint8_t col, row = 0;
Line 215... Line 241...
215
 */
241
 */
216
void doInt(uint8_t pcint) {
242
void doInt(uint8_t pcint) {
217
 
243
 
218
	// Select what rotary we are dealing with
244
	// Select what rotary we are dealing with
219
	//   based on the pc interrupt that fired.
245
	//   based on the pc interrupt that fired.
220
	uint8_t rotnum = 0;
246
	uint8_t rot = 0;
221
	if (pcint == 1) 
247
	if (pcint == 1) 
222
		rotnum = 1;
248
		rot = 1;
223
 
249
 
224
	// If rot_stat is not 0, we havn't sent
250
	// If rot stat is not 0, we havn't sent
225
	//  our last results yet. Skip this click.
251
	//  our last results yet. Skip this click.
226
	if (rot_stat[rotnum] != 0) {
252
	if (rotdata[rot][STAT] != 0) {
227
		pcIntMask[pcint] = 0;
253
		pcIntMask[pcint] = 0;
228
		return;
254
		return;
229
	}
255
	}
230
	// Check which pin caused the interrupt. If they both
256
	// Check which pin caused the interrupt. If they both
231
	//  equal 0, the pin that interrupted is the direction
257
	//  equal 0, the pin that interrupted is the direction
232
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
258
  	if (rbi(pcIntCurr[pcint], PCINT17) == 0 
233
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
259
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
234
		&& rbi(pcIntMask[pcint], PCINT16) ) {
260
		&& rbi(pcIntMask[pcint], PCINT16) ) {
235
			rot_stat[rotnum] = 1;
261
			rotdata[rot][STAT] = 1;
236
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
262
  	} else if (rbi(pcIntCurr[pcint], PCINT16) == 0 
237
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
263
		&& rbi(pcIntCurr[pcint], PCINT17) == 0 
238
		&& rbi(pcIntMask[pcint], PCINT17) ) {
264
		&& rbi(pcIntMask[pcint], PCINT17) ) {
239
			rot_stat[rotnum] = 2;
265
			rotdata[rot][STAT] = 2;
240
  	}
266
  	}
241
 
267
 
242
	// Clear the mask so we know we've delth with it
268
	// Clear the mask so we know we've delth with it
243
	pcIntMask[pcint] = 0;
269
	pcIntMask[pcint] = 0;
244
}
270
}