Subversion Repositories group.electronics

Rev

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

Rev Author Line No. Line
79 pfowler 1
/* Name: main.c
2
 * Project: EasyLogger
3
 * Author: Christian Starkjohann
4
 * Creation Date: 2006-04-23
5
 * Tabsize: 4
6
 * Copyright: (c) 2006 by OBJECTIVE DEVELOPMENT Software GmbH
7
 * License: Proprietary, free under certain conditions. See Documentation.
8
 * This Revision: $Id$
9
 */
10
 
11
#include <avr/io.h>
12
#include <avr/wdt.h>
13
#include <avr/interrupt.h>
14
#include <avr/pgmspace.h>
15
#include <util/delay.h>
16
 
17
#include "usbdrv.h"
81 pfowler 18
//#include "oddebug.h"
79 pfowler 19
#include "config.h"
20
 
21
#ifndef NULL
22
#define NULL    ((void *)0)
23
#endif
24
 
25
/* ------------------------------------------------------------------------- */
26
 
84 pfowler 27
uint8_t getKey(void);
28
void doButtons(void);
85 pfowler 29
inline void setLeds(uint8_t);
84 pfowler 30
 
79 pfowler 31
struct {
32
	union {
84 pfowler 33
		int16_t axis[2];
79 pfowler 34
		struct {
35
			int16_t axis0:16;
84 pfowler 36
			int16_t axis1:16;
79 pfowler 37
		};
38
		uint8_t buttons;
39
		struct {
40
			int8_t b1:1;
41
			int8_t b2:2;
42
			int8_t b3:3;
43
			int8_t b4:4;
44
			int8_t b5:5;
45
			int8_t b6:6;
46
			int8_t b7:7;
47
			int8_t b8:8;
48
		};
49
	};
50
} reportBuffer;
51
 
52
 
53
volatile struct {
84 pfowler 54
	uint8_t buttons;
55
	uint8_t waitup;
79 pfowler 56
	uint8_t timer;
84 pfowler 57
} debounce;
79 pfowler 58
 
84 pfowler 59
uint8_t currleds = 128;
79 pfowler 60
static uchar    idleRate;           /* in 4 ms units */
61
volatile uint8_t tmr0_ovf = 0;
62
volatile uint32_t systime = 0;
63
 
64
/* ------------------------------------------------------------------------- */
65
 
66
const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
67
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
68
    0x09, 0x05,                    // USAGE (Game Pad)
69
    0xa1, 0x01,                    // COLLECTION (Application)
70
    0x09, 0x01,                    //   USAGE (Pointer)
71
    0xa1, 0x00,                    //   COLLECTION (Physical)
72
    0x09, 0x30,                    //     USAGE (X)
73
    0x09, 0x31,                    //     USAGE (Y)
74
    0x16, 0x00, 0x80,		   //	  Log Min -32768
75
    0x26, 0xff, 0x7f,		   //	  Log max 32768
76
    0x75, 0x10,                    //     REPORT_SIZE (16)
77
    0x95, 0x02,                    //     REPORT_COUNT (2)
78
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
79
 
80
    0x05, 0x09,                    //     USAGE_PAGE (Button)
81
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
84 pfowler 82
    0x29, 0x08,                    //     USAGE_MAXIMUM (Button 8)
79 pfowler 83
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
84
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
85
    0x75, 0x01,                    //     REPORT_SIZE (1)
86
    0x95, 0x08,                    //     REPORT_COUNT (2)
87
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
88
 
89
    0xc0,                          //   END_COLLECTION
90
    0xc0                           // END_COLLECTION
91
};
92
 
85 pfowler 93
void usbEventResetReady(void) {
79 pfowler 94
 
85 pfowler 95
}
96
 
97
 
79 pfowler 98
uchar	usbFunctionSetup(uchar data[8])
99
{
100
    usbRequest_t    *rq = (void *)data;
101
 
102
    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ 
103
        if(rq->bRequest == USBRQ_HID_GET_REPORT){ 
104
            return sizeof(reportBuffer);
105
        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
106
            usbMsgPtr = &idleRate;
107
            return 1;
108
        }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
109
            idleRate = rq->wValue.bytes[1];
110
        }
111
    }else{
112
 
113
    }
114
	return 0;
115
}
116
 
117
 
118
void usbSendHidReport(uchar * data, uchar len) {
119
	usbSetInterrupt(data, len);
120
}
121
 
122
 
