Subversion Repositories group.NITPanels

Rev

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

Rev 17 Rev 18
Line 9... Line 9...
9
 
9
 
10
#ifndef NULL
10
#ifndef NULL
11
#define NULL    ((void *)0)
11
#define NULL    ((void *)0)
12
#endif
12
#endif
13
 
13
 
14
#define USB_GET_STATE  100
14
#define USB_GET_STATE  		100
15
#define USB_LEDS_SET  101
15
#define USB_LEDS_SET  		101
-
 
16
#define USB_GET_ANALOG 		102
-
 
17
#define USB_SET_POWER_LED	103
16
 
18
 
17
/* ------------------------------------------------------------------------- */
19
/* ------------------------------------------------------------------------- */
18
 
20
 
19
uint8_t getKey(void);
21
uint8_t getKey(void);
20
void doButtons(uint8_t);
-
 
21
inline void setLeds(uint8_t);
22
inline void setLeds();
22
 
23
 
23
struct {
-
 
24
	int16_t axis[2];
-
 
25
	uint8_t buttons;
-
 
26
} reportBuffer;
-
 
27
 
-
 
28
 
-
 
29
volatile struct {
24
void analogInit();
30
	uint8_t buttons;
25
uint8_t analogRead(uint8_t channel);
31
	uint8_t waitup;
-
 
32
	uint8_t timer;
-
 
33
} debounce;
-
 
34
 
26
 
-
 
27
/*
35
volatile struct {
28
volatile struct {
36
	uint8_t data;
29
	uint8_t data;
37
	union {
30
	union {
38
		uint8_t led_method:1;
31
		uint8_t led_method:1;
39
		uint8_t other:1;
32
		uint8_t other:1;
40
		uint8_t reserved:6;
33
		uint8_t reserved:6;
41
	};
34
	};
42
} config;
35
} config;*/
43
 
36
 
44
static uchar usbReplyBuf[16];
37
static uchar usbReplyBuf[16];
45
 
38
 
-
 
39
volatile uint8_t pwrLed = 0;
46
volatile uint8_t currLeds = 0;
40
volatile uint8_t currLeds = 0;
47
volatile uint8_t currKeys = 0;
41
volatile uint8_t currKeys = 0;
-
 
42
volatile uint8_t analogPin = 0;
48
volatile uint8_t tmr0_ovf = 0;
43
volatile uint8_t tmr0_ovf = 0;
49
volatile uint32_t systime = 0;
44
volatile uint32_t systime = 0;
50
 
45
 
51
/* ------------------------------------------------------------------------- */
46
/* ------------------------------------------------------------------------- */
52
 
47
 
Line 65... Line 60...
65
			usbMsgPtr = usbReplyBuf;
60
			usbMsgPtr = usbReplyBuf;
66
			return sizeof(usbReplyBuf);
61
			return sizeof(usbReplyBuf);
67
		case USB_LEDS_SET:
62
		case USB_LEDS_SET:
68
			currLeds = rq->wValue.bytes[0];
63
			currLeds = rq->wValue.bytes[0];
69
			return 0;
64
			return 0;
70
			
65
		case USB_GET_ANALOG:
-
 
66
			usbReplyBuf[0] = (analogPin);
-
 
67
			usbMsgPtr = usbReplyBuf;
-
 
68
			return sizeof(usbReplyBuf);
-
 
69
		case USB_SET_POWER_LED:
-
 
70
			pwrLed = rq->wValue.bytes[0];
71
        }
71
        }
72
 
72
 
73
	return 0;
73
	return 0;
74
}
74
}
75
 
75
 
Line 82... Line 82...
82
  PORT: 1 = Pullup for Input, otherwise set output
82
  PORT: 1 = Pullup for Input, otherwise set output
83
  PIN : Read input pin
83
  PIN : Read input pin
84
  */
84
  */
85
 
85
 
