USB HID support on Core?

@satishgn @Dave
Are there any news on getting Keyboard and Mouse into master branch and/or WEB IDE of SparkCore since this post events?

Heya @ryotsuke,

I know there was a bunch of work on this in a branch here:

https://github.com/spark/firmware/tree/feature/usb-hid-support

but Iā€™m not sure if itā€™s still in progress or not.

Thanks!
David

@satishgn
Trying to use this branch with test code

#define SPARK_USB_KEYBOARD  
#include "application.h"

USBKeyboard keyboard;

/* This function is called once at start up ----------------------------------*/
void setup()
{
	keyboard.begin();

}

#define KEY_F13				0xCE
#define KEY_F14				0xCF

/* This function loops forever --------------------------------------------*/
void loop()
{
  keyboard.press(KEY_LEFT_ALT);
  keyboard.press(KEY_F13);
  delay(100);
  keyboard.releaseAll();
  delay(2000);
}

But Iā€™m using it wrong, I think

./obj/src/application.o: In function `setup':
n:\SparkHid\core-firmware\build/../src/application.cpp:35: undefined reference t
o `USBKeyboard::begin()'
./obj/src/application.o: In function `loop':
n:\SparkHid\core-firmware\build/../src/application.cpp:45: undefined reference t
o `USBKeyboard::press(unsigned char)'
n:\SparkHid\core-firmware\build/../src/application.cpp:46: undefined reference t
o `USBKeyboard::press(unsigned char)'
n:\SparkHid\core-firmware\build/../src/application.cpp:48: undefined reference t
o `USBKeyboard::releaseAll()'
./obj/src/application.o: In function `__static_initialization_and_destruction_0'
:
n:\SparkHid\core-firmware\build/../src/application.cpp:30: undefined reference t
o `USBKeyboard::USBKeyboard()'
collect2.exe: error: ld returned 1 exit status
make: *** [core-firmware.elf] ŠžŃˆŠøŠ±ŠŗŠ° 1

Do you have some code samples for this?

Also is it possible to use USB Keyboard + USB Serial at same time? I need to send some info to send back to core

@ryotsuke, although I also wished for the possibility to have composite USB HIDs this is not possible yet.
And if you want to build a USB HID firmware locally you need to alter the #defines inside main.h.


You canā€™t build via the WebIDE or Spark Dev.

It still is not compiling. And Iā€™m already using local compiler on custom branch

@ryotsuke, your main.h should now contain this

//#define SPARK_USB_SERIAL
//#define SPARK_USB_MOUSE
#define SPARK_USB_KEYBOARD

And your application.cpp should not contain

#define SPARK_USB_KEYBOARD  // this is done in main.h

USBKeyboard keyboard;  // this is instanciated in spark_wiring_usbkeyboard.cpp 
                       // as 'USBKeyboard Keyboard;

Between your keypresses you should also have a few ms delay (min. 25).
If it still doesnā€™t build, could you post your error list again?

Iā€™ve removed include from application.cpp, replaced with Keyboard (without manual declaration)
Uncommented in main.h like your sample
Error list is same.

