They have some examples that emulate an USB keyboard and mouse, and the app shows how to configure the Gadget API to turn the phone into whatever USB device you want.
The repo is unfortunately inactive, but the underlying feature is exposed through a stable Linux kernel API (via ConfigFS), so everything will continue working as long as Android leaves the feature enabled.
You do need to be root, however, since you will essentially be writing a USB device. Then all you have to do is open `/dev/hidg0`, and when you read from this file you will be reading USB HID packets. Write your response and it is sent on the cable.