Rev 7 | Rev 18 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <avr/io.h>#include <avr/pgmspace.h>#include <avr/interrupt.h>#define F_CPU 12000000#include <util/delay.h>#include <avr/wdt.h>#include <avr/eeprom.h>#include <usbdrv.h>#include <stdlib.h>#include <string.h>#include "config.h"#include "hiddesc.h"#define DEPRESS_TIME 1void doInt(void);volatile uint8_t pcIntCurr = 0;volatile uint8_t pcIntLast = 0;volatile uint8_t pcIntMask = 0;volatile uint8_t timer0_ovf = 0;uint8_t time_rot = 0;struct{union {uint8_t data;struct {uint8_t X:2;uint8_t Y:2;uint8_t B:1;uint8_t A:1;uint8_t rot1:1;uint8_t rot2:1;};};} report;usbMsgLen_t usbFunctionSetup(uchar data[8]) {usbRequest_t *rq = (void *)data;if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {if(rq->bRequest == USBRQ_HID_GET_REPORT) {return sizeof(report);} else if(rq->bRequest == USBRQ_HID_GET_IDLE) {return 1;}}return 0;}void hadUsbReset(void) {}int main(void) {ACSR |= (1<<ACD); // Disable analog comparator// DDR : 1 = Output, 0 = Input// PORT: 1 = Pullup for Input, otherwise set output// PIN : Read input pinDDRB = 0B00011111;PORTB = 0B00011111;DDRD = 0B00110000;PORTD = 0B00110000;// Setup timer0TIMSK = (1<<TOIE0); // Eable timer overflow for Timer0TCNT0 = 0x00; // Set Timer0 to 0TCCR0B = (1<< CS02) | (1<<CS00); // /1024 prescalerusbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */_delay_ms(500);usbDeviceConnect();wdt_enable(WDTO_1S);usbInit();sei();for(;;) {wdt_reset();usbPoll();if (pcIntMask);doInt();if (!time_rot) {report.rot1 = 0;report.rot2 = 0;}if(usbInterruptIsReady()){report.data = 0x05; // Center pad, little endianif (bit_is_clear(PINB, PB0))report.X++;if (bit_is_clear(PINB, PB1))report.Y--;if (bit_is_clear(PINB, PB2))report.X--;if (bit_is_clear(PINB, PB3))report.Y++;if (bit_is_clear(PIND, PD4))report.A = 1;if (bit_is_clear(PIND, PD5))report.B = 1;/* called after every poll of the interrupt endpoint */usbSetInterrupt(&report, sizeof(report));}}}void doInt() {if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {time_rot = 5;report.rot1 = 1;report.rot2 = 0;} else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {time_rot = 5;report.rot1 = 0;report.rot2 = 1;}pcIntMask = 0;}ISR(TIMER0_OVF_vect) {timer0_ovf++;if (time_rot) {time_rot--;}}ISR(PCINT_vect){pcIntCurr = PINB;pcIntMask = pcIntCurr ^ pcIntLast;pcIntLast = pcIntCurr;}