86
  /*
86
  /*
87
        PB0     - Output                - LED 3
87
        PB0     - Output            - LED 3
88
        PB1     - Output                - LED 4
88
        PB1     - Output            - LED 4
89
        PB2     - Output                - LED 5
89
        PB2     - Output            - LED 5
90
        PB3     - Output                - LED 6
90
        PB3     - Output            - LED 6
91
        PB4     - Output                - LED 7
91
        PB4     - Output            - LED 7
92
        PB5     - 
92
        PB5     - Input				- Config Switch 1
93
        PB6     - 
93
        PB6     - Osc
94
        PB7     - 
94
        PB7     - Osc
95
  */
95
  */
96
  DDRB          = 0B00011111;
96
  DDRB          = 0B00011111;
97
  PORTB         = 0B00000000;
97
  PORTB         = 0B00000000;
98
 
98
 
99
  /*
99
  /*
100
        PC0     - Output		- ButtonPad Gnd0
100
        PC0     - Input, ADC		- Potentiometer
101
        PC1     - Output		- ButtonPad Gnd1
101
        PC1     - Output			- Power LED
102
        PC2     - Input, Pullup		- ButtonPad 0
102
        PC2     - Input, Pullup		- ButtonPad 0
103
        PC3     - Input, Pullup		- ButtonPad 1
103
        PC3     - Input, Pullup		- ButtonPad 1
104
        PC4     - Input, Pullup		- ButtonPad 2
104
        PC4     - Input, Pullup		- ButtonPad 2
105
        PC5     - Input, Pullup		- ButtonPad 3
105
        PC5     - Input, Pullup		- ButtonPad 3
-
 
106
        PC6		- Reset
106
  */
107
  */
107
  DDRC          = 0B00000011;
108
  DDRC          = 0B00000010;
108
  PORTC         = 0B00111111;
109
  PORTC         = 0B00111100;
109
 
110
 
110
  /*
111
  /*
111
        PD0     - 
112
        PD0     - Output		- ButtonPad Gnd0
112
        PD1     - 
113
        PD1     - Output		- ButtonPad Gnd1
113
        PD2     - 
114
        PD2     - USB D+
114
        PD3     - 
115
        PD3     - USB D-
115
        PD4     - 
116
        PD4     - Input			- Config Switch 2
116
        PD5     - Output		- LED 0
117
        PD5     - Output		- LED 0
117
        PD6     - Output		- LED 1
118
        PD6     - Output		- LED 1
118
        PD7     - Output		- LED 2
119
        PD7     - Output		- LED 2
119
  */
120
  */
120
  DDRD          = 0B11100000;
121
  DDRD          = 0B11100011;
121
  PORTD         = 0B00000000;
122
  PORTD         = 0B00000011;
122
 
-
 
123
	setLeds(0xff);
-
 
124
 
123
 
-
 
124
	// The USB connect stuff, flash the power led
-
 
125
  	sbi(PORTC, PC0);
125
    usbDeviceDisconnect();
126
    usbDeviceDisconnect();
126
    _delay_ms(500);
127
    _delay_ms(500);
127
    usbDeviceConnect();
128
    usbDeviceConnect();
-
 
129
    cbi(PORTC, PC0);
128
 
130
 
-
 
131
    // @TODO: Don't really need the system time anymore, remove?
-
 
132
    TIMSK0 = (1<<TOIE0);            // Enable timer overflow
-
 
133
    TCNT0 = 0x00;                   // Set Timer0 initial value to 0
-
 
134
    TCCR0B = (1<< CS00) ;           // /1 prescaler
129
 
135
 
130
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
-
 
131
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
-
 
132
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
-
 
133
 
-
 
134
    wdt_enable(WDTO_1S);
136
    wdt_enable(WDTO_1S);			// WatchDog at 1s
135
    usbInit();
137
    usbInit();						// Init the USB port
136
    sei();
138
    sei();							// Enable interrupts
137
 
139
 
138
	config.led_method = 1;
-
 
139
	setLeds(currLeds);
140
	setLeds();						// Set default LED status
140
 
141
 
-
 
142
	analogInit();					// Init the ADC for PC0 (ADC0)
