Add spnav_set_nonblocking

This commit is contained in:
Thibaud Gasser 2018-05-31 00:10:16 +02:00
parent f37537fa83
commit f654a62bdb
3 changed files with 28 additions and 6 deletions

4
main.c
View File

@ -24,8 +24,10 @@ int main(int argc, char const* argv[]) {
spnav_open(SPNAV_VENDOR_ID, SPNAV_3D_EXPLORER_PRODUCT_ID);
spnav_sensitivity(0.1);
spnav_deadzone(10);
spnav_set_nonblocking(true);
for (;;) {
spnav_wait_event_timeout(&ev, 400);
// spnav_wait_event_timeout(&ev, 400);
spnav_wait_event(&ev);
switch (ev.type) {
case MOTION:

29
spnav.c
View File

@ -15,6 +15,8 @@
} while (false)
#endif
#define EVENT_BUF 64
enum {
TRANSLATION = 1,
ROTATION = 2,
@ -56,8 +58,15 @@ bool in_deadzone(unsigned char *data, int threshold) {
}
int read_event(hid_device *device, spnav_event *ev, int ms) {
unsigned char buf[64];
int nbytes = hid_read_timeout(device, buf, sizeof(buf), ms);
unsigned char buf[EVENT_BUF];
int nbytes;
if (ms == -1) {
nbytes = hid_read(device, buf, sizeof(buf));
} else {
nbytes = hid_read_timeout(device, buf, sizeof(buf), ms);
}
if (nbytes < 0) {
DEBUG_PRINT("hid_read_timeout() error");
return -1;
@ -67,6 +76,7 @@ int read_event(hid_device *device, spnav_event *ev, int ms) {
}
ev->type = buf[0];
// Fill spnav_event struct
switch (ev->type) {
case TRANSLATION:
if (in_deadzone(buf, SPNAV_DEADZONE_THRESHOLD)) {
@ -134,25 +144,35 @@ int spnav_open(unsigned short vendor_id, unsigned short product_id) {
}
int spnav_close() {
DEBUG_PRINT("spnav_close()\n");
if (!IS_OPEN) {
DEBUG_PRINT("Device is not opened!\n");
return -1;
}
set_led(device, 0);
hid_close(device);
DEBUG_PRINT("Connection to HID device closed!\n");
hid_exit();
IS_OPEN = false;
return 0;
}
int spnav_set_nonblocking(bool nonblock) {
int ret = hid_set_nonblocking(device, nonblock);
if (ret == -1) {
DEBUG_PRINT("Call to hid_set_nonblocking() failed\n");
return -1;
}
DEBUG_PRINT("Nonblocking state is now %d\n", nonblock);
return 0;
}
int spnav_wait_event(spnav_event *event) {
if (device == NULL) {
DEBUG_PRINT("spnav_wait_event(): device not connected.\n");
return -1;
}
return read_event(device, event, -1);
;
}
int spnav_wait_event_timeout(spnav_event *event, int milliseconds) {
@ -161,7 +181,6 @@ int spnav_wait_event_timeout(spnav_event *event, int milliseconds) {
return -1;
}
return read_event(device, event, milliseconds);
;
}
int spnav_sensitivity(double sens) {

View File

@ -46,6 +46,7 @@ extern "C" {
int SPNAV_API_EXPORT_CALL spnav_open(unsigned short vendor_id, unsigned short product_id);
int SPNAV_API_EXPORT_CALL spnav_close(void);
int SPNAV_API_EXPORT_CALL spnav_set_nonblocking(bool nonblock);
int SPNAV_API_EXPORT_CALL spnav_wait_event(spnav_event *event);
int SPNAV_API_EXPORT_CALL spnav_wait_event_timeout(spnav_event *event, int timeout);
int SPNAV_API_EXPORT_CALL spnav_sensitivity(double sens);