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)
# 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
set(helloworld_SRCS
main.cpp
@@ -73,5 +105,5 @@ set(helloworld_SRCS
add_executable(Rennbahn ${helloworld_SRCS} )
# 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})

View File

@@ -10,7 +10,7 @@
#include <termios.h>
#include <unistd.h>
#ifdef ATMEGA
#include <usb.h>
#include <libusb.h>
#endif
#define USB_LED_ON 1
@@ -37,6 +37,7 @@ HardwareSetup::HardwareSetup() {
this->stop = 0;
#ifdef ATMEGA
this->handle = nullptr;
#endif
this->fd = 0;
}
@@ -55,9 +56,12 @@ HardwareSetup::~HardwareSetup() {
if (HARDWARE_VERSION == 2) {
#ifdef ATMEGA
if (this->handle) {
usb_close(this->handle);
libusb_exit(nullptr);
libusb_close(this->handle);
}
this->handle = NULL;
this->handle = nullptr;
#endif
}
else if (HARDWARE_VERSION == 3) {
@@ -72,99 +76,42 @@ HardwareSetup::~HardwareSetup() {
}
#ifdef ATMEGA
/* Used to get descriptor strings for device identification */
int HardwareSetup::usbGetDescriptorString(usb_dev_handle * dev, int index,
int langid, char * buf, int buflen) {
char buffer[256];
int rval, i;
libusb_device_handle * HardwareSetup::usbOpenDevice(int vendor, int product) {
// make standard request GET_DESCRIPTOR, type string and given index
// (e.g. dev->iProduct)
rval = usb_control_msg(
dev, USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer,
sizeof(buffer), 1000);
libusb_device ** devs;
int r;
ssize_t cnt;
if (rval < 0) // error
return rval;
// rval should be bytes read, but buffer[0] contains the actual response
// size
if ((unsigned char)buffer[0] < rval)
rval = (unsigned char)buffer[0]; // string is shorter than bytes read
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 */
r = libusb_init(nullptr);
if (r < 0) {
// return r;
}
cnt = libusb_get_device_list(nullptr, &devs);
if (cnt < 0) {
libusb_exit(nullptr);
// return cnt;
}
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,
int product, char * productName) {
struct usb_bus * bus;
struct usb_device * dev;
char devVendor[256], devProduct[256];
while ((dev = devs[i++]) != nullptr) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
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;
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);
cout << "handle: " << this->handle << endl;
return this->handle;
}
}
return NULL;
return nullptr;
}
#endif
@@ -188,18 +135,18 @@ void HardwareSetup::run() {
// exit(1);
// }
if (!testmode) {
string vendorName = "test01";
string productName = "USBExample";
while (!this->handle) {
this->handle =
usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample");
this->handle = usbOpenDevice(0x16C0, 0x05DC);
if (this->handle == NULL) {
if (this->handle == nullptr) {
fprintf(stderr, "Could not find USB device!\n");
}
sleep(1);
}
}
int index;
while (!this->stop) {
// test mode
@@ -242,14 +189,14 @@ void HardwareSetup::run() {
}
else {
// normal mode -> no testmode
usleep(150000); // 100ms
usleep(150000); // 150ms
// while(1){
nBytes = usb_control_msg(
nBytes = libusb_control_transfer(
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,
static_cast<char *>(static_cast<void *>(buffer)),
static_cast<unsigned char *>(static_cast<void *>(buffer)),
sizeof(buffer), 5000);
for (int i = 0; i < 6; i++) {
@@ -296,10 +243,12 @@ void HardwareSetup::run() {
//}
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) {
this->handle = usbOpenDevice(0x16C0, "test01", 0x05DC,
"USBExample");
this->handle = usbOpenDevice(0x16C0, 0x05DC);
if (this->handle == nullptr) {
fprintf(stderr, "Could not find USB device!\n");

View File

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