You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainwindow.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QTime>
  4. #include <QMessageBox>
  5. #include <QThread>
  6. #define SEARCH_STR_STIMULUS "StimulusCode "
  7. MainWindow::MainWindow(QWidget *parent) :
  8. QMainWindow(parent),
  9. ui(new Ui::MainWindow)
  10. {
  11. ui->setupUi(this);
  12. PCIP_1 = 0; PCIP_2 = 0; PCIP_3 = 0; PCIP_4 = 0;
  13. FeatherIP_1 = 0; FeatherIP_2 = 0; FeatherIP_3 = 0; FeatherIP_4 = 0;
  14. //default
  15. PCIP_full = QHostAddress("127.0.0.1");
  16. PCPort = 5001;
  17. FeatherIP_full = QHostAddress("192.168.4.1");
  18. FeatherPort = 8888;
  19. udpSocket_PC = new QUdpSocket(this);
  20. udpSocket_Feather = new QUdpSocket(this); //no binding to socket needed. Just send data!
  21. stylesheet_tac_marked = "QLabel {background-color: rgb(0,170,0);}";
  22. stylesheet_tac_unmarked = "QLabel {background-color: white;}";
  23. logBCIData = true;
  24. lastBufferStr = "0";
  25. setupSignalsAndSlots();
  26. }
  27. MainWindow::~MainWindow()
  28. {
  29. delete ui;
  30. }
  31. void MainWindow::setupSignalsAndSlots()
  32. {
  33. connect(ui->push_Apply, SIGNAL(clicked()), this, SLOT(applyButton()));
  34. connect(udpSocket_PC, SIGNAL(readyRead()), this, SLOT(readPCSocketData()));
  35. connect(ui->check_LogBCI, SIGNAL(toggled(bool)), this, SLOT(setLogBCIData(bool)));
  36. connect(ui->push_ClearLog, SIGNAL(clicked()), this, SLOT(clearLog()));
  37. connect(ui->push_Tac1, SIGNAL(clicked(bool)), this, SLOT(pushTac1()));
  38. connect(ui->push_Tac2, SIGNAL(clicked(bool)), this, SLOT(pushTac2()));
  39. connect(ui->push_Tac3, SIGNAL(clicked(bool)), this, SLOT(pushTac3()));
  40. connect(ui->push_Tac4, SIGNAL(clicked(bool)), this, SLOT(pushTac4()));
  41. connect(ui->push_Tac5, SIGNAL(clicked(bool)), this, SLOT(pushTac5()));
  42. connect(ui->push_Tac6, SIGNAL(clicked(bool)), this, SLOT(pushTac6()));
  43. }
  44. void MainWindow::log(QString logtext)
  45. {
  46. QString output = "[" + getCurrentTime() + "] " + logtext;
  47. ui->plain_Log->appendPlainText(output);
  48. }
  49. QString MainWindow::getCurrentTime()
  50. {
  51. QString currenttime = QTime::currentTime().toString("HH:mm:ss");
  52. return currenttime;
  53. }
  54. void MainWindow::applyButton()
  55. {
  56. if(ui->check_ClearLog->isChecked())
  57. {
  58. ui->plain_Log->clear();
  59. }
  60. bindPC();
  61. setupFeather();
  62. }
  63. void MainWindow::bindPC()
  64. {
  65. if(udpSocket_PC->state() == QAbstractSocket::BoundState)
  66. {
  67. udpSocket_PC->abort();
  68. }
  69. QString tmp_PCIP_full = buildPCIP();
  70. if(tmp_PCIP_full == "0") {
  71. log("Please try another IP address");
  72. } else {
  73. PCIP_full = QHostAddress(tmp_PCIP_full);
  74. PCPort = ui->line_PCPort->text().toUShort(nullptr,10);
  75. if(checkPort(PCPort))
  76. {
  77. log("Receive socket BCI-Stream set to:");
  78. log("-- PC-IP: " + PCIP_full.toString());
  79. log("-- PC-Port: " + QString::number(PCPort));
  80. if(udpSocket_PC->bind(PCIP_full, PCPort))
  81. {
  82. log("UDP PC socket: successfully bound.");
  83. } else {
  84. log("!! UDP PC socket: binding error !!");
  85. }
  86. } else {
  87. PCPort = 00000;
  88. ui->line_PCPort->setText("00000");
  89. log("!! PC Port not valid !!");
  90. QMessageBox::critical(this,
  91. "PC Port not valid",
  92. "PC Port is not valid. Please make sure that the port is a number between 0 and 65535.");
  93. }
  94. }
  95. }
  96. void MainWindow::setupFeather()
  97. {
  98. QString tmp_FeatherIP_full = buildFeatherIP();
  99. if(tmp_FeatherIP_full == "0") {
  100. log("Please try another IP address");
  101. } else {
  102. FeatherIP_full = QHostAddress(tmp_FeatherIP_full);
  103. FeatherPort = ui->line_FeatherPort->text().toUShort(nullptr,10);
  104. if(checkPort(FeatherPort))
  105. {
  106. log("Receive socket Adafruit Feather set to:");
  107. log("-- Feather-IP: " + FeatherIP_full.toString());
  108. log("-- Feather-Port: " + QString::number(FeatherPort));
  109. } else {
  110. FeatherPort = 00000;
  111. ui->line_FeatherPort->setText("00000");
  112. log("!! Port not valid !!");
  113. QMessageBox::critical(this,
  114. "Feather Port not valid",
  115. "Feather Port is not valid. Please make sure that the port is a number between 0 and 65535.");
  116. }
  117. }
  118. }
  119. bool MainWindow::checkPort(const quint16 port)
  120. {
  121. bool valid = false;
  122. if(port)
  123. {
  124. valid = true;
  125. }
  126. return valid;
  127. }
  128. QString MainWindow::buildPCIP()
  129. {
  130. int PCIP1 = ui->line_PCIP1->text().toInt();
  131. int PCIP2 = ui->line_PCIP2->text().toInt();
  132. int PCIP3 = ui->line_PCIP3->text().toInt();
  133. int PCIP4 = ui->line_PCIP4->text().toInt();
  134. if(
  135. ((PCIP1 < 0) || (PCIP1 > 255))
  136. || ((PCIP2 < 0) || (PCIP2 > 255))
  137. || ((PCIP3 < 0) || (PCIP3 > 255))
  138. || ((PCIP4 < 0) || (PCIP4 > 255))
  139. )
  140. {
  141. QMessageBox::critical(this,
  142. "PC IP address not valid",
  143. "PC IP address is not valid. Please make sure that the IP components only consist of numbers between 0 and 255.");
  144. log("!! Error while setting PC IP !!");
  145. return "0";
  146. } else {
  147. QString IP = QString::number(PCIP1) + "."
  148. + QString::number(PCIP2) + "."
  149. + QString::number(PCIP3) + "."
  150. + QString::number(PCIP4);
  151. return IP;
  152. }
  153. }
  154. QString MainWindow::buildFeatherIP()
  155. {
  156. int FeatherIP1 = ui->line_FeatherIP1->text().toInt();
  157. int FeatherIP2 = ui->line_FeatherIP2->text().toInt();
  158. int FeatherIP3 = ui->line_FeatherIP3->text().toInt();
  159. int FeatherIP4 = ui->line_FeatherIP4->text().toInt();
  160. if(
  161. ((FeatherIP1 < 0) || (FeatherIP1 > 255))
  162. || ((FeatherIP2 < 0) || (FeatherIP2 > 255))
  163. || ((FeatherIP3 < 0) || (FeatherIP3 > 255))
  164. || ((FeatherIP4 < 0) || (FeatherIP4 > 255))
  165. )
  166. {
  167. QMessageBox::critical(this,
  168. "Feather IP address not valid",
  169. "Feather IP address is not valid. Please make sure that the IP components only consist of numbers between 0 and 255.");
  170. log("!! Error while setting Feather IP !!");
  171. return "0";
  172. } else {
  173. QString IP = QString::number(FeatherIP1) + "."
  174. + QString::number(FeatherIP2) + "."
  175. + QString::number(FeatherIP3) + "."
  176. + QString::number(FeatherIP4);
  177. return IP;
  178. }
  179. }
  180. void MainWindow::readPCSocketData()
  181. {
  182. QString bufferStr;
  183. while (udpSocket_PC->hasPendingDatagrams())
  184. {
  185. QByteArray buffer;
  186. buffer.resize(static_cast<int>(udpSocket_PC->pendingDatagramSize()));
  187. udpSocket_PC->readDatagram(buffer.data(), buffer.size());
  188. bufferStr = QString::fromStdString(buffer.toStdString());
  189. if(bufferStr.contains(SEARCH_STR_STIMULUS,Qt::CaseSensitive))
  190. {
  191. bufferStr.replace(SEARCH_STR_STIMULUS, "");
  192. if(!bufferStr.contains("0"))
  193. {
  194. ui->lineLastSymbol->setText(bufferStr);
  195. }
  196. if(logBCIData)
  197. {
  198. log(bufferStr);
  199. }
  200. if(ui->check_Tactile->isChecked())
  201. {
  202. markDirectionTactile(bufferStr);
  203. }
  204. if(bufferStr != lastBufferStr){
  205. //TODO Buffer nur von 1-6 weiterschicken, andere hier schon rausfiltern!
  206. QByteArray data = bufferStr.toUtf8();
  207. udpSocket_Feather->writeDatagram(data, FeatherIP_full, FeatherPort);
  208. lastBufferStr = bufferStr;
  209. }
  210. }
  211. }
  212. }
  213. void MainWindow::markDirectionTactile(const QString direction)
  214. {
  215. unsigned int i_direction = direction.toUInt(nullptr,10);
  216. //qDebug() << i_direction;
  217. switch (i_direction) {
  218. case 0: //all Low
  219. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  220. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  221. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  222. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  223. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  224. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  225. break;
  226. case 1: //Tac1
  227. ui->label_Tac1->setStyleSheet(stylesheet_tac_marked);
  228. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  229. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  230. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  231. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  232. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  233. break;
  234. case 2: //Tac2
  235. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  236. ui->label_Tac2->setStyleSheet(stylesheet_tac_marked);
  237. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  238. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  239. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  240. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  241. break;
  242. case 3: //Tac3
  243. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  244. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  245. ui->label_Tac3->setStyleSheet(stylesheet_tac_marked);
  246. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  247. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  248. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  249. break;
  250. case 4: //Tac4
  251. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  252. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  253. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  254. ui->label_Tac4->setStyleSheet(stylesheet_tac_marked);
  255. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  256. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  257. break;
  258. case 5: //Tac5
  259. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  260. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  261. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  262. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  263. ui->label_Tac5->setStyleSheet(stylesheet_tac_marked);
  264. ui->label_Tac6->setStyleSheet(stylesheet_tac_unmarked);
  265. break;
  266. case 6: //Tac6
  267. ui->label_Tac1->setStyleSheet(stylesheet_tac_unmarked);
  268. ui->label_Tac2->setStyleSheet(stylesheet_tac_unmarked);
  269. ui->label_Tac3->setStyleSheet(stylesheet_tac_unmarked);
  270. ui->label_Tac4->setStyleSheet(stylesheet_tac_unmarked);
  271. ui->label_Tac5->setStyleSheet(stylesheet_tac_unmarked);
  272. ui->label_Tac6->setStyleSheet(stylesheet_tac_marked);
  273. break;
  274. }
  275. }
  276. void MainWindow::setLogBCIData(const bool log)
  277. {
  278. logBCIData = log;
  279. }
  280. void MainWindow::clearLog()
  281. {
  282. ui->plain_Log->clear();
  283. }
  284. void MainWindow::pushTac1()
  285. {
  286. udpSocket_Feather->writeDatagram(QByteArray::number(1), FeatherIP_full, FeatherPort);
  287. }
  288. void MainWindow::pushTac2()
  289. {
  290. udpSocket_Feather->writeDatagram(QByteArray::number(2), FeatherIP_full, FeatherPort);
  291. }
  292. void MainWindow::pushTac3()
  293. {
  294. udpSocket_Feather->writeDatagram(QByteArray::number(3), FeatherIP_full, FeatherPort);
  295. }
  296. void MainWindow::pushTac4()
  297. {
  298. udpSocket_Feather->writeDatagram(QByteArray::number(4), FeatherIP_full, FeatherPort);
  299. }
  300. void MainWindow::pushTac5()
  301. {
  302. udpSocket_Feather->writeDatagram(QByteArray::number(5), FeatherIP_full, FeatherPort);
  303. }
  304. void MainWindow::pushTac6()
  305. {
  306. udpSocket_Feather->writeDatagram(QByteArray::number(6), FeatherIP_full, FeatherPort);
  307. }