141
 
143
 
142
    for(;;){
144
    for(;;){
143
        wdt_reset();
145
        wdt_reset();
144
        usbPoll();
-
 
145
 
-
 
146
	currKeys = getKey();
-
 
147
 
-
 
148
	if (config.led_method != rbi(PORTB, 5)) {
146
        usbPoll();					// Poll USB port (Every ~10ms!)
149
		// Reset the leds when switcing	to buttons
147
        currKeys = getKey();		// Check the button presses
150
		if (config.led_method == 0) currLeds = 1;
148
        analogPin = analogRead(0);	// Read the pot (VolControl)
151
		config.led_method = rbi(PORTB, 5);
149
		setLeds();					// Set the leds
152
	}
-
 
153
 
-
 
154
	if (config.led_method == 1) {
150
									// @TODO: Only do this on change
155
		uint8_t pressed = getKey();
-
 
156
		doButtons(pressed);
-
 
157
	}
-
 
158
 
-
 
159
	setLeds(currLeds);
-
 
160
 
-
 
161
 
-
 
162
    }
151
    }
163
 
152
 
164
    return 0;
153
    return 0;
165
}
154
}
166
 
155
 
167
inline void setLeds(uint8_t leds) {
156
inline void setLeds() {
-
 
157
	// Set the status leds.
-
 
158
	// @TODO: Verify that this doesn't mess with other port functions
168
	PORTB &= 0xE0;
159
	PORTB &= 0xE0;
169
        PORTB |= (0x1F & (leds >> 3));
160
	PORTB |= (0x1F & (currLeds >> 3));
170
	PORTD &= 0x1F;
161
	PORTD &= 0x1F;
171
        PORTD |= (0xE0 & (leds << 5));
162
	PORTD |= (0xE0 & (currLeds << 5));
172
}
-
 
173
 
-
 
174
void doButtons(uint8_t pressed) {
-
 
175
 
-
 
176
        // Deboucing
-
 
177
        // When i key first goes down, wait 5 ms, check it again to see if its still down
-
 
178
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
-
 
179
                debounce.buttons = pressed;
-
 
180
                debounce.timer = 5;
-
 
181
        }
-
 
182
 
-
 
183
        // The key has come up
-
 
184
        if (pressed != debounce.buttons) {
-
 
185
                debounce.buttons = 0;
-
 
186
                debounce.timer = 0;
-
 
187
                debounce.waitup = 0;
-
 
188
        }
-
 
189
 
163
 
190
        // Debounce timer is up, process our button
164
	// Set the power led if required
191
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
-
 
192
                uint8_t i = 0;
165
	// @TODO: Check this works
193
                for (i=0; i<=7; i++) {
166
	if (pwrLed)
194
                        // Button pressed and the led is currently on
-
 
195
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currLeds, i) == 1 ) {
-
 
196
                                if ( i == 0 && rbi(currLeds, 1) != 1)  //Dont turn off com1 if no comm2
-
 
197
                                        break;
-
 
198
 
-
 
199
                                if ( i == 1 && rbi(currLeds, 0) != 1)  //Dont turn off com2 if no comm1
-
 
200
                                        break;
-
 
201
 
-
 
202
                                cbi(currLeds, i);
167
		sbi(PORTC, PC1);
203
                        // Button is pressed and led is currently off
-
 
204
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currLeds, i) == 0 ) {
-
 
205
                                if ( i == 0 && rbi(currLeds, 1) == 1)  //Turn on comm2, turn off comm1
-
 
206
                                        cbi(currLeds,1);
-
 
207
 
168
	else
208
                                if ( i == 1 && rbi(currLeds, 0) == 1)  //Turn on comm1, turn off comm2
-
 
209
                                        cbi(currLeds,0);
-
 
210
 
-
 
211
                                sbi(currLeds, i);
-
 
212
                        }
-
 
213
                }
169
		cbi(PORTC, PC1);
214
                debounce.waitup = 1;
-
 
215
        }
-
 
