version bump of software suppport for Hardware Version 2 + added readme

This commit is contained in:
2018-12-30 21:34:34 +01:00
parent 50ab4dffef
commit 8dfece7633
4 changed files with 109 additions and 107 deletions

View File

@@ -38,6 +38,38 @@ find_package(Qt5Widgets CONFIG REQUIRED)
find_package(Qt5Sql REQUIRED) find_package(Qt5Sql REQUIRED)
# check dependency for atmega
find_path(LIBUSB_INCLUDE_DIR
NAMES "libusb.h"
PATH_SUFFIXES "include" "libusb" "libusb-1.0")
set(CMAKE_PREFIX_PATH usr)
find_library(LIBUSB_LIBRARY
NAMES
"usb"
"libusb"
"libusb-1.0"
"libusb-1.0.so"
PATH_SUFFIXES
"lib"
"lib32"
"lib64")
if(LIBUSB_LIBRARY)
message("usb lib found" ${LIBUSB_LIBRARY})
else()
message("usb lib not found")
endif()
if(LIBUSB_INCLUDE_DIR)
add_definitions(-DATMEGA)
message("usb include dir found")
include_directories(${LIBUSB_INCLUDE_DIR})
else()
message("usb include dir not found")
endif()
# Populate a CMake variable with the sources # Populate a CMake variable with the sources
set(helloworld_SRCS set(helloworld_SRCS
main.cpp main.cpp
@@ -73,5 +105,5 @@ set(helloworld_SRCS
add_executable(Rennbahn ${helloworld_SRCS} ) add_executable(Rennbahn ${helloworld_SRCS} )
# Use the Widgets module from Qt 5 # Use the Widgets module from Qt 5
target_link_libraries(Rennbahn Qt5::Widgets Qt5::Core Qt5::Sql) target_link_libraries(Rennbahn Qt5::Widgets Qt5::Core Qt5::Sql ${LIBUSB_LIBRARY})
target_link_libraries(Rennbahn ${Boost_LIBRARIES}) target_link_libraries(Rennbahn ${Boost_LIBRARIES})

View File

@@ -10,7 +10,7 @@
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#ifdef ATMEGA #ifdef ATMEGA
#include <usb.h> #include <libusb.h>
#endif #endif
#define USB_LED_ON 1 #define USB_LED_ON 1
@@ -37,6 +37,7 @@ HardwareSetup::HardwareSetup() {
this->stop = 0; this->stop = 0;
#ifdef ATMEGA #ifdef ATMEGA
this->handle = nullptr; this->handle = nullptr;
#endif #endif
this->fd = 0; this->fd = 0;
} }
@@ -55,9 +56,12 @@ HardwareSetup::~HardwareSetup() {
if (HARDWARE_VERSION == 2) { if (HARDWARE_VERSION == 2) {
#ifdef ATMEGA #ifdef ATMEGA
if (this->handle) { if (this->handle) {
usb_close(this->handle);
libusb_exit(nullptr);
libusb_close(this->handle);
} }
this->handle = NULL; this->handle = nullptr;
#endif #endif
} }
else if (HARDWARE_VERSION == 3) { else if (HARDWARE_VERSION == 3) {
@@ -72,99 +76,42 @@ HardwareSetup::~HardwareSetup() {
} }
#ifdef ATMEGA #ifdef ATMEGA
/* Used to get descriptor strings for device identification */ libusb_device_handle * HardwareSetup::usbOpenDevice(int vendor, int product) {
int HardwareSetup::usbGetDescriptorString(usb_dev_handle * dev, int index,
int langid, char * buf, int buflen) {
char buffer[256];
int rval, i;
// make standard request GET_DESCRIPTOR, type string and given index libusb_device ** devs;
// (e.g. dev->iProduct) int r;
rval = usb_control_msg( ssize_t cnt;
dev, USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer,
sizeof(buffer), 1000);
if (rval < 0) // error r = libusb_init(nullptr);
return rval; if (r < 0) {
// return r;
// rval should be bytes read, but buffer[0] contains the actual response }
// size cnt = libusb_get_device_list(nullptr, &devs);
if ((unsigned char)buffer[0] < rval) if (cnt < 0) {
rval = (unsigned char)buffer[0]; // string is shorter than bytes read libusb_exit(nullptr);
// return cnt;
if (buffer[1] != USB_DT_STRING) // second byte is the data type
return 0; // invalid return type
// we're dealing with UTF-16LE here so actual chars is half of rval,
// and index 0 doesn't count
rval /= 2;
/* lossy conversion to ISO Latin1 */
for (i = 1; i < rval && i < buflen; i++) {
if (buffer[2 * i + 1] == 0)
buf[i - 1] = buffer[2 * i];
else
buf[i - 1] = '?'; /* outside of ISO Latin1 range */
} }
buf[i - 1] = 0;
return i - 1; libusb_device * dev;
} this->handle = nullptr;
int i = 0;
usb_dev_handle * HardwareSetup::usbOpenDevice(int vendor, char * vendorName, while ((dev = devs[i++]) != nullptr) {
int product, char * productName) { struct libusb_device_descriptor desc;
struct usb_bus * bus; int r = libusb_get_device_descriptor(dev, &desc);
struct usb_device * dev; if (r < 0) {
char devVendor[256], devProduct[256]; fprintf(stderr, "failed to get device descriptor");
}
if (desc.idVendor == vendor && desc.idProduct == product) {
cout << "id vendor passt und id product auch" << endl;
libusb_open(dev, &this->handle);
usb_dev_handle * handle = NULL; cout << "handle: " << this->handle << endl;
return this->handle;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor != vendor ||
dev->descriptor.idProduct != product)
continue;
/* we need to open the device in order to query strings */
if (!(handle = usb_open(dev))) {
fprintf(stderr, "Warning: cannot open USB device: %sn",
usb_strerror());
continue;
}
/* get vendor name */
if (usbGetDescriptorString(handle, dev->descriptor.iManufacturer,
0x0409, devVendor,
sizeof(devVendor)) < 0) {
fprintf(stderr,
"Warning: cannot query manufacturer for device: %sn",
usb_strerror());
usb_close(handle);
continue;
}
/* get product name */
if (usbGetDescriptorString(handle, dev->descriptor.iProduct, 0x0409,
devProduct, sizeof(devVendor)) < 0) {
fprintf(stderr, "Warning: cannot query product for device: %sn",
usb_strerror());
usb_close(handle);
continue;
}
if (strcmp(devVendor, vendorName) == 0 &&
strcmp(devProduct, productName) == 0)
return handle;
else
usb_close(handle);
} }
} }
return NULL;
return nullptr;
} }
#endif #endif
@@ -188,18 +135,18 @@ void HardwareSetup::run() {
// exit(1); // exit(1);
// } // }
if (!testmode) { if (!testmode) {
string vendorName = "test01";
string productName = "USBExample";
while (!this->handle) { while (!this->handle) {
this->handle = this->handle = usbOpenDevice(0x16C0, 0x05DC);
usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample");
if (this->handle == NULL) { if (this->handle == nullptr) {
fprintf(stderr, "Could not find USB device!\n"); fprintf(stderr, "Could not find USB device!\n");
} }
sleep(1); sleep(1);
} }
} }
int index;
while (!this->stop) { while (!this->stop) {
// test mode // test mode
@@ -242,14 +189,14 @@ void HardwareSetup::run() {
} }
else { else {
// normal mode -> no testmode // normal mode -> no testmode
usleep(150000); // 100ms usleep(150000); // 150ms
// while(1){ // while(1){
nBytes = usb_control_msg( nBytes = libusb_control_transfer(
this->handle, this->handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE |
LIBUSB_ENDPOINT_IN,
USB_DATA_OUT, 0, 0, USB_DATA_OUT, 0, 0,
static_cast<char *>(static_cast<void *>(buffer)), static_cast<unsigned char *>(static_cast<void *>(buffer)),
sizeof(buffer), 5000); sizeof(buffer), 5000);
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@@ -296,10 +243,12 @@ void HardwareSetup::run() {
//} //}
if (nBytes < 0) { if (nBytes < 0) {
fprintf(stderr, "USB error: %sn", usb_strerror()); // fprintf(stderr, "USB error: %sn", libusb_error());
// string vendorName = "test01";
string vendorName = "test01";
string productName = "USBExample";
while (!this->handle) { while (!this->handle) {
this->handle = usbOpenDevice(0x16C0, "test01", 0x05DC, this->handle = usbOpenDevice(0x16C0, 0x05DC);
"USBExample");
if (this->handle == nullptr) { if (this->handle == nullptr) {
fprintf(stderr, "Could not find USB device!\n"); fprintf(stderr, "Could not find USB device!\n");

View File

@@ -7,7 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifdef ATMEGA #ifdef ATMEGA
#include <usb.h> #include <libusb.h>
#endif #endif
struct TransStruct { struct TransStruct {
uint16_t time; uint16_t time;
@@ -30,11 +30,11 @@ class HardwareSetup : public QThread {
bool shellBefore; bool shellBefore;
bool deaBefore; bool deaBefore;
#ifdef ATMEGA #ifdef ATMEGA
usb_dev_handle * handle; libusb_device_handle * handle;
int usbGetDescriptorString(usb_dev_handle * dev, int index, int langid, libusb_context * usbContext;
char * buf, int buflen); int usbGetDescriptorString(libusb_device_handle * dev, int index,
usb_dev_handle * usbOpenDevice(int vendor, char * vendorName, int product, int langid, char * buf, int buflen);
char * productName); libusb_device_handle * usbOpenDevice(int vendor, int product);
#endif #endif
int fd; int fd;

21
readme.txt Normal file
View File

@@ -0,0 +1,21 @@
Software to measure slotcar times
1. Hardware Version 1
Parallelport - not used anymore
2. Hardware Version 2
Based on VUSB https://www.obdev.at/products/vusb/index.html
libusb is required + udev rules needs to be created:
place
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05dc", GROUP="users"
in /etc/udev/rules.d/60-objdev.rules
and reload udev
# udevadm control --reload
if thats not working -> check
$ udevadm monitor --env
to solve the problem
3. Hardware Version 3
Based on STM32F07
user need to be in group uucp