USB HID / USB MIDI as libraries

Please do not post requests
arpruss
Posts: 111
Joined: Sat Sep 30, 2017 3:34 am

Re: USB HID / USB MIDI as libraries

Post by arpruss » Mon Dec 11, 2017 5:39 pm

If you want to try out host-to-device USB communication, the featuresetting branch of USBHID includes examples/softjoystick which is a project that mirrors to the PC whatever report it got from the PC via USB HID feature set. (It's not completely silly. It provides something like vJoy functionality without the need for installing vJoy drivers, so you can implement a joystick in software on the PC. That said, I don't see a very good use case other than for testing communications.)

To test it on Windows, install it to an stm32f1, run send.py (which needs pywinusb) on the PC, and then look in the Game Controllers config tool in Windows to see if the joystick is moving. (Win-R, joy.cpl, enter, click on "Maple", click on "Properties") Another test is whether the fetch line matches the previous send line in the output.

Code: Select all

$ c:/python36/python send.py
HID device (vID=0x1eaf, pID=0x0024, v=0x0200); LeafLabs; Maple, Path: \\?\hid#vid_1eaf&pid_0024#6&149a03ce&2&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
send: [3, 7, 0, 0, 0, 15, 0, 0, 0, 2, 8, 32, 128]
fetch: [3, 7, 0, 0, 0, 15, 0, 0, 0, 2, 8, 32, 128]
(You may need to reset or unplug/plug the board once after installing the sketch for it to work. I haven't quite figured out why that sometimes happens.)

User avatar
RogerClark
Posts: 7549
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: USB HID / USB MIDI as libraries

Post by RogerClark » Mon Dec 11, 2017 7:15 pm

Cool..

So could you send a command via HID to reboot ??

arpruss
Posts: 111
Joined: Sat Sep 30, 2017 3:34 am

Re: USB HID / USB MIDI as libraries

Post by arpruss » Mon Dec 11, 2017 7:47 pm

Yeah. Just add a feature output to a HID description, and then decide on a magic string of bytes that forces a reset when it's sent to the device.

But one would have to decide which HID description(s) to add it to, and there would be some SRAM wasted (40-300 bytes or so) depending on how one implemented it.

User avatar
RogerClark
Posts: 7549
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: USB HID / USB MIDI as libraries

Post by RogerClark » Mon Dec 11, 2017 7:54 pm

OK. it’s not so good that it needs more RAM

Also there would need to be binary tools for all platforms to send this command, as people would not necessarily have python installed

arpruss
Posts: 111
Joined: Sat Sep 30, 2017 3:34 am

Re: USB HID / USB MIDI as libraries

Post by arpruss » Mon Dec 11, 2017 9:28 pm

RogerClark wrote:
Mon Dec 11, 2017 7:54 pm
OK. it’s not so good that it needs more RAM

Also there would need to be binary tools for all platforms to send this command, as people would not necessarily have python installed
Personally, I think this is more trouble than it's worth. :-) I'd rather just pull the plug or press the reset button instead of testing things on three platforms.

User avatar
RogerClark
Posts: 7549
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: USB HID / USB MIDI as libraries

Post by RogerClark » Mon Dec 11, 2017 9:53 pm

arpruss wrote:
Mon Dec 11, 2017 9:28 pm
RogerClark wrote:
Mon Dec 11, 2017 7:54 pm
OK. it’s not so good that it needs more RAM

Also there would need to be binary tools for all platforms to send this command, as people would not necessarily have python installed
Personally, I think this is more trouble than it's worth. :-) I'd rather just pull the plug or press the reset button instead of testing things on three platforms.
Agreed

anv
Posts: 10
Joined: Mon Dec 11, 2017 1:52 pm

Re: USB HID / USB MIDI as libraries

Post by anv » Tue Dec 12, 2017 9:12 am

There is an usb keyboard library for arduino leonardo that support receiving keyboard leds status from the host. May be you could check how it works. I don't remember now but if I find it I will tell you.

arpruss
Posts: 111
Joined: Sat Sep 30, 2017 3:34 am

Re: USB HID / USB MIDI as libraries

Post by arpruss » Tue Dec 12, 2017 5:54 pm

I took a lot of code from libarra111's branch and now the library implements a composite serial / HID device, with reset functionality using the standard upload tools. There is rather more flash/SRAM used because of the serial support, but the serial support should be worth it.

The only thing I couldn't figure out to do is how to override the core's Serial, so currently if you want to do serial i/o, you need to use CompositeSerial.

Maybe one could make a virtual USBSerial class and make the Serial instance be __weak in the core or something like that. I don't know enough about C++ inheritance to figure this out right now.

anv
Posts: 10
Joined: Mon Dec 11, 2017 1:52 pm

Re: USB HID / USB MIDI as libraries

Post by anv » Tue Dec 12, 2017 7:43 pm

Just downloaded master branch. The example for twojoysticks does not compile.
Tried usb keyboard. USB HID is detected but can't be configured.
Serial interface is detected and module loaded.

This is my dmesg output when I plug the device.

74281.945860] usb 1-4: new full-speed USB device number 107 using xhci_hcd
[74282.115902] usb 1-4: New USB device found, idVendor=1eaf, idProduct=0024
[74282.115904] usb 1-4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[74282.115905] usb 1-4: Product: Maple
[74282.115905] usb 1-4: Manufacturer: LeafLabs
[74292.251940] cdc_acm 1-4:1.0: ttyACM0: USB ACM device
[74292.253017] usbhid 1-4:1.2: can't add hid device: -71
[74292.253026] usbhid: probe of 1-4:1.2 failed with error -71
[74293.769062] cdc_acm 1-4:1.0: failed to set dtr/rts
[74306.967792] cdc_acm 1-4:1.0: failed to set dtr/rts
[74306.969538] cdc_acm 1-4:1.0: failed to set dtr/rts
[74316.062451] cdc_acm 1-4:1.0: failed to set dtr/rts
[74316.063964] cdc_acm 1-4:1.0: failed to set dtr/rts
[74322.061371] cdc_acm 1-4:1.0: failed to set dtr/rts