216
}
170
}
217
 
171
 
218
// Gnd = PC0, PC1
172
// Gnd = PD0, PD1
219
// Btn = PC2, PC3, PC4, PC5
173
// Btn = PC2, PC3, PC4, PC5
220
uint8_t getKey() {
174
uint8_t getKey() {
221
        uint8_t key = 0;
175
        uint8_t key = 0;
222
 
176
 
223
        cbi(PORTC, 1);
177
        cbi(PORTD, PD1);		// Read the first row of buttons
224
        _delay_us(10);        // Wait for the port change
178
        _delay_us(10);			// Wait for the port change
225
        if (rbi(PINC, 2) == 0) key = 1;
179
        if (rbi(PINC, PC2) == 0) key = 1;
226
        if (rbi(PINC, 3) == 0) key = 2;
180
        if (rbi(PINC, PC3) == 0) key = 2;
227
        if (rbi(PINC, 4) == 0) key = 4;
181
        if (rbi(PINC, PC4) == 0) key = 4;
228
        if (rbi(PINC, 5) == 0) key = 8;
182
        if (rbi(PINC, PC5) == 0) key = 8;
229
        sbi(PORTC, 1);
183
        sbi(PORTD, PD1);
230
 
184
 
231
        cbi(PORTC, 0);
185
        cbi(PORTD, PD0);		// Read the second row of buttons
232
        _delay_us(10);
186
        _delay_us(10);
233
        if (rbi(PINC, 2) == 0) key = 16;
187
        if (rbi(PINC, PC2) == 0) key = 16;
234
        if (rbi(PINC, 3) == 0) key = 32;
188
        if (rbi(PINC, PC3) == 0) key = 32;
235
        if (rbi(PINC, 4) == 0) key = 64;
189
        if (rbi(PINC, PC4) == 0) key = 64;
236
        if (rbi(PINC, 5) == 0) key = 128;
190
        if (rbi(PINC, PC5) == 0) key = 128;
237
        sbi(PORTC, 0);
191
        sbi(PORTD, PD0);
238
 
192
 
239
        return key;
193
        return key;
240
}
194
}
241
 
195
 
-
 
196
uint8_t analogRead(uint8_t channel) {
-
 
197
        ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (channel & 0x0f);
-
 
198
        ADCSRA |= (1<<ADSC);            	// Start converting
-
 
199
 
-
 
200
        while (((ADCSRA >> ADSC) & 1)) {}   //Wait until conversion finished
-
 
201
        uint8_t result = ADCH;
-
 
202
        //ADCSRA |= (0<<ADSC);          	// Stop converting
-
 
203
 
-
 
204
        return result;
-
 
205
}
-
 
206
 
-
 
207
void analogInit() {
-
 
208
	ACSR |= (1<<ACD); // Disable analog comparator
-
 
209
 
-
 
210
	/*
-
 
211
        Setup ADC
-
 
212
        ADMUX: 8 bit mode, Avcc ref
-
 
213
        ADCSRA: Enable, 128 prescale
-
 
214
	*/
-
 
215
	ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
-
 
216
	ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
-
 
217
}
242
 
218
 
243
ISR(TIMER0_OVF_vect) {
219
ISR(TIMER0_OVF_vect) {
244
        tmr0_ovf++;
220
        tmr0_ovf++;
245
 
221
 
246
	// Clk/1 TCCR0B = (1<< CS00);
222
	// Clk/1 TCCR0B = (1<< CS00);
Line 249... Line 225...
249
	//12.0Mhz, 1ms = 46ovf
225
	//12.0Mhz, 1ms = 46ovf
250
 
226
 
251
        if (tmr0_ovf>=78) {
227
        if (tmr0_ovf>=78) {
252
                systime++;
228
                systime++;
253
                tmr0_ovf = 0;
229
                tmr0_ovf = 0;
254
 
-
 
255
		if (debounce.timer != 0)
-
 
256
			debounce.timer--;
-
 
257
 
-
 
258
        }
230
        }
259
}
231
}