Shop OBEX P1 Docs P2 Docs Learn Events
Super Byte: USB: Raw HID — Parallax Forums

Super Byte: USB: Raw HID

Starting point of discussion: https://pjrc.com/teensy/rawhid.html
If you have a teensy run this example: https://pjrc.com/teensy/rawhid_test.exe
.........................................................
C:\hidapitester>rawhid_test.exe
found rawhid device

recv 64 bytes:
AB CD 00 0C 00 07 00 07 00 08 00 08 03 FF 03 FF
00 10 00 08 00 09 03 F1 03 F7 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 15

recv 64 bytes:
AB CD 00 0D 00 07 00 07 00 08 00 08 03 FF 03 FF
00 10 00 09 00 08 03 F1 03 F7 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 16

recv 64 bytes:
AB CD 00 0C 00 07 00 08 00 08 00 08 03 FF 03 FF
00 10 00 09 00 08 03 F1 03 F7 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 17

got key 'a', sending...

recv 64 bytes:
AB CD 00 0C 00 07 00 07 00 08 00 08 03 FF 03 FF
00 10 00 09 00 09 03 F1 03 F7 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 18
.....................................................................
Teensy source code: https://pjrc.com/teensy/rawhid.tar.gz which includes code for windows: rawhid/hid_WINDOWS.c at bottom of this file.
Information on https://pjrc.com/teensy/rawhid.html for Teensy has been reliable for 15 years.
At the bottom of this page note the link to python code: https://pjrc.com/teensy/rawhid_python.zip which contains: rawhid.py an old but important example which calls a python library: hid.
C:\hidapitester>cd C:\hidapitester\rawhid_python
I should have installed this first:
C:\WINDOWS\system32>pip install wheel
Collecting wheel
Downloading wheel-0.38.4-py3-none-any.whl (36 kB)
Installing collected packages: wheel
Successfully installed wheel-0.38.4
Next I installed: https://pypi.org/project/hid/
C:\hidapitester\rawhid_python>pip install hid
Collecting hid
Downloading hid-1.0.5.tar.gz (3.9 kB) http://biodev.ece.ucsb.edu:4040/bisque/dev/+simple/hid
Using legacy 'setup.py install' for hid, since package 'wheel' is not installed.
Installing collected packages: hid
Running setup.py install for hid ... done
Successfully installed hid-1.0.5
The example at the bottom of this page should be noted.
Install hidapi: https://github.com/libusb/hidapi but for Windows read https://github.com/libusb/hidapi#building-on-windows
Do not install: https://github.com/libusb/hidapi/releases precompiled library files.
Install: https://github.com/libusb/hidapi/archive/refs/tags/hidapi-0.12.0.zip
Unzip: move to: C:\hidapitester\rawhid_python\hidapi-hidapi-0.12.0\hidapi-hidapi-0.12.0\windows\hidapi.vcxproj and run: in Visual Studio 2022:
...............................................
Rebuild started...
1>------ Rebuild All started: Project: hidapi, Configuration: Debug x64 ------
1>hid.c
1> Creating library C:\hidapi\windows\x64\Debug\hidapi.lib and object C:\hidapi\windows\x64\Debug\hidapi.exp
1>hidapi.vcxproj -> C:\hidapi\windows\x64\Debug\hidapi.dll WHERE best library made
2>------ Rebuild All started: Project: hidtest, Configuration: Debug x64 ------
2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppBuild.targets(517,5): warning MSB8028: The intermediate directory (x64\Debug) contains files shared from another project (hidapi.vcxproj). This can lead to incorrect clean and rebuild behavior.
2>test.c
2>hidtest.vcxproj -> C:\hidapi\windows\x64\Debug\hidtest.exe
2>Done building project "hidtest.vcxproj".
Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
Elapsed 00:05.481 ==========
....................................................
Copy hidapi.dll and hidapi.lib to the root of python e.g. C:\Python311 where ever python.exe is.
C:\hidapitester\rawhid_python\hidapi-hidapi-0.12.0\hidapi-hidapi-0.12.0\windows\hidtest.vcxproj
Using Visual Studio 2022 compile and run hidtest.exe
Run C:\hidapi\windows\x64\Debug\hidtest.exe
...............................................
hidapi test/example tool. Compiled with hidapi version 0.13.0, runtime version 0.13.0.
Compile-time version matches runtime version of hidapi.

Device Found
type: 16c0 0486
path: \?\HID#VID_16C0&PID_0486&MI_01#7&507ebe2&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
serial_number: 10550300
Manufacturer: Teensyduino
Product: Teensyduino RawHID
Release: 280
Interface: 1
Usage (page): 0x4 (0xffc9)
Bus type: 1

