Skip to content

Commit

Permalink
[hid_updates] draft of uhid device for HID unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beatboxchad committed Jun 10, 2024
1 parent 0c8f880 commit 2632798
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions testsuite/classlibrary/TestHID.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
HIDTest : UnitTest {
classvar <device;
classvar <addr;
*setUpClass {
var sync = CondVar.new;
var cb = {|msg, time, addr, rcvPort|
var devPath=msg[1].asString;
devPath.postln;
HID.findAvailable;
device = HID.openPath(devPath)
};
Class.initClassTree(HID);
Class.initClassTree(OSCFunc);
super.initClass();

addr=NetAddr("127.0.0.1", 57420);
OSCFunc.new(cb, '/device_created');

sync.wait({HID.running});
addr.sendMsg('/test_hidraw_device_path');

}
*tearDownClass {
addr.sendMsg('/quit');
}
test_Meow {
HIDTest.device.path.postln;
this.assertEquals(1,1);
}
}
59 changes: 59 additions & 0 deletions tools/test_hidapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
from hidtools.device.base_gamepad import AsusGamepad

import signal
import sys
import time
import os

from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import BlockingOSCUDPServer
from pythonosc.udp_client import SimpleUDPClient

class TestGamepad():
def __init__(self):
print("hidapi test tool")
self.device = AsusGamepad()
self.device.create_kernel_device()
self.client = SimpleUDPClient("127.0.0.1", 57120)

def quit(self):
print("quitting uhid service")
self.device.destroy()
exit(0)

def pid(self):
self.client.send_message("/test_hidraw_pid", os.getpid())

def device_path(self, osc_path):
try:
print(osc_path)
pathname = self.device.hidraw_nodes[0]
print(pathname)
self.client.send_message("/device_created", pathname)
return pathname
except:
return None

def default_handler(path, *args):
print(f"DEFAULT {path}: {args}")

def main():
device = TestGamepad()
print("main up")
signal.signal(signal.SIGTERM, device.quit)

dispatcher = Dispatcher()
dispatcher.map("/quit", device.quit)
dispatcher.map("/test_hidraw_device_path", device.device_path)
dispatcher.set_default_handler(default_handler)

server = BlockingOSCUDPServer(("localhost", 57420), dispatcher)
try:
server.serve_forever()
except KeyboardInterrupt:
device.quit()


if __name__ == "__main__":
main()

0 comments on commit 2632798

Please sign in to comment.