Fixe axes and add deadzone setter

This commit is contained in:
Thibaud Gasser 2018-05-23 16:18:19 +02:00
parent 39c8cec778
commit 32d86a8582
3 changed files with 24 additions and 11 deletions

1
main.c
View File

@ -148,6 +148,7 @@ int main(int argc, char const *argv[])
spnav_event ev;
spnav_open();
spnav_sensitivity(0.1);
spnav_deadzone(10);
for (;;) {
spnav_wait_event_timeout(&ev, 400);

32
spnav.c
View File

@ -19,6 +19,7 @@ enum {
static bool IS_OPEN = false;
/* Sensitivity is multiplied with every motion (1.0 normal). */
static double SPNAV_SENS = 1.0;
static int SPNAV_DEADZONE_THRESHOLD = 5;
/* HID device for SpaceNavigator mouse */
hid_device *device = NULL;
@ -63,25 +64,25 @@ int read_event(hid_device *device, spnav_event* ev, int ms) {
switch (ev->type) {
case TRANSLATION:
if (in_deadzone(buf, 4)) {
if (in_deadzone(buf, SPNAV_DEADZONE_THRESHOLD)) {
ev->type = 0;
return ev->type;
}
ev->motion.type = 1;
ev->motion.x = (int) SPNAV_SENS * convert_input((buf[1] & 0x0000ff), buf[2]);
ev->motion.y = (int) SPNAV_SENS * convert_input((buf[3] & 0x0000ff), buf[4]);
ev->motion.z = (int) SPNAV_SENS * convert_input((buf[5] & 0x0000ff), buf[6]);
ev->motion.x = (int) (SPNAV_SENS * convert_input((buf[1] & 0x0000ff), buf[2]));
ev->motion.z = - (int) (SPNAV_SENS * convert_input((buf[3] & 0x0000ff), buf[4]));
ev->motion.y = - (int) (SPNAV_SENS * convert_input((buf[5] & 0x0000ff), buf[6]));
// DEBUG_PRINT("Translation x=%d, y=%d, z=%d\n", ev->motion.x, ev->motion.y, ev->motion.z);
break;
case ROTATION:
if (in_deadzone(buf, 4)) {
if (in_deadzone(buf, SPNAV_DEADZONE_THRESHOLD)) {
ev->type = 0;
return ev->type;
}
ev->motion.type = 1;
ev->motion.rx = (int) SPNAV_SENS * convert_input((buf[1] & 0x0000ff), buf[2]);
ev->motion.ry = (int) SPNAV_SENS * convert_input((buf[3] & 0x0000ff), buf[4]);
ev->motion.rz = (int) SPNAV_SENS * convert_input((buf[5] & 0x0000ff), buf[6]);
ev->motion.rx = -(int) (SPNAV_SENS * convert_input((buf[1] & 0x0000ff), buf[2]));
ev->motion.rz = -(int) (SPNAV_SENS * convert_input((buf[3] & 0x0000ff), buf[4]));
ev->motion.ry = (int) (SPNAV_SENS * convert_input((buf[5] & 0x0000ff), buf[6]));
// DEBUG_PRINT("Rotation rx=%d, ry=%d, rz=%d\n", ev->motion.rx, ev->motion.ry, ev->motion.rz);
break;
case BTN:
@ -157,11 +158,22 @@ int spnav_wait_event_timeout(spnav_event *event, int milliseconds) {
}
int spnav_sensitivity(double sens) {
if (sens < 1.0) {
DEBUG_PRINT("Invalid sensitivity value %f", sens);
if (sens <= 0.0) {
DEBUG_PRINT("Invalid sensitivity value %f\n", sens);
return -1;
}
SPNAV_SENS = sens;
DEBUG_PRINT("Sensitivity set to %f\n", SPNAV_SENS);
return 0;
}
int spnav_deadzone(int value) {
if (value < 0) {
DEBUG_PRINT("Invalid deadzone value %d\n", value);
return -1;
}
SPNAV_DEADZONE_THRESHOLD = value;
DEBUG_PRINT("Deadzone threshold set to %d\n", value);
return 0;
}
/*

View File

@ -47,10 +47,10 @@ extern "C" {
int SPNAV_API_EXPORT_CALL spnav_open(void);
int SPNAV_API_EXPORT_CALL spnav_close(void);
// TODO : return event type
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);
int SPNAV_API_EXPORT_CALL spnav_deadzone(int value);
#ifdef __cplusplus
}