Files
Rennbahn/hardwaresetup.cpp

277 lines
7.4 KiB
C++

#include "hardwaresetup.h"
#include <unistd.h>
#include "sys/io.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <usb.h>
#define USB_LED_ON 1
#define USB_LED_OFF 0
#define USB_DATA_OUT 2
using namespace std;
// not needed anymore -> usb
// #define BASEPORT 0xe050 /* lp1 */
// #define BASEPORT 0x378
// #define BASEPORT 0xd000
HardwareSetup::HardwareSetup()
{
// if (ioperm(BASEPORT, 3, 1)) {
// //perror("ioperm");
// }
this->stop = 0;
this->handle = NULL;
}
void HardwareSetup::setStop(){
this->stop = 1;
}
HardwareSetup::~HardwareSetup(){
/*if (ioperm(BASEPORT, 3, 0)) {
//perror("ioperm");
}*/
std::cout << "Hardware beendet" << std::endl;
this->stop = 1;
if(this->handle){
usb_close(this->handle);
}
this->handle = NULL;
}
/* 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;
// 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);
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 */
}
buf[i-1] = 0;
return i-1;
}
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];
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);
}
}
return NULL;
}
void HardwareSetup::run(){
int nBytes = 0;
//char buffer[256];
struct TransStruct buffer[6];
//struct TransStruct *buffer = buffer1;
//cout << "thread started" << endl;
// if(argc < 2) {
// printf("Usage:\n");
// printf("usbtext.exe on\n");
// printf("usbtext.exe off\n");
// exit(1);
// }
while(!this->handle){
this->handle = usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample");
if(this->handle == NULL) {
fprintf(stderr, "Could not find USB device!\n");
}
sleep(1);
}
int index;
while(!this->stop){
usleep(100000); // 100ms
//while(1){
nBytes = usb_control_msg(this->handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000);
for(int i = 0; i < 6; i++){
if(buffer[i].update != 0){
switch(buffer[i].id){
case 0:
cout << "Shell Zeit 1: " << (int)buffer[i].time << endl;
emit Shell((int)buffer[i].time, 1);
break;
case 1:
cout << "Dea Zeit 1: " << (int)buffer[i].time << endl;
emit Dea((int)buffer[i].time, 1);
break;
case 2:
cout << "Shell Zeit 2: " << (int)buffer[i].time << endl;
emit Shell((int)buffer[i].time, 2);
break;
case 3:
cout << "Dea Zeit 2: " << (int)buffer[i].time << endl;
emit Dea((int)buffer[i].time, 2);
break;
case 4:
cout << "Shell Zeit 2: " << (int)buffer[i].time << endl;
emit Shell((int)buffer[i].time, 3);
break;
case 5:
cout << "Dea Zeit 3: " << (int)buffer[i].time << endl;
emit Dea((int)buffer[i].time, 3);
break;
}
}
}
if(buffer[0].update != 0){
cout << "Got " << nBytes << " bytes: " << (int)buffer[2].time << ", " << (int)buffer[2].id << endl;
}
//}
if(nBytes < 0)
fprintf(stderr, "USB error: %sn", usb_strerror());
}
}
bool HardwareSetup::getShell(){
// int zahl[8] = {0};
// if(findBit(zahl, inb(BASEPORT + 1))[7] == 1){
// return true;
// }
return false;
}
bool HardwareSetup::getDea(){
// int zahl[8] = {0};
// if(findBit(zahl, inb(BASEPORT + 1))[6] == 0 ){
// return true;
// }
return false;
}
int* HardwareSetup::findBit(int *array, int zahl){
if(zahl >=128){
array[7] = 1;
zahl -= 128;
}
if(zahl >=64){
array[6] = 1;
zahl -= 64;
}
if(zahl >=32){
array[5] = 1;
zahl -= 32;
}
if(zahl >=16){
array[4] = 1;
zahl -= 16;
}
if(zahl >=8){
array[3] = 1;
zahl -= 8;
}
if(zahl >=4){
array[2] = 1;
zahl -= 4;
}
if(zahl >=2){
array[1] = 1;
zahl -= 2;
}
if(zahl >=1){
array[0] = 1;
zahl -= 1;
}
return array;
}