Device Found
type: 16c0 0486
path: \?\HID#VID_16C0&PID_0486&MI_00#7&18380820&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
serial_number: 10550300
Manufacturer: Teensyduino
Product: Teensyduino RawHID
Release: 280
Interface: 0
Usage (page): 0x200 (0xffab)
Bus type: 1
.........................................
Source code for example:
.........................................
https://pypi.org/project/hid/
depends upon: https://github.com/libusb/hidapi
C:\hidapi\python
Plugin Teensy 4.1
.........................................
import hid

vid = 0x16C0 # Change it for your device
pid = 0x0486 # Change it for your device

with hid.Device(vid, pid) as h:
print(f'Device manufacturer: {h.manufacturer}')
print(f'Product: {h.product}')
print(f'Serial Number: {h.serial}')
.......................................
Output:
Device manufacturer: Teensyduino
Product: Teensyduino RawHID
Serial Number: 10550300
........................................
Another tool worth having a play with is hidapitester:
https://www.pjrc.com/teensy/rawhid.html USB: Raw HID
https://todbot.com/blog/2019/06/13/hidapitester-command-line-program-to-exercise-hidapi/
Running hidapitester.ino on Teensy 4.1
https://github.com/todbot/hidapitester
C:\hidapitester>hidapitester --vidpid 16C0/0486 --list-detail
16C0/0486: Teensyduino - Teensyduino RawHID
vendorId: 0x16C0
productId: 0x0486
usagePage: 0xFFAB
usage: 0x0200
serial_number: 10550300
interface: 0
path: \?\hid#vid_16c0&pid_0486&mi_00#7&18380820&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

