added support for stm32 hardware + some c++ cosmetic

This commit is contained in:
2018-11-03 11:50:23 +01:00
parent 4e4d558910
commit 1f60eb5d12
2 changed files with 220 additions and 116 deletions

View File

@@ -1,5 +1,7 @@
#include "hardwaresetup.h"
#include "sys/io.h"
// filecontrol
#include <fcntl.h>
#include <iostream>
#include <stdint.h>
#include <stdio.h>
@@ -11,6 +13,13 @@
#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 */
@@ -22,7 +31,8 @@ HardwareSetup::HardwareSetup() {
// //perror("ioperm");
// }
this->stop = 0;
this->handle = NULL;
this->handle = nullptr;
this->fd = 0;
}
void HardwareSetup::setStop() {
this->stop = 1;
@@ -32,12 +42,25 @@ 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);
if (HARDWARE_VERSION == 2) {
if (this->handle) {
usb_close(this->handle);
}
this->handle = NULL;
}
else if (HARDWARE_VERSION == 3) {
// close filedescriptor
if (this->fd < 0) {
// already closed
}
else {
close(this->fd);
}
}
this->handle = NULL;
}
/* Used to get descriptor strings for device identification */
@@ -137,133 +160,211 @@ usb_dev_handle * HardwareSetup::usbOpenDevice(int vendor, char * vendorName,
void HardwareSetup::run() {
int testmode = 0;
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);
// }
if (!testmode) {
while (!this->handle) {
this->handle =
usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample");
if (HARDWARE_VERSION == 2) {
int testmode = 0;
if (this->handle == NULL) {
fprintf(stderr, "Could not find USB device!\n");
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<char *>(static_cast<void *>(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<int>(buffer[i].time) << endl;
emit Shell(static_cast<int>(buffer[i].time), 1);
break;
case 1:
cout << "Dea Zeit 1: "
<< static_cast<int>(buffer[i].time) << endl;
emit Dea(static_cast<int>(buffer[i].time), 1);
break;
case 2:
cout << "Shell Zeit 2: "
<< static_cast<int>(buffer[i].time) << endl;
emit Shell(static_cast<int>(buffer[i].time), 2);
break;
case 3:
cout << "Dea Zeit 2: "
<< static_cast<int>(buffer[i].time) << endl;
emit Dea(static_cast<int>(buffer[i].time), 2);
break;
case 4:
cout << "Shell Zeit 2: "
<< static_cast<int>(buffer[i].time) << endl;
emit Shell(static_cast<int>(buffer[i].time), 3);
break;
case 5:
cout << "Dea Zeit 3: "
<< static_cast<int>(buffer[i].time) << endl;
emit Dea(static_cast<int>(buffer[i].time), 3);
break;
}
}
}
if (buffer[0].update != 0) {
cout << "Got " << nBytes
<< " bytes: " << static_cast<int>(buffer[2].time)
<< ", " << static_cast<int>(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);
}
}
}
}
} // 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;
// port opened
int index;
while (!this->stop) {
// prepare buffer
struct TransStruct * received;
uint8_t uintBuf[4 * 6];
void * voidBuf;
while (!this->stop) {
// test mode
// read data
read(this->fd, uintBuf, 4 * 6);
voidBuf = static_cast<void *>(uintBuf);
received = static_cast<struct TransStruct *>(voidBuf);
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, (char *)buffer, sizeof(buffer), 5000);
usleep(150000); // 150ms
for (int i = 0; i < 6; i++) {
if (buffer[i].update != 0) {
switch (buffer[i].id) {
if (received[i].update != 0) {
switch (received[i].id) {
case 0:
cout << "Shell Zeit 1: " << (int)buffer[i].time << endl;
emit Shell((int)buffer[i].time, 1);
cout << "Shell Zeit 1: "
<< static_cast<int>(received[i].time) << endl;
emit Shell(static_cast<int>(received[i].time), 1);
break;
case 1:
cout << "Dea Zeit 1: " << (int)buffer[i].time << endl;
emit Dea((int)buffer[i].time, 1);
cout << "Dea Zeit 1: "
<< static_cast<int>(received[i].time) << endl;
emit Dea(static_cast<int>(received[i].time), 1);
break;
case 2:
cout << "Shell Zeit 2: " << (int)buffer[i].time << endl;
emit Shell((int)buffer[i].time, 2);
cout << "Shell Zeit 2: "
<< static_cast<int>(received[i].time) << endl;
emit Shell(static_cast<int>(received[i].time), 2);
break;
case 3:
cout << "Dea Zeit 2: " << (int)buffer[i].time << endl;
emit Dea((int)buffer[i].time, 2);
cout << "Dea Zeit 2: "
<< static_cast<int>(received[i].time) << endl;
emit Dea(static_cast<int>(received[i].time), 2);
break;
case 4:
cout << "Shell Zeit 2: " << (int)buffer[i].time << endl;
emit Shell((int)buffer[i].time, 3);
cout << "Shell Zeit 2: "
<< static_cast<int>(received[i].time) << endl;
emit Shell(static_cast<int>(received[i].time), 3);
break;
case 5:
cout << "Dea Zeit 3: " << (int)buffer[i].time << endl;
emit Dea((int)buffer[i].time, 3);
cout << "Dea Zeit 3: "
<< static_cast<int>(received[i].time) << endl;
emit Dea(static_cast<int>(received[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());
while (!this->handle) {
this->handle =
usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample");
if (this->handle == NULL) {
fprintf(stderr, "Could not find USB device!\n");
}
sleep(1);
}
}
}
}
else {
cout << "unknown hardware version" << endl;
}
}
bool HardwareSetup::getShell() {

View File

@@ -3,38 +3,41 @@
#include <QtCore>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <usb.h>
struct TransStruct{
struct TransStruct {
uint16_t time;
uint8_t id;
uint8_t update;
};
class HardwareSetup : public QThread
{
class HardwareSetup : public QThread {
Q_OBJECT
protected:
protected:
void run();
private:
private:
bool shellBefore;
bool deaBefore;
usb_dev_handle *handle;
usb_dev_handle * handle;
int fd;
bool getShell();
bool getDea();
int* findBit(int *array, int zahl);
int * findBit(int * array, int zahl);
bool stop;
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);
public:
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);
public:
void setStop();
~HardwareSetup();
HardwareSetup();
signals:
signals:
void Shell(int, int);
void Dea(int, int);
};