USB Composite library question

Working libraries, libraries being ported and related hardware
arpruss
Posts: 282
Joined: Sat Sep 30, 2017 3:34 am

USB Composite library question

Post by arpruss » Tue Aug 14, 2018 8:02 pm

I've got a question for users of my USB Composite library. Background to the question: Currently, the library works like many Arduino libraries, namely it declares a number of handy objects like Joystick, USBHID, and you can just use the object directly. However, a side-effect of this is that a bunch of code you don't need is linked in. E.g., even if you don't use the serial port, the CompositeSerial object gets linked in. In theory, a smart compiler might be able to figure out how to leave out the objects, but the ARM version of gcc is not smart enough (I even tried the latest gcc 7).

I could modify the library so you have to declare your objects before using them. E.g.:

USBHIDDevice USBHID;
HIDKeyboard Keyboard(USBHID);
HIDJoystick Joystick(USBHID);
USBCompositeSerial CompositeSerial;

This makes the library a bit more cumbersome to use, but would save both flash and RAM. It could save almost 1kb of RAM in some configurations (e.g., if you don't use CompositeSerial).

Should I make the change? I would like feedback.

stevestrong
Posts: 3053
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany
Contact:

Re: USB Composite library question

Post by stevestrong » Wed Aug 15, 2018 5:22 am

I vote for yes.

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

Re: USB Composite library question

Post by ag123 » Wed Aug 15, 2018 6:10 am

thanks, i've not yet use the library literally, but yes separating them would be good.
i'm trying to get on the HID bandwagon but i found the protocol complex to understand. Unlike some of the simpler usb protocols HID use very structured set of commands. Often comprising as sequence of many commands (HID Report Descriptors https://eleccelerator.com/tutorial-abou ... scriptors/) to send individual keystrokes or mouse movements. The main usb HID class specs available on http://www.usb.org/developers/hidpage/ isn't too clear about how the sequence of HID Report Descriptors commands should be built for individual use cases as well.

however, i happened to run into this article about turning the pro-micro into a USB keyboard
https://www.sparkfun.com/tutorials/337
i'd think we can implement similar API so that code like

Code: Select all

Keyboard.write('z');  // send a 'z' to the computer via Keyboard HID
would basically work that way

blue pill / maple mini is very much an equivalent to the pro-micro in that aspecs, it is very usable to make diy stm32duino usb keyboards

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

Re: USB Composite library question

Post by RogerClark » Wed Aug 15, 2018 12:18 pm

I agree

Don’t instantiate an instance of the library from inside the library.

edogaldo
Posts: 432
Joined: Fri Jun 03, 2016 8:19 am

Re: USB Composite library question

Post by edogaldo » Wed Aug 15, 2018 6:27 pm

+1 for letting clients declare the objects they need.

Cheers, E.

User avatar
madias
Posts: 1198
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: USB Composite library question

Post by madias » Wed Aug 15, 2018 8:57 pm

also +1 and again a big thank you for this valuable contribution (this library)

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

Re: USB Composite library question

Post by arpruss » Thu Aug 16, 2018 3:00 am

ag123 wrote:
Wed Aug 15, 2018 6:10 am
i'd think we can implement similar API so that code like

Code: Select all

Keyboard.write('z');  // send a 'z' to the computer via Keyboard HID
would basically work that way
Yes, the library does that, but you need some init code first.

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

Re: USB Composite library question

Post by arpruss » Thu Aug 16, 2018 2:45 pm

Done as of 0.90. This will require minor rewrites to all code that uses the library. I've revised all the examples to give guidance and added a brief upgrading guide file.

Whopper
Posts: 2
Joined: Wed Aug 22, 2018 1:34 pm

Re: USB Composite library question

Post by Whopper » Wed Aug 22, 2018 1:52 pm

While I have absolutely no experience of using your library, I would agree to not instantiating an instance of an object within the library that defines it.

I have had numerous ... discussions ... with OO programmer who want to build systems where EVERY object and meathod is created at signon. This is a horrendous waste of CPU time and resources. Thought needs to be given to what SHOULD be included in a library and what can be left out.

Whopper

User avatar
mrburnette
Posts: 3001
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: USB Composite library question

Post by mrburnette » Wed Aug 22, 2018 4:54 pm

Whopper wrote:
Wed Aug 22, 2018 1:52 pm
<•••>
I have had numerous ... discussions ... with OO programmer who want to build systems where EVERY object and meathod is created at signon. This is a horrendous waste of CPU time and resources. Thought needs to be given to what SHOULD be included in a library and what can be left out.
<•••>
There is a disconnect between OOP PC/minicomputer programmers and embedded system programmers. With gigabytes of 64-bit RAM and an OS with a decent VM manager, there is no reason to not instantiate objects before use; it also sets the application memory footprint early so the OS can efficiently manage the app.

Libraries in Arduino account to a large degree for the popularity of the Arduino-ecosystem. Frankly, many weak programmers simply could not interface with complex sensors without libraries. Libraries and h/w use examples get a novice programmer "up and running" quickly. Adafruit and Sparkfun also understand that sensor libraries sell over-priced hardware; a portion of the over-pricing going to online forums and chat support.

Many libraries would be better written as straight use functions. Often the need for OOP is simply to encapsulate the library ... something better done in some cases with namespace.

Ray

Post Reply