C:\hidapitester>hidapitester --vidpid 16C0/0486 --usagePage 0xFFAB --open --send-output 0x4f,33,22,0xff --timeout 900 --read-input
Opening device, vid/pid:0x16C0/0x0486, usagePage/usage: FFAB/0
Device opened
Writing output report of 64-bytes...wrote 65 bytes:
4F 21 16 FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Reading 64-byte input report 0, 900 msec timeout...read 64 bytes:
AB CD 00 0C 00 07 00 07 00 08 00 08 03 FF 03 FF 00 10 00 09 00 09 00 07 03 F0 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08
Close device
............................................
Another project to consider is pywinusb:
https://github.com/rene-aguirre/pywinusb
C:\WINDOWS\system32>py -3 -m pip install pywinusb
Collecting pywinusb
Using cached pywinusb-0.4.2.zip (61 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pywinusb
Building wheel for pywinusb (setup.py) ... done
Created wheel for pywinusb: filename=pywinusb-0.4.2-py3-none-any.whl size=43445 sha256=d58f66d373c694265af61a033ba02b199c3bbaa0878728609f53e4a8bc89a899
Stored in directory: c:\users\kevin\appdata\local\pip\cache\wheels\c4\cb\8c\ed338f414ede2fbd367c7c71eb553c3d99d8582df8714940fb
Successfully built pywinusb
Installing collected packages: pywinusb
Successfully installed pywinusb-0.4.2
Example: raw_data.py create new project in Visual Studio 2022:
Choose a device to monitor raw input reports:

0 => Exit
1 => Teensyduino Teensyduino RawHID(vID=0x16c0, pID=0x0486)
2 => Teensyduino Teensyduino RawHID(vID=0x16c0, pID=0x0486)
3 => SINO WEALTH USB KEYBOARD(vID=0x258a, pID=0x0001)
4 => SINO WEALTH USB KEYBOARD(vID=0x258a, pID=0x0001)
5 => SINO WEALTH USB KEYBOARD(vID=0x258a, pID=0x0001)
Device ('0' to '5', '0' to exit?) [press enter after number]:

Waiting for data...
Press any (system keyboard) key to stop...
Raw data: [0, 171, 205, 0, 8, 0, 8, 0, 7, 0, 8, 0, 8, 3, 255, 3, 255, 0, 16, 0, 9, 0, 9, 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109]
Raw data: [0, 171, 205, 0, 7, 0, 7, 0, 7, 0, 8, 0, 8, 3, 255, 3, 255, 0, 16, 0, 9, 0, 9, 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110]
Raw data: [0, 171, 205, 0, 8, 0, 7, 0, 7, 0, 8, 0, 8, 3, 255, 3, 255, 0, 16, 0, 9, 0, 8, 0, 6, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111]
Raw data: [0, 171, 205, 0, 8, 0, 7, 0, 7, 0, 8, 0, 8, 3, 255, 3, 255, 0, 16, 0, 9, 0, 9, 0, 7, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112]
The thread 0x2 has exited with code 0 (0x0).
The thread 0x3 has exited with code 0 (0x0).
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
The program 'python.exe' has exited with code 0 (0x0).
https://github.com/rene-aguirre/pywinusb
.....................................
https://github.com/libusb/hidapi/blob/master/BUILD.md
Embedding HIDAPI directly into your source tree
https://github.com/libusb/hidapi/blob/master/windows/hid.c and investigate porting this code to P2.
C:\hidapi\hidapi-master.zip\hidapi-master\windows
https://github.com/libusb/hidapi/blob/master/windows/hidapi.vcxproj and build hidapi.dll
Download hid-1.0.5.tar.gz (3.9 kB) and see what functions are available. biodev.ece.ucsb.edu:4040/bisque/dev/+simple/hid
Read about Python Bindings: Calling C or C++ From Python: https://realpython.com/python-bindings-overview/
.....................................
The goal is to create code to run on the P2 and provide USB: Raw HID. Do you have any ideas as to how this might be achieved?
Kevin

c
c
37K

Comments

  • RaymanRayman Posts: 14,754

    Looks like it wouldn't be terribly hard to convert the example you found here: https://www.pjrc.com/teensy/rawhid.html
    to work on P2.
    Guess I'd use FlexProp C so wouldn't have to convert between C and some other language.
    Do have to change all the platform specific code, some work but looks doable.

    Then, guess you'd use the Serial Device (slave) accessory to connect a Type-A to micro-USB cable between PC and P2.
    It'd be interesting to see this working.
    The PC side code should work as is, I think...

    Not exactly sure how useful this is though as one presumable already has an FTDI USB serial chip in the system.
    But, I guess if you didn't have that, then could be useful.
    What did you have in mind using this for?

  • @Rayman Here is one possible use:

    with a document attached.
    Merry Christmas
    Kevin

  • I don't think this is a good example of using HID. The GT911 connects to the board using I2C which can be done natively on the P1/P2.
    Putting USB on the front of this board just makes it more complicated for the P2 and simpler for Windows.

    However, thou the HID code looks interesting and I may convert it just to play around with some HID examples.

    Mike

  • Well, the teensy comes with a built in USB 2.0 controller so in order to do what the teensy is doing you would have to write a USB controller for the P2 since it does not come with one.

    Mike

  • RaymanRayman Posts: 14,754

    The touch screen might work with the existing USB keyboard/mouse driver?
    Either way, that needs a USB host and I think the teensy becomes a USB slave with this HID code.

    Does this HID code need USB 2.0? That would be a problem...

  • @iseries Thank you for your interest.

    Well, the teensy comes with a built in USB 2.0 controller so in order to do what the teensy is doing you would have to write a USB controller for the P2 since it does not come with one.

    I have a Teensy 4.1 but the Teensy 2 has been around for a long time: probably 2012.

    I have continued to search for good data:

    BuyDisplay.com page: 7" inch USB Capacitive Touch Panel Screen Controller for Rasperry PI.

    Interface description: Capacitive Screen Digitizer IIC to USB Controller.

    GT911 Touchscreen Driver https://github.com/blackketter/GT911. Written in C++ for Arduino Library.

    Goodix Touch HID Shenzhen Huiding Technology Co.,Ltd. Supplier of GT911.

    Nanjing Qinheng Electronics Co., Ltd, 18 Ningshuang Road, Yuhuatai District, Nanjing City, Jiangsu 210012. Probably supplier of HX6518.

    This helped me find ric355:

    new full-speed USB device number 6 using xhci_hcd
    [ 2220.452161] usb 1-1.4.2: New USB device found, idVendor=1a86, idProduct=e5e3, bcdDevice= 0.00
    [ 2220.452177] usb 1-1.4.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
    [ 2220.452188] usb 1-1.4.2: Product: USB2IIC_CTP_CONTROL
    [ 2220.452198] usb 1-1.4.2: Manufacturer: wch.cn

    ric355 excellent research Generic Chinese capacitive touch screen support.

    Unfortunately the code by ric355 is written in Pascal. However ric355 research is well presented in the Ultibo Forum and makes interesting reading. See ric355 code attached below. https://github.com/ric355/ultibocapacitivetouch. ultibocapacitivetouch-master.zip

    When I plugin the USB cable running from the HX6518 to my Windows 10 PC I obtain two connections:



    I have just received a delivery from China much to my surprise!



    @Rayman Thank you for your comments. Most interested in:
    The touch screen might work with the existing USB keyboard/mouse driver?

    That would be great if possible.
    If you create anything that I could play with or test I would appreciate it very much.
    My email address is in my profile.
    Kevin.

  • @kg1
    As @iseries and others have pointed out before, wouldn't it be orders of magnitude simpler to just forget about all this complicated USB stuff and do it by simple I2C, skipping the HX6518 board?

  • @Electrodude I was not expecting the parcel of components from China until 2nd Feb, but they arrive: A great surprise! I have had other parcels stolen while in transit to the Chinese departure airport or sent to the wrong country. I will Try I2C.

  • There's also BradC's "california dreaming" demo code, that runs on the Propeller 1, as a possible starting point for HID

    That demo 'types' the words to the song 'California Dreaming' into a PC via USB using HID

    It may also be worth looking at porting TinyUSB to the P2. While that might be a considerable effort, there would be lots of benefit

Sign In or Register to comment.