Source code for the program:

Code: Select all

#include <USBHID.h>

const uint8_t reportDescription[] = {
  USB_HID_ABS_MOUSE_REPORT_DESCRIPTOR(USB_HID_MOUSE_REPORT_ID)
  ,USB_HID_KEYBOARD_REPORT_DESCRIPTOR(USB_HID_KEYBOARD_REPORT_ID)
};

HIDAbsMouse mouse;


void setup(){
  HID.begin(reportDescription, sizeof(reportDescription));
  delay(1000);
  mouse.move(0,0);
  delay(1000);
  mouse.press(MOUSE_LEFT);
  mouse.move(500,500);
  mouse.release(MOUSE_ALL);
  mouse.click(MOUSE_RIGHT);

}

void loop(){
  delay(100);
  mouse.move(16384,16384);
  
  Keyboard.write('h');
  Keyboard.write('o');
  Keyboard.write('l');
  Keyboard.write('a');
  delay(100);
}

arpruss
Posts: 111
Joined: Sat Sep 30, 2017 3:34 am

Re: USB HID / USB MIDI as libraries

Post by arpruss » Tue Dec 12, 2017 8:27 pm

anv wrote:
Tue Dec 12, 2017 7:43 pm
Just downloaded master branch. The example for twojoysticks does not compile.
Sorry: I changed the API without changing the example. It's fixed now.
Tried usb keyboard. USB HID is detected but can't be configured.
That's not good. I've only tested on Windows.

1. Do you know if the libarra111 USB core works for you?

2. Can you try something super-basic like:

Code: Select all

#include <USBHID.h>

void setup() {
  USB.begin(USB_HID_JOYSTICK);
}

void loop() {
  Joystick.X(0);
  delay(500);
  Joystick.X(1023);
  delay(500);
}
Also, if that doesn't work, could you try the older, simpler version of my joystick library? https://github.com/arpruss/Joystick_stm32f1

Code: Select all

#include <Joystick.h>

void setup() {
}

void loop() {
  Joystick.X(0);
  delay(500);
  Joystick.X(1023);
  delay(500);
}

Post Reply