Handle deadzone, return event type on read_event

This commit is contained in:
Thibaud Gasser 2018-05-23 14:42:09 +02:00
parent 1a2e82040d
commit 1902bdaf7a
4 changed files with 45 additions and 16 deletions

View File

@ -2,8 +2,8 @@ TARGET = spnav
TARGET_LIB = libspnavhdi.so # target lib TARGET_LIB = libspnavhdi.so # target lib
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -fPIC -pedantic -O2 # C flags for building library #CFLAGS = -Wall -Wextra -fPIC -pedantic -O2 # C flags for building library
#CFLAGS = -Wall -Wextra -pedantic -g # C flags for developpement CFLAGS = -Wall -Wextra -pedantic -g # C flags for developpement
LDFLAGS = -Wall -Wextra -O2 LDFLAGS = -Wall -Wextra -O2

3
main.c
View File

@ -148,7 +148,7 @@ int main(int argc, char const *argv[])
spnav_event ev; spnav_event ev;
spnav_open(); spnav_open();
for (;;) { for (;;) {
spnav_wait_event(&ev); spnav_wait_event_timeout(&ev, 400);
switch (ev.type) { switch (ev.type) {
case MOTION: case MOTION:
@ -156,6 +156,7 @@ int main(int argc, char const *argv[])
printf("rx=%d, ry=%d, rz=%d\n", ev.motion.rx, ev.motion.ry, ev.motion.rz); printf("rx=%d, ry=%d, rz=%d\n", ev.motion.rx, ev.motion.ry, ev.motion.rz);
break; break;
case BUTTON: case BUTTON:
printf("bnum=%d, pressed=%d\n", ev.button.bnum, ev.button.press);
break; break;
} }

49
spnav.c
View File

@ -37,8 +37,21 @@ int convert_input(int first, unsigned char val) {
} }
} }
bool in_deadzone(unsigned char *data) {
/* data[0] is the event type */
int i;
for (i=1; i<SPNAV_NAXIS; i++) {
if (data[i] != 0) {
return false;
}
}
DEBUG_PRINT("in_deadzone\n");
return true;
}
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[64]; unsigned char buf[64];
int i;
int nbytes = hid_read_timeout(device, buf, sizeof(buf), ms); int 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");
@ -51,26 +64,42 @@ int read_event(hid_device *device, spnav_event* ev, int ms) {
switch (ev->type) { switch (ev->type) {
case TRANSLATION: case TRANSLATION:
ev->type = 1; if (in_deadzone(buf)) {
ev->type = 0;
return ev->type;
}
ev->motion.type = 1;
ev->motion.x = convert_input((buf[1] & 0x0000ff), buf[2]); ev->motion.x = convert_input((buf[1] & 0x0000ff), buf[2]);
ev->motion.y = convert_input((buf[3] & 0x0000ff), buf[4]); ev->motion.y = convert_input((buf[3] & 0x0000ff), buf[4]);
ev->motion.z = convert_input((buf[5] & 0x0000ff), buf[6]); ev->motion.z = convert_input((buf[5] & 0x0000ff), buf[6]);
for (i=0; i < SPNAV_NAXIS; i++) {
ev->motion.data[i] = buf[i];
}
// DEBUG_PRINT("Translation x=%d, y=%d, z=%d\n", ev->motion.x, ev->motion.y, ev->motion.z); // DEBUG_PRINT("Translation x=%d, y=%d, z=%d\n", ev->motion.x, ev->motion.y, ev->motion.z);
break; break;
case ROTATION: case ROTATION:
ev->type = 1; if (in_deadzone(buf)) {
ev->type = 0;
return ev->type;
}
ev->motion.type = 1;
ev->motion.rx = convert_input((buf[1] & 0x0000ff), buf[2]); ev->motion.rx = convert_input((buf[1] & 0x0000ff), buf[2]);
ev->motion.ry = convert_input((buf[3] & 0x0000ff), buf[4]); ev->motion.ry = convert_input((buf[3] & 0x0000ff), buf[4]);
ev->motion.rz = convert_input((buf[5] & 0x0000ff), buf[6]); ev->motion.rz = convert_input((buf[5] & 0x0000ff), buf[6]);
for (i=0; i < SPNAV_NAXIS; i++) {
ev->motion.data[i] = buf[i];
}
// DEBUG_PRINT("Rotation rx=%d, ry=%d, rz=%d\n", ev->motion.rx, ev->motion.ry, ev->motion.rz); // DEBUG_PRINT("Rotation rx=%d, ry=%d, rz=%d\n", ev->motion.rx, ev->motion.ry, ev->motion.rz);
break; break;
case BTN: case BTN:
ev->type = 2; ev->button.type = 2;
DEBUG_PRINT("Buttons: %d %d\n", /* btn 1 */buf[1] & 0x01, /* btn 2 */ buf[1] & 0x02); ev->button.press = buf[1] == 0x01;
ev->button.bnum = buf[1];
//DEBUG_PRINT("Buttons: %d %d\n", /* btn 1 */buf[1] & 0x01, /* btn 2 */ buf[1] & 0x02);
break; break;
} }
return 0; return ev->type;
} }
int set_led(hid_device *dev, char state) { int set_led(hid_device *dev, char state) {
@ -81,7 +110,7 @@ int set_led(hid_device *dev, char state) {
nbytes, sizeof(led_data)); nbytes, sizeof(led_data));
return -1; return -1;
} }
return 0; return nbytes;
} }
int spnav_open() { int spnav_open() {
@ -123,17 +152,15 @@ int spnav_wait_event(spnav_event *event) {
DEBUG_PRINT("spnav_wait_event(): device not connected.\n"); DEBUG_PRINT("spnav_wait_event(): device not connected.\n");
return -1; return -1;
} }
read_event(device, event, -1); return read_event(device, event, -1);;
return 0;
} }
spnav_wait_event_timeout(spnav_event *event, int milliseconds) { int spnav_wait_event_timeout(spnav_event *event, int milliseconds) {
if (device == NULL) { if (device == NULL) {
DEBUG_PRINT("spnav_wait_event_timeout(): device not connected.\n"); DEBUG_PRINT("spnav_wait_event_timeout(): device not connected.\n");
return -1; return -1;
} }
read_event(device, event, milliseconds); return read_event(device, event, milliseconds);;
return 0;
} }
/* /*
int spnav_wait_event(spnav_event *event); int spnav_wait_event(spnav_event *event);

View File

@ -3,6 +3,7 @@
#define SPNAV_VENDOR_ID 0x046d #define SPNAV_VENDOR_ID 0x046d
#define SPNAV_PRODUCT_ID 0xc626 #define SPNAV_PRODUCT_ID 0xc626
#define SPNAV_NAXIS 6
#ifdef _WIN32 #ifdef _WIN32
#define SPNAV_API_EXPORT __declspec(dllexport) #define SPNAV_API_EXPORT __declspec(dllexport)
@ -25,12 +26,12 @@ struct event_motion {
int x, y, z; int x, y, z;
int rx, ry, rz; int rx, ry, rz;
unsigned int period; unsigned int period;
int *data; int data[SPNAV_NAXIS];
}; };
struct event_button { struct event_button {
int type; int type;
int press; bool press;
int bnum; int bnum;
}; };