USB Device - HIDAPI or ?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: USB Device - HIDAPI or ?

Post by SparkOut » Thu Mar 04, 2021 10:44 pm

I must admit, I don't know what I'm doing, but on trying this out on Windows 10 (x64) I get an error:

button "HIDAPI Initialize": execution error at line 5 (LCB Error in file D:/path to my LCB folder/LCB_HIDAPI/HIDAPI.lcb at line 250: Unable to bind foreign handler community.livecode.paulmcclernan.hidapi.PointerToHIDDevices)

Let me know if there's anything I'm doing wrong or should try. Apologies for my shortcomings, but accept kudos and my gratitude that you're looking at this. A cross-platform USB library will be a huge benefit to the LiveCode community. Much appreciated.

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Fri Mar 05, 2021 5:02 am

SparkOut wrote:
Thu Mar 04, 2021 10:44 pm
I must admit, I don't know what I'm doing, but on trying this out on Windows 10 (x64) I get an error:

button "HIDAPI Initialize": execution error at line 5 (LCB Error in file D:/path to my LCB folder/LCB_HIDAPI/HIDAPI.lcb at line 250: Unable to bind foreign handler community.livecode.paulmcclernan.hidapi.PointerToHIDDevices)

Let me know if there's anything I'm doing wrong or should try. Apologies for my shortcomings, but accept kudos and my gratitude that you're looking at this. A cross-platform USB library will be a huge benefit to the LiveCode community. Much appreciated.
Thanks for testing!
Hmmm, that IS a strange error, PointerToHIDDevices binds to memcpy, which is part of standard C language, I don't get that error on macOS.
Maybe I'll reinstall Windows this weekend if I have time.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Fri Mar 05, 2021 6:27 am

PaulDaMacMan wrote:
Fri Mar 05, 2021 5:02 am
SparkOut wrote:
Thu Mar 04, 2021 10:44 pm
I must admit, I don't know what I'm doing, but on trying this out on Windows 10 (x64) I get an error:

button "HIDAPI Initialize": execution error at line 5 (LCB Error in file D:/path to my LCB folder/LCB_HIDAPI/HIDAPI.lcb at line 250: Unable to bind foreign handler community.livecode.paulmcclernan.hidapi.PointerToHIDDevices)

Let me know if there's anything I'm doing wrong or should try. Apologies for my shortcomings, but accept kudos and my gratitude that you're looking at this. A cross-platform USB library will be a huge benefit to the LiveCode community. Much appreciated.
Thanks for testing!
Hmmm, that IS a strange error, PointerToHIDDevices binds to memcpy, which is part of standard C language, I don't get that error on macOS.
Maybe I'll reinstall Windows this weekend if I have time.
I think I know the problem, for Windows it should be:

Code: Select all

 binds to "msvcrt>memcpy"
I just now added some Windows specific code, but I have no way of testing on Windows at the moment.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: USB Device - HIDAPI or ?

Post by SparkOut » Fri Mar 05, 2021 8:29 am

Great, thanks Paul. I will try again when I can, if not today then I should have a chance over the weekend.

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Fri Mar 05, 2021 4:36 pm

I might just wrap the parts of libusb that are related to HID descriptors and combine it with this HIDAPI lib wrapper. I think I could do the whole thing but libusb has a LOT to it, like 100 or so functions, and that's just a bit too much work for this hobbyist when all I wanted was just to play around with some HID input devices.
This site is great for information about USB: https://www.beyondlogic.org/usbnutshell/
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Sat Mar 06, 2021 7:12 pm

FYI:
I just dug out a generic Bluetooth (pretty sure it's HID) keyboard that I used to occasionally use with my old Nexus 7 Android tablet and synced it to my laptop to test with this HIDAPI, the result was the keyboard is immediately recognized by the OS (I'm typing on it now), but when HIDAPI scans for the HID device list it locks up the engine and never returns. It never crashes the LC engine though, I just get a permanent system busy cursor (aka "beachball of death"). I'm guessing that maybe it's because the device is already in use by the OS? My laptop's internal keyboard can't be opened by the HIDAPI and I'm pretty sure it's because the OS has already opened that device and so it's in-use and therefore cannot be accessed by the lib. This behavior may be specific to macOS, I don't know.

EDIT:
My guess was wrong, I tested it with a command line app that uses the lib and that reads the device info fine, so it must be something wrong with my code.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Sun Mar 07, 2021 9:18 pm

