USB HID

Please do not post requests
User avatar
RogerClark
Posts: 7413
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: USB HID

Post by RogerClark » Sun May 21, 2017 12:59 am

I agree...

Having the USB device selectable by code is generally better than needing to select it from the IDE menu, because people can then just load the code + libraries and it works.

Having code which requires people to change the IDE settings to get it to work is confusing to say the least, so we generally avoid it if possible.

libarra
Posts: 56
Joined: Tue Sep 15, 2015 2:51 pm

Re: USB HID

Post by libarra » Sun May 21, 2017 9:51 pm

I may be the only one, but I don't find it confusing nor hard to use a menu option next to the one I used to select the board I'm using...

Other thing I wanted to mention is that trying to change the usb interface of the device on the fly, at least in windows, is not really a good idea if you will be doing it frequently. At least with libmaple is not convenient because windows will still have the stm32 board installed with one interface, lets say serial, and when you change on the same 'sketch' the interface to any other type, windows will think there is an error in the device, so you will have to unistall the current driver for the board and check for changes in the device manager so windows detects it as the new interface. I you change the usb interface of your device once in a long while It will not really matter, but if you want to do it to send for example serial messages to the PC while using other interface it is not practical.

I'm not trying to be negative about the idea of the usb interfaces as independent libraries but I don't think of it as a practical task to achieve.

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

Re: USB HID

Post by RogerClark » Sun May 21, 2017 10:07 pm

I think we would need to test for whether its practical on Windows.

When switching from DFU. bootloader to Serial Sketch, Windows does not have a big problem.

We could also prevent Serial.end() being called by default, so the only thing Windows would see is when the new device e.g. HID is enumerated.

Coincidentally, there is another thread where someone reported that they do not get the Serial device unless their sketch calls Serial.begin() in setup.

I checked the core, and its supposed to be called automatically in the board setup function, but either its not getting called or it doesnt work for everyone.

libarra
Posts: 56
Joined: Tue Sep 15, 2015 2:51 pm

Re: USB HID

Post by libarra » Mon May 22, 2017 10:06 pm

RogerClark wrote:Coincidentally, there is another thread where someone reported that they do not get the Serial device unless their sketch calls Serial.begin() in setup.

I checked the core, and its supposed to be called automatically in the board setup function, but either its not getting called or it doesnt work for everyone.
I think it is in one post of this thread, talking about the addMidiHId branch. But Serial.begin() should be called by libmaple if using your master branch or mine.
RogerClark wrote:When switching from DFU. bootloader to Serial Sketch, Windows does not have a big problem.
Indeed, changing from the bootloader DFU to any other USB interface does not cause any trouble.

The problem comes when changing between interfaces in the same 'sketch'. I noticed it when using my serial-hid code, the default option is only serial, so it works and is registered in windows as always, and when I choose any HID option (which includes serial) the HID part of the code won't interact with the PC because windows has it registered only as serial. I need to uninstall the board as serial, check for changes in the device manager and then it will be installed as a composite with serial and hid, then both the serial and hid parts of the code work. If you try it the other way around, pasing from composite serial-hid to only serial, there is no real problem because windows in spite of still having the composite device installed, one of the interfaces is serial, so the only serial with the composite installation works.

But it won't behave the same between other interfaces.

ag123
Posts: 795
Joined: Thu Jul 21, 2016 4:24 pm

Re: USB HID

Post by ag123 » Mon May 22, 2017 10:30 pm

the catch is probably that you need to enumerate as a different VID/PID e.g. 1eaf:0005
the host would then think that it is a new device and initiates that accordingly

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

Re: USB HID

Post by RogerClark » Tue May 23, 2017 12:02 am

ag123 wrote:the catch is probably that you need to enumerate as a different VID/PID e.g. 1eaf:0005
the host would then think that it is a new device and initiates that accordingly
I've not checked what VID/PID are used, but I thought it was a generic code for the HID and keyboard and mini devices.
Hence not anything like 1eaf:xxxx

libarra
Posts: 56
Joined: Tue Sep 15, 2015 2:51 pm