123
int main(void) {
124
 
80 pfowler 125
  /*
126
  DDR : 1 = Output, 0 = Input
127
  PORT: 1 = Pullup for Input, otherwise set output
128
  PIN : Read input pin
129
  */
130
 
131
  /*
85 pfowler 132
        PB0     - Output                - LED 3
133
        PB1     - Output                - LED 4
134
        PB2     - Output                - LED 5
135
        PB3     - Output                - LED 6
136
        PB4     - Output                - LED 7
137
        PB5     - 
138
        PB6     - 
139
        PB7     - 
80 pfowler 140
  */
85 pfowler 141
  DDRB          = 0B00011111;
80 pfowler 142
  PORTB         = 0B00000000;
85 pfowler 143
 
80 pfowler 144
  /*
85 pfowler 145
        PC0     - Output		- ButtonPad Gnd0
146
        PC1     - Output		- ButtonPad Gnd1
147
        PC2     - Input, Pullup		- ButtonPad 0
148
        PC3     - Input, Pullup		- ButtonPad 1
149
        PC4     - Input, Pullup		- ButtonPad 2
150
        PC5     - Input, Pullup		- ButtonPad 3
80 pfowler 151
  */
84 pfowler 152
  DDRD          = 0B00000011;
85 pfowler 153
  PORTD         = 0B00111111;
80 pfowler 154
 
85 pfowler 155
  /*
156
        PD0     - 
157
        PD1     - 
158
        PD2     - 
159
        PD3     - 
160
        PD4     - 
161
        PD5     - Output		- LED 0
162
        PD6     - Output		- LED 1
163
        PD7     - Output		- LED 2
164
  */
165
  DDRD          = 0B11100000;
166
  PORTD         = 0B00000000;
167
 
168
	setLeds(255);
84 pfowler 169
	_delay_ms(20);
85 pfowler 170
	setLeds(currleds);
80 pfowler 171
 
79 pfowler 172
    usbDeviceDisconnect();
85 pfowler 173
        _delay_ms(500);
79 pfowler 174
 
175
    usbDeviceConnect();
176
    wdt_enable(WDTO_1S);
177
 
178
 
85 pfowler 179
    TIMSK0 = (1<<TOIE0);                    // Enable timer overflow
79 pfowler 180
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
85 pfowler 181
    TCCR0B = (1<< CS00) ;                   // /1 prescaler
79 pfowler 182
 
183
    usbInit();
184
    sei();
185
 
186
    reportBuffer.axis0 = 0;
84 pfowler 187
    reportBuffer.axis1 = 0;
79 pfowler 188
    reportBuffer.buttons = 0;
189
 
80 pfowler 190
 
79 pfowler 191
    for(;;){    /* main event loop */
192
        wdt_reset();
193
        usbPoll();
194
 
84 pfowler 195
	doButtons();	
80 pfowler 196
 
79 pfowler 197
        if(usbInterruptIsReady()){ 
198
 
199
		usbSendHidReport(&reportBuffer, sizeof(reportBuffer));
200
        }
84 pfowler 201
 
202
	_delay_ms(1);
79 pfowler 203
    }
204
    return 0;
205
}
206
 
85 pfowler 207
inline void setLeds(uint8_t leds) {
208
        PORTB |= (0x1F & (leds >> 3));
209
        PORTD |= (0xE0 & (leds << 5));
210
}
211
 
84 pfowler 212
void doButtons() {
213
        uint8_t pressed = getKey();
79 pfowler 214
 
84 pfowler 215
        // Deboucing
216
        // When i key first goes down, wait 5 ms, check it again to see if its still down
217
        if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
218
                debounce.buttons = pressed;
219
                debounce.timer = 5;
79 pfowler 220
        }
221
 
84 pfowler 222
        // The key has come up
223
        if (pressed != debounce.buttons) {
224
                debounce.buttons = 0;
225
                debounce.timer = 0;
226
                debounce.waitup = 0;
227
        }
79 pfowler 228
 
84 pfowler 229
        // Debounce timer is up, process our button
230
        if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
231
                uint8_t i = 0;
232
                for (i=0; i<=7; i++) {
233
                        // Button pressed and the led is currently on
234
                        if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
235
                                if ( i == 6 && rbi(currleds, 7) != 1)  //Dont turn off com1 if no comm2
236
                                        break;
79 pfowler 237
 
84 pfowler 238
                                if ( i == 7 && rbi(currleds, 6) != 1)  //Dont turn off com2 if no comm1
239
                                        break;
240
 
241
                                cbi(currleds, i);
242
                        // Button is pressed and led is currently off
243
                         } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
244
                                if ( i == 6 && rbi(currleds, 7) == 1)  //Turn on comm2, turn off comm1
245
                                        cbi(currleds,7);
246
 
247
                                if ( i == 7 && rbi(currleds, 6) == 1)  //Turn on comm1, turn off comm2
248
                                        cbi(currleds,6);
249
 
250
                                sbi(currleds, i);
251
                        }
252
                }
85 pfowler 253
                setLeds(currleds);
84 pfowler 254
                reportBuffer.buttons = debounce.buttons;
255
                // Set debounce to wait to button up
256
                debounce.waitup = 1;
257
        }
79 pfowler 258
}
259
 
85 pfowler 260
// Gnd = PC0, PC1
261
// Btn = PC2, PC3, PC4, PC5
84 pfowler 262
uint8_t getKey() {
263
        uint8_t key = 0;
264
 
85 pfowler 265
        cbi(PORTC, 0);
84 pfowler 266
        _delay_us(10);        // Wait for the port change
85 pfowler 267
        if (rbi(PINC, 2) == 0) key = 1;
268
        if (rbi(PINC, 3) == 0) key = 2;
269
        if (rbi(PINC, 4) == 0) key = 4;
270
        if (rbi(PINC, 5) == 0) key = 8;
271
        sbi(PORTC, 0);
84 pfowler 272
 
85 pfowler 273
        cbi(PORTC, 1);
84 pfowler 274
        _delay_us(10);
85 pfowler 275
        if (rbi(PINC, 2) == 0) key = 16;
276
        if (rbi(PINC, 3) == 0) key = 32;
277
        if (rbi(PINC, 4) == 0) key = 64;
278
        if (rbi(PINC, 5) == 0) key = 128;
279
        sbi(PORTC, 1);
84 pfowler 280
 
281
        return key;
79 pfowler 282
}
283
 
84 pfowler 284
 
79 pfowler 285
ISR(TIMER0_OVF_vect) {
286
        tmr0_ovf++;
84 pfowler 287
 
288
	//16.5Mhz, 1ms = 50ovf
85 pfowler 289
	//12.0Mhz, 1ms = 46ovf
84 pfowler 290
 
85 pfowler 291
        if (tmr0_ovf>=46) {
79 pfowler 292
                systime++;
293
                tmr0_ovf = 0;
84 pfowler 294
 
295
		if (debounce.timer != 0)
296
			debounce.timer--;
79 pfowler 297
        }
298
}