I did find some code for dumping descriptors to use for parsing incoming data.
These are command line tools that dump the raw descriptors:
https://github.com/todbot/mac-hid-dump
https://github.com/todbot/win-hid-dump
This online JS script can convert the raw dumps into something more human-readable:
https://eleccelerator.com/usbdescreqparser/
Might be possible to translate some of the above into LiveCode builder code (mac-hid-dump looks fairly simple).
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: USB Device - HIDAPI or ?

Post by SparkOut » Sun Mar 07, 2021 11:12 pm

Erm, trying on Windows again I get:

button "HIDAPI Initialize": execution error at line 5 (LCB Error in file <path to LCB>/LCB_HIDAPI/HIAPI.lcb at line 288: Value is not of correct type for assignment to variable - expected type <type: __builtin__.pointer> for assigning to variable tStrPtr in community.livecode.paulmcclernan.hidapi.HIDAPIEnumerate)

If I knew what to do, I'd try and help but I'm not going to be much use, I don't think.

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Mon Mar 08, 2021 2:12 pm

SparkOut wrote:
Sun Mar 07, 2021 11:12 pm
Erm, trying on Windows again I get:

button "HIDAPI Initialize": execution error at line 5 (LCB Error in file <path to LCB>/LCB_HIDAPI/HIAPI.lcb at line 288: Value is not of correct type for assignment to variable - expected type <type: __builtin__.pointer> for assigning to variable tStrPtr in community.livecode.paulmcclernan.hidapi.HIDAPIEnumerate)

If I knew what to do, I'd try and help but I'm not going to be much use, I don't think.
Could you re-download and try again? I made some changes over the weekend and pushed them this morning. Some of the changes included adding some basic error checking where there was none, like checking pointers for <null> ('empty' in LiveCodeScript-speak). I think that's what you're seeing. Not all devices provide all of the information the lib is trying to parse so some may return 'nothing' (<null> or empty).

EDIT:
Hopefully it's all fixed up now (working nicely on macOS), there wasn't actually a problem related to the Bluetooth device I had added for testing. It turned out to be that somewhere along the way I simply mixed up two variable names that I had made too similar ("tDevice" and "mDevices")
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Tue Mar 09, 2021 3:24 pm

So I'm thinking it would be nice to have a property in this library that can be set to a something like a formula that will then be used to post messages back to the LC engine based on the values of incoming bytes
I was thinking a script would set this property by passing a string with a blank space as the byte delimiter, a keyword for the type of processing (ignore byte, per bit, decimal value based) followed by a : colon sub-delimiter and then some token(s) for processing the byte with = as another delimiter followed by the message string to post back to the engine corresponding to that token/value... something like this would be like what you would set it to for a GamePad:

Code: Select all

ignore bits:B8,,B6,B5,B4,B3,B2,B1 dec:>127=DpadUp,<127=DPadDown dec:value=Zaxis bits:value=bitFeildByteFive
in the above "processing template" string:
the first byte would be ignored entirely
the second byte would be parse as bits and send "B8",no message,"B6", "B5",etc. back to the engine based on the on/off value of each bit in the byte
the third byte would be parsed as decimal 0 to 255, if the value is above 127 it would post "DpadUp" back to the engine, less than 127 posts "DPadDown"
the fourth byte would post "Zaxis" along with it's decimal value
the fifth byte would post "bitFeildByteFive" along with 8 characters of 1s or 0s

then in your stack you have scripts to handle these callbacks

Code: Select all

on DpadUp
    -- do upward stuff here
end DpadUp

on Zaxis pZaxisValue
   put pZaxisValue into fld "Depth"
end Zaxis

on bitFeildByteFive pBits
   if char 8 of pBits = "1" then send "MouseUp" to btn "whatevs"
end bitFeildByteFive

on B1
   --- do Button 1 stuff here
