Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Codes changed/added:
- - (change) usbFunctionSetup(uchar data[8]) > add build report func call
- - (add) usbFunctionDescriptor
- -
- */
- #include <avr/io.h>
- #include <avr/wdt.h>
- #include <avr/interrupt.h> /* for sei() */
- #include <util/delay.h> /* for _delay_ms() */
- #include <avr/pgmspace.h> /* required by usbdrv.h */
- #include "usbdrv.h"
- //#include "oddebug.h" /* This is also an example for using debug macros */
- #include "a2d.h"
- /* ------------------------------------------------------------------------- */
- /* ----------------------------- USB interface ----------------------------- */
- /* ------------------------------------------------------------------------- */
- PROGMEM const char usbHidReportDescriptor[45] = { /* USB report descriptor, size must match usbconfig.h */
- 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
- 0x09, 0x05, // USAGE (Game Pad)
- 0xa1, 0x01, // COLLECTION (Application)
- 0xa1, 0x00, // COLLECTION (Physical)
- 0x05, 0x09, // USAGE_PAGE (Button)
- 0x19, 0x01, // USAGE_MINIMUM (Button 1)
- 0x29, 0x08, // USAGE_MAXIMUM (Button 8)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x25, 0x01, // LOGICAL_MAXIMUM (1)
- 0x95, 0x08, // REPORT_COUNT (8)
- 0x75, 0x01, // REPORT_SIZE (1)
- 0x81, 0x02, // INPUT (Data,Var,Abs)
- 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
- 0x09, 0x33, // USAGE (Rx)
- 0x09, 0x34, // USAGE (Ry)
- 0x09, 0x35, // USAGE (Rz)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x95, 0x03, // REPORT_COUNT (3)
- 0x81, 0x02, // INPUT (Data,Var,Abs)
- 0xc0, // END_COLLECTION
- 0xc0 // END_COLLECTION
- };
- typedef struct{
- uchar buttonMask; //8 button but only 7 actually used
- uchar rx; //3 analog in
- uchar ry;
- uchar rz;
- }report_t;
- static report_t reportBuffer;
- static uchar idleRate; /* repeat rate for keyboards, never used for mice */
- usbMsgLen_t usbFunctionSetup(uchar data[8])
- {
- usbRequest_t *rq = (void *)data;
- /* The following requests are never used. But since they are required by
- * the specification, we implement them in this example.
- */
- if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
- //DBG1(0x50, &rq->bRequest, 1); /* debug output: print our request */
- if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
- /* we only have one report type, so don't look at wValue */
- usbMsgPtr = (void *)&reportBuffer;
- return sizeof(reportBuffer);
- }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
- usbMsgPtr = &idleRate;
- return 1;
- }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
- idleRate = rq->wValue.bytes[1];
- }
- }else{
- /* no vendor specific requests implemented */
- }
- return 0; /* default for not implemented requests: return no data back to host */
- }
- /* ------------------------------------------------------------------------- */
- int __attribute__((noreturn)) main(void)
- {
- uchar i;
- wdt_enable(WDTO_1S);
- /* Even if you don't use the watchdog, turn it off here. On newer devices,
- * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
- */
- /* RESET status: all port bits are inputs without pull-up.
- * That's the way we need D+ and D-. Therefore we don't need any
- * additional hardware initialization.
- */
- /*
- used I/O pin:
- as digital in: PB0-5, PD0
- as analog in : PC0-2
- PD1 led output
- */
- PORTB |= 0b00111111; //enable internal pull up
- PORTD |= 0b00000001; //enable internal pull up
- DDRD |= 0b00000010; //set pd1 as out
- a2dInit();
- //odDebugInit();
- //DBG1(0x00, 0, 0); /* debug output: main starts */
- usbInit();
- usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
- i = 0;
- while(--i){ /* fake USB disconnect for > 250 ms */
- wdt_reset();
- _delay_ms(1);
- }
- usbDeviceConnect();
- sei();
- //DBG1(0x01, 0, 0); /* debug output: main loop starts */
- for(;;){ /* main event loop */
- //DBG1(0x02, 0, 0); /* debug output: main loop iterates */
- wdt_reset();
- usbPoll();
- if(usbInterruptIsReady()){
- /* called after every poll of the interrupt endpoint */
- //prepare report buffer here
- reportBuffer.buttonMask = ~((PINB & 0b00111111) | ((PIND & 1) << 6));
- reportBuffer.rx = i;
- i++;
- //reportBuffer.rx = a2dRead(0);
- //reportBuffer.ry = a2dRead(1);
- //reportBuffer.rz = a2dRead(2);
- //DBG1(0x03, 0, 0); /* debug output: interrupt report prepared */
- usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
- }
- }
- }
- /* ------------------------------------------------------------------------- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement