Add threshold to deadzone

This commit is contained in:
Thibaud Gasser 2018-05-23 15:06:49 +02:00
parent 1902bdaf7a
commit 6454d27d6e
2 changed files with 7 additions and 15 deletions

17
spnav.c
View File

@ -4,8 +4,6 @@
#include "spnav.h" #include "spnav.h"
#include "hidapi.h" #include "hidapi.h"
#define DEBUG
#ifdef DEBUG #ifdef DEBUG
#define DEBUG_PRINT(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( false ) #define DEBUG_PRINT(...) do{ fprintf( stderr, __VA_ARGS__ ); } while( false )
#else #else
@ -37,11 +35,11 @@ int convert_input(int first, unsigned char val) {
} }
} }
bool in_deadzone(unsigned char *data) { bool in_deadzone(unsigned char *data, int threshold) {
/* data[0] is the event type */ /* data[0] is the event type */
int i; int i;
for (i=1; i<SPNAV_NAXIS; i++) { for (i=1; i<SPNAV_NAXIS; i++) {
if (data[i] != 0) { if (data[i] > threshold) {
return false; return false;
} }
} }
@ -51,7 +49,6 @@ bool in_deadzone(unsigned char *data) {
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");
@ -64,7 +61,7 @@ int read_event(hid_device *device, spnav_event* ev, int ms) {
switch (ev->type) { switch (ev->type) {
case TRANSLATION: case TRANSLATION:
if (in_deadzone(buf)) { if (in_deadzone(buf, 4)) {
ev->type = 0; ev->type = 0;
return ev->type; return ev->type;
} }
@ -72,13 +69,10 @@ int read_event(hid_device *device, spnav_event* ev, int ms) {
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:
if (in_deadzone(buf)) { if (in_deadzone(buf, 4)) {
ev->type = 0; ev->type = 0;
return ev->type; return ev->type;
} }
@ -86,9 +80,6 @@ int read_event(hid_device *device, spnav_event* ev, int ms) {
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:

View File

@ -26,7 +26,7 @@ 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[SPNAV_NAXIS]; int *data;
}; };
struct event_button { struct event_button {
@ -50,9 +50,10 @@ int SPNAV_API_EXPORT_CALL spnav_close(void);
// TODO : return event type // TODO : return event type
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);
//spnav_event SPNAV_API_EXPORT_CALL read_event(int timeout); int SPNAV_API_EXPORT_CALL spnav_sensitivity(double sens);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* SPNAV_H__ */ #endif /* SPNAV_H__ */