summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c119
-rw-r--r--pc-client.c159
-rw-r--r--usbdrv/usbconfig.h16
3 files changed, 80 insertions, 214 deletions
diff --git a/main.c b/main.c
index a3d1b28..9f287a5 100644
--- a/main.c
+++ b/main.c
@@ -6,11 +6,56 @@
#include <avr/interrupt.h>
#include "usbdrv/usbdrv.h"
-static uchar replyBuf[18]= "Hello, World!";
+PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+ 0x09, 0x02, // USAGE (Mouse)
+ 0xa1, 0x01, // COLLECTION (Application)
+ 0x09, 0x01, // USAGE (Pointer)
+ 0xA1, 0x00, // COLLECTION (Physical)
+ 0x05, 0x09, // USAGE_PAGE (Button)
+ 0x19, 0x01, // USAGE_MINIMUM
+ 0x29, 0x03, // USAGE_MAXIMUM
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x95, 0x03, // REPORT_COUNT (3)
+ 0x75, 0x01, // REPORT_SIZE (1)
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0x75, 0x05, // REPORT_SIZE (5)
+ 0x81, 0x03, // INPUT (Const,Var,Abs)
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+ 0x09, 0x30, // USAGE (X)
+ 0x09, 0x31, // USAGE (Y)
+ 0x09, 0x38, // USAGE (Wheel)
+ 0x15, 0x81, // LOGICAL_MINIMUM (-127)
+ 0x25, 0x7F, // LOGICAL_MAXIMUM (127)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x95, 0x03, // REPORT_COUNT (3)
+ 0x81, 0x06, // INPUT (Data,Var,Rel)
+ 0xC0, // END_COLLECTION
+ 0xC0, // END COLLECTION
+};
+
+/* This is the same report descriptor as seen in a Logitech mouse. The data
+ * described by this descriptor consists of 4 bytes:
+ * . . . . . B2 B1 B0 .... one byte with mouse button states
+ * X7 X6 X5 X4 X3 X2 X1 X0 .... 8 bit signed relative coordinate x
+ * Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 .... 8 bit signed relative coordinate y
+ * W7 W6 W5 W4 W3 W2 W1 W0 .... 8 bit signed relative coordinate wheel
+ */
+typedef struct{
+ uchar buttonMask;
+ char dx;
+ char dy;
+ char dWheel;
+} report_t;
+
+
+report_t reportBuffer;
+
/* device is detected, however unreliably. dunno why. */
-static uchar dataReceived, dataLength;
void debug(int i)
{
@@ -22,47 +67,23 @@ USB_PUBLIC uchar usbFunctionSetup(uchar data[8])
{
usbRequest_t* rq = (usbRequest_t*) data;
- switch(rq->bRequest)
- {
- case 0: PORTB |= 1; break;
- case 1: PORTB &= ~1; break;
-
- case 2: usbMsgPtr = replyBuf; return sizeof(replyBuf); // pc reads
- case 3: // alter byte
- PORTB = ~(rq->wIndex.word & 0xFF);
- if ((rq->wIndex.word >= 0) && (rq->wIndex.word < sizeof(replyBuf)))
- {
- replyBuf[rq->wIndex.word] = rq->wValue.word;
- }
- break;
- case 4: // pc writes
- dataLength = rq->wLength.word;
- debug(dataLength);
- dataReceived = 0;
-
- return USB_NO_MSG;
-
- }
+ if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS &&
+ (rq->bRequest == USBRQ_HID_GET_REPORT))
+ {
+ debug(32);
+ // wValue: ReportType (highbyte), ReportID (lowbyte)
+ usbMsgPtr = (void *)&reportBuffer; // we only have this one
+ return sizeof(reportBuffer);
+ }
return 0;
}
-USB_PUBLIC uchar usbFunctionWrite(uchar* data, uchar len)
-{
- PORTB&=~32;
- for (uchar i = 0; dataReceived < dataLength && i < len; i++, dataReceived++)
- {
- if (0 <= dataReceived && dataReceived < sizeof(replyBuf))
- replyBuf[dataReceived] = data[i];
- }
-
- return (dataReceived >= dataLength);
-}
-
-
int main (void)
{
+ char rand=123;
+
DDRC=0x00;
PORTC=0x00;
@@ -70,16 +91,6 @@ int main (void)
wdt_enable(WDTO_1S);
-/*
- for (int i=0;i<20;i++)
- {
- PORTB|=1;
- _delay_ms(50);
- PORTB&=~1;
- _delay_ms(50);
- wdt_reset();
- } */
-
usbInit();
usbDeviceDisconnect();
@@ -94,11 +105,25 @@ int main (void)
PORTB=~42;
sei();
-
+debug(4);
while(1)
{
wdt_reset();
usbPoll();
+
+ if(usbInterruptIsReady()) { // if the interrupt is ready, feed data
+ // pseudo-random sequence generator, thanks to Dan Frederiksen @AVRfreaks
+ // http://en.wikipedia.org/wiki/Linear_congruential_generator
+ rand=(rand*109+89)%251;
+
+ // move to a random direction
+ reportBuffer.dx = (rand&0xf)-8;
+ reportBuffer.dy = ((rand&0xf0)>>4)-8;
+
+ usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
+
+ debug(2);
+ }
}
return 0; // never reached
diff --git a/pc-client.c b/pc-client.c
deleted file mode 100644
index 1b6cb21..0000000
--- a/pc-client.c
+++ /dev/null
@@ -1,159 +0,0 @@
-#include <string.h>
-#include <usb.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-
-#define USB_LED_OFF 0
-#define USB_LED_ON 1
-
-
-/* Used to get descriptor strings for device identification */
-static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
- char *buf, int buflen) {
- char buffer[256];
- int rval, i;
-
- // make standard request GET_DESCRIPTOR, type string and given index
- // (e.g. dev->iProduct)
- rval = usb_control_msg(dev,
- USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
- USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
- buffer, sizeof(buffer), 1000);
-
- if(rval < 0) // error
- return rval;
-
- // rval should be bytes read, but buffer[0] contains the actual response size
- if((unsigned char)buffer[0] < rval)
- rval = (unsigned char)buffer[0]; // string is shorter than bytes read
-
- if(buffer[1] != USB_DT_STRING) // second byte is the data type
- return 0; // invalid return type
-
- // we're dealing with UTF-16LE here so actual chars is half of rval,
- // and index 0 doesn't count
- rval /= 2;
-
- /* lossy conversion to ISO Latin1 */
- for(i = 1; i < rval && i < buflen; i++) {
- if(buffer[2 * i + 1] == 0)
- buf[i-1] = buffer[2 * i];
- else
- buf[i-1] = '?'; /* outside of ISO Latin1 range */
- }
- buf[i-1] = 0;
-
- return i-1;
-}
-
-static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
- int product, char *productName) {
- struct usb_bus *bus;
- struct usb_device *dev;
- char devVendor[256], devProduct[256];
-
- usb_dev_handle * handle = NULL;
-
- usb_init();
- usb_find_busses();
- usb_find_devices();
-
- for(bus=usb_get_busses(); bus; bus=bus->next) {
- for(dev=bus->devices; dev; dev=dev->next) {
- if(dev->descriptor.idVendor != vendor ||
- dev->descriptor.idProduct != product)
- continue;
-
- /* we need to open the device in order to query strings */
- if(!(handle = usb_open(dev))) {
- fprintf(stderr, "Warning: cannot open USB device: %s\n",
- usb_strerror());
- continue;
- }
-
- /* get vendor name */
- if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer,
- 0x0409, devVendor, sizeof(devVendor)) < 0) {
- fprintf(stderr,
- "Warning: cannot query manufacturer for device: %s\n",
- usb_strerror());
- usb_close(handle);
- continue;
- }
-
- /* get product name */
- if(usbGetDescriptorString(handle, dev->descriptor.iProduct,
- 0x0409, devProduct, sizeof(devVendor)) < 0) {
- fprintf(stderr,
- "Warning: cannot query product for device: %s\n",
- usb_strerror());
- usb_close(handle);
- continue;
- }
-
- if(strcmp(devVendor, vendorName) == 0 &&
- strcmp(devProduct, productName) == 0)
- return handle;
- else
- usb_close(handle);
- }
- }
-
- return NULL;
-}
-
-int main(int argc, char **argv) {
- usb_dev_handle *handle = NULL;
- int nBytes = 0;
- char buffer[256];
-
- if(argc < 2) {
- printf("Usage:\n");
- printf("usbtext.exe on\n");
- printf("usbtext.exe off\n");
- exit(1);
- }
-
- handle = usbOpenDevice(0x16C0, "windfisch.org", 0x05DC, "Flo Test");
-
- if(handle == NULL) {
- fprintf(stderr, "Could not find USB device!\n");
- exit(1);
- }
-
- if(strcmp(argv[1], "on") == 0) {
- nBytes = usb_control_msg(handle,
- USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
- USB_LED_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000);
- } else if(strcmp(argv[1], "off") == 0) {
- nBytes = usb_control_msg(handle,
- USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
- USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000);
- } else if (strcmp(argv[1], "read") == 0) {
- char buffer[1024];
- nBytes = usb_control_msg(handle,
- USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
- 2, 0, 0, buffer, sizeof(buffer), 5000);
- printf("received %i bytes: %s\n", nBytes, buffer);
- } else if (strcmp(argv[1], "change") == 0) {
- nBytes = usb_control_msg(handle,
- USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
- 3, 32, 4, buffer, sizeof(buffer), 5000);
- buffer[nBytes]=0;
- printf("received %i bytes: %s\n", nBytes, buffer);
- } else if (strcmp(argv[1], "write") == 0) {
- nBytes = usb_control_msg(handle,
- USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
- 4, 0, 0, argv[2], strlen(argv[2])+1, 5000);
- buffer[nBytes]=0;
- printf("received %i bytes: %s\n", nBytes, buffer);
- }
-
- if(nBytes < 0)
- fprintf(stderr, "USB error: %s\n", usb_strerror());
-
- usb_close(handle);
-
- return 0;
-}
diff --git a/usbdrv/usbconfig.h b/usbdrv/usbconfig.h
index 48d4a93..e4b61ff 100644
--- a/usbdrv/usbconfig.h
+++ b/usbdrv/usbconfig.h
@@ -74,7 +74,7 @@ section at the end of this file).
/* --------------------------- Functional Range ---------------------------- */
-#define USB_CFG_HAVE_INTRIN_ENDPOINT 0
+#define USB_CFG_HAVE_INTRIN_ENDPOINT 1
/* Define this to 1 if you want to compile a version with two endpoints: The
* default control endpoint 0 and an interrupt-in endpoint (any other endpoint
* number).
@@ -109,7 +109,7 @@ section at the end of this file).
* (e.g. HID), but never want to send any data. This option saves a couple
* of bytes in flash memory and the transmit buffers in RAM.
*/
-#define USB_CFG_INTR_POLL_INTERVAL 10
+#define USB_CFG_INTR_POLL_INTERVAL 100
/* If you compile a version with endpoint 1 (interrupt-in), this is the poll
* interval. The value is in milliseconds and must not be less than 10 ms for
* low speed devices.
@@ -123,7 +123,7 @@ section at the end of this file).
* The value is in milliamperes. [It will be divided by two since USB
* communicates power requirements in units of 2 mA.]
*/
-#define USB_CFG_IMPLEMENT_FN_WRITE 1
+#define USB_CFG_IMPLEMENT_FN_WRITE 0
/* Set this to 1 if you want usbFunctionWrite() to be called for control-out
* transfers. Set it to 0 if you don't need it and want to save a couple of
* bytes.
@@ -227,7 +227,7 @@ section at the end of this file).
* with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand
* the implications!
*/
-#define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x05dc = 1500 */
+#define USB_CFG_DEVICE_ID 0xe8, 0x03 /* = 0x05dc = 1500 */
/* This is the ID of the product, low byte first. It is interpreted in the
* scope of the vendor ID. If you have registered your own VID with usb.org
* or if you have licensed a PID from somebody else, define it here. Otherwise
@@ -251,8 +251,8 @@ section at the end of this file).
* obdev's free shared VID/PID pair. See the file USB-IDs-for-free.txt for
* details.
*/
-#define USB_CFG_DEVICE_NAME 'F','l','o',' ','T','e','s','t'
-#define USB_CFG_DEVICE_NAME_LEN 8
+#define USB_CFG_DEVICE_NAME 'F','l','o',' ','H','i','d'
+#define USB_CFG_DEVICE_NAME_LEN 7
/* Same as above for the device name. If you don't want a device name, undefine
* the macros. See the file USB-IDs-for-free.txt before you assign a name if
* you use a shared VID/PID.
@@ -266,12 +266,12 @@ section at the end of this file).
* to fine tune control over USB descriptors such as the string descriptor
* for the serial number.
*/
-#define USB_CFG_DEVICE_CLASS 0xff /* set to 0 if deferred to interface */
+#define USB_CFG_DEVICE_CLASS 0 /* set to 0 if deferred to interface */
#define USB_CFG_DEVICE_SUBCLASS 0
/* See USB specification if you want to conform to an existing device class.
* Class 0xff is "vendor specific".
*/
-#define USB_CFG_INTERFACE_CLASS 0 /* define class here if not at device level */
+#define USB_CFG_INTERFACE_CLASS 3 /* define class here if not at device level */
#define USB_CFG_INTERFACE_SUBCLASS 0
#define USB_CFG_INTERFACE_PROTOCOL 0
/* See USB specification if you want to conform to an existing device class or