Posted to tcl by schelte at Mon Oct 31 08:10:04 GMT 2022view raw

  1. package require cffi
  2.  
  3. namespace eval hid {
  4. namespace ensemble create -subcommands {open enumerate}
  5.  
  6. cffi::Wrapper create hidapi /usr/lib64/libhidapi-hidraw.so
  7. cffi::Struct create hid_device_info {
  8. path string
  9. vid ushort
  10. pid ushort
  11. serial pointer.wchar_t
  12. release ushort
  13. mfr pointer.wchar_t
  14. product pointer.wchar_t
  15. page ushort
  16. usage ushort
  17. intf int
  18. next pointer.hid_device_info
  19. }
  20. hidapi functions {
  21. hid_init int {}
  22. hid_open_path pointer.hid_device {
  23. path string
  24. }
  25. hid_enumerate pointer.hid_device_info {
  26. vid ushort
  27. pid ushort
  28. }
  29. hid_free_enumeration void {
  30. devs {pointer.hid_device_info dispose}
  31. }
  32. }
  33.  
  34. hid_init
  35.  
  36. proc open {path} {
  37. return [hid_open_path $path]
  38. }
  39.  
  40. proc enumerate {vid pid} {
  41. set devs [hid_enumerate $vid $pid]
  42. try {
  43. set rc {}
  44. set devptr $devs
  45. set size [dict get [hid_device_info info] Size]
  46. while {![cffi::pointer isnull $devptr]} {
  47. set data [cffi::memory tobinary $devptr $size]]
  48. set struct [hid_device_info frombinary $data]
  49. set devptr [dict get $struct next]
  50. lappend rc [dict remove $struct next]
  51. }
  52. return $rc
  53. } finally {
  54. # Commenting out this makes the error go away
  55. hid_free_enumeration $devs
  56. }
  57. }
  58. }
  59.  
  60. set list [hid enumerate 0x1209 0x5432]
  61. set path [dict get [lindex $list 0] path]
  62. hid open $path
  63.  
  64.  
  65. Value has the wrong type. Pointer tag does not match registered tag.
  66. while executing
  67. "hid_open_path $path"
  68. (procedure "open" line 2)
  69. invoked from within
  70. "hid open $path"
  71. (file "hidapi-bug.tcl" line 64)
  72.