Re: USB HID

Post by libarra » Wed May 24, 2017 12:08 am

RogerClark wrote:
ag123 wrote:the catch is probably that you need to enumerate as a different VID/PID e.g. 1eaf:0005
the host would then think that it is a new device and initiates that accordingly
I've not checked what VID/PID are used, but I thought it was a generic code for the HID and keyboard and mini devices.
Hence not anything like 1eaf:xxxx
The VID and PID in libmaple are 1eaf:0004, but as far as I'm aware, we do not own any other PID's or VID's (not that I own any) to freely use and distribute with the libmaple core, that's another catch.

If I remember well, that is sort of the problem trying to recoginze the various stm32 boards within the arduino IDE as different boards and not just as the maple mini

Collie147
Posts: 2
Joined: Wed May 24, 2017 12:20 pm

Re: USB HID

Post by Collie147 » Wed May 24, 2017 12:30 pm

Probably not the best place to put this, but as its libarra's library and the thread is active I thought I'd have a pop.

Having some trouble getting the USB HID stuff to work. Keyboard.begin()/Mouse.begin() is fine, but as soon as I put in Keyboard.write() or Keyboard.print() the board does nothing after upload. It's a bluepill by the way. Changed R10 from a 10K to 1K5 still the same. Once I comment out the Keyboard.write and reupload it works fine. No Keyboard detected by the computer either.

Any ideas?

I'm presuming it should be fine and its most likely my cheapo aliexpress bluepill.
Sketch below with basic keyboard output (based on blink for convenience)

Code: Select all

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(PC13, OUTPUT);
  //Serial.begin(115200);
  
  Keyboard.begin();

}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
  Keyboard.println("ON");
  //Serial.println("HIGH");
  delay(1000);              // wait for a second
  digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
  Keyboard.println("OFF");
  //Serial.println("LOW");
  delay(1000);              // wait for a second
}

libarra
Posts: 56
Joined: Tue Sep 15, 2015 2:51 pm

Re: USB HID

Post by libarra » Thu May 25, 2017 2:19 am

Collie147 wrote:Probably not the best place to put this, but as its libarra's library and the thread is active I thought I'd have a pop.

Having some trouble getting the USB HID stuff to work. Keyboard.begin()/Mouse.begin() is fine, but as soon as I put in Keyboard.write() or Keyboard.print() the board does nothing after upload. It's a bluepill by the way. Changed R10 from a 10K to 1K5 still the same. Once I comment out the Keyboard.write and reupload it works fine. No Keyboard detected by the computer either.

Any ideas?

I'm presuming it should be fine and its most likely my cheapo aliexpress bluepill.
Sketch below with basic keyboard output (based on blink for convenience)

Code: Select all

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(PC13, OUTPUT);
  //Serial.begin(115200);
  
  Keyboard.begin();

}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
  Keyboard.println("ON");
  //Serial.println("HIGH");
  delay(1000);              // wait for a second
  digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
  Keyboard.println("OFF");
  //Serial.println("LOW");
  delay(1000);              // wait for a second
}
Hi Collie,

Now that you mention it, I tried your sketch and one of mine, but it no longer works...

I believe it is because I updated the library to work with the changes made to the libmaple usb code, but since it gave no compilation errors I assumed everything worked fine. Let me check what's wrong with the Keyboard class so I can commit the necesary changes.

libarra
Posts: 56
Joined: Tue Sep 15, 2015 2:51 pm

Re: USB HID

Post by libarra » Thu May 25, 2017 5:19 am

Collie,

I have commited the changes, your sketch should work now.

By the way, you don't need to initialize the Keyboard, Mouse, or Joystick because all of them with the Serial are initialized by libmaple with USBDev.begin(), but it doesn't hurt to do it.
Collie147 wrote: No Keyboard detected by the computer either.
If you are using windows you may need to uninstall your board's driver as Serial in the device manager, and check for changes in the hardware. When it is detected it will install 3 usb drivers: Composite, Serial and HID. Once it's done installing the drivers your board should work as a composite device with Serial and HID.

Post Reply