#include "hardwaresetup.h" #include "sys/io.h" // filecontrol #include #include #include #include #include #include #include #ifdef ATMEGA #include #endif #define USB_LED_ON 1 #define USB_LED_OFF 0 #define USB_DATA_OUT 2 #define HARDWARE_VERSION 3 // V1 Parallelport // V2 USB Atmega8 // V3 STM32F07 #define PORT_PATH "/dev/ttyACM0" 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; #ifdef ATMEGA this->handle = nullptr; #endif this->fd = 0; } 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 (HARDWARE_VERSION == 2) { #ifdef ATMEGA if (this->handle) { usb_close(this->handle); } this->handle = NULL; #endif } else if (HARDWARE_VERSION == 3) { // close filedescriptor if (this->fd < 0) { // already closed } else { close(this->fd); } } } #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; // 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; } #endif void HardwareSetup::run() { if (HARDWARE_VERSION == 2) { #ifdef ATMEGA struct TransStruct buffer[6]; int testmode = 0; int nBytes = 0; // char buffer[256]; // 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); // } if (!testmode) { 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) { // test mode if (testmode) { while (1) { int validTimes = 0; if (validTimes) { Shell(1100, 1); sleep(1); Shell(1200, 2); sleep(1); Shell(1300, 3); sleep(1); Dea(1100, 1); sleep(1); Dea(1200, 2); sleep(1); Dea(1300, 3); // this->stop = 0; sleep(1); } else { // invalid times Shell(30, 1); sleep(1); Shell(700, 2); sleep(1); Shell(700, 3); sleep(1); Dea(500, 1); sleep(1); Dea(700, 2); sleep(1); Dea(3000, 3); // this->stop = 0; sleep(1); } } } else { // normal mode -> no testmode usleep(150000); // 100ms // while(1){ nBytes = usb_control_msg( this->handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_OUT, 0, 0, static_cast(static_cast(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: " << static_cast(buffer[i].time) << endl; emit Shell(static_cast(buffer[i].time), 1); break; case 1: cout << "Dea Zeit 1: " << static_cast(buffer[i].time) << endl; emit Dea(static_cast(buffer[i].time), 1); break; case 2: cout << "Shell Zeit 2: " << static_cast(buffer[i].time) << endl; emit Shell(static_cast(buffer[i].time), 2); break; case 3: cout << "Dea Zeit 2: " << static_cast(buffer[i].time) << endl; emit Dea(static_cast(buffer[i].time), 2); break; case 4: cout << "Shell Zeit 2: " << static_cast(buffer[i].time) << endl; emit Shell(static_cast(buffer[i].time), 3); break; case 5: cout << "Dea Zeit 3: " << static_cast(buffer[i].time) << endl; emit Dea(static_cast(buffer[i].time), 3); break; } } } if (buffer[0].update != 0) { cout << "Got " << nBytes << " bytes: " << static_cast(buffer[2].time) << ", " << static_cast(buffer[2].id) << endl; } //} if (nBytes < 0) { fprintf(stderr, "USB error: %sn", usb_strerror()); while (!this->handle) { this->handle = usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample"); if (this->handle == nullptr) { fprintf(stderr, "Could not find USB device!\n"); } sleep(1); } } } } #endif // ATMEGA } // end HARDWARE_VERSION 2 else if (HARDWARE_VERSION == 3) { fd = open(PORT_PATH, O_RDONLY); while (this->fd < 0) { // wait 1 second sleep(1); cout << "Port can't be opened" << endl; fd = open(PORT_PATH, O_RDONLY); } cout << "Port opened" << endl; // prepare buffer struct TransStruct * received; uint8_t uintBuf[4 * 6]; void * voidBuf; // configure among others non-canonical mode (important) struct termios config; tcgetattr(fd, &config); config.c_lflag = 0; // standard value is bad choosen tcsetattr(fd, TCSANOW, &config); // read raw data to buffer ssize_t length = read(fd, uintBuf, 24); // flush existing data if (length < 24) { tcflush(fd, TCIFLUSH); } while (!this->stop) { // read data cout << "try to read data" << endl; read(this->fd, uintBuf, 4 * 6); cout << "data read" << endl; voidBuf = static_cast(uintBuf); received = static_cast(voidBuf); usleep(150000); // 150ms for (int i = 0; i < 6; i++) { if (received[i].update != 0) { switch (received[i].id) { case 0: cout << "Shell Zeit 1: " << static_cast(received[i].time) << endl; emit Shell(static_cast(received[i].time), 1); break; case 1: cout << "Dea Zeit 1: " << static_cast(received[i].time) << endl; emit Dea(static_cast(received[i].time), 1); break; case 2: cout << "Shell Zeit 2: " << static_cast(received[i].time) << endl; emit Shell(static_cast(received[i].time), 2); break; case 3: cout << "Dea Zeit 2: " << static_cast(received[i].time) << endl; emit Dea(static_cast(received[i].time), 2); break; case 4: cout << "Shell Zeit 2: " << static_cast(received[i].time) << endl; emit Shell(static_cast(received[i].time), 3); break; case 5: cout << "Dea Zeit 3: " << static_cast(received[i].time) << endl; emit Dea(static_cast(received[i].time), 3); break; } } } } } else { cout << "unknown hardware version" << endl; } } 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; }