Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ libspnavdev.a
libspnavdev.so*
libspnavdev.dll
test
build*/
.vscode/
52 changes: 52 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# libspnavdev - direct 6dof device handling library
# Copyright (C) 2021 Collabora, Ltd.
#
# SPDX-License-Identifier: GPL-3-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.1...3.19)
project(
libspnavdev
VERSION 0.1
LANGUAGES C)

find_package(HIDAPI)


add_library(spnavdev
src/serdev.c
src/spnavdev.c
src/usbdev.c
)

if(HIDAPI_FOUND)
set(HAVE_HIDAPI TRUE)
target_link_libraries(spnavdev PRIVATE ${HIDAPI_LIBRARY})
target_include_directories(spnavdev PRIVATE ${HIDAPI_INCLUDE_DIR})
endif()
target_include_directories(spnavdev PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>)

configure_file(config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
target_include_directories(spnavdev PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_compile_definitions(spnavdev PRIVATE HAVE_CONFIG_H)

if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
# We are our own project, not a subproject
if(WIN32)
add_subdirectory(examples/win32)
else()
add_subdirectory(examples/unix)
endif()
endif()
21 changes: 21 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
libspnavdev - direct 6dof device handling library
Copyright (C) 2020 John Tsiombikas <nuclear@member.fsf.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPNAVDEV_CONFIG_H_
#define SPNAVDEV_CONFIG_H_
#cmakedefine HAVE_HIDAPI
#endif
20 changes: 20 additions & 0 deletions examples/unix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# libspnavdev - direct 6dof device handling library
# Copyright (C) 2021 Collabora, Ltd.
#
# SPDX-License-Identifier: GPL-3-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

add_executable(test test.c)
target_link_libraries(test PRIVATE spnavdev)
9 changes: 8 additions & 1 deletion examples/unix/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ int main(int argc, char **argv)
fd_set rdset;
union spndev_event ev;
const char *s;
int led=0;

signal(SIGINT, sighandler);

Expand Down Expand Up @@ -53,11 +54,17 @@ int main(int argc, char **argv)

case SPNDEV_BUTTON:
if((s = spndev_button_name(dev, ev.bn.num))) {
printf("button %d (%s) ", ev.bn.num, s);
printf("button %d (\"%s\") ", ev.bn.num, s);
} else {
printf("button %d ", ev.bn.num);
}
puts(ev.bn.press ? "pressed" : "released");

if (ev.bn.press) {
spndev_set_led(dev, led);
led = !led;
}

break;

default:
Expand Down
20 changes: 20 additions & 0 deletions examples/win32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# libspnavdev - direct 6dof device handling library
# Copyright (C) 2021 Collabora, Ltd.
#
# SPDX-License-Identifier: GPL-3-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

add_executable(test test.c)
target_link_libraries(test PRIVATE spnavdev)
62 changes: 62 additions & 0 deletions examples/win32/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include "spnavdev.h"

static struct spndev *dev;
static int quit = 0;

int main(int argc, char **argv)
{
int fd;
union spndev_event ev;
const char *s;
int led=0;

if(!(dev = spndev_open(argv[1]))) {
fprintf(stderr, "Failed to open 6dof device %s\n", argv[1] ? argv[1] : "");
return 1;
}
fd = spndev_fd(dev);

printf("Monitoring device, ctrl-c to quit\n");

while(!quit) {

if (spndev_process(dev, &ev)) {
switch (ev.type) {
case SPNDEV_MOTION:
printf("motion: T[%+6d %+6d %+6d] R[%+6d %+6d %+6d]\n",
ev.mot.v[0], ev.mot.v[1], ev.mot.v[2], ev.mot.v[3],
ev.mot.v[4], ev.mot.v[5]);
break;

case SPNDEV_BUTTON:
if ((s = spndev_button_name(dev, ev.bn.num))) {
printf("button %d (\"%s\") ", ev.bn.num, s);
} else {
printf("button %d ", ev.bn.num);
}
puts(ev.bn.press ? "pressed" : "released");

if (ev.bn.press) {
spndev_set_led(dev, led);
spndev_write_lcd(dev, 0xAA);
spndev_set_lcd_bl(dev, led);
led = !led;
}

break;

default:
break;
}
}

}

spndev_close(dev);
return 0;
}
15 changes: 11 additions & 4 deletions src/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "spnavdev.h"

struct axisprop {
char *name;
const char *name;
int minval, maxval, deadz;
};

struct spndev {
char *name, *path;
int usb_vendor, usb_product;
uint16_t usb_vendor, usb_product;
unsigned internal_id;

int fd; /* UNIX file descriptor */
void *handle; /* Win32 handle */

int num_axes, num_buttons;
struct axisprop *aprop;
char **bn_name;
const char **bn_name;
int led;
int lcdbl;

void *uptr, *drvdata;

Expand All @@ -44,10 +46,15 @@ struct spndev {

void (*setled)(struct spndev*, int led);
int (*getled)(struct spndev*);

void (*setlcdbl)(struct spndev *, int bl);
int (*getlcdbl)(struct spndev *);

void (*writelcd)(struct spndev *, int state);
};


int spndev_usb_open(struct spndev *dev, const char *devstr, int vend, int prod);
int spndev_usb_open(struct spndev *dev, const char *devstr, uint16_t vend, uint16_t prod);
int spndev_ser_open(struct spndev *dev, const char *devstr);


Expand Down
Loading