Compare commits

..

No commits in common. "master" and "refactoring" have entirely different histories.

3 changed files with 6 additions and 28 deletions

4
main.c
View File

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

29
spnav.c
View File

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

View File

@ -46,7 +46,6 @@ extern "C" {
int SPNAV_API_EXPORT_CALL spnav_open(unsigned short vendor_id, unsigned short product_id); 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_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(spnav_event *event);
int SPNAV_API_EXPORT_CALL spnav_wait_event_timeout(spnav_event *event, int timeout); int SPNAV_API_EXPORT_CALL spnav_wait_event_timeout(spnav_event *event, int timeout);
int SPNAV_API_EXPORT_CALL spnav_sensitivity(double sens); int SPNAV_API_EXPORT_CALL spnav_sensitivity(double sens);