end B1
Then these "device template strings" could be to collected for common devices such as Playstation controllers, and then could maybe be included in the library or collected into some sort of database file, or perhaps converted from other sources (SDL's Gamepad/Joystick DB for example).

What say we? Thoughts? Suggestions? Name for the property?
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: USB Device - HIDAPI or ?

Post by SparkOut » Tue Mar 09, 2021 9:29 pm

I can't give you any suggestions or thoughts yet, just starting to look and report.
There is no error thrown now on Windows, and I get a list of devices in the datagrid when I try the HIDAPI List HID Devices button.
Each time I click it, the list refreshes, but there are definitely some data glitches - the serial number will append some random text snipped from elsewhere (such as "9999riptlibrary" or "9999assp" and the Manufacturer frequently has some "non 7bit ASCII" characters appended, such as "Microsoftmey`s" or "Microsoftmec{`A" and frequently the datagrid is populated with columns offset and gaps. I'm not sure what it's parsing but it's certainly impressive, but not working quite properly cross-platform yet.
HIDAPI list.png

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Tue Mar 09, 2021 11:17 pm

SparkOut wrote:
Tue Mar 09, 2021 9:29 pm
I can't give you any suggestions or thoughts yet, just starting to look and report.
There is no error thrown now on Windows, and I get a list of devices in the datagrid when I try the HIDAPI List HID Devices button.
Each time I click it, the list refreshes, but there are definitely some data glitches - the serial number will append some random text snipped from elsewhere (such as "9999riptlibrary" or "9999assp" and the Manufacturer frequently has some "non 7bit ASCII" characters appended, such as "Microsoftmey`s" or "Microsoftmec{`A" and frequently the datagrid is populated with columns offset and gaps. I'm not sure what it's parsing but it's certainly impressive, but not working quite properly cross-platform yet.
HIDAPI list.png
Thanks for testing again!
This, I'm pretty sure, is related to those *w_char (wide-character) string pointers I was complaining about earlier. These are like multi-byte characters like Unicode, but don't seem to be Unicode, or at least they aren't handled well by LiveCode Builder's ZNativeUTF16 variable type? They should be null-terminated strings, but they must be double-byte "nulls" (0 & 0 ) so I "rolled my own" function to try to determine the wide-char string length by looking for two "0"/null bytes in a row. This is a bit of a hack, but it worked OK for me so far. My guess is this is a bit different on Windows since Windows has a different default character enccoding (ISO-somenumber) than macOS (Mac Roman).

I didn't have a chance to reinstall Windows yet, but it's becoming very clear I'm going to need to if I want this to work on other platforms.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Fri Mar 12, 2021 9:43 pm

OK, I reinstalled Windows 10 and got that generic bluetooth keyboard to sync with it (a process that reminded my why, for the last 30+ years, I've usually avoided using Windows OS like it's COVID). I've put in a stop-gap measures to ensure no return/linefeeds get through the process of pulling these "w_char" (wide characters) strings from the data buffer that the list is put into by HIDAPI. So far this gets rid of "garbled" characters & line breaks in the returned list, but it seems it can also truncate the strings ("Logitec(R) Gampad Pro" becomes just "Logitec") and also this breaks the functions on the macOS side so that I only get the first character back from my functions... After some more adjustments... it's become clear that the way that Windows and macOS system HID stuff is parsed and returned from the HIDAPI library is very much not the same. For example the generic BT keyboard on macOS returns "Unknown" for the Manufacturer and Product strings, but on Windows it returns "BEKON". I'll be looking further into these problem some more ASAP.

Looks like this exchange with the maintainer of HIDAPI is zeroing in on the exact problem I'm having:
https://sourceforge.net/p/libusb/mailma ... /26396025/

The good news is that I tested the more important part of retrieving actual data from the devices from a system path for the device and that works as expected. I'm getting the same data for a device on both macOS (10.14.6) & Windows 10.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: USB Device - HIDAPI or ?

Post by PaulDaMacMan » Wed Mar 17, 2021 4:53 pm

SparkOut wrote:
Tue Mar 09, 2021 9:29 pm
I can't give you any suggestions or thoughts yet, just starting to look and report.
There is no error thrown now on Windows, and I get a list of devices in the datagrid when I try the HIDAPI List HID Devices button.
Each time I click it, the list refreshes, but there are definitely some data glitches - the serial number will append some random text snipped from elsewhere (such as "9999riptlibrary" or "9999assp" and the Manufacturer frequently has some "non 7bit ASCII" characters appended, such as "Microsoftmey`s" or "Microsoftmec{`A" and frequently the datagrid is populated with columns offset and gaps. I'm not sure what it's parsing but it's certainly impressive, but not working quite properly cross-platform yet.
HIDAPI list.png
I just pushed a new version, I think I have it all worked out. I'm getting slightly different results for name manufacturer/product on macOS vs. Windows. With the very generic ($5 USD) bluethooth keyboard I tested, it lists the manufacturer as "Unknown" on mac and "BEKEN" on Widows, which might just be a difference in the operating systems, but those fields are for human reading and the API doesn't use them for anything anyway.

Here's a short video of it working on Windows 10, showing some GamePad input data at the very end:
https://www.youtube.com/watch?v=WFUW6iTvozE
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: USB Device - HIDAPI or ?

Post by SparkOut » Wed Mar 17, 2021 8:47 pm

Great, thanks Paul. I can't tell you how much I applaud your efforts.
I will probably get a chance to try it out this weekend, hopefully so, because I'll be away in the Western Isles without decent mobile data or my own computer next week. I will give you feedback as soon as I can.

Post Reply

Return to “Talking LiveCode”