How to write with USBC_Stream like a keyboard

The following code i already have (found in an other forum post):


                const int HID_DESCRIPTOR_TYPE = 0x21;

                ushort myVID = 0x03F0;
                ushort myPID = 0x0324;
                ushort myDeviceVersion = 0x100;
                ushort myDeviceMaxPower = 70; // in milli amps
                string companyName = "Hewlett-Packard";
                string productName = "HP Basic USB Keyboard";
                string myDeviceSerialNumber = "0";

                USBC_Device device = new USBC_Device(myVID, myPID, myDeviceVersion, myDeviceMaxPower, companyName, productName, myDeviceSerialNumber);

                // Endpoints
                byte writeEPNumber = device.ReserveNewEndpoint();
                byte readEPNumber = device.ReserveNewEndpoint();
                MS.Configuration.Endpoint[] epDesc = {
                    new MS.Configuration.Endpoint(writeEPNumber, MS.Configuration.Endpoint.ATTRIB_Write | MS.Configuration.Endpoint.ATTRIB_Interrupt),
                    new MS.Configuration.Endpoint(readEPNumber, MS.Configuration.Endpoint.ATTRIB_Read | MS.Configuration.Endpoint.ATTRIB_Interrupt),
                };
                epDesc[0].wMaxPacketSize = 8;
                epDesc[0].bInterval = 18;
                epDesc[1].wMaxPacketSize = 8;
                epDesc[1].bInterval = 18;

                byte[] hidGenericReportDescriptorPayload = new byte[]
                {
                    0x05, 0x01,         //Usage Page (Generic Desktop) 05 01  
                    0x09, 0x06,         //Usage (Keyboard) 09 06  
                    0xA1, 0x01,         //Collection (Application) A1 01  
                    0x05, 0x07,         //    Usage Page (Keyboard/Keypad) 05 07  
                    0x19, 0xE0,         //    Usage Minimum (Keyboard Left Control) 19 E0  
                    0x29, 0xE7,         //    Usage Maximum (Keyboard Right GUI) 29 E7  
                    0x15, 0x00,         //    Logical Minimum (0) 15 00  
                    0x25, 0x01,         //    Logical Maximum (1) 25 01  
                    0x95, 0x08,         //    Report Count (8) 95 08  
                    0x75, 0x01,         //    Report Size (1) 75 01  
                    0x81, 0x02,         //    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
                    0x95, 0x08,         //    Report Count (8) 95 08  
                    0x75, 0x01,         //    Report Size (1) 75 01  
                    0x81, 0x01,         //    Input (Cnst,Ary,Abs) 81 01  
                    0x05, 0x08,         //    Usage Page (LEDs) 05 08  
                    0x19, 0x01,         //    Usage Minimum (Num Lock) 19 01  
                    0x29, 0x03,         //    Usage Maximum (Scroll Lock) 29 03  
                    0x95, 0x03,         //    Report Count (3) 95 03  
                    0x75, 0x01,         //    Report Size (1) 75 01  
                    0x91, 0x02,         //    Output (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) 91 02  
                    0x95, 0x01,         //    Report Count (1) 95 01  
                    0x75, 0x05,         //    Report Size (5) 75 05  
                    0x91, 0x01,         //    Output (Cnst,Ary,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) 91 01  
                    0x05, 0x07,         //    Usage Page (Keyboard/Keypad) 05 07  
                    0x19, 0x00,         //    Usage Minimum (Undefined) 19 00  
                    0x2A, 0xFF, 0x00,   //    Usage Maximum 2A FF 00  
                    0x15, 0x00,         //    Logical Minimum (0) 15 00  
                    0x26, 0xFF, 0x00,   //    Logical Maximum (255) 26 FF 00  
                    0x95, 0x00,         //    Report Count (6) 95 06  
                    0x75, 0x08,         //    Report Size (8) 75 08  
                    0x81, 0x00,         //    Input (Data,Ary,Abs) 81 00  
                    0xC0,               //End Collection C0
                };

                byte[] hidClassDescriptorPayload = new byte[]
                {
                    0x01, 0x00, //bcHID
                    0x00,       //bCountryCode
                    0x01,       //bNumDescriptors
                    0x22,       //bDescriptorType (report)
                    (byte)hidGenericReportDescriptorPayload.Length, 0x00
                };

                MS.Configuration.ClassDescriptor hidClassDescriptor = new MS.Configuration.ClassDescriptor(HID_DESCRIPTOR_TYPE, hidClassDescriptorPayload);

                MS.Configuration.UsbInterface usbInterface = new MS.Configuration.UsbInterface(1, epDesc);
                usbInterface.classDescriptors = new MS.Configuration.ClassDescriptor[] { hidClassDescriptor };
                usbInterface.bInterfaceClass = 0x03; // HID
                usbInterface.bInterfaceSubClass = 0x00;
                usbInterface.bInterfaceProtocol = 0x00;
                // Attach interface
                byte interfaceIndex = device.AddInterface(usbInterface, "HP Basic USB Keyboard");

                MS.Configuration.GenericDescriptor hidGenericReportDescriptor = new MS.Configuration.GenericDescriptor(0x81, 0x2200, hidGenericReportDescriptorPayload);
                hidGenericReportDescriptor.bRequest = 0x06;
                hidGenericReportDescriptor.wIndex = 0x00;
                device.AddDescriptor(hidGenericReportDescriptor);
                // Strings
                MS.Configuration.StringDescriptor stringDescriptor1 = new MS.Configuration.StringDescriptor(1, "USB-Eingabegerät");
                MS.Configuration.StringDescriptor stringDescriptor2 = new MS.Configuration.StringDescriptor(2, "HP Basic USB Keyboard");
                device.AddDescriptor(stringDescriptor1);
                device.AddDescriptor(stringDescriptor2);
                USBC_Stream stream = device.CreateUSBStream(writeEPNumber, readEPNumber);
                
                USBClientController.Start(device);

                byte[] send = Encoding.UTF8.GetBytes("test");
                stream.Write(send, 0, send.Length); // <-- does not work

The gadgeteer is coming up as keyboard in device manager already. But i dont know how to write with the stream like as someone is typing the keyboard. Is it possible?

Thanks for any help

Spider?

Yes FEZ Spider

premium products, like spider, have a built in class ot handle USB client. Maybe use it instead? http://ghielectronics.com/docs/20/usb-client

1 Like