Home

Awesome

MoonUSB: Lua bindings for libusb

MoonUSB is a Lua binding library for libusb, allowing applications to access and use USB devices.

MoonUSB also provides a submodule for emulating USB devices via USB/IP.

It runs on GNU/Linux <!-- and on Windows (MSYS2/MinGW) --> and requires Lua (>=5.3) and libusb (>= 1.0.24).

Author: Stefano Trettel

Lua logo

License

MIT/X11 license (same as Lua). See LICENSE.

Documentation

See the Reference Manual.

Getting and installing

Setup the build environment as described here, then:

$ git clone https://github.com/stetre/moonusb/
$ cd moonusb
moonusb$ make
moonusb$ sudo make install

Example

The example below lists the devices currently attached to the system.

Other examples can be found in the examples/ directory.

-- MoonUSB example: hello.lua
local usb = require("moonusb")

local ctx = usb.init()

local devices = ctx:get_device_list()

for i, dev in ipairs(devices) do
   local descr = dev:get_device_descriptor()
   local devhandle = dev:open()
   print(string.format("USB %s - bus:%d port:%d %.4x:%.4x %s %s (%s)",
      descr.usb_version, dev:get_bus_number(), dev:get_port_number(),
      descr.vendor_id, descr.product_id,
      devhandle:get_string_descriptor(descr.manufacturer_index) or "???",
      devhandle:get_string_descriptor(descr.product_index) or "???",
      descr.class))
   devhandle:close()
   dev:free()
end

The script can be executed at the shell prompt with the standard Lua interpreter:

$ lua hello.lua

See also