n:\SparkHid\core-firmware\build/ā€¦/src/application.cpp:56: undefined reference t
o USBKeyboard::begin()' n:\SparkHid\core-firmware\build/../src/application.cpp:62: undefined reference t oKeyboardā€™
./obj/src/application.o: In function loop': n:\SparkHid\core-firmware\build/../src/application.cpp:73: undefined reference t oUSBKeyboard::press(unsigned char)'
n:\SparkHid\core-firmware\build/ā€¦/src/application.cpp:74: undefined reference t
o USBKeyboard::press(unsigned char)' n:\SparkHid\core-firmware\build/../src/application.cpp:76: undefined reference t oUSBKeyboard::releaseAll()'
n:\SparkHid\core-firmware\build/ā€¦/src/application.cpp:77: undefined reference t
o `Keyboardā€™
collect2.exe: error: ld returned 1 exit status
make: *** [core-firmware.elf] ŠžŃˆŠøŠ±ŠŗŠ° 1

Then Iā€™d guess you might notactually have switched to the correct branch.
Just to make sure, could you download the three repos

https://github.com/spark/firmware/archive/feature/usb-hid-support.zip
https://github.com/spark/core-common-lib/archive/master.zip
https://github.com/spark/core-communication-lib/archive/master.zip

and build from these - first only as is and then with your adaptions.


Have you got the respective files spark_wiring_usbkeyboard.h/.cpp and spark_wiring_usbmouse.h/.cpp in your local repo?

Have you got the respective files spark_wiring_usbkeyboard.h/.cpp and spark_wiring_usbmouse.h/.cpp in your local repo?

Yes
core-firmware\src\spark_wiring_usbmouse.cpp + spark_wiring_usbkeyboard.cpp and .h files as well
And I get spark_wiring_usbkeyboard.o and spark_wiring_usbmouse.o generated during build in obj subfolder

Current application.cpp looks like http://screenshots.ryotsuke.ru/scr_60ac8331bdf4.png

What toolchain are you using and how do you build?


As I had no problem with the repos I was using for my own USB HID Mouse project, I assumed things would still be so straight forward.
But seeing your troubles, I downloaded a fresh set of the Spark repos and tried your project and got the same result.
So I went back to my own working project and tried it and it doesnā€™t build with these repos either, so I guess something must have changed in the makefile and/or spark-common-lib/spark-communication-lib

Maybe @satishgn and @Dave can have another look and maybe fix whatā€™s gone off track since then.

Used this as a hint.

If branch feature/usb-hid-support is checked out to just before commits
Conditionally compile USBMouse class using macro SPARK_USB_MOUSE and Conditionally compile USBKeyboard class using macro SPARK_USB_KEYBOARD

It compiled OK (On commit 57b1aef00c6f75a55af3c1cae97b8c35002c2f37 [57b1aef])

Not sure if will work, but at least compiled

1 Like

Canā€™t make it send F13/F14 codes
F12 goes ok, but next codes are registered differently
F12 that is 0xCD in spark code is incoming as decimal 123 code
But when I send F13 that as 0xCE is incoming as decimal 44 code, thats weird

Have you tried F13 as Shift+F1?

Otherwise you could try 0xF0/240 for F13 and 0xF1/241 for F14.


I guess 0xCE might be PrintScreen.

I found 0xF0 and 0xF1 indeed should be matching F13/F14
And according to .cpp code they are just written in struct and sent to USB_HID sending functions as code-136 same as for F11/F12
Unfortunaly these keypresses are not registered on my machine at all, like never happened
@satishgn @Dave ?

Shift+F1 is registered as Shift+F1 and is working accordingly

Current code: http://screenshots.ryotsuke.ru/scr_41f9f27cb306.png

Oh, I finally located it
usb_desc.cpp was describing keyboard with max code limied to 0x65 (keyboard key Application Menu)
Iā€™ve changed it like so http://screenshots.ryotsuke.ru/scr_c27ab92c1712.png
And it is working!

Now I need to figure out if it is possible to send both keyboard and mouse events

For this youā€™d need to create a composite HID descriptor.

I once tried to integrate Paul Stoffregens (Teensy/Teensyduino) approach where he cleverly wrapped the respective parts in #ifdef/#endif blocks to build one composite descriptor rather than three seperate ones which canā€™t be combined easily.

But since I had other things more important I didnā€™t follow it through.

If you want to have a go, I could send you a link to the respective Teensyduino files :wink:

1 Like

Unfortunatelly have no c++ experience and HID handling knowledge
Iā€™ve spent al evening today to try to figure it out and ended up with creating 2 enpoints with 2 callbacks with 2 ID dsccriptors
I even figred out I need Report_Id thing
And it has not exploded :smiley: I consider this a success, but still only Keyboard works
May be Iā€™ll zip all current code to take a look at? I might be missing some small thing, as It moslty works
Iā€™ve removed most ifdef to make it easier for me
https://www.dropbox.com/s/lk99jt8tsrbr86l/core-firmware.zip?dl=0 - the most recent version Iā€™ve ended up today. Kbd works, Mouse still not

1 Like

Does anyone know if USB HID support will be released as part of the main branch, specifically for the Photon? We use the Teensy 3.1 / Teensy LC when we need a controller with USD HID but would be great to have a similar facility on the Photon out of the box.

This was asked here also.. hope it helps.. might be possible but requires extensive porting