main.c

by JohnyBravo12 on December 26th, 2009
No notes
Syntax: C
Show lines - Hide lines - Show in textbox - Download
 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
 
#include "usbdrv/usbdrv.c"
 
 
 
 
#define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 23
PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] =
{
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x06,                    // USAGE (Keyboard)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x0b, 0x06, 0x00, 0x01, 0x00,  //   USAGE (Generic Desktop:Keyboard)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
    0xc0                           // END_COLLECTION
};
 
 
 
 
static uchar buffer[1], idleRate, change;
 
 
 
 
 
 
usbMsgLen_t usbFunctionSetup(uchar setupData[8])
{
    usbRequest_t *rq = (void *)setupData;
 
   if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)
   {   
        if(rq->bRequest == USBRQ_HID_GET_REPORT)
        {
            buffer[0] = 0x31;
 
            if(change)
            {
                    buffer[0] = 0;
            }
            change = ~change;
 
            usbMsgPtr = buffer;
            return sizeof(buffer);
 
        } 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];
        }
    }
 
    return 0;
}
 
 
 
 
int main(void)
{
   change = 0;
 
    usbInit();
    usbDeviceDisconnect();
 
    unsigned char i;
    for(i = 0; i < 20; i++) // 300 ms
    {
        _delay_ms(15);
    }
 
    usbDeviceConnect();
    sei();
 
    for(;;)
    {
        usbPoll();
        if(usbInterruptIsReady())
        {
            usbSetInterrupt(buffer, sizeof(buffer));
        }
 
        for(i = 0; i < 100; i++) // 1s
        {
            _delay_ms(10);
        }
    }
}
 

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS